| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * IEEE 1394 for Linux | 
 | 3 |  * | 
 | 4 |  * Low level (host adapter) management. | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 1999 Andreas E. Bombe | 
 | 7 |  * Copyright (C) 1999 Emanuel Pirker | 
 | 8 |  * | 
 | 9 |  * This code is licensed under the GPL.  See the file COPYING in the root | 
 | 10 |  * directory of the kernel sources for details. | 
 | 11 |  */ | 
 | 12 |  | 
 | 13 | #include <linux/config.h> | 
 | 14 | #include <linux/module.h> | 
 | 15 | #include <linux/types.h> | 
 | 16 | #include <linux/list.h> | 
 | 17 | #include <linux/init.h> | 
 | 18 | #include <linux/slab.h> | 
 | 19 | #include <linux/pci.h> | 
 | 20 | #include <linux/timer.h> | 
 | 21 |  | 
 | 22 | #include "csr1212.h" | 
 | 23 | #include "ieee1394.h" | 
 | 24 | #include "ieee1394_types.h" | 
 | 25 | #include "hosts.h" | 
 | 26 | #include "ieee1394_core.h" | 
 | 27 | #include "highlevel.h" | 
 | 28 | #include "nodemgr.h" | 
 | 29 | #include "csr.h" | 
 | 30 | #include "config_roms.h" | 
 | 31 |  | 
 | 32 |  | 
 | 33 | static void delayed_reset_bus(void * __reset_info) | 
 | 34 | { | 
 | 35 | 	struct hpsb_host *host = (struct hpsb_host*)__reset_info; | 
 | 36 | 	int generation = host->csr.generation + 1; | 
 | 37 |  | 
 | 38 | 	/* The generation field rolls over to 2 rather than 0 per IEEE | 
 | 39 | 	 * 1394a-2000. */ | 
 | 40 | 	if (generation > 0xf || generation < 2) | 
 | 41 | 		generation = 2; | 
 | 42 |  | 
 | 43 | 	CSR_SET_BUS_INFO_GENERATION(host->csr.rom, generation); | 
 | 44 | 	if (csr1212_generate_csr_image(host->csr.rom) != CSR1212_SUCCESS) { | 
 | 45 | 		/* CSR image creation failed, reset generation field and do not | 
 | 46 | 		 * issue a bus reset. */ | 
 | 47 | 		CSR_SET_BUS_INFO_GENERATION(host->csr.rom, host->csr.generation); | 
 | 48 | 		return; | 
 | 49 | 	} | 
 | 50 |  | 
 | 51 | 	host->csr.generation = generation; | 
 | 52 |  | 
 | 53 | 	host->update_config_rom = 0; | 
 | 54 | 	if (host->driver->set_hw_config_rom) | 
 | 55 | 		host->driver->set_hw_config_rom(host, host->csr.rom->bus_info_data); | 
 | 56 |  | 
 | 57 | 	host->csr.gen_timestamp[host->csr.generation] = jiffies; | 
 | 58 | 	hpsb_reset_bus(host, SHORT_RESET); | 
 | 59 | } | 
 | 60 |  | 
 | 61 | static int dummy_transmit_packet(struct hpsb_host *h, struct hpsb_packet *p) | 
 | 62 | { | 
 | 63 |         return 0; | 
 | 64 | } | 
 | 65 |  | 
 | 66 | static int dummy_devctl(struct hpsb_host *h, enum devctl_cmd c, int arg) | 
 | 67 | { | 
 | 68 |         return -1; | 
 | 69 | } | 
 | 70 |  | 
 | 71 | static int dummy_isoctl(struct hpsb_iso *iso, enum isoctl_cmd command, unsigned long arg) | 
 | 72 | { | 
 | 73 | 	return -1; | 
 | 74 | } | 
 | 75 |  | 
 | 76 | static struct hpsb_host_driver dummy_driver = { | 
 | 77 |         .transmit_packet = dummy_transmit_packet, | 
 | 78 |         .devctl =          dummy_devctl, | 
 | 79 | 	.isoctl =          dummy_isoctl | 
 | 80 | }; | 
 | 81 |  | 
 | 82 | static int alloc_hostnum_cb(struct hpsb_host *host, void *__data) | 
 | 83 | { | 
 | 84 | 	int *hostnum = __data; | 
 | 85 |  | 
 | 86 | 	if (host->id == *hostnum) | 
 | 87 | 		return 1; | 
 | 88 |  | 
 | 89 | 	return 0; | 
 | 90 | } | 
 | 91 |  | 
 | 92 | /** | 
 | 93 |  * hpsb_alloc_host - allocate a new host controller. | 
 | 94 |  * @drv: the driver that will manage the host controller | 
 | 95 |  * @extra: number of extra bytes to allocate for the driver | 
 | 96 |  * | 
 | 97 |  * Allocate a &hpsb_host and initialize the general subsystem specific | 
 | 98 |  * fields.  If the driver needs to store per host data, as drivers | 
 | 99 |  * usually do, the amount of memory required can be specified by the | 
 | 100 |  * @extra parameter.  Once allocated, the driver should initialize the | 
 | 101 |  * driver specific parts, enable the controller and make it available | 
 | 102 |  * to the general subsystem using hpsb_add_host(). | 
 | 103 |  * | 
 | 104 |  * Return Value: a pointer to the &hpsb_host if succesful, %NULL if | 
 | 105 |  * no memory was available. | 
 | 106 |  */ | 
 | 107 | static DECLARE_MUTEX(host_num_alloc); | 
 | 108 |  | 
 | 109 | struct hpsb_host *hpsb_alloc_host(struct hpsb_host_driver *drv, size_t extra, | 
 | 110 | 				  struct device *dev) | 
 | 111 | { | 
 | 112 |         struct hpsb_host *h; | 
 | 113 | 	int i; | 
 | 114 | 	int hostnum = 0; | 
 | 115 |  | 
 | 116 |         h = kmalloc(sizeof(struct hpsb_host) + extra, SLAB_KERNEL); | 
 | 117 |         if (!h) return NULL; | 
 | 118 |         memset(h, 0, sizeof(struct hpsb_host) + extra); | 
 | 119 |  | 
 | 120 | 	h->csr.rom = csr1212_create_csr(&csr_bus_ops, CSR_BUS_INFO_SIZE, h); | 
 | 121 | 	if (!h->csr.rom) { | 
 | 122 | 		kfree(h); | 
 | 123 | 		return NULL; | 
 | 124 | 	} | 
 | 125 |  | 
 | 126 | 	h->hostdata = h + 1; | 
 | 127 |         h->driver = drv; | 
 | 128 |  | 
 | 129 | 	skb_queue_head_init(&h->pending_packet_queue); | 
 | 130 | 	INIT_LIST_HEAD(&h->addr_space); | 
 | 131 |  | 
 | 132 | 	for (i = 2; i < 16; i++) | 
 | 133 | 		h->csr.gen_timestamp[i] = jiffies - 60 * HZ; | 
 | 134 |  | 
 | 135 | 	for (i = 0; i < ARRAY_SIZE(h->tpool); i++) | 
 | 136 | 		HPSB_TPOOL_INIT(&h->tpool[i]); | 
 | 137 |  | 
 | 138 | 	atomic_set(&h->generation, 0); | 
 | 139 |  | 
 | 140 | 	INIT_WORK(&h->delayed_reset, delayed_reset_bus, h); | 
 | 141 | 	 | 
 | 142 | 	init_timer(&h->timeout); | 
 | 143 | 	h->timeout.data = (unsigned long) h; | 
 | 144 | 	h->timeout.function = abort_timedouts; | 
 | 145 | 	h->timeout_interval = HZ / 20; // 50ms by default | 
 | 146 |  | 
 | 147 |         h->topology_map = h->csr.topology_map + 3; | 
 | 148 |         h->speed_map = (u8 *)(h->csr.speed_map + 2); | 
 | 149 |  | 
 | 150 | 	down(&host_num_alloc); | 
 | 151 |  | 
 | 152 | 	while (nodemgr_for_each_host(&hostnum, alloc_hostnum_cb)) | 
 | 153 | 		hostnum++; | 
 | 154 |  | 
 | 155 | 	h->id = hostnum; | 
 | 156 |  | 
 | 157 | 	memcpy(&h->device, &nodemgr_dev_template_host, sizeof(h->device)); | 
 | 158 | 	h->device.parent = dev; | 
 | 159 | 	snprintf(h->device.bus_id, BUS_ID_SIZE, "fw-host%d", h->id); | 
 | 160 |  | 
 | 161 | 	h->class_dev.dev = &h->device; | 
 | 162 | 	h->class_dev.class = &hpsb_host_class; | 
 | 163 | 	snprintf(h->class_dev.class_id, BUS_ID_SIZE, "fw-host%d", h->id); | 
 | 164 |  | 
 | 165 | 	device_register(&h->device); | 
 | 166 | 	class_device_register(&h->class_dev); | 
 | 167 | 	get_device(&h->device); | 
 | 168 |  | 
 | 169 | 	up(&host_num_alloc); | 
 | 170 |  | 
 | 171 | 	return h; | 
 | 172 | } | 
 | 173 |  | 
 | 174 | int hpsb_add_host(struct hpsb_host *host) | 
 | 175 | { | 
 | 176 | 	if (hpsb_default_host_entry(host)) | 
 | 177 | 		return -ENOMEM; | 
 | 178 |  | 
 | 179 | 	hpsb_add_extra_config_roms(host); | 
 | 180 |  | 
 | 181 | 	highlevel_add_host(host); | 
 | 182 |  | 
 | 183 | 	return 0; | 
 | 184 | } | 
 | 185 |  | 
 | 186 | void hpsb_remove_host(struct hpsb_host *host) | 
 | 187 | { | 
 | 188 |         host->is_shutdown = 1; | 
 | 189 |  | 
 | 190 | 	cancel_delayed_work(&host->delayed_reset); | 
 | 191 | 	flush_scheduled_work(); | 
 | 192 |  | 
 | 193 |         host->driver = &dummy_driver; | 
 | 194 |  | 
 | 195 |         highlevel_remove_host(host); | 
 | 196 |  | 
 | 197 | 	hpsb_remove_extra_config_roms(host); | 
 | 198 |  | 
 | 199 | 	class_device_unregister(&host->class_dev); | 
 | 200 | 	device_unregister(&host->device); | 
 | 201 | } | 
 | 202 |  | 
 | 203 | int hpsb_update_config_rom_image(struct hpsb_host *host) | 
 | 204 | { | 
 | 205 | 	unsigned long reset_delay; | 
 | 206 | 	int next_gen = host->csr.generation + 1; | 
 | 207 |  | 
 | 208 | 	if (!host->update_config_rom) | 
 | 209 | 		return -EINVAL; | 
 | 210 |  | 
 | 211 | 	if (next_gen > 0xf) | 
 | 212 | 		next_gen = 2; | 
 | 213 |  | 
 | 214 | 	/* Stop the delayed interrupt, we're about to change the config rom and | 
 | 215 | 	 * it would be a waste to do a bus reset twice. */ | 
 | 216 | 	cancel_delayed_work(&host->delayed_reset); | 
 | 217 |  | 
 | 218 | 	/* IEEE 1394a-2000 prohibits using the same generation number | 
 | 219 | 	 * twice in a 60 second period. */ | 
 | 220 | 	if (jiffies - host->csr.gen_timestamp[next_gen] < 60 * HZ) | 
 | 221 | 		/* Wait 60 seconds from the last time this generation number was | 
 | 222 | 		 * used. */ | 
 | 223 | 		reset_delay = (60 * HZ) + host->csr.gen_timestamp[next_gen] - jiffies; | 
 | 224 | 	else | 
 | 225 | 		/* Wait 1 second in case some other code wants to change the | 
 | 226 | 		 * Config ROM in the near future. */ | 
 | 227 | 		reset_delay = HZ; | 
 | 228 |  | 
 | 229 | 	PREPARE_WORK(&host->delayed_reset, delayed_reset_bus, host); | 
 | 230 | 	schedule_delayed_work(&host->delayed_reset, reset_delay); | 
 | 231 |  | 
 | 232 | 	return 0; | 
 | 233 | } |