HACKING 7.0 KB

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