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