HACKING 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Driver API
  2. ==========
  3. NOTE: This API is subject to change. It is recommended that you submit your
  4. driver, even if obscure, to the mainline BFGMiner codebase so that it will be
  5. updated when the API changes.
  6. BFGMiner defines 3 different units that drivers can use:
  7. - "Device" is a logical unit used for mining. It is represented by its first
  8. processor's `struct cgpu_info`. Example: ButterFly Labs MiniRig SC.
  9. - "Processor" is a logical work processing unit. It is represented by a `struct
  10. cgpu_info` and one or more `struct thr_info`. Example: a single board within
  11. ButterFly Labs MiniRig SC.
  12. - "Thread" is a sequence of instructions and stack that manages hashing on one
  13. or more Processors within a single Device. It is represented by a `struct
  14. thr_info`.
  15. It should be noted that while every Processor has a `struct thr_info`, this may
  16. not represent the same Thread which is managing hashing on the Processor.
  17. Instead, this `struct thr_info` is only used to store status information needed
  18. for the Processor, and is maintained by the managing Thread in addition to its
  19. own `struct thr_info`. New drivers are encouraged to use an asynchronous model
  20. to manage as many Processors as possible within a single Thread.
  21. struct device_api basics
  22. ------------------------
  23. Every driver defines a `struct device_api`. The `dname` field contains a
  24. short name of the driver. This should be the same name used in the source file:
  25. driver-foobar.c defines `dname` "foobar". The `name` field contains a three-
  26. letter abbreviation for the device, used in the representation of devices. For
  27. example, `dname` "FOO" would result in devices represented as "FOO 0", "FOO 1",
  28. etc and processors represented as "FOO 0a", "FOO 0b", etc.
  29. Drivers must define a function `api_detect`, which is run at startup to detect
  30. devices. For each device (note: NOT each processor), it should allocate a
  31. `struct cgpu_info`, set some basic parameters on it, and call the `add_cgpu`
  32. function with it as an argument. Various values you can initialize are:
  33. .api This MUST be set to your driver's `struct driver_api`!
  34. .deven Should be set to DEV_ENABLED
  35. .procs Number of Processors for this device
  36. .threads Number of threads your device needs - should be either a
  37. multiple of .procs (threads will be allocated to each
  38. Processor), or one (a single thread will be allocated only to
  39. the Device, to manage all Processors)
  40. .name Null-terminated name of the device itself
  41. `api_detect` should return the total number of devices created. It should leave
  42. the device in an unused state, as the user may opt to delete it outright.
  43. Threads
  44. -------
  45. The first interaction BFGMiner will have with a device is by calling the
  46. driver's `thread_prepare` function for each Thread. This occurs while BFGMiner
  47. is still in a single-threaded state, before any Threads have actually started
  48. running independently. It should do only the minimal initialization necessary
  49. to proceed, and return true iff successful.
  50. Once all the Threads are setup, BFGMiner starts them off by calling the
  51. `thread_init` function. This should do all initialization that can occur in
  52. parallel with other Threads.
  53. The driver should specify a `minerloop` to use. For the purposes of this
  54. document, it is assumed you will be using `minerloop_async`. Please note that
  55. the default is currently `minerloop_scanhash`, and much of the documentation
  56. here will NOT work with this `minerloop`.
  57. Processors
  58. ----------
  59. Processors work with `struct work` objects, which each represent a block header
  60. to find a solution for. Before your driver sees a `struct work`, it will be
  61. passed to the function `prepare_work` with pointers to the Processor `struct
  62. thr_info` and the `struct work` as arguments. Most drivers do not need to do
  63. anything at this stage, so feel free to omit the `prepare_work` function.
  64. For each job, the `job_prepare` function is called in advance, with three
  65. arguments: Processor `struct thr_info *`, `struct work *`, and a `uint64_t`
  66. limiting how many nonces to check (starting from `work->blk.nonce`). Unless you
  67. implement a `can_limit_work` function, you will always receive a full nonce
  68. range from 0 to 0xffffffff. `job_prepare` increments `work->blk.nonce` to the
  69. last nonce the processor will be attempting and returns true when successful.
  70. Please note this will be called while the previous job is still executing.
  71. When it is time to actually start the new job, the `job_start` function will be
  72. called. This is given the Processor `struct thr_info *` as its only argument,
  73. and should start the job most recently prepared with `job_prepare`. Note that
  74. it is possible for `job_prepare` to be called for a job that never starts
  75. (another `job_prepare` may be executed to override the previous one instead).
  76. `job_start` must call `mt_job_transition` as soon as the actual switchover to
  77. the new job takes place, and must call `job_start_complete` when successful;
  78. in case of a failure, it should call `job_start_abort` instead. `job_start`
  79. must set `thr->tv_morework` to the time the device expects to need its next
  80. work item. It is generally advisable to set this a bit early to ensure any
  81. delays do not make it late. `job_start` is expected to always succeed and does
  82. not have a return value.
  83. Immediately before `job_start` is called to change from one job to the next,
  84. `job_get_results` will be called to fetch any volatile results from the
  85. previous job. It is provided the Processor's `struct thr_info *` and the
  86. currently executing job's `struct work *`. It should ONLY fetch the raw data
  87. for the results, and not spend any time processing or submitting it. If
  88. `job_get_results` is defined for your driver, it must (directly or indirectly)
  89. ensure `job_results_fetched` is called when complete (including the case of
  90. failure). After the new job has been started, your driver's
  91. `job_process_results` function will be called to complete the submission of
  92. these results with the same arguments, plus a bool to tell you whether the
  93. processor is being stopped. If it is, your driver must call `mt_disable_start`
  94. when it has successfully stopped hashing.
  95. Drivers may define a `poll` function. If this is defined, `thr->tv_poll` must
  96. always be set to a valid time to next execute it, for each Processor.
  97. Whenever a solution is found (at any point), the function `submit_nonce` should
  98. be called, passing the Processor `struct thr_info *`, `struct work *`, and
  99. nonce as arguments. If the solution is invalid (any of the final 32 bits of the
  100. hash are nonzero), it will be recorded as a hardware error and your driver's
  101. `hw_error` function (if one is defined) will be called.
  102. As often as results are processed, your driver should call the `hashes_done`
  103. function with a number of arguments: Processor `struct thr_info *`, count of
  104. hashes completed (including calls to `submit_nonce`), a `struct timeval *`
  105. that tells how long it took to find these hashes (usually time since the last
  106. call to `hashes_done`, and a `uint32_t *` which should usually be NULL.