Blame view

drivers/rapidio/rio-scan.c 32.5 KB
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1
2
3
4
5
6
  /*
   * RapidIO enumeration and discovery support
   *
   * Copyright 2005 MontaVista Software, Inc.
   * Matt Porter <mporter@kernel.crashing.org>
   *
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
7
8
9
10
   * Copyright 2009 Integrated Device Technology, Inc.
   * Alex Bounine <alexandre.bounine@idt.com>
   * - Added Port-Write/Error Management initialization and handling
   *
933af4a6c   Thomas Moll   rapidio: add enab...
11
12
13
14
   * Copyright 2009 Sysgo AG
   * Thomas Moll <thomas.moll@sysgo.com>
   * - Added Input- Output- enable functionality, to allow full communication
   *
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
15
16
17
18
19
   * This program is free software; you can redistribute  it and/or modify it
   * under  the terms of  the GNU General  Public License as published by the
   * Free Software Foundation;  either version 2 of the  License, or (at your
   * option) any later version.
   */
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
20
21
22
23
  #include <linux/types.h>
  #include <linux/kernel.h>
  
  #include <linux/delay.h>
fa78cc517   Matt Porter   [PATCH] rapidio: ...
24
  #include <linux/dma-mapping.h>
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
25
26
27
28
29
30
31
32
  #include <linux/init.h>
  #include <linux/rio.h>
  #include <linux/rio_drv.h>
  #include <linux/rio_ids.h>
  #include <linux/rio_regs.h>
  #include <linux/module.h>
  #include <linux/spinlock.h>
  #include <linux/timer.h>
fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
33
  #include <linux/sched.h>
de25968cc   Tim Schmielau   [PATCH] fix more ...
34
35
  #include <linux/jiffies.h>
  #include <linux/slab.h>
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
36
37
  
  #include "rio.h"
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
38
  static void rio_init_em(struct rio_dev *rdev);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
39
  static int next_destid = 0;
af84ca38a   Alexandre Bounine   rapidio: add hand...
40
  static int next_comptag = 1;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
41

eb188d0e8   Matt Porter   [PATCH] RapidIO s...
42
43
44
45
46
47
48
  static int rio_mport_phys_table[] = {
  	RIO_EFB_PAR_EP_ID,
  	RIO_EFB_PAR_EP_REC_ID,
  	RIO_EFB_SER_EP_ID,
  	RIO_EFB_SER_EP_REC_ID,
  	-1,
  };
de74e00a9   Alexandre Bounine   rapidio: add dest...
49

4ed134bee   Alexandre Bounine   rapidio: update f...
50
  /**
de74e00a9   Alexandre Bounine   rapidio: add dest...
51
   * rio_destid_alloc - Allocate next available destID for given network
4ed134bee   Alexandre Bounine   rapidio: update f...
52
   * @net: RIO network
de74e00a9   Alexandre Bounine   rapidio: add dest...
53
54
55
56
57
58
59
60
61
62
63
   *
   * Returns next available device destination ID for the specified RIO network.
   * Marks allocated ID as one in use.
   * Returns RIO_INVALID_DESTID if new destID is not available.
   */
  static u16 rio_destid_alloc(struct rio_net *net)
  {
  	int destid;
  	struct rio_id_table *idtab = &net->destid_table;
  
  	spin_lock(&idtab->lock);
4ed134bee   Alexandre Bounine   rapidio: update f...
64
  	destid = find_first_zero_bit(idtab->table, idtab->max);
de74e00a9   Alexandre Bounine   rapidio: add dest...
65
66
  
  	if (destid < idtab->max) {
de74e00a9   Alexandre Bounine   rapidio: add dest...
67
68
69
70
71
72
73
74
  		set_bit(destid, idtab->table);
  		destid += idtab->start;
  	} else
  		destid = RIO_INVALID_DESTID;
  
  	spin_unlock(&idtab->lock);
  	return (u16)destid;
  }
4ed134bee   Alexandre Bounine   rapidio: update f...
75
  /**
de74e00a9   Alexandre Bounine   rapidio: add dest...
76
   * rio_destid_reserve - Reserve the specivied destID
4ed134bee   Alexandre Bounine   rapidio: update f...
77
78
   * @net: RIO network
   * @destid: destID to reserve
de74e00a9   Alexandre Bounine   rapidio: add dest...
79
80
   *
   * Tries to reserve the specified destID.
766792860   Masanari Iida   rapidio: Fix kern...
81
   * Returns 0 if successful.
de74e00a9   Alexandre Bounine   rapidio: add dest...
82
83
84
85
86
87
88
89
90
91
92
93
   */
  static int rio_destid_reserve(struct rio_net *net, u16 destid)
  {
  	int oldbit;
  	struct rio_id_table *idtab = &net->destid_table;
  
  	destid -= idtab->start;
  	spin_lock(&idtab->lock);
  	oldbit = test_and_set_bit(destid, idtab->table);
  	spin_unlock(&idtab->lock);
  	return oldbit;
  }
4ed134bee   Alexandre Bounine   rapidio: update f...
94
  /**
de74e00a9   Alexandre Bounine   rapidio: add dest...
95
   * rio_destid_free - free a previously allocated destID
4ed134bee   Alexandre Bounine   rapidio: update f...
96
97
   * @net: RIO network
   * @destid: destID to free
de74e00a9   Alexandre Bounine   rapidio: add dest...
98
99
100
101
102
103
104
105
106
107
108
109
   *
   * Makes the specified destID available for use.
   */
  static void rio_destid_free(struct rio_net *net, u16 destid)
  {
  	struct rio_id_table *idtab = &net->destid_table;
  
  	destid -= idtab->start;
  	spin_lock(&idtab->lock);
  	clear_bit(destid, idtab->table);
  	spin_unlock(&idtab->lock);
  }
4ed134bee   Alexandre Bounine   rapidio: update f...
110
  /**
de74e00a9   Alexandre Bounine   rapidio: add dest...
111
   * rio_destid_first - return first destID in use
4ed134bee   Alexandre Bounine   rapidio: update f...
112
   * @net: RIO network
de74e00a9   Alexandre Bounine   rapidio: add dest...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
   */
  static u16 rio_destid_first(struct rio_net *net)
  {
  	int destid;
  	struct rio_id_table *idtab = &net->destid_table;
  
  	spin_lock(&idtab->lock);
  	destid = find_first_bit(idtab->table, idtab->max);
  	if (destid >= idtab->max)
  		destid = RIO_INVALID_DESTID;
  	else
  		destid += idtab->start;
  	spin_unlock(&idtab->lock);
  	return (u16)destid;
  }
4ed134bee   Alexandre Bounine   rapidio: update f...
128
  /**
de74e00a9   Alexandre Bounine   rapidio: add dest...
129
   * rio_destid_next - return next destID in use
4ed134bee   Alexandre Bounine   rapidio: update f...
130
131
   * @net: RIO network
   * @from: destination ID from which search shall continue
de74e00a9   Alexandre Bounine   rapidio: add dest...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
   */
  static u16 rio_destid_next(struct rio_net *net, u16 from)
  {
  	int destid;
  	struct rio_id_table *idtab = &net->destid_table;
  
  	spin_lock(&idtab->lock);
  	destid = find_next_bit(idtab->table, idtab->max, from);
  	if (destid >= idtab->max)
  		destid = RIO_INVALID_DESTID;
  	else
  		destid += idtab->start;
  	spin_unlock(&idtab->lock);
  	return (u16)destid;
  }
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  /**
   * rio_get_device_id - Get the base/extended device id for a device
   * @port: RIO master port
   * @destid: Destination ID of device
   * @hopcount: Hopcount to device
   *
   * Reads the base/extended device id from a device. Returns the
   * 8/16-bit device ID.
   */
  static u16 rio_get_device_id(struct rio_mport *port, u16 destid, u8 hopcount)
  {
  	u32 result;
  
  	rio_mport_read_config_32(port, destid, hopcount, RIO_DID_CSR, &result);
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
161
  	return RIO_GET_DID(port->sys_size, result);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
162
163
164
165
166
167
168
169
170
171
172
  }
  
  /**
   * rio_set_device_id - Set the base/extended device id for a device
   * @port: RIO master port
   * @destid: Destination ID of device
   * @hopcount: Hopcount to device
   * @did: Device ID value to be written
   *
   * Writes the base/extended device id from a device.
   */
fa78cc517   Matt Porter   [PATCH] rapidio: ...
173
  static void rio_set_device_id(struct rio_mport *port, u16 destid, u8 hopcount, u16 did)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
174
175
  {
  	rio_mport_write_config_32(port, destid, hopcount, RIO_DID_CSR,
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
176
  				  RIO_SET_DID(port->sys_size, did));
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
177
178
179
180
181
182
183
184
185
186
187
  }
  
  /**
   * rio_local_set_device_id - Set the base/extended device id for a port
   * @port: RIO master port
   * @did: Device ID value to be written
   *
   * Writes the base/extended device id from a device.
   */
  static void rio_local_set_device_id(struct rio_mport *port, u16 did)
  {
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
188
189
  	rio_local_write_config_32(port, RIO_DID_CSR, RIO_SET_DID(port->sys_size,
  				did));
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
190
191
192
193
  }
  
  /**
   * rio_clear_locks- Release all host locks and signal enumeration complete
a7071efc2   Alexandre Bounine   rapidio: use devi...
194
   * @net: RIO network to run on
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
195
196
197
198
199
   *
   * Marks the component tag CSR on each device with the enumeration
   * complete flag. When complete, it then release the host locks on
   * each device. Returns 0 on success or %-EINVAL on failure.
   */
a7071efc2   Alexandre Bounine   rapidio: use devi...
200
  static int rio_clear_locks(struct rio_net *net)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
201
  {
a7071efc2   Alexandre Bounine   rapidio: use devi...
202
  	struct rio_mport *port = net->hport;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
203
204
205
  	struct rio_dev *rdev;
  	u32 result;
  	int ret = 0;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
206
207
208
209
210
211
212
213
214
215
216
  	/* Release host device id locks */
  	rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  				  port->host_deviceid);
  	rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  	if ((result & 0xffff) != 0xffff) {
  		printk(KERN_INFO
  		       "RIO: badness when releasing host lock on master port, result %8.8x
  ",
  		       result);
  		ret = -EINVAL;
  	}
a7071efc2   Alexandre Bounine   rapidio: use devi...
217
  	list_for_each_entry(rdev, &net->devices, net_list) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
218
219
220
221
222
223
224
225
226
227
  		rio_write_config_32(rdev, RIO_HOST_DID_LOCK_CSR,
  				    port->host_deviceid);
  		rio_read_config_32(rdev, RIO_HOST_DID_LOCK_CSR, &result);
  		if ((result & 0xffff) != 0xffff) {
  			printk(KERN_INFO
  			       "RIO: badness when releasing host lock on vid %4.4x did %4.4x
  ",
  			       rdev->vid, rdev->did);
  			ret = -EINVAL;
  		}
af84ca38a   Alexandre Bounine   rapidio: add hand...
228
229
230
231
232
233
234
235
236
  
  		/* Mark device as discovered and enable master */
  		rio_read_config_32(rdev,
  				   rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  				   &result);
  		result |= RIO_PORT_GEN_DISCOVERED | RIO_PORT_GEN_MASTER;
  		rio_write_config_32(rdev,
  				    rdev->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  				    result);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  	}
  
  	return ret;
  }
  
  /**
   * rio_enum_host- Set host lock and initialize host destination ID
   * @port: Master port to issue transaction
   *
   * Sets the local host master port lock and destination ID register
   * with the host device ID value. The host device ID value is provided
   * by the platform. Returns %0 on success or %-1 on failure.
   */
  static int rio_enum_host(struct rio_mport *port)
  {
  	u32 result;
  
  	/* Set master port host device id lock */
  	rio_local_write_config_32(port, RIO_HOST_DID_LOCK_CSR,
  				  port->host_deviceid);
  
  	rio_local_read_config_32(port, RIO_HOST_DID_LOCK_CSR, &result);
  	if ((result & 0xffff) != port->host_deviceid)
  		return -1;
  
  	/* Set master port destid and init destid ctr */
  	rio_local_set_device_id(port, port->host_deviceid);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  	return 0;
  }
  
  /**
   * rio_device_has_destid- Test if a device contains a destination ID register
   * @port: Master port to issue transaction
   * @src_ops: RIO device source operations
   * @dst_ops: RIO device destination operations
   *
   * Checks the provided @src_ops and @dst_ops for the necessary transaction
   * capabilities that indicate whether or not a device will implement a
   * destination ID register. Returns 1 if true or 0 if false.
   */
  static int rio_device_has_destid(struct rio_mport *port, int src_ops,
  				 int dst_ops)
  {
fa78cc517   Matt Porter   [PATCH] rapidio: ...
280
281
282
  	u32 mask = RIO_OPS_READ | RIO_OPS_WRITE | RIO_OPS_ATOMIC_TST_SWP | RIO_OPS_ATOMIC_INC | RIO_OPS_ATOMIC_DEC | RIO_OPS_ATOMIC_SET | RIO_OPS_ATOMIC_CLR;
  
  	return !!((src_ops | dst_ops) & mask);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  }
  
  /**
   * rio_release_dev- Frees a RIO device struct
   * @dev: LDM device associated with a RIO device struct
   *
   * Gets the RIO device struct associated a RIO device struct.
   * The RIO device struct is freed.
   */
  static void rio_release_dev(struct device *dev)
  {
  	struct rio_dev *rdev;
  
  	rdev = to_rio_dev(dev);
  	kfree(rdev);
  }
  
  /**
   * rio_is_switch- Tests if a RIO device has switch capabilities
   * @rdev: RIO device
   *
   * Gets the RIO device Processing Element Features register
   * contents and tests for switch capabilities. Returns 1 if
   * the device is a switch or 0 if it is not a switch.
   * The RIO device struct is freed.
   */
  static int rio_is_switch(struct rio_dev *rdev)
  {
  	if (rdev->pef & RIO_PEF_SWITCH)
  		return 1;
  	return 0;
  }
  
  /**
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
   * rio_setup_device- Allocates and sets up a RIO device
   * @net: RIO network
   * @port: Master port to send transactions
   * @destid: Current destination ID
   * @hopcount: Current hopcount
   * @do_enum: Enumeration/Discovery mode flag
   *
   * Allocates a RIO device and configures fields based on configuration
   * space contents. If device has a destination ID register, a destination
   * ID is either assigned in enumeration mode or read from configuration
   * space in discovery mode.  If the device has switch capabilities, then
   * a switch is allocated and configured appropriately. Returns a pointer
   * to a RIO device on success or NULL on failure.
   *
   */
305c891e2   Bill Pemberton   rapidio: remove u...
332
  static struct rio_dev *rio_setup_device(struct rio_net *net,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
333
334
335
  					struct rio_mport *port, u16 destid,
  					u8 hopcount, int do_enum)
  {
5f28c5200   Yang Li   rio: warn_unused_...
336
  	int ret = 0;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
337
  	struct rio_dev *rdev;
5f28c5200   Yang Li   rio: warn_unused_...
338
  	struct rio_switch *rswitch = NULL;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
339
  	int result, rdid;
ded057827   Alexandre Bounine   rapidio: integrat...
340
341
  	size_t size;
  	u32 swpinfo = 0;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
342

ded057827   Alexandre Bounine   rapidio: integrat...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  	size = sizeof(struct rio_dev);
  	if (rio_mport_read_config_32(port, destid, hopcount,
  				     RIO_PEF_CAR, &result))
  		return NULL;
  
  	if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
  		rio_mport_read_config_32(port, destid, hopcount,
  					 RIO_SWP_INFO_CAR, &swpinfo);
  		if (result & RIO_PEF_SWITCH) {
  			size += (RIO_GET_TOTAL_PORTS(swpinfo) *
  				sizeof(rswitch->nextdev[0])) + sizeof(*rswitch);
  		}
  	}
  
  	rdev = kzalloc(size, GFP_KERNEL);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
358
  	if (!rdev)
5f28c5200   Yang Li   rio: warn_unused_...
359
  		return NULL;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
360

eb188d0e8   Matt Porter   [PATCH] RapidIO s...
361
  	rdev->net = net;
ded057827   Alexandre Bounine   rapidio: integrat...
362
363
  	rdev->pef = result;
  	rdev->swpinfo = swpinfo;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
364
365
366
367
368
369
370
371
372
373
374
375
376
  	rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_ID_CAR,
  				 &result);
  	rdev->did = result >> 16;
  	rdev->vid = result & 0xffff;
  	rio_mport_read_config_32(port, destid, hopcount, RIO_DEV_INFO_CAR,
  				 &rdev->device_rev);
  	rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_ID_CAR,
  				 &result);
  	rdev->asm_did = result >> 16;
  	rdev->asm_vid = result & 0xffff;
  	rio_mport_read_config_32(port, destid, hopcount, RIO_ASM_INFO_CAR,
  				 &result);
  	rdev->asm_rev = result >> 16;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
377
  	if (rdev->pef & RIO_PEF_EXT_FEATURES) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
378
  		rdev->efptr = result & 0xffff;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
379
380
381
382
383
384
  		rdev->phys_efptr = rio_mport_get_physefb(port, 0, destid,
  							 hopcount);
  
  		rdev->em_efptr = rio_mport_get_feature(port, 0, destid,
  						hopcount, RIO_EFB_ERR_MGMNT);
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
385
386
387
388
389
  
  	rio_mport_read_config_32(port, destid, hopcount, RIO_SRC_OPS_CAR,
  				 &rdev->src_ops);
  	rio_mport_read_config_32(port, destid, hopcount, RIO_DST_OPS_CAR,
  				 &rdev->dst_ops);
af84ca38a   Alexandre Bounine   rapidio: add hand...
390
391
392
393
394
395
396
397
398
399
  	if (do_enum) {
  		/* Assign component tag to device */
  		if (next_comptag >= 0x10000) {
  			pr_err("RIO: Component Tag Counter Overflow
  ");
  			goto cleanup;
  		}
  		rio_mport_write_config_32(port, destid, hopcount,
  					  RIO_COMPONENT_TAG_CSR, next_comptag);
  		rdev->comp_tag = next_comptag++;
2ec3ba69f   Alexandre Bounine   rapidio: convert ...
400
  		rdev->do_enum = true;
558bda657   Alexandre Bounine   rapidio: use Comp...
401
402
403
404
  	}  else {
  		rio_mport_read_config_32(port, destid, hopcount,
  					 RIO_COMPONENT_TAG_CSR,
  					 &rdev->comp_tag);
af84ca38a   Alexandre Bounine   rapidio: add hand...
405
  	}
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
406
407
408
  	if (rio_device_has_destid(port, rdev->src_ops, rdev->dst_ops)) {
  		if (do_enum) {
  			rio_set_device_id(port, destid, hopcount, next_destid);
de74e00a9   Alexandre Bounine   rapidio: add dest...
409
410
  			rdev->destid = next_destid;
  			next_destid = rio_destid_alloc(net);
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
411
412
  		} else
  			rdev->destid = rio_get_device_id(port, destid, hopcount);
a93192a5d   Alexandre Bounine   rapidio: use comm...
413
414
415
416
417
418
419
420
421
  
  		rdev->hopcount = 0xff;
  	} else {
  		/* Switch device has an associated destID which
  		 * will be adjusted later
  		 */
  		rdev->destid = destid;
  		rdev->hopcount = hopcount;
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
422
423
424
  
  	/* If a PE has both switch and other functions, show it as a switch */
  	if (rio_is_switch(rdev)) {
ded057827   Alexandre Bounine   rapidio: integrat...
425
  		rswitch = rdev->rswitch;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
426
  		rswitch->port_ok = 0;
2ec3ba69f   Alexandre Bounine   rapidio: convert ...
427
  		spin_lock_init(&rswitch->lock);
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
428
429
430
  		rswitch->route_table = kzalloc(sizeof(u8)*
  					RIO_MAX_ROUTE_ENTRIES(port->sys_size),
  					GFP_KERNEL);
5f28c5200   Yang Li   rio: warn_unused_...
431
432
  		if (!rswitch->route_table)
  			goto cleanup;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
433
  		/* Initialize switch route table */
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
434
435
  		for (rdid = 0; rdid < RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  				rdid++)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
436
  			rswitch->route_table[rdid] = RIO_INVALID_ROUTE;
b53c7583e   Kay Sievers   rapidio: struct d...
437
  		dev_set_name(&rdev->dev, "%02x:s:%04x", rdev->net->id,
6ca40c256   Alexandre Bounine   rapidio: change e...
438
  			     rdev->comp_tag & RIO_CTAG_UDEVID);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
439

2ec3ba69f   Alexandre Bounine   rapidio: convert ...
440
441
  		if (do_enum)
  			rio_route_clr_table(rdev, RIO_GLOBAL_TABLE, 0);
07590ff03   Alexandre Bounine   rapidio: add IDT ...
442

a7071efc2   Alexandre Bounine   rapidio: use devi...
443
  		list_add_tail(&rswitch->node, &net->switches);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
444

933af4a6c   Thomas Moll   rapidio: add enab...
445
446
447
448
  	} else {
  		if (do_enum)
  			/*Enable Input Output Port (transmitter reviever)*/
  			rio_enable_rx_tx_port(port, 0, destid, hopcount, 0);
b53c7583e   Kay Sievers   rapidio: struct d...
449
  		dev_set_name(&rdev->dev, "%02x:e:%04x", rdev->net->id,
6ca40c256   Alexandre Bounine   rapidio: change e...
450
  			     rdev->comp_tag & RIO_CTAG_UDEVID);
933af4a6c   Thomas Moll   rapidio: add enab...
451
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
452

2aaf308b9   Alexandre Bounine   rapidio: rework d...
453
  	rdev->dev.parent = &port->dev;
a11650e11   Alexandre Bounine   rapidio: make enu...
454
  	rio_attach_device(rdev);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
455
456
457
458
  
  	device_initialize(&rdev->dev);
  	rdev->dev.release = rio_release_dev;
  	rio_dev_get(rdev);
284901a90   Yang Hongyang   dma-mapping: repl...
459
  	rdev->dma_mask = DMA_BIT_MASK(32);
fa78cc517   Matt Porter   [PATCH] rapidio: ...
460
  	rdev->dev.dma_mask = &rdev->dma_mask;
284901a90   Yang Hongyang   dma-mapping: repl...
461
  	rdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
462

284fb68d0   Alexandre Bounine   rapidio: fix use ...
463
  	if (rdev->dst_ops & RIO_DST_OPS_DOORBELL)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
464
465
  		rio_init_dbell_res(&rdev->riores[RIO_DOORBELL_RESOURCE],
  				   0, 0xffff);
5f28c5200   Yang Li   rio: warn_unused_...
466
467
468
  	ret = rio_add_device(rdev);
  	if (ret)
  		goto cleanup;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
469

eb188d0e8   Matt Porter   [PATCH] RapidIO s...
470
  	return rdev;
5f28c5200   Yang Li   rio: warn_unused_...
471
472
  
  cleanup:
166c050bd   Alexandre Bounine   RapidIO: fix pote...
473
  	if (rswitch)
5f28c5200   Yang Li   rio: warn_unused_...
474
  		kfree(rswitch->route_table);
ded057827   Alexandre Bounine   rapidio: integrat...
475

5f28c5200   Yang Li   rio: warn_unused_...
476
477
  	kfree(rdev);
  	return NULL;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
478
479
480
481
482
483
484
485
486
487
488
  }
  
  /**
   * rio_sport_is_active- Tests if a switch port has an active connection.
   * @port: Master port to send transaction
   * @destid: Associated destination ID for switch
   * @hopcount: Hopcount to reach switch
   * @sport: Switch port number
   *
   * Reads the port error status CSR for a particular switch port to
   * determine if the port has an active link.  Returns
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
489
   * %RIO_PORT_N_ERR_STS_PORT_OK if the port is active or %0 if it is
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
490
491
492
493
494
   * inactive.
   */
  static int
  rio_sport_is_active(struct rio_mport *port, u16 destid, u8 hopcount, int sport)
  {
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
495
  	u32 result = 0;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
496
  	u32 ext_ftr_ptr;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
497
  	ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount, 0);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
498

e5cabeb3d   Alexandre Bounine   rapidio: add Port...
499
500
501
502
503
504
505
  	while (ext_ftr_ptr) {
  		rio_mport_read_config_32(port, destid, hopcount,
  					 ext_ftr_ptr, &result);
  		result = RIO_GET_BLOCK_ID(result);
  		if ((result == RIO_EFB_SER_EP_FREE_ID) ||
  		    (result == RIO_EFB_SER_EP_FREE_ID_V13P) ||
  		    (result == RIO_EFB_SER_EP_FREC_ID))
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
506
  			break;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
507
508
509
510
  
  		ext_ftr_ptr = rio_mport_get_efb(port, 0, destid, hopcount,
  						ext_ftr_ptr);
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
511
512
513
514
515
516
  
  	if (ext_ftr_ptr)
  		rio_mport_read_config_32(port, destid, hopcount,
  					 ext_ftr_ptr +
  					 RIO_PORT_N_ERR_STS_CSR(sport),
  					 &result);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
517
  	return result & RIO_PORT_N_ERR_STS_PORT_OK;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
518
519
520
  }
  
  /**
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
521
522
523
524
525
526
527
528
529
530
   * rio_get_host_deviceid_lock- Reads the Host Device ID Lock CSR on a device
   * @port: Master port to send transaction
   * @hopcount: Number of hops to the device
   *
   * Used during enumeration to read the Host Device ID Lock CSR on a
   * RIO device. Returns the value of the lock register.
   */
  static u16 rio_get_host_deviceid_lock(struct rio_mport *port, u8 hopcount)
  {
  	u32 result;
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
531
  	rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size), hopcount,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
532
533
534
535
536
537
  				 RIO_HOST_DID_LOCK_CSR, &result);
  
  	return (u16) (result & 0xffff);
  }
  
  /**
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
538
539
540
541
   * rio_enum_peer- Recursively enumerate a RIO network through a master port
   * @net: RIO network being enumerated
   * @port: Master port to send transactions
   * @hopcount: Number of hops into the network
68fe4df5d   Alexandre Bounine   rapidio: add rela...
542
543
   * @prev: Previous RIO device connected to the enumerated one
   * @prev_port: Port on previous RIO device
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
544
545
546
547
   *
   * Recursively enumerates a RIO network.  Transactions are sent via the
   * master port passed in @port.
   */
305c891e2   Bill Pemberton   rapidio: remove u...
548
  static int rio_enum_peer(struct rio_net *net, struct rio_mport *port,
68fe4df5d   Alexandre Bounine   rapidio: add rela...
549
  			 u8 hopcount, struct rio_dev *prev, int prev_port)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
550
  {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
551
  	struct rio_dev *rdev;
af84ca38a   Alexandre Bounine   rapidio: add hand...
552
  	u32 regval;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
553
  	int tmp;
e274e0ed0   Alexandre Bounine   rapidio: add devi...
554
555
556
557
558
559
  	if (rio_mport_chk_dev_access(port,
  			RIO_ANY_DESTID(port->sys_size), hopcount)) {
  		pr_debug("RIO: device access check failed
  ");
  		return -1;
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
560
561
562
563
564
  	if (rio_get_host_deviceid_lock(port, hopcount) == port->host_deviceid) {
  		pr_debug("RIO: PE already discovered by this host
  ");
  		/*
  		 * Already discovered by this host. Add it as another
af84ca38a   Alexandre Bounine   rapidio: add hand...
565
  		 * link to the existing device.
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
566
  		 */
af84ca38a   Alexandre Bounine   rapidio: add hand...
567
568
569
570
571
572
573
574
575
576
577
578
579
  		rio_mport_read_config_32(port, RIO_ANY_DESTID(port->sys_size),
  				hopcount, RIO_COMPONENT_TAG_CSR, &regval);
  
  		if (regval) {
  			rdev = rio_get_comptag((regval & 0xffff), NULL);
  
  			if (rdev && prev && rio_is_switch(prev)) {
  				pr_debug("RIO: redundant path to %s
  ",
  					 rio_name(rdev));
  				prev->rswitch->nextdev[prev_port] = rdev;
  			}
  		}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
580
581
582
583
  		return 0;
  	}
  
  	/* Attempt to acquire device lock */
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
584
585
  	rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  				  hopcount,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
586
587
588
589
590
591
  				  RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
  	while ((tmp = rio_get_host_deviceid_lock(port, hopcount))
  	       < port->host_deviceid) {
  		/* Delay a bit */
  		mdelay(1);
  		/* Attempt to acquire device lock again */
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
592
593
  		rio_mport_write_config_32(port, RIO_ANY_DESTID(port->sys_size),
  					  hopcount,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
594
595
596
597
598
599
600
601
602
603
604
605
  					  RIO_HOST_DID_LOCK_CSR,
  					  port->host_deviceid);
  	}
  
  	if (rio_get_host_deviceid_lock(port, hopcount) > port->host_deviceid) {
  		pr_debug(
  		    "RIO: PE locked by a higher priority host...retreating
  ");
  		return -1;
  	}
  
  	/* Setup new RIO device */
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
606
607
608
  	rdev = rio_setup_device(net, port, RIO_ANY_DESTID(port->sys_size),
  					hopcount, 1);
  	if (rdev) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
609
610
  		/* Add device to the global and bus/net specific list. */
  		list_add_tail(&rdev->net_list, &net->devices);
68fe4df5d   Alexandre Bounine   rapidio: add rela...
611
612
613
  		rdev->prev = prev;
  		if (prev && rio_is_switch(prev))
  			prev->rswitch->nextdev[prev_port] = rdev;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
614
615
616
617
  	} else
  		return -1;
  
  	if (rio_is_switch(rdev)) {
de74e00a9   Alexandre Bounine   rapidio: add dest...
618
619
620
621
622
  		int sw_destid;
  		int cur_destid;
  		int sw_inport;
  		u16 destid;
  		int port_num;
ae05cbd5a   Alexandre Bounine   rapidio: use stor...
623
  		sw_inport = RIO_GET_PORT_NUM(rdev->swpinfo);
a93192a5d   Alexandre Bounine   rapidio: use comm...
624
  		rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
818a04a0b   Alexandre Bounine   rapidio: add swit...
625
  				    port->host_deviceid, sw_inport, 0);
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
626
  		rdev->rswitch->route_table[port->host_deviceid] = sw_inport;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
627

de74e00a9   Alexandre Bounine   rapidio: add dest...
628
629
630
631
632
633
634
635
  		destid = rio_destid_first(net);
  		while (destid != RIO_INVALID_DESTID && destid < next_destid) {
  			if (destid != port->host_deviceid) {
  				rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
  						    destid, sw_inport, 0);
  				rdev->rswitch->route_table[destid] = sw_inport;
  			}
  			destid = rio_destid_next(net, destid + 1);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
636
  		}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
637
638
639
  		pr_debug(
  		    "RIO: found %s (vid %4.4x did %4.4x) with %d ports
  ",
68fe4df5d   Alexandre Bounine   rapidio: add rela...
640
641
  		    rio_name(rdev), rdev->vid, rdev->did,
  		    RIO_GET_TOTAL_PORTS(rdev->swpinfo));
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
642
  		sw_destid = next_destid;
68fe4df5d   Alexandre Bounine   rapidio: add rela...
643
644
645
  		for (port_num = 0;
  		     port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  		     port_num++) {
8d4630dcf   Alexandre Bounine   rapidio: apply RX...
646
647
  			if (sw_inport == port_num) {
  				rio_enable_rx_tx_port(port, 0,
933af4a6c   Thomas Moll   rapidio: add enab...
648
649
  					      RIO_ANY_DESTID(port->sys_size),
  					      hopcount, port_num);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
650
  				rdev->rswitch->port_ok |= (1 << port_num);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
651
  				continue;
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
652
  			}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
653
654
655
656
  
  			cur_destid = next_destid;
  
  			if (rio_sport_is_active
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
657
658
  			    (port, RIO_ANY_DESTID(port->sys_size), hopcount,
  			     port_num)) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
659
660
661
662
  				pr_debug(
  				    "RIO: scanning device on port %d
  ",
  				    port_num);
8d4630dcf   Alexandre Bounine   rapidio: apply RX...
663
664
665
  				rio_enable_rx_tx_port(port, 0,
  					      RIO_ANY_DESTID(port->sys_size),
  					      hopcount, port_num);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
666
  				rdev->rswitch->port_ok |= (1 << port_num);
a93192a5d   Alexandre Bounine   rapidio: use comm...
667
  				rio_route_add_entry(rdev, RIO_GLOBAL_TABLE,
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
668
  						RIO_ANY_DESTID(port->sys_size),
818a04a0b   Alexandre Bounine   rapidio: add swit...
669
  						port_num, 0);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
670

68fe4df5d   Alexandre Bounine   rapidio: add rela...
671
672
  				if (rio_enum_peer(net, port, hopcount + 1,
  						  rdev, port_num) < 0)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
673
674
675
  					return -1;
  
  				/* Update routing tables */
de74e00a9   Alexandre Bounine   rapidio: add dest...
676
677
  				destid = rio_destid_next(net, cur_destid + 1);
  				if (destid != RIO_INVALID_DESTID) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
678
  					for (destid = cur_destid;
de74e00a9   Alexandre Bounine   rapidio: add dest...
679
680
681
  					     destid < next_destid;) {
  						if (destid != port->host_deviceid) {
  							rio_route_add_entry(rdev,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
682
683
  								    RIO_GLOBAL_TABLE,
  								    destid,
818a04a0b   Alexandre Bounine   rapidio: add swit...
684
685
  								    port_num,
  								    0);
de74e00a9   Alexandre Bounine   rapidio: add dest...
686
687
688
689
690
691
  							rdev->rswitch->
  								route_table[destid] =
  								port_num;
  						}
  						destid = rio_destid_next(net,
  								destid + 1);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
692
  					}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
693
  				}
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
694
695
696
697
698
699
700
701
  			} else {
  				/* If switch supports Error Management,
  				 * set PORT_LOCKOUT bit for unused port
  				 */
  				if (rdev->em_efptr)
  					rio_set_port_lockout(rdev, port_num, 1);
  
  				rdev->rswitch->port_ok &= ~(1 << port_num);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
702
703
  			}
  		}
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
704

e5cabeb3d   Alexandre Bounine   rapidio: add Port...
705
706
707
708
709
710
711
712
713
714
  		/* Direct Port-write messages to the enumeratiing host */
  		if ((rdev->src_ops & RIO_SRC_OPS_PORT_WRITE) &&
  		    (rdev->em_efptr)) {
  			rio_write_config_32(rdev,
  					rdev->em_efptr + RIO_EM_PW_TGT_DEVID,
  					(port->host_deviceid << 16) |
  					(port->sys_size << 15));
  		}
  
  		rio_init_em(rdev);
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
715
  		/* Check for empty switch */
de74e00a9   Alexandre Bounine   rapidio: add dest...
716
717
  		if (next_destid == sw_destid)
  			next_destid = rio_destid_alloc(net);
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
718

a93192a5d   Alexandre Bounine   rapidio: use comm...
719
  		rdev->destid = sw_destid;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
720
721
722
723
724
725
726
727
728
729
730
731
  	} else
  		pr_debug("RIO: found %s (vid %4.4x did %4.4x)
  ",
  		    rio_name(rdev), rdev->vid, rdev->did);
  
  	return 0;
  }
  
  /**
   * rio_enum_complete- Tests if enumeration of a network is complete
   * @port: Master port to send transaction
   *
a571259f4   Liu Gang   drivers/rapidio/r...
732
   * Tests the PGCCSR discovered bit for non-zero value (enumeration
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
733
   * complete flag). Return %1 if enumeration is complete or %0 if
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
734
735
736
737
   * enumeration is incomplete.
   */
  static int rio_enum_complete(struct rio_mport *port)
  {
af84ca38a   Alexandre Bounine   rapidio: add hand...
738
  	u32 regval;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
739

af84ca38a   Alexandre Bounine   rapidio: add hand...
740
741
  	rio_local_read_config_32(port, port->phys_efptr + RIO_PORT_GEN_CTL_CSR,
  				 &regval);
a571259f4   Liu Gang   drivers/rapidio/r...
742
  	return (regval & RIO_PORT_GEN_DISCOVERED) ? 1 : 0;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
743
744
745
746
747
748
749
750
  }
  
  /**
   * rio_disc_peer- Recursively discovers a RIO network through a master port
   * @net: RIO network being discovered
   * @port: Master port to send transactions
   * @destid: Current destination ID in network
   * @hopcount: Number of hops into the network
9b310acc3   Randy Dunlap   rapidio: fix new ...
751
752
   * @prev: previous rio_dev
   * @prev_port: previous port number
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
753
754
755
756
   *
   * Recursively discovers a RIO network.  Transactions are sent via the
   * master port passed in @port.
   */
305c891e2   Bill Pemberton   rapidio: remove u...
757
  static int
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
758
  rio_disc_peer(struct rio_net *net, struct rio_mport *port, u16 destid,
17e962056   Alexandre Bounine   rapidio: add devi...
759
  	      u8 hopcount, struct rio_dev *prev, int prev_port)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
760
761
  {
  	u8 port_num, route_port;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
762
763
764
765
766
767
768
  	struct rio_dev *rdev;
  	u16 ndestid;
  
  	/* Setup new RIO device */
  	if ((rdev = rio_setup_device(net, port, destid, hopcount, 0))) {
  		/* Add device to the global and bus/net specific list. */
  		list_add_tail(&rdev->net_list, &net->devices);
17e962056   Alexandre Bounine   rapidio: add devi...
769
770
771
  		rdev->prev = prev;
  		if (prev && rio_is_switch(prev))
  			prev->rswitch->nextdev[prev_port] = rdev;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
772
773
774
775
  	} else
  		return -1;
  
  	if (rio_is_switch(rdev)) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
776
  		/* Associated destid is how we accessed this switch */
a93192a5d   Alexandre Bounine   rapidio: use comm...
777
  		rdev->destid = destid;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
778

eb188d0e8   Matt Porter   [PATCH] RapidIO s...
779
780
781
  		pr_debug(
  		    "RIO: found %s (vid %4.4x did %4.4x) with %d ports
  ",
68fe4df5d   Alexandre Bounine   rapidio: add rela...
782
783
784
785
786
  		    rio_name(rdev), rdev->vid, rdev->did,
  		    RIO_GET_TOTAL_PORTS(rdev->swpinfo));
  		for (port_num = 0;
  		     port_num < RIO_GET_TOTAL_PORTS(rdev->swpinfo);
  		     port_num++) {
ae05cbd5a   Alexandre Bounine   rapidio: use stor...
787
  			if (RIO_GET_PORT_NUM(rdev->swpinfo) == port_num)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
788
789
790
791
792
793
794
795
  				continue;
  
  			if (rio_sport_is_active
  			    (port, destid, hopcount, port_num)) {
  				pr_debug(
  				    "RIO: scanning device on port %d
  ",
  				    port_num);
818a04a0b   Alexandre Bounine   rapidio: add swit...
796
797
  
  				rio_lock_device(port, destid, hopcount, 1000);
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
798
799
  				for (ndestid = 0;
  				     ndestid < RIO_ANY_DESTID(port->sys_size);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
800
  				     ndestid++) {
a93192a5d   Alexandre Bounine   rapidio: use comm...
801
  					rio_route_get_entry(rdev,
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
802
803
  							    RIO_GLOBAL_TABLE,
  							    ndestid,
818a04a0b   Alexandre Bounine   rapidio: add swit...
804
  							    &route_port, 0);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
805
806
807
  					if (route_port == port_num)
  						break;
  				}
af84ca38a   Alexandre Bounine   rapidio: add hand...
808
809
  				if (ndestid == RIO_ANY_DESTID(port->sys_size))
  					continue;
818a04a0b   Alexandre Bounine   rapidio: add swit...
810
  				rio_unlock_device(port, destid, hopcount);
17e962056   Alexandre Bounine   rapidio: add devi...
811
812
  				if (rio_disc_peer(net, port, ndestid,
  					hopcount + 1, rdev, port_num) < 0)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
  					return -1;
  			}
  		}
  	} else
  		pr_debug("RIO: found %s (vid %4.4x did %4.4x)
  ",
  		    rio_name(rdev), rdev->vid, rdev->did);
  
  	return 0;
  }
  
  /**
   * rio_mport_is_active- Tests if master port link is active
   * @port: Master port to test
   *
   * Reads the port error status CSR for the master port to
   * determine if the port has an active link.  Returns
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
830
   * %RIO_PORT_N_ERR_STS_PORT_OK if the  master port is active
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
   * or %0 if it is inactive.
   */
  static int rio_mport_is_active(struct rio_mport *port)
  {
  	u32 result = 0;
  	u32 ext_ftr_ptr;
  	int *entry = rio_mport_phys_table;
  
  	do {
  		if ((ext_ftr_ptr =
  		     rio_mport_get_feature(port, 1, 0, 0, *entry)))
  			break;
  	} while (*++entry >= 0);
  
  	if (ext_ftr_ptr)
  		rio_local_read_config_32(port,
  					 ext_ftr_ptr +
  					 RIO_PORT_N_ERR_STS_CSR(port->index),
  					 &result);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
850
  	return result & RIO_PORT_N_ERR_STS_PORT_OK;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
851
852
853
854
855
  }
  
  /**
   * rio_alloc_net- Allocate and configure a new RIO network
   * @port: Master port associated with the RIO network
de74e00a9   Alexandre Bounine   rapidio: add dest...
856
857
   * @do_enum: Enumeration/Discovery mode flag
   * @start: logical minimal start id for new net
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
858
859
860
861
862
863
   *
   * Allocates a RIO network structure, initializes per-network
   * list heads, and adds the associated master port to the
   * network list of associated master ports. Returns a
   * RIO network pointer on success or %NULL on failure.
   */
305c891e2   Bill Pemberton   rapidio: remove u...
864
  static struct rio_net *rio_alloc_net(struct rio_mport *port,
de74e00a9   Alexandre Bounine   rapidio: add dest...
865
  					       int do_enum, u16 start)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
866
867
  {
  	struct rio_net *net;
dd00cc486   Yoann Padioleau   some kmalloc/mems...
868
  	net = kzalloc(sizeof(struct rio_net), GFP_KERNEL);
de74e00a9   Alexandre Bounine   rapidio: add dest...
869
  	if (net && do_enum) {
4ed134bee   Alexandre Bounine   rapidio: update f...
870
871
  		net->destid_table.table = kcalloc(
  			BITS_TO_LONGS(RIO_MAX_ROUTE_ENTRIES(port->sys_size)),
de74e00a9   Alexandre Bounine   rapidio: add dest...
872
873
874
875
876
877
878
879
880
881
  			sizeof(long),
  			GFP_KERNEL);
  
  		if (net->destid_table.table == NULL) {
  			pr_err("RIO: failed to allocate destID table
  ");
  			kfree(net);
  			net = NULL;
  		} else {
  			net->destid_table.start = start;
de74e00a9   Alexandre Bounine   rapidio: add dest...
882
883
884
885
886
  			net->destid_table.max =
  					RIO_MAX_ROUTE_ENTRIES(port->sys_size);
  			spin_lock_init(&net->destid_table.lock);
  		}
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
887
  	if (net) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
888
889
  		INIT_LIST_HEAD(&net->node);
  		INIT_LIST_HEAD(&net->devices);
a7071efc2   Alexandre Bounine   rapidio: use devi...
890
  		INIT_LIST_HEAD(&net->switches);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
891
892
893
  		INIT_LIST_HEAD(&net->mports);
  		list_add_tail(&port->nnode, &net->mports);
  		net->hport = port;
005842efd   Alexandre Bounine   rapidio: run disc...
894
  		net->id = port->id;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
895
896
897
898
899
  	}
  	return net;
  }
  
  /**
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
900
   * rio_update_route_tables- Updates route tables in switches
a7071efc2   Alexandre Bounine   rapidio: use devi...
901
   * @net: RIO network to run update on
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
902
903
904
905
906
   *
   * For each enumerated device, ensure that each switch in a system
   * has correct routing entries. Add routes for devices that where
   * unknown dirung the first enumeration pass through the switch.
   */
a7071efc2   Alexandre Bounine   rapidio: use devi...
907
  static void rio_update_route_tables(struct rio_net *net)
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
908
  {
ded057827   Alexandre Bounine   rapidio: integrat...
909
  	struct rio_dev *rdev, *swrdev;
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
910
911
912
  	struct rio_switch *rswitch;
  	u8 sport;
  	u16 destid;
a7071efc2   Alexandre Bounine   rapidio: use devi...
913
  	list_for_each_entry(rdev, &net->devices, net_list) {
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
914

a93192a5d   Alexandre Bounine   rapidio: use comm...
915
  		destid = rdev->destid;
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
916

a7071efc2   Alexandre Bounine   rapidio: use devi...
917
  		list_for_each_entry(rswitch, &net->switches, node) {
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
918
919
920
921
922
  
  			if (rio_is_switch(rdev)	&& (rdev->rswitch == rswitch))
  				continue;
  
  			if (RIO_INVALID_ROUTE == rswitch->route_table[destid]) {
ded057827   Alexandre Bounine   rapidio: integrat...
923
  				swrdev = sw_to_rio_dev(rswitch);
07590ff03   Alexandre Bounine   rapidio: add IDT ...
924
  				/* Skip if destid ends in empty switch*/
ded057827   Alexandre Bounine   rapidio: integrat...
925
  				if (swrdev->destid == destid)
07590ff03   Alexandre Bounine   rapidio: add IDT ...
926
  					continue;
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
927

ded057827   Alexandre Bounine   rapidio: integrat...
928
  				sport = RIO_GET_PORT_NUM(swrdev->swpinfo);
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
929

2ec3ba69f   Alexandre Bounine   rapidio: convert ...
930
931
932
  				rio_route_add_entry(swrdev, RIO_GLOBAL_TABLE,
  						    destid, sport, 0);
  				rswitch->route_table[destid] = sport;
c70555b05   Alexandre Bounine   [PATCH] rapidio: ...
933
934
935
936
937
938
  			}
  		}
  	}
  }
  
  /**
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
939
   * rio_init_em - Initializes RIO Error Management (for switches)
97ef6f744   Randy Dunlap   rapidio: fix new ...
940
   * @rdev: RIO device
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
941
942
943
944
945
946
947
   *
   * For each enumerated switch, call device-specific error management
   * initialization routine (if supplied by the switch driver).
   */
  static void rio_init_em(struct rio_dev *rdev)
  {
  	if (rio_is_switch(rdev) && (rdev->em_efptr) &&
2ec3ba69f   Alexandre Bounine   rapidio: convert ...
948
949
  	    rdev->rswitch->ops && rdev->rswitch->ops->em_init) {
  		rdev->rswitch->ops->em_init(rdev);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  	}
  }
  
  /**
   * rio_pw_enable - Enables/disables port-write handling by a master port
   * @port: Master port associated with port-write handling
   * @enable:  1=enable,  0=disable
   */
  static void rio_pw_enable(struct rio_mport *port, int enable)
  {
  	if (port->ops->pwenable)
  		port->ops->pwenable(port, enable);
  }
  
  /**
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
965
966
   * rio_enum_mport- Start enumeration through a master port
   * @mport: Master port to send transactions
bc8fcfea1   Alexandre Bounine   rapidio: add enum...
967
   * @flags: Enumeration control flags
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
968
969
970
971
972
973
   *
   * Starts the enumeration process. If somebody has enumerated our
   * master port device, then give up. If not and we have an active
   * link, then start recursive peer enumeration. Returns %0 if
   * enumeration succeeds or %-EBUSY if enumeration fails.
   */
36f0efbbe   Wu Fengguang   drivers/rapidio/r...
974
  static int rio_enum_mport(struct rio_mport *mport, u32 flags)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
975
976
977
978
979
980
981
  {
  	struct rio_net *net = NULL;
  	int rc = 0;
  
  	printk(KERN_INFO "RIO: enumerate master port %d, %s
  ", mport->id,
  	       mport->name);
bc8fcfea1   Alexandre Bounine   rapidio: add enum...
982
983
984
985
986
987
988
989
990
  
  	/*
  	 * To avoid multiple start requests (repeat enumeration is not supported
  	 * by this method) check if enumeration/discovery was performed for this
  	 * mport: if mport was added into the list of mports for a net exit
  	 * with error.
  	 */
  	if (mport->nnode.next || mport->nnode.prev)
  		return -EBUSY;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
991
992
993
994
995
996
997
998
999
1000
1001
1002
  	/* If somebody else enumerated our master port device, bail. */
  	if (rio_enum_host(mport) < 0) {
  		printk(KERN_INFO
  		       "RIO: master port %d device has been enumerated by a remote host
  ",
  		       mport->id);
  		rc = -EBUSY;
  		goto out;
  	}
  
  	/* If master port has an active link, allocate net and enum peers */
  	if (rio_mport_is_active(mport)) {
de74e00a9   Alexandre Bounine   rapidio: add dest...
1003
1004
  		net = rio_alloc_net(mport, 1, 0);
  		if (!net) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1005
1006
1007
1008
1009
  			printk(KERN_ERR "RIO: failed to allocate new net
  ");
  			rc = -ENOMEM;
  			goto out;
  		}
933af4a6c   Thomas Moll   rapidio: add enab...
1010

de74e00a9   Alexandre Bounine   rapidio: add dest...
1011
1012
  		/* reserve mport destID in new net */
  		rio_destid_reserve(net, mport->host_deviceid);
933af4a6c   Thomas Moll   rapidio: add enab...
1013
1014
  		/* Enable Input Output Port (transmitter reviever) */
  		rio_enable_rx_tx_port(mport, 1, 0, 0, 0);
af84ca38a   Alexandre Bounine   rapidio: add hand...
1015
1016
1017
  		/* Set component tag for host */
  		rio_local_write_config_32(mport, RIO_COMPONENT_TAG_CSR,
  					  next_comptag++);
de74e00a9   Alexandre Bounine   rapidio: add dest...
1018
  		next_destid = rio_destid_alloc(net);
68fe4df5d   Alexandre Bounine   rapidio: add rela...
1019
  		if (rio_enum_peer(net, mport, 0, NULL, 0) < 0) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1020
1021
1022
1023
1024
  			/* A higher priority host won enumeration, bail. */
  			printk(KERN_INFO
  			       "RIO: master port %d device has lost enumeration to a remote host
  ",
  			       mport->id);
a7071efc2   Alexandre Bounine   rapidio: use devi...
1025
  			rio_clear_locks(net);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1026
1027
1028
  			rc = -EBUSY;
  			goto out;
  		}
de74e00a9   Alexandre Bounine   rapidio: add dest...
1029
1030
  		/* free the last allocated destID (unused) */
  		rio_destid_free(net, next_destid);
a7071efc2   Alexandre Bounine   rapidio: use devi...
1031
1032
  		rio_update_route_tables(net);
  		rio_clear_locks(net);
e5cabeb3d   Alexandre Bounine   rapidio: add Port...
1033
  		rio_pw_enable(mport, 1);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
  	} else {
  		printk(KERN_INFO "RIO: master port %d link inactive
  ",
  		       mport->id);
  		rc = -EINVAL;
  	}
  
        out:
  	return rc;
  }
  
  /**
   * rio_build_route_tables- Generate route tables from switch route entries
a7071efc2   Alexandre Bounine   rapidio: use devi...
1047
   * @net: RIO network to run route tables scan on
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1048
1049
1050
1051
   *
   * For each switch device, generate a route table by copying existing
   * route entries from the switch.
   */
a7071efc2   Alexandre Bounine   rapidio: use devi...
1052
  static void rio_build_route_tables(struct rio_net *net)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1053
  {
a7071efc2   Alexandre Bounine   rapidio: use devi...
1054
  	struct rio_switch *rswitch;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1055
1056
1057
  	struct rio_dev *rdev;
  	int i;
  	u8 sport;
a7071efc2   Alexandre Bounine   rapidio: use devi...
1058
1059
  	list_for_each_entry(rswitch, &net->switches, node) {
  		rdev = sw_to_rio_dev(rswitch);
818a04a0b   Alexandre Bounine   rapidio: add swit...
1060

a7071efc2   Alexandre Bounine   rapidio: use devi...
1061
1062
1063
1064
1065
1066
1067
1068
1069
  		rio_lock_device(net->hport, rdev->destid,
  				rdev->hopcount, 1000);
  		for (i = 0;
  		     i < RIO_MAX_ROUTE_ENTRIES(net->hport->sys_size);
  		     i++) {
  			if (rio_route_get_entry(rdev, RIO_GLOBAL_TABLE,
  						i, &sport, 0) < 0)
  				continue;
  			rswitch->route_table[i] = sport;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1070
  		}
a7071efc2   Alexandre Bounine   rapidio: use devi...
1071
1072
1073
  
  		rio_unlock_device(net->hport, rdev->destid, rdev->hopcount);
  	}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1074
1075
1076
  }
  
  /**
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1077
1078
   * rio_disc_mport- Start discovery through a master port
   * @mport: Master port to send transactions
bc8fcfea1   Alexandre Bounine   rapidio: add enum...
1079
   * @flags: discovery control flags
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1080
1081
   *
   * Starts the discovery process. If we have an active link,
bc8fcfea1   Alexandre Bounine   rapidio: add enum...
1082
1083
   * then wait for the signal that enumeration is complete (if wait
   * is allowed).
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1084
1085
1086
1087
   * When enumeration completion is signaled, start recursive
   * peer discovery. Returns %0 if discovery succeeds or %-EBUSY
   * on failure.
   */
36f0efbbe   Wu Fengguang   drivers/rapidio/r...
1088
  static int rio_disc_mport(struct rio_mport *mport, u32 flags)
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1089
1090
  {
  	struct rio_net *net = NULL;
fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1091
  	unsigned long to_end;
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1092
1093
1094
1095
1096
1097
1098
  
  	printk(KERN_INFO "RIO: discover master port %d, %s
  ", mport->id,
  	       mport->name);
  
  	/* If master port has an active link, allocate net and discover peers */
  	if (rio_mport_is_active(mport)) {
bc8fcfea1   Alexandre Bounine   rapidio: add enum...
1099
1100
1101
1102
  		if (rio_enum_complete(mport))
  			goto enum_done;
  		else if (flags & RIO_SCAN_ENUM_NO_WAIT)
  			return -EAGAIN;
fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1103
1104
  		pr_debug("RIO: wait for enumeration to complete...
  ");
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1105

fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1106
1107
1108
1109
  		to_end = jiffies + CONFIG_RAPIDIO_DISC_TIMEOUT * HZ;
  		while (time_before(jiffies, to_end)) {
  			if (rio_enum_complete(mport))
  				goto enum_done;
f4c9c0e83   Alexandre Bounine   rapidio: use msle...
1110
  			msleep(10);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1111
  		}
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1112

fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1113
1114
1115
1116
1117
1118
1119
  		pr_debug("RIO: discovery timeout on mport %d %s
  ",
  			 mport->id, mport->name);
  		goto bail;
  enum_done:
  		pr_debug("RIO: ... enumeration done
  ");
de74e00a9   Alexandre Bounine   rapidio: add dest...
1120
  		net = rio_alloc_net(mport, 0, 0);
fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1121
1122
1123
1124
1125
  		if (!net) {
  			printk(KERN_ERR "RIO: Failed to allocate new net
  ");
  			goto bail;
  		}
818a04a0b   Alexandre Bounine   rapidio: add swit...
1126
1127
1128
1129
1130
1131
  
  		/* Read DestID assigned by enumerator */
  		rio_local_read_config_32(mport, RIO_DID_CSR,
  					 &mport->host_deviceid);
  		mport->host_deviceid = RIO_GET_DID(mport->sys_size,
  						   mport->host_deviceid);
e04232360   Zhang Wei   [RAPIDIO] Auto-pr...
1132
  		if (rio_disc_peer(net, mport, RIO_ANY_DESTID(mport->sys_size),
17e962056   Alexandre Bounine   rapidio: add devi...
1133
  					0, NULL, 0) < 0) {
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1134
1135
1136
1137
1138
1139
  			printk(KERN_INFO
  			       "RIO: master port %d device has failed discovery
  ",
  			       mport->id);
  			goto bail;
  		}
a7071efc2   Alexandre Bounine   rapidio: use devi...
1140
  		rio_build_route_tables(net);
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1141
1142
1143
  	}
  
  	return 0;
fa3dbaa01   Alexandre Bounine   rapidio: fix bloc...
1144
  bail:
eb188d0e8   Matt Porter   [PATCH] RapidIO s...
1145
1146
  	return -EBUSY;
  }
a11650e11   Alexandre Bounine   rapidio: make enu...
1147
1148
  
  static struct rio_scan rio_scan_ops = {
9edbc30b4   Alexandre Bounine   rapidio: update e...
1149
  	.owner = THIS_MODULE,
a11650e11   Alexandre Bounine   rapidio: make enu...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
  	.enumerate = rio_enum_mport,
  	.discover = rio_disc_mport,
  };
  
  static bool scan;
  module_param(scan, bool, 0);
  MODULE_PARM_DESC(scan, "Start RapidIO network enumeration/discovery "
  			"(default = 0)");
  
  /**
   * rio_basic_attach:
   *
   * When this enumeration/discovery method is loaded as a module this function
   * registers its specific enumeration and discover routines for all available
   * RapidIO mport devices. The "scan" command line parameter controls ability of
   * the module to start RapidIO enumeration/discovery automatically.
   *
   * Returns 0 for success or -EIO if unable to register itself.
   *
   * This enumeration/discovery method cannot be unloaded and therefore does not
   * provide a matching cleanup_module routine.
   */
  
  static int __init rio_basic_attach(void)
  {
  	if (rio_register_scan(RIO_MPORT_ANY, &rio_scan_ops))
  		return -EIO;
  	if (scan)
  		rio_init_mports();
  	return 0;
  }
  
  late_initcall(rio_basic_attach);
  
  MODULE_DESCRIPTION("Basic RapidIO enumeration/discovery");
  MODULE_LICENSE("GPL");