2 * Copyright (c) 2002-2003
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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:
16 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
34 * $DragonFly: src/sys/bus/firewire/fwcrom.c,v 1.6 2005/03/21 22:07:24 dillon Exp $
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 $");
42 #include <sys/param.h>
43 #if defined(_KERNEL) || defined(TEST)
44 #include <sys/queue.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <netinet/in.h>
62 #include <dev/firewire/firewire.h>
63 #include <dev/firewire/iec13213.h>
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)
70 crom_init_context(struct crom_context *cc, u_int32_t *p)
74 hdr = (struct csrhdr *)p;
75 if (hdr->info_len == 0) {
77 * This isn't supposed to happen but it does. The problem
78 * is possibly related to fw_attach_dev()'s going from an
79 * FWDEVINIT to a FWDEVATTACHED state, or in a host<->host
80 * situation where one host gets the root directory for
81 * another before the other has actually initialized it.
83 printf("crom_init_context: WARNING, info_len is 0\n");
87 if (hdr->info_len == 1) {
91 p += 1 + hdr->info_len;
93 /* check size of root directory */
94 if (((struct csrdirectory *)p)->crc_len == 0) {
99 cc->stack[0].dir = (struct csrdirectory *)p;
100 cc->stack[0].index = 0;
104 crom_get(struct crom_context *cc)
106 struct crom_ptr *ptr;
108 ptr = &cc->stack[cc->depth];
109 return (&ptr->dir->entry[ptr->index]);
113 crom_next(struct crom_context *cc)
115 struct crom_ptr *ptr;
121 if ((reg->key & CSRTYPE_MASK) == CSRTYPE_D) {
122 if (cc->depth >= CROM_MAX_DEPTH) {
123 printf("crom_next: too deep\n");
128 ptr = &cc->stack[cc->depth];
129 ptr->dir = (struct csrdirectory *) (reg + reg->val);
134 ptr = &cc->stack[cc->depth];
137 if (ptr->index < ptr->dir->crc_len &&
138 (vm_offset_t)crom_get(cc) <= CROM_END(cc))
141 if (ptr->index < ptr->dir->crc_len)
142 printf("crom_next: bound check failed\n");
154 crom_search_key(struct crom_context *cc, u_int8_t key)
158 while(cc->depth >= 0) {
168 crom_has_specver(u_int32_t *p, u_int32_t spec, u_int32_t ver)
171 struct crom_context c, *cc;
175 crom_init_context(cc, p);
176 while(cc->depth >= 0) {
179 if (reg->key == CSRKEY_SPEC && reg->val == spec)
184 if (reg->key == CSRKEY_VER && reg->val == ver)
195 crom_parse_text(struct crom_context *cc, char *buf, int len)
198 struct csrtext *textleaf;
201 static char *nullstr = "(null)";
207 if (reg->key != CROM_TEXTLEAF ||
208 (vm_offset_t)(reg + reg->val) > CROM_END(cc)) {
209 strncpy(buf, nullstr, len);
212 textleaf = (struct csrtext *)(reg + reg->val);
214 if ((vm_offset_t)textleaf + textleaf->crc_len > CROM_END(cc)) {
215 strncpy(buf, nullstr, len);
219 /* XXX should check spec and type */
221 bp = (u_int32_t *)&buf[0];
222 qlen = textleaf->crc_len - 2;
225 for (i = 0; i < qlen; i ++)
226 *bp++ = ntohl(textleaf->text[i]);
227 /* make sure to terminate the string */
235 crom_crc(u_int32_t *ptr, int len)
238 u_int32_t data, sum, crc = 0;
240 for (i = 0; i < len; i++) {
242 for (shift = 28; shift >= 0; shift -= 4) {
243 sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
244 crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum;
248 return((u_int16_t) crc);
253 crom_desc_specver(u_int32_t spec, u_int32_t ver, char *buf, int len)
257 if (spec == CSRVAL_ANSIT10 || spec == 0) {
264 s = "unknown ANSIT10";
267 if (spec == CSRVAL_1394TA || spec == 0) {
291 s = "1394 Direct print";
294 s = "Industrial & Instrument";
298 s = "unknown 1394TA";
302 snprintf(buf, len, "%s", s);
306 crom_desc(struct crom_context *cc, char *buf, int len)
309 struct csrdirectory *dir;
314 switch (reg->key & CSRTYPE_MASK) {
317 len -= snprintf(buf, len, "%d", reg->val);
324 len -= snprintf(buf, len, "offset=0x%04x(%d)",
329 /* XXX fall through */
331 dir = (struct csrdirectory *) (reg + reg->val);
332 crc = crom_crc((u_int32_t *)&dir->entry[0], dir->crc_len);
333 len -= snprintf(buf, len, "len=%d crc=0x%04x(%s) ",
334 dir->crc_len, dir->crc,
335 (crc == dir->crc) ? "OK" : "NG");
340 desc = "module_vendor_ID";
343 desc = "hardware_version";
346 desc = "node_capabilities";
349 desc = "unit_spec_ID";
352 desc = "unit_sw_version";
353 crom_desc_specver(0, reg->val, buf, len);
356 desc = "logical_unit_number";
362 desc = "command_set_spec_ID";
365 desc = "command_set";
368 desc = "unit_characteristics";
371 desc = "command_set_revision";
374 desc = "firmware_revision";
377 desc = "reconnect_timeout";
380 desc = "management_agent";
384 crom_parse_text(cc, buf + strlen(buf), len);
387 desc = "unit_directory";
390 desc = "logical_unit_directory";
399 #if defined(_KERNEL) || defined(TEST)
402 crom_add_quad(struct crom_chunk *chunk, u_int32_t entry)
406 index = chunk->data.crc_len;
407 if (index >= CROM_MAX_CHUNK_LEN - 1) {
408 printf("too large chunk %d\n", index);
411 chunk->data.buf[index] = entry;
412 chunk->data.crc_len++;
417 crom_add_entry(struct crom_chunk *chunk, int key, int val)
422 reg = (struct csrreg *)&i;
425 return(crom_add_quad(chunk, (u_int32_t) i));
429 crom_add_chunk(struct crom_src *src, struct crom_chunk *parent,
430 struct crom_chunk *child, int key)
434 if (parent == NULL) {
435 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
439 index = crom_add_entry(parent, key, 0);
443 child->ref_chunk = parent;
444 child->ref_index = index;
445 STAILQ_INSERT_TAIL(&src->chunk_list, child, link);
449 #define MAX_TEXT ((CROM_MAX_CHUNK_LEN + 1) * 4 - sizeof(struct csrtext))
451 crom_add_simple_text(struct crom_src *src, struct crom_chunk *parent,
452 struct crom_chunk *chunk, char *buf)
460 if (len > MAX_TEXT) {
461 #if defined(__DragonFly__) || __FreeBSD_version < 500000
462 printf("text(%d) trancated to %d.\n", len, MAX_TEXT);
464 printf("text(%d) trancated to %td.\n", len, MAX_TEXT);
469 tl = (struct csrtext *) &chunk->data;
470 tl->crc_len = howmany(sizeof(struct csrtext) + len, sizeof(u_int32_t));
474 bzero(&t[0], roundup2(len, sizeof(u_int32_t)));
475 bcopy(buf, &t[0], len);
476 p = (u_int32_t *)&t[0];
477 for (i = 0; i < howmany(len, sizeof(u_int32_t)); i ++)
478 tl->text[i] = ntohl(*p++);
479 return (crom_add_chunk(src, parent, chunk, CROM_TEXTLEAF));
483 crom_copy(u_int32_t *src, u_int32_t *dst, int *offset, int len, int maxlen)
485 if (*offset + len > maxlen) {
486 printf("Config. ROM is too large for the buffer\n");
489 bcopy(src, (char *)(dst + *offset), len * sizeof(u_int32_t));
495 crom_load(struct crom_src *src, u_int32_t *buf, int maxlen)
497 struct crom_chunk *chunk, *parent;
507 /* Determine offset */
508 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
509 chunk->offset = offset;
510 /* Assume the offset of the parent is already known */
511 parent = chunk->ref_chunk;
512 if (parent != NULL) {
514 reg = (struct csrreg *)
515 &parent->data.buf[chunk->ref_index];
517 (parent->offset + 1 + chunk->ref_index);
519 offset += 1 + chunk->data.crc_len;
522 /* Calculate CRC and dump to the buffer */
523 len = 1 + src->hdr.info_len;
525 if (crom_copy((u_int32_t *)&src->hdr, buf, &count, len, maxlen) < 0)
527 STAILQ_FOREACH(chunk, &src->chunk_list, link) {
529 crom_crc(&chunk->data.buf[0], chunk->data.crc_len);
531 len = 1 + chunk->data.crc_len;
532 if (crom_copy((u_int32_t *)&chunk->data, buf,
533 &count, len, maxlen) < 0)
536 hdr = (struct csrhdr *)buf;
537 hdr->crc_len = count - 1;
538 hdr->crc = crom_crc(&buf[1], hdr->crc_len);
543 for (i = 0; i < count; i ++) {
557 struct crom_chunk root,unit1,unit2,unit3;
558 struct crom_chunk text1,text2,text3,text4,text5,text6,text7;
559 u_int32_t buf[256], *p;
562 bzero(&src, sizeof(src));
563 bzero(&root, sizeof(root));
564 bzero(&unit1, sizeof(unit1));
565 bzero(&unit2, sizeof(unit2));
566 bzero(&unit3, sizeof(unit3));
567 bzero(&text1, sizeof(text1));
568 bzero(&text2, sizeof(text2));
569 bzero(&text3, sizeof(text3));
570 bzero(&text3, sizeof(text4));
571 bzero(&text3, sizeof(text5));
572 bzero(&text3, sizeof(text6));
573 bzero(&text3, sizeof(text7));
574 bzero(buf, sizeof(buf));
576 /* BUS info sample */
577 src.hdr.info_len = 4;
578 src.businfo.bus_name = CSR_BUS_NAME_IEEE1394;
579 src.businfo.eui64.hi = 0x11223344;
580 src.businfo.eui64.lo = 0x55667788;
581 src.businfo.link_spd = FWSPD_S400;
582 src.businfo.generation = 0;
583 src.businfo.max_rom = MAXROM_4;
584 src.businfo.max_rec = 10;
585 src.businfo.cyc_clk_acc = 100;
590 src.businfo.irmc = 1;
591 STAILQ_INIT(&src.chunk_list);
594 crom_add_chunk(&src, NULL, &root, 0);
595 crom_add_entry(&root, CSRKEY_NCAP, 0x123456);
596 /* private company_id */
597 crom_add_entry(&root, CSRKEY_VENDOR, 0xacde48);
600 crom_add_simple_text(&src, &root, &text1, "DragonFly");
601 crom_add_entry(&root, CSRKEY_HW, __DragonFly_cc_version);
602 crom_add_simple_text(&src, &root, &text2, "DragonFly-1");
604 crom_add_simple_text(&src, &root, &text1, "FreeBSD");
605 crom_add_entry(&root, CSRKEY_HW, __FreeBSD_version);
606 crom_add_simple_text(&src, &root, &text2, "FreeBSD-5");
609 /* SBP unit directory */
610 crom_add_chunk(&src, &root, &unit1, CROM_UDIR);
611 crom_add_entry(&unit1, CSRKEY_SPEC, CSRVAL_ANSIT10);
612 crom_add_entry(&unit1, CSRKEY_VER, CSRVAL_T10SBP2);
613 crom_add_entry(&unit1, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
614 crom_add_entry(&unit1, CSRKEY_COM_SET, CSRVAL_SCSI);
615 /* management_agent */
616 crom_add_entry(&unit1, CROM_MGM, 0x1000);
617 crom_add_entry(&unit1, CSRKEY_UNIT_CH, (10<<8) | 8);
618 /* Device type and LUN */
619 crom_add_entry(&unit1, CROM_LUN, 0);
620 crom_add_entry(&unit1, CSRKEY_MODEL, 1);
621 crom_add_simple_text(&src, &unit1, &text3, "scsi_target");
623 /* RFC2734 IPv4 over IEEE1394 */
624 crom_add_chunk(&src, &root, &unit2, CROM_UDIR);
625 crom_add_entry(&unit2, CSRKEY_SPEC, CSRVAL_IETF);
626 crom_add_simple_text(&src, &unit2, &text4, "IANA");
627 crom_add_entry(&unit2, CSRKEY_VER, 1);
628 crom_add_simple_text(&src, &unit2, &text5, "IPv4");
630 /* RFC3146 IPv6 over IEEE1394 */
631 crom_add_chunk(&src, &root, &unit3, CROM_UDIR);
632 crom_add_entry(&unit3, CSRKEY_SPEC, CSRVAL_IETF);
633 crom_add_simple_text(&src, &unit3, &text6, "IANA");
634 crom_add_entry(&unit3, CSRKEY_VER, 2);
635 crom_add_simple_text(&src, &unit3, &text7, "IPv6");
637 crom_load(&src, buf, 256);
639 #define DUMP_FORMAT "%08x %08x %08x %08x %08x %08x %08x %08x\n"
640 for (i = 0; i < 256/8; i ++) {
642 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);