1 /* -*- c-basic-offset: 8 -*-
3 * fw-card.c - card level functions
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/device.h>
25 #include "fw-transaction.h"
26 #include "fw-topology.h"
28 /* The lib/crc16.c implementation uses the standard (0x8005)
29 * polynomial, but we need the ITU-T (or CCITT) polynomial (0x1021).
30 * The implementation below works on an array of host-endian u32
31 * words, assuming they'll be transmited msb first. */
33 crc16_itu_t(const u32
*buffer
, size_t length
)
39 for (i
= 0; i
< length
; i
++) {
41 for (shift
= 28; shift
>= 0; shift
-= 4 ) {
42 sum
= ((crc
>> 12) ^ (data
>> shift
)) & 0xf;
43 crc
= (crc
<< 4) ^ (sum
<< 12) ^ (sum
<< 5) ^ (sum
);
51 static LIST_HEAD(card_list
);
53 static LIST_HEAD(descriptor_list
);
54 static int descriptor_count
;
56 #define bib_crc(v) ((v) << 0)
57 #define bib_crc_length(v) ((v) << 16)
58 #define bib_info_length(v) ((v) << 24)
60 #define bib_link_speed(v) ((v) << 0)
61 #define bib_generation(v) ((v) << 4)
62 #define bib_max_rom(v) ((v) << 8)
63 #define bib_max_receive(v) ((v) << 12)
64 #define bib_cyc_clk_acc(v) ((v) << 16)
65 #define bib_pmc ((1) << 27)
66 #define bib_bmc ((1) << 28)
67 #define bib_isc ((1) << 29)
68 #define bib_cmc ((1) << 30)
69 #define bib_imc ((1) << 31)
72 generate_config_rom (struct fw_card
*card
, size_t *config_rom_length
)
74 struct fw_descriptor
*desc
;
75 static u32 config_rom
[256];
78 /* Initialize contents of config rom buffer. On the OHCI
79 * controller, block reads to the config rom accesses the host
80 * memory, but quadlet read access the hardware bus info block
81 * registers. That's just crack, but it means we should make
82 * sure the contents of bus info block in host memory mathces
83 * the version stored in the OHCI registers. */
85 memset(config_rom
, 0, sizeof config_rom
);
86 config_rom
[0] = bib_crc_length(4) | bib_info_length(4) | bib_crc(0);
87 config_rom
[1] = 0x31333934;
90 bib_link_speed(card
->link_speed
) |
91 bib_generation(card
->config_rom_generation
++ % 14 + 2) |
93 bib_max_receive(card
->max_receive
) |
94 bib_isc
| bib_cmc
| bib_imc
;
95 config_rom
[3] = card
->guid
>> 32;
96 config_rom
[4] = card
->guid
;
98 /* Generate root directory. */
101 config_rom
[i
++] = 0x0c0083c0; /* node capabilities */
102 config_rom
[i
++] = 0x03d00d1e; /* vendor id */
103 j
= i
+ descriptor_count
;
105 /* Generate root directory entries for descriptors. */
106 list_for_each_entry (desc
, &descriptor_list
, link
) {
107 config_rom
[i
] = desc
->key
| (j
- i
);
112 /* Update root directory length. */
113 config_rom
[5] = (i
- 5 - 1) << 16;
115 /* End of root directory, now copy in descriptors. */
116 list_for_each_entry (desc
, &descriptor_list
, link
) {
117 memcpy(&config_rom
[i
], desc
->data
, desc
->length
* 4);
121 /* Calculate CRCs for all blocks in the config rom. This
122 * assumes that CRC length and info length are identical for
123 * the bus info block, which is always the case for this
125 for (i
= 0; i
< j
; i
+= length
+ 1) {
126 length
= (config_rom
[i
] >> 16) & 0xff;
127 config_rom
[i
] |= crc16_itu_t(&config_rom
[i
+ 1], length
);
130 *config_rom_length
= j
;
136 update_config_roms (void)
138 struct fw_card
*card
;
142 list_for_each_entry (card
, &card_list
, link
) {
143 config_rom
= generate_config_rom(card
, &length
);
144 card
->driver
->set_config_rom(card
, config_rom
, length
);
149 fw_core_add_descriptor (struct fw_descriptor
*desc
)
153 /* Check descriptor is valid; the length of all blocks in the
154 * descriptor has to add up to exactly the length of the
157 while (i
< desc
->length
)
158 i
+= (desc
->data
[i
] >> 16) + 1;
160 if (i
!= desc
->length
)
163 down_write(&fw_bus_type
.subsys
.rwsem
);
165 list_add_tail (&desc
->link
, &descriptor_list
);
167 update_config_roms();
169 up_write(&fw_bus_type
.subsys
.rwsem
);
173 EXPORT_SYMBOL(fw_core_add_descriptor
);
176 fw_core_remove_descriptor (struct fw_descriptor
*desc
)
178 down_write(&fw_bus_type
.subsys
.rwsem
);
180 list_del(&desc
->link
);
182 update_config_roms();
184 up_write(&fw_bus_type
.subsys
.rwsem
);
186 EXPORT_SYMBOL(fw_core_remove_descriptor
);
189 release_card(struct device
*device
)
191 struct fw_card
*card
=
192 container_of(device
, struct fw_card
, card_device
);
198 flush_timer_callback(unsigned long data
)
200 struct fw_card
*card
= (struct fw_card
*)data
;
202 fw_flush_transactions(card
);
206 fw_card_initialize(struct fw_card
*card
, struct fw_card_driver
*driver
,
207 struct device
*device
)
211 card
->index
= index
++;
212 card
->driver
= driver
;
213 card
->device
= device
;
214 card
->current_tlabel
= 0;
215 card
->tlabel_mask
= 0;
218 INIT_LIST_HEAD(&card
->transaction_list
);
219 spin_lock_init(&card
->lock
);
220 setup_timer(&card
->flush_timer
,
221 flush_timer_callback
, (unsigned long)card
);
223 card
->local_node
= NULL
;
225 card
->card_device
.bus
= &fw_bus_type
;
226 card
->card_device
.release
= release_card
;
227 card
->card_device
.parent
= card
->device
;
228 snprintf(card
->card_device
.bus_id
, sizeof card
->card_device
.bus_id
,
229 "fwcard%d", card
->index
);
231 device_initialize(&card
->card_device
);
233 EXPORT_SYMBOL(fw_card_initialize
);
236 fw_card_add(struct fw_card
*card
,
237 u32 max_receive
, u32 link_speed
, u64 guid
)
243 card
->max_receive
= max_receive
;
244 card
->link_speed
= link_speed
;
247 /* FIXME: add #define's for phy registers. */
248 /* Activate link_on bit and contender bit in our self ID packets.*/
249 if (card
->driver
->update_phy_reg(card
, 4, 0, 0x80 | 0x40) < 0)
252 retval
= device_add(&card
->card_device
);
254 fw_error("Failed to register card device.");
258 /* The subsystem grabs a reference when the card is added and
259 * drops it when the driver calls fw_core_remove_card. */
262 down_write(&fw_bus_type
.subsys
.rwsem
);
263 config_rom
= generate_config_rom (card
, &length
);
264 list_add_tail(&card
->link
, &card_list
);
265 up_write(&fw_bus_type
.subsys
.rwsem
);
267 return card
->driver
->enable(card
, config_rom
, length
);
269 EXPORT_SYMBOL(fw_card_add
);
272 /* The next few functions implements a dummy driver that use once a
273 * card driver shuts down an fw_card. This allows the driver to
274 * cleanly unload, as all IO to the card will be handled by the dummy
275 * driver instead of calling into the (possibly) unloaded module. The
276 * dummy driver just fails all IO. */
279 dummy_enable(struct fw_card
*card
, u32
*config_rom
, size_t length
)
286 dummy_update_phy_reg(struct fw_card
*card
, int address
,
287 int clear_bits
, int set_bits
)
293 dummy_set_config_rom(struct fw_card
*card
,
294 u32
*config_rom
, size_t length
)
296 /* We take the card out of card_list before setting the dummy
297 * driver, so this should never get called. */
303 dummy_send_request(struct fw_card
*card
, struct fw_packet
*packet
)
305 packet
->callback(packet
, card
, -ENODEV
);
309 dummy_send_response(struct fw_card
*card
, struct fw_packet
*packet
)
311 packet
->callback(packet
, card
, -ENODEV
);
315 dummy_enable_phys_dma(struct fw_card
*card
,
316 int node_id
, int generation
)
321 static struct fw_card_driver dummy_driver
= {
323 .enable
= dummy_enable
,
324 .update_phy_reg
= dummy_update_phy_reg
,
325 .set_config_rom
= dummy_set_config_rom
,
326 .send_request
= dummy_send_request
,
327 .send_response
= dummy_send_response
,
328 .enable_phys_dma
= dummy_enable_phys_dma
332 fw_core_remove_card(struct fw_card
*card
)
334 card
->driver
->update_phy_reg(card
, 4, 0x80 | 0x40, 0);
335 fw_core_initiate_bus_reset(card
, 1);
337 down_write(&fw_bus_type
.subsys
.rwsem
);
338 list_del(&card
->link
);
339 up_write(&fw_bus_type
.subsys
.rwsem
);
341 /* Set up the dummy driver. */
342 card
->driver
= &dummy_driver
;
344 fw_flush_transactions(card
);
346 fw_destroy_nodes(card
);
348 /* This also drops the subsystem reference. */
349 device_unregister(&card
->card_device
);
351 EXPORT_SYMBOL(fw_core_remove_card
);
354 fw_card_get(struct fw_card
*card
)
356 get_device(&card
->card_device
);
360 EXPORT_SYMBOL(fw_card_get
);
362 /* An assumption for fw_card_put() is that the card driver allocates
363 * the fw_card struct with kalloc and that it has been shut down
364 * before the last ref is dropped. */
366 fw_card_put(struct fw_card
*card
)
368 put_device(&card
->card_device
);
370 EXPORT_SYMBOL(fw_card_put
);
373 fw_core_initiate_bus_reset(struct fw_card
*card
, int short_reset
)
382 return card
->driver
->update_phy_reg(card
, address
, 0, 0x40);
384 EXPORT_SYMBOL(fw_core_initiate_bus_reset
);