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