Exemplo n.º 1
0
static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
	char buf[BITFORCE_BUFSIZ+1];
	int err, amount;
	char *s;
	struct timeval init_start, init_now;
	int init_sleep, init_count;
	bool ident_first;

	struct cgpu_info *bitforce = usb_alloc_cgpu(&bitforce_drv, 1);

	if (!usb_init(bitforce, dev, found))
		goto shin;

	// Allow 2 complete attempts if the 1st time returns an unrecognised reply
	ident_first = true;
retry:
	init_count = 0;
	init_sleep = REINIT_TIME_FIRST_MS;
	cgtime(&init_start);
reinit:
	bitforce_initialise(bitforce, false);
	if ((err = usb_write(bitforce, BITFORCE_IDENTIFY, BITFORCE_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY)) < 0 || amount != BITFORCE_IDENTIFY_LEN) {
		applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
			bitforce->drv->dname, bitforce->device_path, amount, err);
		goto unshin;
	}

	if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
		init_count++;
		cgtime(&init_now);
		if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
			if (init_count == 2) {
				applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
					bitforce->drv->dname, bitforce->device_path, amount, err);
			}
			nmsleep(init_sleep);
			if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
				init_sleep *= 2;
			goto reinit;
		}

		if (init_count > 0)
			applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
				bitforce->drv->dname, bitforce->device_path, init_count, tdiff(&init_now, &init_start));

		if (err < 0) {
			applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
				bitforce->drv->dname, bitforce->device_path, amount, err);
		} else {
			applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
				bitforce->drv->dname, bitforce->device_path, amount);
		}

		goto unshin;
	}
	buf[amount] = '\0';

	if (unlikely(!strstr(buf, "SHA256"))) {
		if (ident_first) {
			applog(LOG_WARNING, "%s detect (%s) didn't recognise '%s' trying again ...",
				bitforce->drv->dname, bitforce->device_path, buf);
			ident_first = false;
			goto retry;
		}
		applog(LOG_ERR, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
			bitforce->drv->dname, bitforce->device_path, buf);
		goto unshin;
	}

	if (strstr(buf, "SHA256 SC")) {
#ifdef USE_BFLSC
		applog(LOG_DEBUG, "SC device detected, will defer to BFLSC driver");
#else
		applog(LOG_WARNING, "SC device detected but no BFLSC support compiled in!");
#endif
		goto unshin;
	}

	if (likely((!memcmp(buf, ">>>ID: ", 7)) && (s = strstr(buf + 3, ">>>")))) {
		s[0] = '\0';
		bitforce->name = strdup(buf + 7);
	} else {
		bitforce->name = (char *)blank;
	}

	// We have a real BitForce!
	applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
		bitforce->drv->dname, bitforce->device_path, bitforce->name);

	/* Initially enable support for nonce range and disable it later if it
	 * fails */
	if (opt_bfl_noncerange) {
		bitforce->nonce_range = true;
		bitforce->sleep_ms = BITFORCE_SLEEP_MS;
		bitforce->kname = KNAME_RANGE;
	} else {
		bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
		bitforce->kname = KNAME_WORK;
	}

	if (!add_cgpu(bitforce))
		goto unshin;

	update_usb_stats(bitforce);

	mutex_init(&bitforce->device_mutex);

	return true;

unshin:

	usb_uninit(bitforce);

shin:

	if (bitforce->name != blank) {
		free(bitforce->name);
		bitforce->name = NULL;
	}

	bitforce = usb_free_cgpu(bitforce);

	return false;
}
Exemplo n.º 2
0
static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
				int64_t __maybe_unused max_nonce)
{
	const int thr_id = thr->id;
	struct opencl_thread_data *thrdata = thr->cgpu_data;
	struct cgpu_info *gpu = thr->cgpu;
	_clState *clState = clStates[thr_id];
	const cl_kernel *kernel = &clState->kernel;
	const int dynamic_us = opt_dynamic_interval * 1000;

	cl_int status;
	size_t globalThreads[1];
	size_t localThreads[1] = { clState->wsize };
	int64_t hashes;

	/* Windows' timer resolution is only 15ms so oversample 5x */
	if (gpu->dynamic && (++gpu->intervals * dynamic_us) > 70000) {
		struct timeval tv_gpuend;
		double gpu_us;

		gettimeofday(&tv_gpuend, NULL);
		gpu_us = us_tdiff(&tv_gpuend, &gpu->tv_gpustart) / gpu->intervals;
		if (gpu_us > dynamic_us) {
			if (gpu->intensity > MIN_INTENSITY)
				--gpu->intensity;
		} else if (gpu_us < dynamic_us / 2) {
			if (gpu->intensity < MAX_INTENSITY)
				++gpu->intensity;
		}
		memcpy(&(gpu->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
		gpu->intervals = 0;
	}

	set_threads_hashes(clState->vwidth, &hashes, globalThreads, localThreads[0], &gpu->intensity);
	if (hashes > gpu->max_hashes)
		gpu->max_hashes = hashes;

	status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
	if (unlikely(status != CL_SUCCESS)) {
		applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
		return -1;
	}

	if (clState->goffset) {
		size_t global_work_offset[1];

		global_work_offset[0] = work->blk.nonce;
		status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
						globalThreads, localThreads, 0,  NULL, NULL);
	} else
		status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
						globalThreads, localThreads, 0,  NULL, NULL);
	if (unlikely(status != CL_SUCCESS)) {
		applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
		return -1;
	}

	status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
			BUFFERSIZE, thrdata->res, 0, NULL, NULL);
	if (unlikely(status != CL_SUCCESS)) {
		applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
		return -1;
	}

	/* The amount of work scanned can fluctuate when intensity changes
	 * and since we do this one cycle behind, we increment the work more
	 * than enough to prevent repeating work */
	work->blk.nonce += gpu->max_hashes;

	/* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
	clFinish(clState->commandQueue);

	/* FOUND entry is used as a counter to say how many nonces exist */
	if (thrdata->res[FOUND]) {
		/* Clear the buffer again */
		status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
				BUFFERSIZE, blank_res, 0, NULL, NULL);
		if (unlikely(status != CL_SUCCESS)) {
			applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
			return -1;
		}
		applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
		postcalc_hash_async(thr, work, thrdata->res);
		memset(thrdata->res, 0, BUFFERSIZE);
		/* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
		clFinish(clState->commandQueue);
	}

	return hashes;
}
Exemplo n.º 3
0
static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
    char buf[BITFORCE_BUFSIZ+1];
    char devpath[20];
    int err, amount;
    char *s;
    struct timeval init_start, init_now;
    int init_sleep, init_count;
    bool ident_first;

    struct cgpu_info *bitforce = NULL;
    bitforce = calloc(1, sizeof(*bitforce));
    bitforce->drv = &bitforce_drv;
    bitforce->deven = DEV_ENABLED;
    bitforce->threads = 1;

    if (!usb_init(bitforce, dev, found)) {
        applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
               bitforce->drv->dname,
               (int)(bitforce->usbinfo.bus_number),
               (int)(bitforce->usbinfo.device_address));
        goto shin;
    }

    sprintf(devpath, "%d:%d",
            (int)(bitforce->usbinfo.bus_number),
            (int)(bitforce->usbinfo.device_address));


    // Allow 2 complete attempts if the 1st time returns an unrecognised reply
    ident_first = true;
retry:
    init_count = 0;
    init_sleep = REINIT_TIME_FIRST_MS;
    gettimeofday(&init_start, NULL);
reinit:
    bitforce_initialise(bitforce, false);
    if ((err = usb_write(bitforce, BITFORCE_IDENTIFY, BITFORCE_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY)) < 0 || amount != BITFORCE_IDENTIFY_LEN) {
        applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
               bitforce->drv->dname, devpath, amount, err);
        goto unshin;
    }

    if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
        init_count++;
        gettimeofday(&init_now, NULL);
        if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
            if (init_count == 2) {
                applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying",
                       bitforce->drv->dname, devpath, amount, err);
            }
            nmsleep(init_sleep);
            if ((init_sleep * 2) <= REINIT_TIME_MAX_MS)
                init_sleep *= 2;
            goto reinit;
        }

        if (init_count > 0)
            applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
                   bitforce->drv->dname, devpath, init_count, tdiff(&init_now, &init_start));

        if (err < 0) {
            applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
                   bitforce->drv->dname, devpath, amount, err);
        } else {
            applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
                   bitforce->drv->dname, devpath, amount);
        }

        goto unshin;
    }
    buf[amount] = '\0';

    if (unlikely(!strstr(buf, "SHA256"))) {
        if (ident_first) {
            applog(LOG_WARNING, "%s detect (%s) didn't recognise '%s' trying again ...",
                   bitforce->drv->dname, devpath, buf);
            ident_first = false;
            goto retry;
        }
        applog(LOG_ERR, "%s detect (%s) didn't recognise '%s' on 2nd attempt",
               bitforce->drv->dname, devpath, buf);
        goto unshin;
    }

    if (likely((!memcmp(buf, ">>>ID: ", 7)) && (s = strstr(buf + 3, ">>>")))) {
        s[0] = '\0';
        bitforce->name = strdup(buf + 7);
    } else {
        bitforce->name = (char *)blank;
    }

    // We have a real BitForce!
    applog(LOG_DEBUG, "%s (%s) identified as: '%s'",
           bitforce->drv->dname, devpath, bitforce->name);

    /* Initially enable support for nonce range and disable it later if it
     * fails */
    if (opt_bfl_noncerange) {
        bitforce->nonce_range = true;
        bitforce->sleep_ms = BITFORCE_SLEEP_MS;
        bitforce->kname = KNAME_RANGE;
    } else {
        bitforce->sleep_ms = BITFORCE_SLEEP_MS * 5;
        bitforce->kname = KNAME_WORK;
    }

    bitforce->device_path = strdup(devpath);

    if (!add_cgpu(bitforce))
        goto unshin;

    update_usb_stats(bitforce);

    mutex_init(&bitforce->device_mutex);

    return true;

unshin:

    usb_uninit(bitforce);

shin:

    free(bitforce->device_path);

    if (bitforce->name != blank)
        free(bitforce->name);

    if (bitforce->drv->copy)
        free(bitforce->drv);

    free(bitforce);

    return false;
}