~registry/cgminer/git

4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
1
#ifndef BF16_BITFURY16_H
2
#define BF16_BITFURY16_H
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
3
4
#include <stdint.h>
5
6
#include "bf16-spidevice.h"
7
8
/******************************************************
9
 *                      Macros
10
 ******************************************************/
11
12
#define LIST_PUSH_HEAD(_list, _item) { \
13
	if (_list->tail == NULL) {         \
14
		_list->tail = _item;           \
15
		_list->head = _item;           \
16
	} else {                           \
17
		_list->head->prev = _item;     \
18
		_item->next = _list->head;     \
19
		_list->head = _item;           \
20
	}                                  \
21
}
22
23
#define LIST_PUSH_TAIL(_list, _item) { \
24
	if (_list->head == NULL) {         \
25
		_list->head = _item;           \
26
		_list->tail = _item;           \
27
	} else {                           \
28
		_list->tail->next = _item;     \
29
		_item->prev = _list->tail;     \
30
		_list->tail = _item;           \
31
	}                                  \
32
}
33
34
#define LIST_POP_HEAD(_list) {           \
35
	if (_list->head != _list->tail) {    \
36
		_list->head = _list->head->next; \
37
		_list->head->prev = NULL;        \
38
	} else {                             \
39
		_list->head = NULL;              \
40
		_list->tail = NULL;              \
41
	}                                    \
42
}
43
44
#define LIST_POP_TAIL(_list) {           \
45
	if (_list->head != _list->tail) {    \
46
		_list->tail = _list->tail->prev; \
47
		_list->tail->next = NULL;        \
48
	} else {                             \
49
		_list->head = NULL;              \
50
		_list->tail = NULL;              \
51
	}                                    \
52
}
53
54
#define LIST_REMOVE(_list, _item) {          \
55
	if (_list->head != _list->tail) {        \
56
		if (_list->head == _item) {          \
57
			_list->head = _list->head->next; \
58
			_list->head->prev = NULL;        \
59
		} else if (_list->tail == _item) {   \
60
			_list->tail = _list->tail->prev; \
61
			_list->tail->next = NULL;        \
62
		} else {                             \
63
			bf_data_t* prev;                 \
64
			bf_data_t* next;                 \
65
			prev = _item->prev;              \
66
			next = _item->next;              \
67
			prev->next = next;               \
68
			next->prev = prev;               \
69
		}	                                 \
70
	} else {                                 \
71
		_list->head = NULL;                  \
72
		_list->tail = NULL;                  \
73
	}                                        \
74
}
75
76
#define L_LOCK(_list)   pthread_mutex_lock  (&_list->lock);
77
#define L_UNLOCK(_list) pthread_mutex_unlock(&_list->lock);
78
79
/******************************************************
80
 *                    Constants
81
 ******************************************************/
82
83
#define CHIP_COEFF	        4.295
84
#define CHIP_CMD_NUM        8
85
#define CMD_BUFFER_LEN      4096
86
87
/******************************************************
88
 *                   Enumerations
89
 ******************************************************/
90
91
typedef enum {
92
	CHIP_CMD_TASK_STATUS,
93
	CHIP_CMD_TASK_WRITE,
94
	CHIP_CMD_TASK_SWITCH,
95
	CHIP_CMD_READ_NONCE  = 0x04,
96
	CHIP_CMD_SET_CLOCK   = 0x08,
97
	CHIP_CMD_TOGGLE      = 0x10,
98
	CHIP_CMD_SET_MASK    = 0x20,
99
	CHIP_CMD_CREATE_CHANNEL
100
} bf_cmd_code_t;
101
102
/* chip state enumeration */
103
typedef enum {
104
	UNINITIALIZED,
105
	TOGGLE_SET,
106
	CLOCK_SET,
107
	MASK_SET,
108
	TASK_SENT,
109
	TASK_SWITCHED,
110
	FAILING,
111
	DISABLED
112
} bf_chip_status_t;
113
114
enum bf_channel_id {
115
	BF250_NONE  = 0x00,
116
	BF250_LOCAL = 0x04,
117
	BF250_CHAN1 = 0x06,
118
	BF250_CHAN2 = 0x07
119
};
120
121
typedef enum {
122
	RENONCE_STAGE0,
123
	RENONCE_STAGE1,
124
	RENONCE_STAGE2,
125
	RENONCE_STAGE3,
126
	RENONCE_STAGE_FINISHED
127
} bf_renonce_stage_t;
128
129
/******************************************************
130
 *                 Type Definitions
131
 ******************************************************/
132
133
/* list definition */
134
struct bf_data {
135
	struct bf_data* next;
136
	struct bf_data* prev;
137
	void*           data;
138
};
139
140
typedef struct bf_data bf_data_t;
141
142
typedef struct {
143
	bf_data_t*      head;
144
	bf_data_t*      tail;
145
	uint32_t        count;
146
	pthread_mutex_t lock;
147
} bf_list_t;
148
149
/* general chip command staff */
150
typedef struct {
151
	bf_cmd_code_t  cmd_code;
152
	char           cmd_description[32];
153
} bf_cmd_description_t;
154
155
typedef struct {
156
	int8_t         board_id;
157
	int8_t         bcm250_id;
158
	int8_t         chip_id;
159
} bf_chip_address_t;
160
161
typedef struct {
162
	bf_chip_address_t   chip_address;
163
	uint8_t             depth;
164
	bf_cmd_code_t       cmd_code;
165
	uint8_t             data_length;
166
	uint8_t             tx[128];
167
	uint8_t             rx[64];
168
	uint8_t             status;
169
	uint8_t             checksum;
170
	bool                checksum_error;
171
	uint8_t             nonce_checksum;
172
	bool                nonce_checksum_error;
173
} bf_command_t;
174
175
/* work stuff */
176
typedef struct {
177
	uint32_t    midstate[8];
178
	uint32_t    m7;
179
	uint32_t    ntime;
180
	uint32_t    nbits;
181
} bf_payload_t;
182
183
typedef struct {
184
	struct work*    work;
185
	bf_payload_t    payload;
186
	bool            rolled;
187
	time_t          generated;
188
} bf_workd_t;
189
190
typedef struct {
191
	struct work     work;
192
	bf_payload_t    payload;
193
	uint8_t         task[80];
194
} bf_works_t;
195
196
/* nonceworker stuff */
197
typedef struct {
198
	bf_chip_address_t   chip_address;
199
	bf_chip_address_t   src_address;
200
	bf_works_t          cwork;
201
	bf_works_t          owork;
202
	uint32_t            nonce;
203
} bf_noncework_t;
204
205
/* renonceworker stuff */
206
typedef struct {
207
	bf_chip_address_t   src_address;
208
	uint32_t            nonce;
209
} bf_renoncework_t;
210
211
/* nonce recalculation staff */
212
/* nonce list */
213
typedef struct {
214
	uint32_t            nonce;
215
} bf_nonce_t;
216
217
/* task + nonces list */
218
typedef struct {
219
	uint32_t            id;
220
	uint32_t            nonce;
221
	bf_works_t          cwork;
222
	bf_works_t          owork;
223
	bf_chip_address_t   src_address;
224
	bf_renonce_stage_t  stage;
225
	bool                sent;
226
	bool                received;
227
	bool                match;
228
} bf_renonce_t;
229
230
/* command buffer staff */
231
typedef struct bf_cmd {
232
	bf_chip_address_t   chip_address;   /* address of chip calculating result */
233
	bf_chip_address_t   src_address;    /* track chip address during nonce recalculation */
234
	bf_works_t          work;
235
	uint32_t            id;             /* renonce id */
236
	uint8_t             depth;
237
	bf_cmd_code_t       cmd_code;
238
	uint8_t             data_length;
239
	uint8_t             checksum;
240
} bf_cmd_t;
241
242
#define CMD(_item)          ((bf_cmd_t *)          (_item->data))
243
#define NONCE(_item)        ((bf_nonce_t *)        (_item->data))
244
#define RENONCE(_item)      ((bf_renonce_t *)      (_item->data))
245
#define NONCEWORK(_item)    ((bf_noncework_t *)    (_item->data))
246
#define RENONCEWORK(_item)  ((bf_renoncework_t *)  (_item->data))
247
#define WORKD(_item)        ((bf_workd_t *)        (_item->data))
248
#define WORKS(_item)        ((bf_works_t *)        (_item->data))
249
250
typedef struct {
251
	bf_chip_address_t   chip_address;
252
	bf_chip_address_t   src_address;
253
	bf_works_t          work;
254
	uint32_t            id;
255
	bf_cmd_code_t       cmd_code;
256
	uint8_t             status;
257
	uint8_t             checksum_expected;
258
	uint8_t             checksum_received;
259
	bool                checksum_error;
260
	uint8_t             nonce_checksum_expected;
261
	uint8_t             nonce_checksum_received;
262
	bool                nonce_checksum_error;
263
} bf_cmd_status_t;
264
265
typedef enum {
266
	EMPTY,
267
	TX_READY,
268
	EXECUTED
269
} bf_cmd_buffer_status_t;
270
271
typedef struct {
272
	bf_list_t*              cmd_list;
273
274
	uint8_t*                tx_buffer;
275
	uint8_t*                rx_buffer;
276
	uint32_t                free_bytes; /* TX buffer bytes free */
277
	uint32_t                tx_offset;
278
	uint32_t                rx_offset;
279
	bf_cmd_buffer_status_t  status;
280
} bf_cmd_buffer_t;
281
282
/******************************************************
283
 *                    Structures
284
 ******************************************************/
285
286
/******************************************************
287
 *               Static Function Declarations
288
 ******************************************************/
289
290
/******************************************************
291
 *               Variables Definitions
292
 ******************************************************/
293
294
extern bf_cmd_description_t cmd_description[CHIP_CMD_NUM];
295
296
/******************************************************
297
 *               Function Definitions
298
 ******************************************************/
299
300
/* BF16 command primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
301
uint8_t spi_command_init(bf_command_t* command, const uint8_t depth,
302
		const bf_chip_address_t chip_address, const bf_cmd_code_t cmd_code,
303
		const uint8_t data_length, const uint8_t* tx);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
304
uint8_t spi_command_exec(spi_channel_id_t spi_channel, bf_command_t* command, uint32_t* nonces);
305
306
/* data preparation routines */
307
uint8_t gen_clock_data(uint8_t clock, uint8_t prescaler, uint8_t data[4]);
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
308
uint8_t gen_task_data(uint32_t* midstate, uint32_t merkle, uint32_t ntime,
309
		uint32_t nbits, uint32_t mask, uint8_t* task);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
310
uint32_t gen_mask(uint32_t nonce, uint8_t nbits);
311
312
/* SPI BCM250 primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
313
uint8_t create_channel(spi_channel_id_t spi_channel, uint8_t* channel_path, uint8_t channel_length);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
314
uint8_t destroy_channel(spi_channel_id_t spi_channel, uint8_t depth);
315
316
/* SPI BTC16 primitives  */
317
void    spi_emit_reset(spi_channel_id_t spi_channel);
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
318
uint8_t send_toggle(spi_channel_id_t spi_channel, uint8_t depth,
319
		bf_chip_address_t chip_address);
320
uint8_t set_clock(spi_channel_id_t spi_channel, uint8_t depth,
321
		bf_chip_address_t chip_address, uint8_t clock);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
322
323
/* cmd buffer primitives */
324
int8_t  cmd_buffer_init(bf_cmd_buffer_t* cmd_buffer);
325
int8_t  cmd_buffer_deinit(bf_cmd_buffer_t* cmd_buffer);
326
int8_t  cmd_buffer_clear(bf_cmd_buffer_t* cmd_buffer);
327
int8_t  cmd_buffer_push(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
328
		const bf_chip_address_t chip_address, const bf_chip_address_t src_address,
329
		const bf_works_t work, const uint32_t id,
330
		const bf_cmd_code_t cmd_code, const uint8_t data_length, const uint8_t* tx);
331
int8_t  cmd_buffer_pop(bf_cmd_buffer_t* cmd_buffer, bf_cmd_status_t* cmd_status, uint32_t* nonces);
332
int8_t  cmd_buffer_exec(spi_channel_id_t spi_channel, bf_cmd_buffer_t* cmd_buffer);
333
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
334
int8_t  cmd_buffer_push_create_channel(bf_cmd_buffer_t* cmd_buffer, uint8_t* channel_path,
335
		uint8_t channel_length);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
336
int8_t  cmd_buffer_push_destroy_channel(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth);
337
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
338
int8_t  cmd_buffer_push_send_toggle(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
339
		const bf_chip_address_t chip_address);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
340
int8_t  cmd_buffer_push_set_clock(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
341
		const bf_chip_address_t chip_address, uint8_t clock);
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
342
int8_t  cmd_buffer_push_set_mask(bf_cmd_buffer_t* cmd_buffer, const uint8_t depth,
343
		const bf_chip_address_t chip_address, uint8_t mask);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
344
345
char*   get_cmd_description(bf_cmd_code_t cmd_code);
346
347
/* dynamic work list primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
348
bf_list_t* workd_list_init(void);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
349
int8_t workd_list_deinit(bf_list_t* list, struct cgpu_info *bitfury);
350
int8_t workd_list_push(bf_list_t* list, bf_workd_t* work);
351
int8_t workd_list_pop(bf_list_t* list, struct cgpu_info *bitfury);
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
352
int8_t workd_list_remove(bf_list_t* list, bf_works_t* work);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
353
354
/* nonce list primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
355
bf_list_t* nonce_list_init(void);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
356
int8_t nonce_list_deinit(bf_list_t* list);
357
int8_t nonce_list_push(bf_list_t* list, uint32_t nonce);
358
uint32_t nonce_list_pop(bf_list_t* list);
359
360
/* noncework list primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
361
bf_list_t* noncework_list_init(void);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
362
int8_t noncework_list_deinit(bf_list_t* list);
363
int8_t noncework_list_push(bf_list_t* list, bf_chip_address_t chip_address,
364
		bf_chip_address_t src_address, bf_works_t cwork, bf_works_t owork, uint32_t nonce);
365
int8_t noncework_list_pop(bf_list_t* list);
366
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
367
bf_list_t* renoncework_list_init(void);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
368
int8_t renoncework_list_deinit(bf_list_t* list);
369
int8_t renoncework_list_push(bf_list_t* list, bf_chip_address_t src_address, uint32_t nonce);
370
int8_t renoncework_list_pop(bf_list_t* list);
371
	
372
/* renonce list primitives */
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
373
bf_list_t* renonce_list_init(void);
4844.5.1 by Alexander I. Mogilny
initial import of BITFURY16 driver
374
int8_t renonce_list_deinit(bf_list_t* list);
375
int8_t renonce_list_push(bf_list_t* list, uint32_t id, uint32_t nonce, bf_chip_address_t src_address,
376
		bf_works_t cwork, bf_works_t owork);
377
int8_t renonce_list_pop(bf_list_t* list);
378
int8_t renonce_list_remove(bf_list_t* list, bf_data_t* rdata);
379
380
uint8_t find_nonces(uint32_t* curr_nonces, uint32_t* prev_nonces, uint32_t* valid_nonces);
381
bool match_nonce(uint32_t nonce, uint32_t mask, uint8_t nbits);
382
4844.5.2 by Alexander I. Mogilny
merge with cgminer rev 87e3da18638275d2b5adbd0ccd0a17225793bf8f
383
#endif /* BF16_BITFURY16_H */