Add generated pcidevs files. Fix a small typo in devlist2h.awk.
[dragonfly.git] / sys / bus / firewire / fwcrom.c
1 /*-
2  * Copyright (c) 2002-2003
3  *      Hidetoshi Shimokawa. All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *
16  *      This product includes software developed by Hidetoshi Shimokawa.
17  *
18  * 4. Neither the name of the author nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  * 
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/bus/firewire/fwcrom.c,v 1.5 2004/02/05 17:51:43 joerg Exp $
35  */
36
37 #ifndef __DragonFly__
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/sys/dev/firewire/fwcrom.c,v 1.9 2003/10/02 04:06:55 simokawa Exp $");
40 #endif
41
42 #include <sys/param.h>
43 #if defined(_KERNEL) || defined(TEST)
44 #include <sys/queue.h>
45 #endif
46 #ifdef _KERNEL
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #else
50 #include <netinet/in.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <err.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #endif
57
58 #ifdef __DragonFly__
59 #include "firewire.h"
60 #include "iec13213.h"
61 #else
62 #include <dev/firewire/firewire.h>
63 #include <dev/firewire/iec13213.h>
64 #endif
65
66 #define MAX_ROM (1024 - sizeof(u_int32_t) * 5)
67 #define CROM_END(cc) ((vm_offset_t)(cc)->stack[0].dir + MAX_ROM - 1)
68
69 void
70 crom_init_context(struct crom_context *cc, u_int32_t *p)
71 {
72         struct csrhdr *hdr;
73
74         hdr = (struct csrhdr *)p;
75         if (hdr->info_len == 1) {
76                 /* minimum ROM */
77                 cc->depth = -1;
78         }
79         p += 1 + hdr->info_len;
80
81         /* check size of root directory */
82         if (((struct csrdirectory *)p)->crc_len == 0) {
83                 cc->depth = -1;
84                 return;
85         }
86         cc->depth = 0;
87         cc->stack[0].dir = (struct csrdirectory *)p;
88         cc->stack[0].index = 0;
89 }
90
91 struct csrreg *
92 crom_get(struct crom_context *cc)
93 {
94         struct crom_ptr *ptr;
95
96         ptr = &cc->stack[cc->depth];
97         return (&ptr->dir->entry[ptr->index]);
98 }
99
100 void
101 crom_next(struct crom_context *cc)
102 {
103         struct crom_ptr *ptr;
104         struct csrreg *reg;
105
106         if (cc->depth < 0)
107                 return;
108         reg = crom_get(cc);
109         if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
110                 if (cc->depth >= CROM_MAX_DEPTH) {
111                         printf("crom_next: too deep\n");
112                         goto again;
113                 }
114                 cc->depth ++;
115
116                 ptr = &cc->stack[cc->depth];
117                 ptr->dir = (struct csrdirectory *) (reg + reg->val);
118                 ptr->index = 0;
119                 goto check;
120         }
121 again:
122         ptr = &cc->stack[cc->depth];
123         ptr->index ++;
124 check:
125         if (ptr->index < ptr->dir->crc_len &&
126                         (vm_offset_t)crom_get(cc) <= CROM_END(cc))
127                 return;
128
129         if (ptr->index < ptr->dir->crc_len)
130                 printf("crom_next: bound check failed\n");
131
132         if (cc->depth > 0) {
133                 cc->depth--;
134                 goto again;
135         }
136         /* no more data */
137         cc->depth = -1;
138 }
139
140
141 struct csrreg *
142 crom_search_key(struct crom_context *cc, u_int8_t key)
143 {
144         struct csrreg *reg;
145
146         while(cc->depth >= 0) {
147                 reg = crom_get(cc);
148                 if (reg->key == key)
149                         return reg;
150                 crom_next(cc);
151         }
152         return NULL;
153 }
154
155 int
156 crom_has_specver(u_int32_t *p, u_int32_t spec, u_int32_t ver)
157 {
158         struct csrreg *reg;
159         struct crom_context c, *cc;
160         int state = 0;
161
162         cc = &c;
163         crom_init_context(cc, p);
164         while(cc->depth >= 0) {
165                 reg = crom_get(cc);
166                 if (state == 0) {
167                         if (reg->key == CSRKEY_SPEC && reg->val == spec)
168                                 state = 1;
169                         else
170                                 state = 0;
171                 } else {
172                         if (reg->key == CSRKEY_VER && reg->val == ver)
173                                 return 1;
174                         else
175                                 state = 0;
176                 }
177                 crom_next(cc);
178         }
179         return 0;
180 }
181
182 void
183 crom_parse_text(struct crom_context *cc, char *buf, int len)
184 {
185         struct csrreg *reg;
186         struct csrtext *textleaf;
187         u_int32_t *bp;
188         int i, qlen;
189         static char *nullstr = "(null)";
190
191         if (cc->depth < 0)
192                 return;
193
194         reg = crom_get(cc);
195         if (reg->key != CROM_TEXTLEAF ||
196                         (vm_offset_t)(reg + reg->val) > CROM_END(cc)) {
197                 strncpy(buf, nullstr, len);
198                 return;
199         }
200         textleaf = (struct csrtext *)(reg + reg->val);
201
202         if ((vm_offset_t)textleaf + textleaf->crc_len > CROM_END(cc)) {
203                 strncpy(buf, nullstr, len);
204                 return;
205         }
206
207         /* XXX should check spec and type */
208
209         bp = (u_int32_t *)&buf[0];
210         qlen = textleaf->crc_len - 2;
211         if (len < qlen * 4)
212                 qlen = len/4;
213         for (i = 0; i < qlen; i ++)
214                 *bp++ = ntohl(textleaf->text[i]);
215         /* make sure to terminate the string */
216         if (len <= qlen * 4)
217                 buf[len - 1] = 0;
218         else
219                 buf[qlen * 4] = 0;
220 }
221
222 u_int16_t
223 crom_crc(u_int32_t *ptr, int len)
224 {
225         int i, shift;
226         u_int32_t data, sum, crc = 0;
227
228         for (i = 0; i < len; i++) {
229                 data = ptr[i];
230                 for (shift = 28; shift >= 0; shift -= 4) {
231                         sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
232                         crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
233                 }
234                 crc &= 0xffff;
235         }
236         return((u_int16_t) crc);
237 }
238
239 #ifndef _KERNEL
240 static void
241 crom_desc_specver(u_int32_t spec, u_int32_t ver, char *buf, int len)
242 {
243         char *s = NULL;
244
245         if (spec == CSRVAL_ANSIT10 || spec == 0) {
246                 switch (ver) {
247                 case CSRVAL_T10SBP2:
248                         s = "SBP-2";
249                         break;
250                 default:
251                         if (spec != 0)
252                                 s = "unknown ANSIT10";
253                 }
254         }
255         if (spec == CSRVAL_1394TA || spec == 0) {
256                 switch (ver) {
257                 case CSR_PROTAVC:
258                         s = "AV/C";
259                         break;
260                 case CSR_PROTCAL:
261                         s = "CAL";
262                         break;
263                 case CSR_PROTEHS:
264                         s = "EHS";
265                         break;
266                 case CSR_PROTHAVI:
267                         s = "HAVi";
268                         break;
269                 case CSR_PROTCAM104:
270                         s = "1394 Cam 1.04";
271                         break;
272                 case CSR_PROTCAM120:
273                         s = "1394 Cam 1.20";
274                         break;
275                 case CSR_PROTCAM130:
276                         s = "1394 Cam 1.30";
277                         break;
278                 case CSR_PROTDPP:
279                         s = "1394 Direct print";
280                         break;
281                 case CSR_PROTIICP:
282                         s = "Industrial & Instrument";
283                         break;
284                 default:
285                         if (spec != 0)
286                                 s = "unknown 1394TA";
287                 }
288         }
289         if (s != NULL)
290                 snprintf(buf, len, "%s", s);
291 }
292
293 char *
294 crom_desc(struct crom_context *cc, char *buf, int len)
295 {
296         struct csrreg *reg;
297         struct csrdirectory *dir;
298         char *desc, st;
299         u_int16_t crc;
300
301         reg = crom_get(cc);
302         switch (reg->key & CSRTYPE_MASK) {
303         case CSRTYPE_I:
304 #if 0
305                 len -= snprintf(buf, len, "%d", reg->val);
306                 buf += strlen(buf);
307 #else
308                 *buf = '\0';
309 #endif
310                 break;
311         case CSRTYPE_C:
312                 len -= snprintf(buf, len, "offset=0x%04x(%d)",
313                                                 reg->val, reg->val);
314                 buf += strlen(buf);
315                 break;
316         case CSRTYPE_L:
317                 /* XXX fall through */
318         case CSRTYPE_D:
319                 dir = (struct csrdirectory *) (reg + reg->val);
320                 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
321                 len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
322                         dir->crc_len, dir->crc,
323                         (crc == dir->crc) ? "OK" : "NG");
324                 buf += strlen(buf);
325         }
326         switch (reg->key) {
327         case 0x03:
328                 desc = "module_vendor_ID";
329                 break;
330         case 0x04:
331                 desc = "hardware_version";
332                 break;
333         case 0x0c:
334                 desc = "node_capabilities";
335                 break;
336         case 0x12:
337                 desc = "unit_spec_ID";
338                 break;
339         case 0x13:
340                 desc = "unit_sw_version";
341                 crom_desc_specver(0, reg->val, buf, len);
342                 break;
343         case 0x14:
344                 desc = "logical_unit_number";
345                 break;
346         case 0x17:
347                 desc = "model_ID";
348                 break;
349         case 0x38:
350                 desc = "command_set_spec_ID";
351                 break;
352         case 0x39:
353                 desc = "command_set";
354                 break;
355         case 0x3a:
356                 desc = "unit_characteristics";
357                 break;
358         case 0x3b:
359                 desc = "command_set_revision";
360                 break;
361         case 0x3c:
362                 desc = "firmware_revision";
363                 break;
364         case 0x3d:
365                 desc = "reconnect_timeout";
366                 break;
367         case 0x54:
368                 desc = "management_agent";
369                 break;
370         case 0x81:
371                 desc = "text_leaf";
372                 crom_parse_text(cc, buf + strlen(buf), len);
373                 break;
374         case 0xd1:
375                 desc = "unit_directory";
376                 break;
377         case 0xd4:
378                 desc = "logical_unit_directory";
379                 break;
380         default:
381                 desc = "unknown";
382         }
383         return desc;
384 }
385 #endif
386
387 #if defined(_KERNEL) || defined(TEST)
388
389 int
390 crom_add_quad(struct crom_chunk *chunk, u_int32_t entry)
391 {
392         int index;
393
394         index = chunk->data.crc_len;
395         if (index >= CROM_MAX_CHUNK_LEN - 1) {
396                 printf("too large chunk %d\n", index);
397                 return(-1);
398         }
399         chunk->data.buf[index] = entry;
400         chunk->data.crc_len++;
401         return(index);
402 }
403
404 int
405 crom_add_entry(struct crom_chunk *chunk, int key, int val)
406 {
407         struct csrreg *reg;
408         u_int32_t i;
409         
410         reg = (struct csrreg *)&i;
411         reg->key = key;
412         reg->val = val;
413         return(crom_add_quad(chunk, (u_int32_t) i));
414 }
415
416 int
417 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
418                                 struct crom_chunk *child, int key)
419 {
420         int index;
421
422         if (parent == NULL) {
423                 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
424                 return(0);
425         }
426
427         index = crom_add_entry(parent, key, 0);
428         if (index < 0) {
429                 return(-1);
430         }
431         child->ref_chunk = parent;
432         child->ref_index = index;
433         STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
434         return(index);
435 }
436
437 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
438 int
439 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
440                                 struct crom_chunk *chunk, char *buf)
441 {
442         struct csrtext *tl;
443         u_int32_t *p;
444         int len, i;
445         char t[MAX_TEXT];
446
447         len = strlen(buf);
448         if (len > MAX_TEXT) {
449 #if defined(__DragonFly__) || __FreeBSD_version < 500000
450                 printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
451 #else
452                 printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
453 #endif
454                 len = MAX_TEXT;
455         }
456
457         tl = (struct csrtext *) &chunk->data;
458         tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(u_int32_t));
459         tl->spec_id = 0;
460         tl->spec_type = 0;
461         tl->lang_id = 0;
462         bzero(&t[0], roundup2(len, sizeof(u_int32_t)));
463         bcopy(buf, &t[0], len);
464         p = (u_int32_t *)&t[0];
465         for (i = 0; i < howmany(len, sizeof(u_int32_t)); i ++)
466                 tl->text[i] = ntohl(*p++);
467         return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
468 }
469
470 static int
471 crom_copy(u_int32_t *src, u_int32_t *dst, int *offset, int len, int maxlen)
472 {
473         if (*offset + len > maxlen) {
474                 printf("Config. ROM is too large for the buffer\n");
475                 return(-1);
476         }
477         bcopy(src, (char *)(dst + *offset), len * sizeof(u_int32_t));
478         *offset += len;
479         return(0);
480 }
481
482 int
483 crom_load(struct crom_src *src, u_int32_t *buf, int maxlen)
484 {
485         struct crom_chunk *chunk, *parent;
486         struct csrhdr *hdr;
487 #ifdef _KERNEL
488         u_int32_t *ptr;
489         int i;
490 #endif
491         int count, offset;
492         int len;
493
494         offset = 0;
495         /* Determine offset */
496         STAILQ_FOREACH(chunk, &src->chunk_list, link) {
497                 chunk->offset = offset;
498                 /* Assume the offset of the parent is already known */
499                 parent = chunk->ref_chunk;
500                 if (parent != NULL) {
501                         struct csrreg *reg;
502                         reg = (struct csrreg *)
503                                 &parent->data.buf[chunk->ref_index];
504                         reg->val = offset -
505                                 (parent->offset + 1 + chunk->ref_index);
506                 }
507                 offset += 1 + chunk->data.crc_len;
508         }
509
510         /* Calculate CRC and dump to the buffer */
511         len = 1 + src->hdr.info_len;
512         count = 0;
513         if (crom_copy((u_int32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
514                 return(-1);
515         STAILQ_FOREACH(chunk, &src->chunk_list, link) {
516                 chunk->data.crc =
517                         crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
518
519                 len = 1 + chunk->data.crc_len;
520                 if (crom_copy((u_int32_t *)&chunk->data, buf,
521                                         &count, len, maxlen) < 0)
522                         return(-1);
523         }
524         hdr = (struct csrhdr *)buf;
525         hdr->crc_len = count - 1;
526         hdr->crc = crom_crc(&buf[1], hdr->crc_len);
527
528 #ifdef _KERNEL
529         /* byte swap */
530         ptr = buf;
531         for (i = 0; i < count; i ++) {
532                 *ptr = htonl(*ptr);
533                 ptr++;
534         }
535 #endif
536
537         return(count);
538 }
539 #endif
540
541 #ifdef TEST
542 int
543 main () {
544         struct crom_src src;
545         struct crom_chunk root,unit1,unit2,unit3;
546         struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
547         u_int32_t buf[256], *p;
548         int i;
549
550         bzero(&src, sizeof(src));
551         bzero(&root, sizeof(root));
552         bzero(&unit1, sizeof(unit1));
553         bzero(&unit2, sizeof(unit2));
554         bzero(&unit3, sizeof(unit3));
555         bzero(&text1, sizeof(text1));
556         bzero(&text2, sizeof(text2));
557         bzero(&text3, sizeof(text3));
558         bzero(&text3, sizeof(text4));
559         bzero(&text3, sizeof(text5));
560         bzero(&text3, sizeof(text6));
561         bzero(&text3, sizeof(text7));
562         bzero(buf, sizeof(buf));
563
564         /* BUS info sample */
565         src.hdr.info_len = 4;
566         src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
567         src.businfo.eui64.hi = 0x11223344;
568         src.businfo.eui64.lo = 0x55667788;
569         src.businfo.link_spd = FWSPD_S400;
570         src.businfo.generation = 0;
571         src.businfo.max_rom = MAXROM_4;
572         src.businfo.max_rec = 10;
573         src.businfo.cyc_clk_acc = 100;
574         src.businfo.pmc = 0;
575         src.businfo.bmc = 1;
576         src.businfo.isc = 1;
577         src.businfo.cmc = 1;
578         src.businfo.irmc = 1;
579         STAILQ_INIT(&src.chunk_list);
580
581         /* Root directory */
582         crom_add_chunk(&src, NULL, &root, 0);
583         crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
584         /* private company_id */
585         crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
586
587 #ifdef __DragonFly__
588         crom_add_simple_text(&src, &root, &text1, "DragonFly");
589         crom_add_entry(&root, CSRKEY_HW, __DragonFly_cc_version);
590         crom_add_simple_text(&src, &root, &text2, "DragonFly-1");
591 #else
592         crom_add_simple_text(&src, &root, &text1, "FreeBSD");
593         crom_add_entry(&root, CSRKEY_HW, __FreeBSD_version);
594         crom_add_simple_text(&src, &root, &text2, "FreeBSD-5");
595 #endif
596
597         /* SBP unit directory */
598         crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
599         crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
600         crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
601         crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
602         crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
603         /* management_agent */
604         crom_add_entry(&unit1, CROM_MGM, 0x1000);
605         crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
606         /* Device type and LUN */
607         crom_add_entry(&unit1, CROM_LUN, 0);
608         crom_add_entry(&unit1, CSRKEY_MODEL, 1);
609         crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
610
611         /* RFC2734 IPv4 over IEEE1394 */
612         crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
613         crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
614         crom_add_simple_text(&src, &unit2, &text4, "IANA");
615         crom_add_entry(&unit2, CSRKEY_VER, 1);
616         crom_add_simple_text(&src, &unit2, &text5, "IPv4");
617
618         /* RFC3146 IPv6 over IEEE1394 */
619         crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
620         crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
621         crom_add_simple_text(&src, &unit3, &text6, "IANA");
622         crom_add_entry(&unit3, CSRKEY_VER, 2);
623         crom_add_simple_text(&src, &unit3, &text7, "IPv6");
624
625         crom_load(&src, buf, 256);
626         p = buf;
627 #define DUMP_FORMAT     "%08x %08x %08x %08x %08x %08x %08x %08x\n"
628         for (i = 0; i < 256/8; i ++) {
629                 printf(DUMP_FORMAT,
630                         p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
631                 p += 8;
632         }
633         return(0);
634 }
635 #endif