ccp_hardware.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2017 Chelsio Communications, Inc.
  5. * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
  6. * All rights reserved.
  7. * Largely borrowed from ccr(4), Written by: John Baldwin <jhb@FreeBSD.org>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD$");
  32. #include "opt_ddb.h"
  33. #include <sys/param.h>
  34. #include <sys/bus.h>
  35. #include <sys/lock.h>
  36. #include <sys/kernel.h>
  37. #include <sys/malloc.h>
  38. #include <sys/mutex.h>
  39. #include <sys/module.h>
  40. #include <sys/rman.h>
  41. #include <sys/sglist.h>
  42. #include <sys/sysctl.h>
  43. #ifdef DDB
  44. #include <ddb/ddb.h>
  45. #endif
  46. #include <dev/pci/pcireg.h>
  47. #include <dev/pci/pcivar.h>
  48. #include <machine/bus.h>
  49. #include <machine/resource.h>
  50. #include <machine/vmparam.h>
  51. #include <opencrypto/cryptodev.h>
  52. #include <opencrypto/xform.h>
  53. #include <vm/vm.h>
  54. #include <vm/pmap.h>
  55. #include "cryptodev_if.h"
  56. #include "ccp.h"
  57. #include "ccp_hardware.h"
  58. #include "ccp_lsb.h"
  59. CTASSERT(sizeof(struct ccp_desc) == 32);
  60. static struct ccp_xts_unitsize_map_entry {
  61. enum ccp_xts_unitsize cxu_id;
  62. unsigned cxu_size;
  63. } ccp_xts_unitsize_map[] = {
  64. { CCP_XTS_AES_UNIT_SIZE_16, 16 },
  65. { CCP_XTS_AES_UNIT_SIZE_512, 512 },
  66. { CCP_XTS_AES_UNIT_SIZE_1024, 1024 },
  67. { CCP_XTS_AES_UNIT_SIZE_2048, 2048 },
  68. { CCP_XTS_AES_UNIT_SIZE_4096, 4096 },
  69. };
  70. SYSCTL_NODE(_hw, OID_AUTO, ccp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  71. "ccp node");
  72. unsigned g_ccp_ring_order = 11;
  73. SYSCTL_UINT(_hw_ccp, OID_AUTO, ring_order, CTLFLAG_RDTUN, &g_ccp_ring_order,
  74. 0, "Set CCP ring order. (1 << this) == ring size. Min: 6, Max: 16");
  75. /*
  76. * Zero buffer, sufficient for padding LSB entries, that does not span a page
  77. * boundary
  78. */
  79. static const char g_zeroes[32] __aligned(32);
  80. static inline uint32_t
  81. ccp_read_4(struct ccp_softc *sc, uint32_t offset)
  82. {
  83. return (bus_space_read_4(sc->pci_bus_tag, sc->pci_bus_handle, offset));
  84. }
  85. static inline void
  86. ccp_write_4(struct ccp_softc *sc, uint32_t offset, uint32_t value)
  87. {
  88. bus_space_write_4(sc->pci_bus_tag, sc->pci_bus_handle, offset, value);
  89. }
  90. static inline uint32_t
  91. ccp_read_queue_4(struct ccp_softc *sc, unsigned queue, uint32_t offset)
  92. {
  93. /*
  94. * Each queue gets its own 4kB register space. Queue 0 is at 0x1000.
  95. */
  96. return (ccp_read_4(sc, (CMD_Q_STATUS_INCR * (1 + queue)) + offset));
  97. }
  98. static inline void
  99. ccp_write_queue_4(struct ccp_softc *sc, unsigned queue, uint32_t offset,
  100. uint32_t value)
  101. {
  102. ccp_write_4(sc, (CMD_Q_STATUS_INCR * (1 + queue)) + offset, value);
  103. }
  104. void
  105. ccp_queue_write_tail(struct ccp_queue *qp)
  106. {
  107. ccp_write_queue_4(qp->cq_softc, qp->cq_qindex, CMD_Q_TAIL_LO_BASE,
  108. ((uint32_t)qp->desc_ring_bus_addr) + (Q_DESC_SIZE * qp->cq_tail));
  109. }
  110. /*
  111. * Given a queue and a reserved LSB entry index, compute the LSB *entry id* of
  112. * that entry for the queue's private LSB region.
  113. */
  114. static inline uint8_t
  115. ccp_queue_lsb_entry(struct ccp_queue *qp, unsigned lsb_entry)
  116. {
  117. return ((qp->private_lsb * LSB_REGION_LENGTH + lsb_entry));
  118. }
  119. /*
  120. * Given a queue and a reserved LSB entry index, compute the LSB *address* of
  121. * that entry for the queue's private LSB region.
  122. */
  123. static inline uint32_t
  124. ccp_queue_lsb_address(struct ccp_queue *qp, unsigned lsb_entry)
  125. {
  126. return (ccp_queue_lsb_entry(qp, lsb_entry) * LSB_ENTRY_SIZE);
  127. }
  128. /*
  129. * Some terminology:
  130. *
  131. * LSB - Local Storage Block
  132. * =========================
  133. *
  134. * 8 segments/regions, each containing 16 entries.
  135. *
  136. * Each entry contains 256 bits (32 bytes).
  137. *
  138. * Segments are virtually addressed in commands, but accesses cannot cross
  139. * segment boundaries. Virtual map uses an identity mapping by default
  140. * (virtual segment N corresponds to physical segment N).
  141. *
  142. * Access to a physical region can be restricted to any subset of all five
  143. * queues.
  144. *
  145. * "Pass-through" mode
  146. * ===================
  147. *
  148. * Pass-through is a generic DMA engine, much like ioat(4). Some nice
  149. * features:
  150. *
  151. * - Supports byte-swapping for endian conversion (32- or 256-bit words)
  152. * - AND, OR, XOR with fixed 256-bit mask
  153. * - CRC32 of data (may be used in tandem with bswap, but not bit operations)
  154. * - Read/write of LSB
  155. * - Memset
  156. *
  157. * If bit manipulation mode is enabled, input must be a multiple of 256 bits
  158. * (32 bytes).
  159. *
  160. * If byte-swapping is enabled, input must be a multiple of the word size.
  161. *
  162. * Zlib mode -- only usable from one queue at a time, single job at a time.
  163. * ========================================================================
  164. *
  165. * Only usable from private host, aka PSP? Not host processor?
  166. *
  167. * RNG.
  168. * ====
  169. *
  170. * Raw bits are conditioned with AES and fed through CTR_DRBG. Output goes in
  171. * a ring buffer readable by software.
  172. *
  173. * NIST SP 800-90B Repetition Count and Adaptive Proportion health checks are
  174. * implemented on the raw input stream and may be enabled to verify min-entropy
  175. * of 0.5 bits per bit.
  176. */
  177. static void
  178. ccp_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  179. {
  180. bus_addr_t *baddr;
  181. KASSERT(error == 0, ("%s: error:%d", __func__, error));
  182. baddr = arg;
  183. *baddr = segs->ds_addr;
  184. }
  185. static int
  186. ccp_hw_attach_queue(device_t dev, uint64_t lsbmask, unsigned queue)
  187. {
  188. struct ccp_softc *sc;
  189. struct ccp_queue *qp;
  190. void *desc;
  191. size_t ringsz, num_descriptors;
  192. int error;
  193. desc = NULL;
  194. sc = device_get_softc(dev);
  195. qp = &sc->queues[queue];
  196. /*
  197. * Don't bother allocating a ring for queues the host isn't allowed to
  198. * drive.
  199. */
  200. if ((sc->valid_queues & (1 << queue)) == 0)
  201. return (0);
  202. ccp_queue_decode_lsb_regions(sc, lsbmask, queue);
  203. /* Ignore queues that do not have any LSB access. */
  204. if (qp->lsb_mask == 0) {
  205. device_printf(dev, "Ignoring queue %u with no LSB access\n",
  206. queue);
  207. sc->valid_queues &= ~(1 << queue);
  208. return (0);
  209. }
  210. num_descriptors = 1 << sc->ring_size_order;
  211. ringsz = sizeof(struct ccp_desc) * num_descriptors;
  212. /*
  213. * "Queue_Size" is order - 1.
  214. *
  215. * Queue must be aligned to 5+Queue_Size+1 == 5 + order bits.
  216. */
  217. error = bus_dma_tag_create(bus_get_dma_tag(dev),
  218. 1 << (5 + sc->ring_size_order),
  219. #if defined(__i386__) && !defined(PAE)
  220. 0, BUS_SPACE_MAXADDR,
  221. #else
  222. (bus_addr_t)1 << 32, BUS_SPACE_MAXADDR_48BIT,
  223. #endif
  224. BUS_SPACE_MAXADDR, NULL, NULL, ringsz, 1,
  225. ringsz, 0, NULL, NULL, &qp->ring_desc_tag);
  226. if (error != 0)
  227. goto out;
  228. error = bus_dmamem_alloc(qp->ring_desc_tag, &desc,
  229. BUS_DMA_ZERO | BUS_DMA_WAITOK, &qp->ring_desc_map);
  230. if (error != 0)
  231. goto out;
  232. error = bus_dmamap_load(qp->ring_desc_tag, qp->ring_desc_map, desc,
  233. ringsz, ccp_dmamap_cb, &qp->desc_ring_bus_addr, BUS_DMA_WAITOK);
  234. if (error != 0)
  235. goto out;
  236. qp->desc_ring = desc;
  237. qp->completions_ring = malloc(num_descriptors *
  238. sizeof(*qp->completions_ring), M_CCP, M_ZERO | M_WAITOK);
  239. /* Zero control register; among other things, clears the RUN flag. */
  240. qp->qcontrol = 0;
  241. ccp_write_queue_4(sc, queue, CMD_Q_CONTROL_BASE, qp->qcontrol);
  242. ccp_write_queue_4(sc, queue, CMD_Q_INT_ENABLE_BASE, 0);
  243. /* Clear any leftover interrupt status flags */
  244. ccp_write_queue_4(sc, queue, CMD_Q_INTERRUPT_STATUS_BASE,
  245. ALL_INTERRUPTS);
  246. qp->qcontrol |= (sc->ring_size_order - 1) << CMD_Q_SIZE_SHIFT;
  247. ccp_write_queue_4(sc, queue, CMD_Q_TAIL_LO_BASE,
  248. (uint32_t)qp->desc_ring_bus_addr);
  249. ccp_write_queue_4(sc, queue, CMD_Q_HEAD_LO_BASE,
  250. (uint32_t)qp->desc_ring_bus_addr);
  251. /*
  252. * Enable completion interrupts, as well as error or administrative
  253. * halt interrupts. We don't use administrative halts, but they
  254. * shouldn't trip unless we do, so it ought to be harmless.
  255. */
  256. ccp_write_queue_4(sc, queue, CMD_Q_INT_ENABLE_BASE,
  257. INT_COMPLETION | INT_ERROR | INT_QUEUE_STOPPED);
  258. qp->qcontrol |= (qp->desc_ring_bus_addr >> 32) << CMD_Q_PTR_HI_SHIFT;
  259. qp->qcontrol |= CMD_Q_RUN;
  260. ccp_write_queue_4(sc, queue, CMD_Q_CONTROL_BASE, qp->qcontrol);
  261. out:
  262. if (error != 0) {
  263. if (qp->desc_ring != NULL)
  264. bus_dmamap_unload(qp->ring_desc_tag,
  265. qp->ring_desc_map);
  266. if (desc != NULL)
  267. bus_dmamem_free(qp->ring_desc_tag, desc,
  268. qp->ring_desc_map);
  269. if (qp->ring_desc_tag != NULL)
  270. bus_dma_tag_destroy(qp->ring_desc_tag);
  271. }
  272. return (error);
  273. }
  274. static void
  275. ccp_hw_detach_queue(device_t dev, unsigned queue)
  276. {
  277. struct ccp_softc *sc;
  278. struct ccp_queue *qp;
  279. sc = device_get_softc(dev);
  280. qp = &sc->queues[queue];
  281. /*
  282. * Don't bother allocating a ring for queues the host isn't allowed to
  283. * drive.
  284. */
  285. if ((sc->valid_queues & (1 << queue)) == 0)
  286. return;
  287. free(qp->completions_ring, M_CCP);
  288. bus_dmamap_unload(qp->ring_desc_tag, qp->ring_desc_map);
  289. bus_dmamem_free(qp->ring_desc_tag, qp->desc_ring, qp->ring_desc_map);
  290. bus_dma_tag_destroy(qp->ring_desc_tag);
  291. }
  292. static int
  293. ccp_map_pci_bar(device_t dev)
  294. {
  295. struct ccp_softc *sc;
  296. sc = device_get_softc(dev);
  297. sc->pci_resource_id = PCIR_BAR(2);
  298. sc->pci_resource = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  299. &sc->pci_resource_id, RF_ACTIVE);
  300. if (sc->pci_resource == NULL) {
  301. device_printf(dev, "unable to allocate pci resource\n");
  302. return (ENODEV);
  303. }
  304. sc->pci_resource_id_msix = PCIR_BAR(5);
  305. sc->pci_resource_msix = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  306. &sc->pci_resource_id_msix, RF_ACTIVE);
  307. if (sc->pci_resource_msix == NULL) {
  308. device_printf(dev, "unable to allocate pci resource msix\n");
  309. bus_release_resource(dev, SYS_RES_MEMORY, sc->pci_resource_id,
  310. sc->pci_resource);
  311. return (ENODEV);
  312. }
  313. sc->pci_bus_tag = rman_get_bustag(sc->pci_resource);
  314. sc->pci_bus_handle = rman_get_bushandle(sc->pci_resource);
  315. return (0);
  316. }
  317. static void
  318. ccp_unmap_pci_bar(device_t dev)
  319. {
  320. struct ccp_softc *sc;
  321. sc = device_get_softc(dev);
  322. bus_release_resource(dev, SYS_RES_MEMORY, sc->pci_resource_id_msix,
  323. sc->pci_resource_msix);
  324. bus_release_resource(dev, SYS_RES_MEMORY, sc->pci_resource_id,
  325. sc->pci_resource);
  326. }
  327. const static struct ccp_error_code {
  328. uint8_t ce_code;
  329. const char *ce_name;
  330. int ce_errno;
  331. const char *ce_desc;
  332. } ccp_error_codes[] = {
  333. { 0x01, "ILLEGAL_ENGINE", EIO, "Requested engine was invalid" },
  334. { 0x03, "ILLEGAL_FUNCTION_TYPE", EIO,
  335. "A non-supported function type was specified" },
  336. { 0x04, "ILLEGAL_FUNCTION_MODE", EIO,
  337. "A non-supported function mode was specified" },
  338. { 0x05, "ILLEGAL_FUNCTION_ENCRYPT", EIO,
  339. "A CMAC type was specified when ENCRYPT was not specified" },
  340. { 0x06, "ILLEGAL_FUNCTION_SIZE", EIO,
  341. "A non-supported function size was specified.\n"
  342. "AES-CFB: Size was not 127 or 7;\n"
  343. "3DES-CFB: Size was not 7;\n"
  344. "RSA: See supported size table (7.4.2);\n"
  345. "ECC: Size was greater than 576 bits." },
  346. { 0x07, "Zlib_MISSING_INIT_EOM", EIO,
  347. "Zlib command does not have INIT and EOM set" },
  348. { 0x08, "ILLEGAL_FUNCTION_RSVD", EIO,
  349. "Reserved bits in a function specification were not 0" },
  350. { 0x09, "ILLEGAL_BUFFER_LENGTH", EIO,
  351. "The buffer length specified was not correct for the selected engine"
  352. },
  353. { 0x0A, "VLSB_FAULT", EIO, "Illegal VLSB segment mapping:\n"
  354. "Undefined VLSB segment mapping or\n"
  355. "mapping to unsupported LSB segment id" },
  356. { 0x0B, "ILLEGAL_MEM_ADDR", EFAULT,
  357. "The specified source/destination buffer access was illegal:\n"
  358. "Data buffer located in a LSB location disallowed by the LSB protection masks; or\n"
  359. "Data buffer not completely contained within a single segment; or\n"
  360. "Pointer with Fixed=1 is not 32-bit aligned; or\n"
  361. "Pointer with Fixed=1 attempted to reference non-AXI1 (local) memory."
  362. },
  363. { 0x0C, "ILLEGAL_MEM_SEL", EIO,
  364. "A src_mem, dst_mem, or key_mem field was illegal:\n"
  365. "A field was set to a reserved value; or\n"
  366. "A public command attempted to reference AXI1 (local) or GART memory; or\n"
  367. "A Zlib command attmpted to use the LSB." },
  368. { 0x0D, "ILLEGAL_CONTEXT_ADDR", EIO,
  369. "The specified context location was illegal:\n"
  370. "Context located in a LSB location disallowed by the LSB protection masks; or\n"
  371. "Context not completely contained within a single segment." },
  372. { 0x0E, "ILLEGAL_KEY_ADDR", EIO,
  373. "The specified key location was illegal:\n"
  374. "Key located in a LSB location disallowed by the LSB protection masks; or\n"
  375. "Key not completely contained within a single segment." },
  376. { 0x12, "CMD_TIMEOUT", EIO, "A command timeout violation occurred" },
  377. /* XXX Could fill out these descriptions too */
  378. { 0x13, "IDMA0_AXI_SLVERR", EIO, "" },
  379. { 0x14, "IDMA0_AXI_DECERR", EIO, "" },
  380. { 0x16, "IDMA1_AXI_SLVERR", EIO, "" },
  381. { 0x17, "IDMA1_AXI_DECERR", EIO, "" },
  382. { 0x19, "ZLIBVHB_AXI_SLVERR", EIO, "" },
  383. { 0x1A, "ZLIBVHB_AXI_DECERR", EIO, "" },
  384. { 0x1C, "ZLIB_UNEXPECTED_EOM", EIO, "" },
  385. { 0x1D, "ZLIB_EXTRA_DATA", EIO, "" },
  386. { 0x1E, "ZLIB_BTYPE", EIO, "" },
  387. { 0x20, "ZLIB_UNDEFINED_DISTANCE_SYMBOL", EIO, "" },
  388. { 0x21, "ZLIB_CODE_LENGTH_SYMBOL", EIO, "" },
  389. { 0x22, "ZLIB_VHB_ILLEGAL_FETCH", EIO, "" },
  390. { 0x23, "ZLIB_UNCOMPRESSED_LEN", EIO, "" },
  391. { 0x24, "ZLIB_LIMIT_REACHED", EIO, "" },
  392. { 0x25, "ZLIB_CHECKSUM_MISMATCH", EIO, "" },
  393. { 0x26, "ODMA0_AXI_SLVERR", EIO, "" },
  394. { 0x27, "ODMA0_AXI_DECERR", EIO, "" },
  395. { 0x29, "ODMA1_AXI_SLVERR", EIO, "" },
  396. { 0x2A, "ODMA1_AXI_DECERR", EIO, "" },
  397. { 0x2B, "LSB_PARITY_ERR", EIO,
  398. "A read from the LSB encountered a parity error" },
  399. };
  400. static void
  401. ccp_intr_handle_error(struct ccp_queue *qp, const struct ccp_desc *desc)
  402. {
  403. struct ccp_completion_ctx *cctx;
  404. const struct ccp_error_code *ec;
  405. struct ccp_softc *sc;
  406. uint32_t status, error, esource, faultblock;
  407. unsigned q, idx;
  408. int errno;
  409. sc = qp->cq_softc;
  410. q = qp->cq_qindex;
  411. status = ccp_read_queue_4(sc, q, CMD_Q_STATUS_BASE);
  412. error = status & STATUS_ERROR_MASK;
  413. /* Decode error status */
  414. ec = NULL;
  415. for (idx = 0; idx < nitems(ccp_error_codes); idx++)
  416. if (ccp_error_codes[idx].ce_code == error) {
  417. ec = &ccp_error_codes[idx];
  418. break;
  419. }
  420. esource = (status >> STATUS_ERRORSOURCE_SHIFT) &
  421. STATUS_ERRORSOURCE_MASK;
  422. faultblock = (status >> STATUS_VLSB_FAULTBLOCK_SHIFT) &
  423. STATUS_VLSB_FAULTBLOCK_MASK;
  424. device_printf(sc->dev, "Error: %s (%u) Source: %u Faulting LSB block: %u\n",
  425. (ec != NULL) ? ec->ce_name : "(reserved)", error, esource,
  426. faultblock);
  427. if (ec != NULL)
  428. device_printf(sc->dev, "Error description: %s\n", ec->ce_desc);
  429. /* TODO Could format the desc nicely here */
  430. idx = desc - qp->desc_ring;
  431. DPRINTF(sc->dev, "Bad descriptor index: %u contents: %32D\n", idx,
  432. (const void *)desc, " ");
  433. /*
  434. * TODO Per § 14.4 "Error Handling," DMA_Status, DMA_Read/Write_Status,
  435. * Zlib Decompress status may be interesting.
  436. */
  437. while (true) {
  438. /* Keep unused descriptors zero for next use. */
  439. memset(&qp->desc_ring[idx], 0, sizeof(qp->desc_ring[idx]));
  440. cctx = &qp->completions_ring[idx];
  441. /*
  442. * Restart procedure described in § 14.2.5. Could be used by HoC if we
  443. * used that.
  444. *
  445. * Advance HEAD_LO past bad descriptor + any remaining in
  446. * transaction manually, then restart queue.
  447. */
  448. idx = (idx + 1) % (1 << sc->ring_size_order);
  449. /* Callback function signals end of transaction */
  450. if (cctx->callback_fn != NULL) {
  451. if (ec == NULL)
  452. errno = EIO;
  453. else
  454. errno = ec->ce_errno;
  455. /* TODO More specific error code */
  456. cctx->callback_fn(qp, cctx->session, cctx->callback_arg, errno);
  457. cctx->callback_fn = NULL;
  458. break;
  459. }
  460. }
  461. qp->cq_head = idx;
  462. qp->cq_waiting = false;
  463. wakeup(&qp->cq_tail);
  464. DPRINTF(sc->dev, "%s: wrote sw head:%u\n", __func__, qp->cq_head);
  465. ccp_write_queue_4(sc, q, CMD_Q_HEAD_LO_BASE,
  466. (uint32_t)qp->desc_ring_bus_addr + (idx * Q_DESC_SIZE));
  467. ccp_write_queue_4(sc, q, CMD_Q_CONTROL_BASE, qp->qcontrol);
  468. DPRINTF(sc->dev, "%s: Restarted queue\n", __func__);
  469. }
  470. static void
  471. ccp_intr_run_completions(struct ccp_queue *qp, uint32_t ints)
  472. {
  473. struct ccp_completion_ctx *cctx;
  474. struct ccp_softc *sc;
  475. const struct ccp_desc *desc;
  476. uint32_t headlo, idx;
  477. unsigned q, completed;
  478. sc = qp->cq_softc;
  479. q = qp->cq_qindex;
  480. mtx_lock(&qp->cq_lock);
  481. /*
  482. * Hardware HEAD_LO points to the first incomplete descriptor. Process
  483. * any submitted and completed descriptors, up to but not including
  484. * HEAD_LO.
  485. */
  486. headlo = ccp_read_queue_4(sc, q, CMD_Q_HEAD_LO_BASE);
  487. idx = (headlo - (uint32_t)qp->desc_ring_bus_addr) / Q_DESC_SIZE;
  488. DPRINTF(sc->dev, "%s: hw head:%u sw head:%u\n", __func__, idx,
  489. qp->cq_head);
  490. completed = 0;
  491. while (qp->cq_head != idx) {
  492. DPRINTF(sc->dev, "%s: completing:%u\n", __func__, qp->cq_head);
  493. cctx = &qp->completions_ring[qp->cq_head];
  494. if (cctx->callback_fn != NULL) {
  495. cctx->callback_fn(qp, cctx->session,
  496. cctx->callback_arg, 0);
  497. cctx->callback_fn = NULL;
  498. }
  499. /* Keep unused descriptors zero for next use. */
  500. memset(&qp->desc_ring[qp->cq_head], 0,
  501. sizeof(qp->desc_ring[qp->cq_head]));
  502. qp->cq_head = (qp->cq_head + 1) % (1 << sc->ring_size_order);
  503. completed++;
  504. }
  505. if (completed > 0) {
  506. qp->cq_waiting = false;
  507. wakeup(&qp->cq_tail);
  508. }
  509. DPRINTF(sc->dev, "%s: wrote sw head:%u\n", __func__, qp->cq_head);
  510. /*
  511. * Desc points to the first incomplete descriptor, at the time we read
  512. * HEAD_LO. If there was an error flagged in interrupt status, the HW
  513. * will not proceed past the erroneous descriptor by itself.
  514. */
  515. desc = &qp->desc_ring[idx];
  516. if ((ints & INT_ERROR) != 0)
  517. ccp_intr_handle_error(qp, desc);
  518. mtx_unlock(&qp->cq_lock);
  519. }
  520. static void
  521. ccp_intr_handler(void *arg)
  522. {
  523. struct ccp_softc *sc = arg;
  524. size_t i;
  525. uint32_t ints;
  526. DPRINTF(sc->dev, "%s: interrupt\n", __func__);
  527. /*
  528. * We get one global interrupt per PCI device, shared over all of
  529. * its queues. Scan each valid queue on interrupt for flags indicating
  530. * activity.
  531. */
  532. for (i = 0; i < nitems(sc->queues); i++) {
  533. if ((sc->valid_queues & (1 << i)) == 0)
  534. continue;
  535. ints = ccp_read_queue_4(sc, i, CMD_Q_INTERRUPT_STATUS_BASE);
  536. if (ints == 0)
  537. continue;
  538. #if 0
  539. DPRINTF(sc->dev, "%s: %x interrupts on queue %zu\n", __func__,
  540. (unsigned)ints, i);
  541. #endif
  542. /* Write back 1s to clear interrupt status bits. */
  543. ccp_write_queue_4(sc, i, CMD_Q_INTERRUPT_STATUS_BASE, ints);
  544. /*
  545. * If there was an error, we still need to run completions on
  546. * any descriptors prior to the error. The completions handler
  547. * invoked below will also handle the error descriptor.
  548. */
  549. if ((ints & (INT_COMPLETION | INT_ERROR)) != 0)
  550. ccp_intr_run_completions(&sc->queues[i], ints);
  551. if ((ints & INT_QUEUE_STOPPED) != 0)
  552. device_printf(sc->dev, "%s: queue %zu stopped\n",
  553. __func__, i);
  554. }
  555. /* Re-enable interrupts after processing */
  556. for (i = 0; i < nitems(sc->queues); i++) {
  557. if ((sc->valid_queues & (1 << i)) == 0)
  558. continue;
  559. ccp_write_queue_4(sc, i, CMD_Q_INT_ENABLE_BASE,
  560. INT_COMPLETION | INT_ERROR | INT_QUEUE_STOPPED);
  561. }
  562. }
  563. static int
  564. ccp_intr_filter(void *arg)
  565. {
  566. struct ccp_softc *sc = arg;
  567. size_t i;
  568. /* TODO: Split individual queues into separate taskqueues? */
  569. for (i = 0; i < nitems(sc->queues); i++) {
  570. if ((sc->valid_queues & (1 << i)) == 0)
  571. continue;
  572. /* Mask interrupt until task completes */
  573. ccp_write_queue_4(sc, i, CMD_Q_INT_ENABLE_BASE, 0);
  574. }
  575. return (FILTER_SCHEDULE_THREAD);
  576. }
  577. static int
  578. ccp_setup_interrupts(struct ccp_softc *sc)
  579. {
  580. uint32_t nvec;
  581. int rid, error, n, ridcopy;
  582. n = pci_msix_count(sc->dev);
  583. if (n < 1) {
  584. device_printf(sc->dev, "%s: msix_count: %d\n", __func__, n);
  585. return (ENXIO);
  586. }
  587. nvec = n;
  588. error = pci_alloc_msix(sc->dev, &nvec);
  589. if (error != 0) {
  590. device_printf(sc->dev, "%s: alloc_msix error: %d\n", __func__,
  591. error);
  592. return (error);
  593. }
  594. if (nvec < 1) {
  595. device_printf(sc->dev, "%s: alloc_msix: 0 vectors\n",
  596. __func__);
  597. return (ENXIO);
  598. }
  599. if (nvec > nitems(sc->intr_res)) {
  600. device_printf(sc->dev, "%s: too many vectors: %u\n", __func__,
  601. nvec);
  602. nvec = nitems(sc->intr_res);
  603. }
  604. for (rid = 1; rid < 1 + nvec; rid++) {
  605. ridcopy = rid;
  606. sc->intr_res[rid - 1] = bus_alloc_resource_any(sc->dev,
  607. SYS_RES_IRQ, &ridcopy, RF_ACTIVE);
  608. if (sc->intr_res[rid - 1] == NULL) {
  609. device_printf(sc->dev, "%s: Failed to alloc IRQ resource\n",
  610. __func__);
  611. return (ENXIO);
  612. }
  613. sc->intr_tag[rid - 1] = NULL;
  614. error = bus_setup_intr(sc->dev, sc->intr_res[rid - 1],
  615. INTR_MPSAFE | INTR_TYPE_MISC, ccp_intr_filter,
  616. ccp_intr_handler, sc, &sc->intr_tag[rid - 1]);
  617. if (error != 0)
  618. device_printf(sc->dev, "%s: setup_intr: %d\n",
  619. __func__, error);
  620. }
  621. sc->intr_count = nvec;
  622. return (error);
  623. }
  624. static void
  625. ccp_release_interrupts(struct ccp_softc *sc)
  626. {
  627. unsigned i;
  628. for (i = 0; i < sc->intr_count; i++) {
  629. if (sc->intr_tag[i] != NULL)
  630. bus_teardown_intr(sc->dev, sc->intr_res[i],
  631. sc->intr_tag[i]);
  632. if (sc->intr_res[i] != NULL)
  633. bus_release_resource(sc->dev, SYS_RES_IRQ,
  634. rman_get_rid(sc->intr_res[i]), sc->intr_res[i]);
  635. }
  636. pci_release_msi(sc->dev);
  637. }
  638. int
  639. ccp_hw_attach(device_t dev)
  640. {
  641. struct ccp_softc *sc;
  642. uint64_t lsbmask;
  643. uint32_t version, lsbmasklo, lsbmaskhi;
  644. unsigned queue_idx, j;
  645. int error;
  646. bool bars_mapped, interrupts_setup;
  647. queue_idx = 0;
  648. bars_mapped = interrupts_setup = false;
  649. sc = device_get_softc(dev);
  650. error = ccp_map_pci_bar(dev);
  651. if (error != 0) {
  652. device_printf(dev, "%s: couldn't map BAR(s)\n", __func__);
  653. goto out;
  654. }
  655. bars_mapped = true;
  656. error = pci_enable_busmaster(dev);
  657. if (error != 0) {
  658. device_printf(dev, "%s: couldn't enable busmaster\n",
  659. __func__);
  660. goto out;
  661. }
  662. sc->ring_size_order = g_ccp_ring_order;
  663. if (sc->ring_size_order < 6 || sc->ring_size_order > 16) {
  664. device_printf(dev, "bogus hw.ccp.ring_order\n");
  665. error = EINVAL;
  666. goto out;
  667. }
  668. sc->valid_queues = ccp_read_4(sc, CMD_QUEUE_MASK_OFFSET);
  669. version = ccp_read_4(sc, VERSION_REG);
  670. if ((version & VERSION_NUM_MASK) < 5) {
  671. device_printf(dev,
  672. "driver supports version 5 and later hardware\n");
  673. error = ENXIO;
  674. goto out;
  675. }
  676. error = ccp_setup_interrupts(sc);
  677. if (error != 0)
  678. goto out;
  679. interrupts_setup = true;
  680. sc->hw_version = version & VERSION_NUM_MASK;
  681. sc->num_queues = (version >> VERSION_NUMVQM_SHIFT) &
  682. VERSION_NUMVQM_MASK;
  683. sc->num_lsb_entries = (version >> VERSION_LSBSIZE_SHIFT) &
  684. VERSION_LSBSIZE_MASK;
  685. sc->hw_features = version & VERSION_CAP_MASK;
  686. /*
  687. * Copy private LSB mask to public registers to enable access to LSB
  688. * from all queues allowed by BIOS.
  689. */
  690. lsbmasklo = ccp_read_4(sc, LSB_PRIVATE_MASK_LO_OFFSET);
  691. lsbmaskhi = ccp_read_4(sc, LSB_PRIVATE_MASK_HI_OFFSET);
  692. ccp_write_4(sc, LSB_PUBLIC_MASK_LO_OFFSET, lsbmasklo);
  693. ccp_write_4(sc, LSB_PUBLIC_MASK_HI_OFFSET, lsbmaskhi);
  694. lsbmask = ((uint64_t)lsbmaskhi << 30) | lsbmasklo;
  695. for (; queue_idx < nitems(sc->queues); queue_idx++) {
  696. error = ccp_hw_attach_queue(dev, lsbmask, queue_idx);
  697. if (error != 0) {
  698. device_printf(dev, "%s: couldn't attach queue %u\n",
  699. __func__, queue_idx);
  700. goto out;
  701. }
  702. }
  703. ccp_assign_lsb_regions(sc, lsbmask);
  704. out:
  705. if (error != 0) {
  706. if (interrupts_setup)
  707. ccp_release_interrupts(sc);
  708. for (j = 0; j < queue_idx; j++)
  709. ccp_hw_detach_queue(dev, j);
  710. if (sc->ring_size_order != 0)
  711. pci_disable_busmaster(dev);
  712. if (bars_mapped)
  713. ccp_unmap_pci_bar(dev);
  714. }
  715. return (error);
  716. }
  717. void
  718. ccp_hw_detach(device_t dev)
  719. {
  720. struct ccp_softc *sc;
  721. unsigned i;
  722. sc = device_get_softc(dev);
  723. for (i = 0; i < nitems(sc->queues); i++)
  724. ccp_hw_detach_queue(dev, i);
  725. ccp_release_interrupts(sc);
  726. pci_disable_busmaster(dev);
  727. ccp_unmap_pci_bar(dev);
  728. }
  729. static int __must_check
  730. ccp_passthrough(struct ccp_queue *qp, bus_addr_t dst,
  731. enum ccp_memtype dst_type, bus_addr_t src, enum ccp_memtype src_type,
  732. bus_size_t len, enum ccp_passthru_byteswap swapmode,
  733. enum ccp_passthru_bitwise bitmode, bool interrupt,
  734. const struct ccp_completion_ctx *cctx)
  735. {
  736. struct ccp_desc *desc;
  737. if (ccp_queue_get_ring_space(qp) == 0)
  738. return (EAGAIN);
  739. desc = &qp->desc_ring[qp->cq_tail];
  740. memset(desc, 0, sizeof(*desc));
  741. desc->engine = CCP_ENGINE_PASSTHRU;
  742. desc->pt.ioc = interrupt;
  743. desc->pt.byteswap = swapmode;
  744. desc->pt.bitwise = bitmode;
  745. desc->length = len;
  746. desc->src_lo = (uint32_t)src;
  747. desc->src_hi = src >> 32;
  748. desc->src_mem = src_type;
  749. desc->dst_lo = (uint32_t)dst;
  750. desc->dst_hi = dst >> 32;
  751. desc->dst_mem = dst_type;
  752. if (bitmode != CCP_PASSTHRU_BITWISE_NOOP)
  753. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_KEY);
  754. if (cctx != NULL)
  755. memcpy(&qp->completions_ring[qp->cq_tail], cctx, sizeof(*cctx));
  756. qp->cq_tail = (qp->cq_tail + 1) % (1 << qp->cq_softc->ring_size_order);
  757. return (0);
  758. }
  759. static int __must_check
  760. ccp_passthrough_sgl(struct ccp_queue *qp, bus_addr_t lsb_addr, bool tolsb,
  761. struct sglist *sgl, bus_size_t len, bool interrupt,
  762. const struct ccp_completion_ctx *cctx)
  763. {
  764. struct sglist_seg *seg;
  765. size_t i, remain, nb;
  766. int error;
  767. remain = len;
  768. for (i = 0; i < sgl->sg_nseg && remain != 0; i++) {
  769. seg = &sgl->sg_segs[i];
  770. /* crp lengths are int, so 32-bit min() is ok. */
  771. nb = min(remain, seg->ss_len);
  772. if (tolsb)
  773. error = ccp_passthrough(qp, lsb_addr, CCP_MEMTYPE_SB,
  774. seg->ss_paddr, CCP_MEMTYPE_SYSTEM, nb,
  775. CCP_PASSTHRU_BYTESWAP_NOOP,
  776. CCP_PASSTHRU_BITWISE_NOOP,
  777. (nb == remain) && interrupt, cctx);
  778. else
  779. error = ccp_passthrough(qp, seg->ss_paddr,
  780. CCP_MEMTYPE_SYSTEM, lsb_addr, CCP_MEMTYPE_SB, nb,
  781. CCP_PASSTHRU_BYTESWAP_NOOP,
  782. CCP_PASSTHRU_BITWISE_NOOP,
  783. (nb == remain) && interrupt, cctx);
  784. if (error != 0)
  785. return (error);
  786. remain -= nb;
  787. }
  788. return (0);
  789. }
  790. /*
  791. * Note that these vectors are in reverse of the usual order.
  792. */
  793. const struct SHA_vectors {
  794. uint32_t SHA1[8];
  795. uint32_t SHA224[8];
  796. uint32_t SHA256[8];
  797. uint64_t SHA384[8];
  798. uint64_t SHA512[8];
  799. } SHA_H __aligned(PAGE_SIZE) = {
  800. .SHA1 = {
  801. 0xc3d2e1f0ul,
  802. 0x10325476ul,
  803. 0x98badcfeul,
  804. 0xefcdab89ul,
  805. 0x67452301ul,
  806. 0,
  807. 0,
  808. 0,
  809. },
  810. .SHA224 = {
  811. 0xbefa4fa4ul,
  812. 0x64f98fa7ul,
  813. 0x68581511ul,
  814. 0xffc00b31ul,
  815. 0xf70e5939ul,
  816. 0x3070dd17ul,
  817. 0x367cd507ul,
  818. 0xc1059ed8ul,
  819. },
  820. .SHA256 = {
  821. 0x5be0cd19ul,
  822. 0x1f83d9abul,
  823. 0x9b05688cul,
  824. 0x510e527ful,
  825. 0xa54ff53aul,
  826. 0x3c6ef372ul,
  827. 0xbb67ae85ul,
  828. 0x6a09e667ul,
  829. },
  830. .SHA384 = {
  831. 0x47b5481dbefa4fa4ull,
  832. 0xdb0c2e0d64f98fa7ull,
  833. 0x8eb44a8768581511ull,
  834. 0x67332667ffc00b31ull,
  835. 0x152fecd8f70e5939ull,
  836. 0x9159015a3070dd17ull,
  837. 0x629a292a367cd507ull,
  838. 0xcbbb9d5dc1059ed8ull,
  839. },
  840. .SHA512 = {
  841. 0x5be0cd19137e2179ull,
  842. 0x1f83d9abfb41bd6bull,
  843. 0x9b05688c2b3e6c1full,
  844. 0x510e527fade682d1ull,
  845. 0xa54ff53a5f1d36f1ull,
  846. 0x3c6ef372fe94f82bull,
  847. 0xbb67ae8584caa73bull,
  848. 0x6a09e667f3bcc908ull,
  849. },
  850. };
  851. /*
  852. * Ensure vectors do not cross a page boundary.
  853. *
  854. * Disabled due to a new Clang error: "expression is not an integral constant
  855. * expression." GCC (cross toolchain) seems to handle this assertion with
  856. * _Static_assert just fine.
  857. */
  858. #if 0
  859. CTASSERT(PAGE_SIZE - ((uintptr_t)&SHA_H % PAGE_SIZE) >= sizeof(SHA_H));
  860. #endif
  861. const struct SHA_Defn {
  862. enum sha_version version;
  863. const void *H_vectors;
  864. size_t H_size;
  865. struct auth_hash *axf;
  866. enum ccp_sha_type engine_type;
  867. } SHA_definitions[] = {
  868. {
  869. .version = SHA1,
  870. .H_vectors = SHA_H.SHA1,
  871. .H_size = sizeof(SHA_H.SHA1),
  872. .axf = &auth_hash_hmac_sha1,
  873. .engine_type = CCP_SHA_TYPE_1,
  874. },
  875. #if 0
  876. {
  877. .version = SHA2_224,
  878. .H_vectors = SHA_H.SHA224,
  879. .H_size = sizeof(SHA_H.SHA224),
  880. .axf = &auth_hash_hmac_sha2_224,
  881. .engine_type = CCP_SHA_TYPE_224,
  882. },
  883. #endif
  884. {
  885. .version = SHA2_256,
  886. .H_vectors = SHA_H.SHA256,
  887. .H_size = sizeof(SHA_H.SHA256),
  888. .axf = &auth_hash_hmac_sha2_256,
  889. .engine_type = CCP_SHA_TYPE_256,
  890. },
  891. {
  892. .version = SHA2_384,
  893. .H_vectors = SHA_H.SHA384,
  894. .H_size = sizeof(SHA_H.SHA384),
  895. .axf = &auth_hash_hmac_sha2_384,
  896. .engine_type = CCP_SHA_TYPE_384,
  897. },
  898. {
  899. .version = SHA2_512,
  900. .H_vectors = SHA_H.SHA512,
  901. .H_size = sizeof(SHA_H.SHA512),
  902. .axf = &auth_hash_hmac_sha2_512,
  903. .engine_type = CCP_SHA_TYPE_512,
  904. },
  905. };
  906. static int __must_check
  907. ccp_sha_single_desc(struct ccp_queue *qp, const struct SHA_Defn *defn,
  908. vm_paddr_t addr, size_t len, bool start, bool end, uint64_t msgbits)
  909. {
  910. struct ccp_desc *desc;
  911. if (ccp_queue_get_ring_space(qp) == 0)
  912. return (EAGAIN);
  913. desc = &qp->desc_ring[qp->cq_tail];
  914. memset(desc, 0, sizeof(*desc));
  915. desc->engine = CCP_ENGINE_SHA;
  916. desc->som = start;
  917. desc->eom = end;
  918. desc->sha.type = defn->engine_type;
  919. desc->length = len;
  920. if (end) {
  921. desc->sha_len_lo = (uint32_t)msgbits;
  922. desc->sha_len_hi = msgbits >> 32;
  923. }
  924. desc->src_lo = (uint32_t)addr;
  925. desc->src_hi = addr >> 32;
  926. desc->src_mem = CCP_MEMTYPE_SYSTEM;
  927. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_SHA);
  928. qp->cq_tail = (qp->cq_tail + 1) % (1 << qp->cq_softc->ring_size_order);
  929. return (0);
  930. }
  931. static int __must_check
  932. ccp_sha(struct ccp_queue *qp, enum sha_version version, struct sglist *sgl_src,
  933. struct sglist *sgl_dst, const struct ccp_completion_ctx *cctx)
  934. {
  935. const struct SHA_Defn *defn;
  936. struct sglist_seg *seg;
  937. size_t i, msgsize, remaining, nb;
  938. uint32_t lsbaddr;
  939. int error;
  940. for (i = 0; i < nitems(SHA_definitions); i++)
  941. if (SHA_definitions[i].version == version)
  942. break;
  943. if (i == nitems(SHA_definitions))
  944. return (EINVAL);
  945. defn = &SHA_definitions[i];
  946. /* XXX validate input ??? */
  947. /* Load initial SHA state into LSB */
  948. /* XXX ensure H_vectors don't span page boundaries */
  949. error = ccp_passthrough(qp, ccp_queue_lsb_address(qp, LSB_ENTRY_SHA),
  950. CCP_MEMTYPE_SB, pmap_kextract((vm_offset_t)defn->H_vectors),
  951. CCP_MEMTYPE_SYSTEM, roundup2(defn->H_size, LSB_ENTRY_SIZE),
  952. CCP_PASSTHRU_BYTESWAP_NOOP, CCP_PASSTHRU_BITWISE_NOOP, false,
  953. NULL);
  954. if (error != 0)
  955. return (error);
  956. /* Execute series of SHA updates on correctly sized buffers */
  957. msgsize = 0;
  958. for (i = 0; i < sgl_src->sg_nseg; i++) {
  959. seg = &sgl_src->sg_segs[i];
  960. msgsize += seg->ss_len;
  961. error = ccp_sha_single_desc(qp, defn, seg->ss_paddr,
  962. seg->ss_len, i == 0, i == sgl_src->sg_nseg - 1,
  963. msgsize << 3);
  964. if (error != 0)
  965. return (error);
  966. }
  967. /* Copy result out to sgl_dst */
  968. remaining = roundup2(defn->H_size, LSB_ENTRY_SIZE);
  969. lsbaddr = ccp_queue_lsb_address(qp, LSB_ENTRY_SHA);
  970. for (i = 0; i < sgl_dst->sg_nseg; i++) {
  971. seg = &sgl_dst->sg_segs[i];
  972. /* crp lengths are int, so 32-bit min() is ok. */
  973. nb = min(remaining, seg->ss_len);
  974. error = ccp_passthrough(qp, seg->ss_paddr, CCP_MEMTYPE_SYSTEM,
  975. lsbaddr, CCP_MEMTYPE_SB, nb, CCP_PASSTHRU_BYTESWAP_NOOP,
  976. CCP_PASSTHRU_BITWISE_NOOP,
  977. (cctx != NULL) ? (nb == remaining) : false,
  978. (nb == remaining) ? cctx : NULL);
  979. if (error != 0)
  980. return (error);
  981. remaining -= nb;
  982. lsbaddr += nb;
  983. if (remaining == 0)
  984. break;
  985. }
  986. return (0);
  987. }
  988. static void
  989. byteswap256(uint64_t *buffer)
  990. {
  991. uint64_t t;
  992. t = bswap64(buffer[3]);
  993. buffer[3] = bswap64(buffer[0]);
  994. buffer[0] = t;
  995. t = bswap64(buffer[2]);
  996. buffer[2] = bswap64(buffer[1]);
  997. buffer[1] = t;
  998. }
  999. /*
  1000. * Translate CCP internal LSB hash format into a standard hash ouput.
  1001. *
  1002. * Manipulates input buffer with byteswap256 operation.
  1003. */
  1004. static void
  1005. ccp_sha_copy_result(char *output, char *buffer, enum sha_version version)
  1006. {
  1007. const struct SHA_Defn *defn;
  1008. size_t i;
  1009. for (i = 0; i < nitems(SHA_definitions); i++)
  1010. if (SHA_definitions[i].version == version)
  1011. break;
  1012. if (i == nitems(SHA_definitions))
  1013. panic("bogus sha version auth_mode %u\n", (unsigned)version);
  1014. defn = &SHA_definitions[i];
  1015. /* Swap 256bit manually -- DMA engine can, but with limitations */
  1016. byteswap256((void *)buffer);
  1017. if (defn->axf->hashsize > LSB_ENTRY_SIZE)
  1018. byteswap256((void *)(buffer + LSB_ENTRY_SIZE));
  1019. switch (defn->version) {
  1020. case SHA1:
  1021. memcpy(output, buffer + 12, defn->axf->hashsize);
  1022. break;
  1023. #if 0
  1024. case SHA2_224:
  1025. memcpy(output, buffer + XXX, defn->axf->hashsize);
  1026. break;
  1027. #endif
  1028. case SHA2_256:
  1029. memcpy(output, buffer, defn->axf->hashsize);
  1030. break;
  1031. case SHA2_384:
  1032. memcpy(output,
  1033. buffer + LSB_ENTRY_SIZE * 3 - defn->axf->hashsize,
  1034. defn->axf->hashsize - LSB_ENTRY_SIZE);
  1035. memcpy(output + defn->axf->hashsize - LSB_ENTRY_SIZE, buffer,
  1036. LSB_ENTRY_SIZE);
  1037. break;
  1038. case SHA2_512:
  1039. memcpy(output, buffer + LSB_ENTRY_SIZE, LSB_ENTRY_SIZE);
  1040. memcpy(output + LSB_ENTRY_SIZE, buffer, LSB_ENTRY_SIZE);
  1041. break;
  1042. }
  1043. }
  1044. static void
  1045. ccp_do_hmac_done(struct ccp_queue *qp, struct ccp_session *s,
  1046. struct cryptop *crp, int error)
  1047. {
  1048. char ihash[SHA2_512_HASH_LEN /* max hash len */];
  1049. union authctx auth_ctx;
  1050. struct auth_hash *axf;
  1051. axf = s->hmac.auth_hash;
  1052. s->pending--;
  1053. if (error != 0) {
  1054. crp->crp_etype = error;
  1055. goto out;
  1056. }
  1057. /* Do remaining outer hash over small inner hash in software */
  1058. axf->Init(&auth_ctx);
  1059. axf->Update(&auth_ctx, s->hmac.opad, axf->blocksize);
  1060. ccp_sha_copy_result(ihash, s->hmac.res, s->hmac.auth_mode);
  1061. #if 0
  1062. INSECURE_DEBUG(dev, "%s sha intermediate=%64D\n", __func__,
  1063. (u_char *)ihash, " ");
  1064. #endif
  1065. axf->Update(&auth_ctx, ihash, axf->hashsize);
  1066. axf->Final(s->hmac.res, &auth_ctx);
  1067. if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
  1068. crypto_copydata(crp, crp->crp_digest_start, s->hmac.hash_len,
  1069. ihash);
  1070. if (timingsafe_bcmp(s->hmac.res, ihash, s->hmac.hash_len) != 0)
  1071. crp->crp_etype = EBADMSG;
  1072. } else
  1073. crypto_copyback(crp, crp->crp_digest_start, s->hmac.hash_len,
  1074. s->hmac.res);
  1075. /* Avoid leaking key material */
  1076. explicit_bzero(&auth_ctx, sizeof(auth_ctx));
  1077. explicit_bzero(s->hmac.res, sizeof(s->hmac.res));
  1078. out:
  1079. crypto_done(crp);
  1080. }
  1081. static void
  1082. ccp_hmac_done(struct ccp_queue *qp, struct ccp_session *s, void *vcrp,
  1083. int error)
  1084. {
  1085. struct cryptop *crp;
  1086. crp = vcrp;
  1087. ccp_do_hmac_done(qp, s, crp, error);
  1088. }
  1089. static int __must_check
  1090. ccp_do_hmac(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp,
  1091. const struct ccp_completion_ctx *cctx)
  1092. {
  1093. device_t dev;
  1094. struct auth_hash *axf;
  1095. int error;
  1096. dev = qp->cq_softc->dev;
  1097. axf = s->hmac.auth_hash;
  1098. /*
  1099. * Populate the SGL describing inside hash contents. We want to hash
  1100. * the ipad (key XOR fixed bit pattern) concatenated with the user
  1101. * data.
  1102. */
  1103. sglist_reset(qp->cq_sg_ulptx);
  1104. error = sglist_append(qp->cq_sg_ulptx, s->hmac.ipad, axf->blocksize);
  1105. if (error != 0)
  1106. return (error);
  1107. if (crp->crp_aad_length != 0) {
  1108. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1109. crp->crp_aad_start, crp->crp_aad_length);
  1110. if (error != 0)
  1111. return (error);
  1112. }
  1113. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1114. crp->crp_payload_start, crp->crp_payload_length);
  1115. if (error != 0) {
  1116. DPRINTF(dev, "%s: sglist too short\n", __func__);
  1117. return (error);
  1118. }
  1119. /* Populate SGL for output -- use hmac.res buffer. */
  1120. sglist_reset(qp->cq_sg_dst);
  1121. error = sglist_append(qp->cq_sg_dst, s->hmac.res,
  1122. roundup2(axf->hashsize, LSB_ENTRY_SIZE));
  1123. if (error != 0)
  1124. return (error);
  1125. error = ccp_sha(qp, s->hmac.auth_mode, qp->cq_sg_ulptx, qp->cq_sg_dst,
  1126. cctx);
  1127. if (error != 0) {
  1128. DPRINTF(dev, "%s: ccp_sha error\n", __func__);
  1129. return (error);
  1130. }
  1131. return (0);
  1132. }
  1133. int __must_check
  1134. ccp_hmac(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp)
  1135. {
  1136. struct ccp_completion_ctx ctx;
  1137. ctx.callback_fn = ccp_hmac_done;
  1138. ctx.callback_arg = crp;
  1139. ctx.session = s;
  1140. return (ccp_do_hmac(qp, s, crp, &ctx));
  1141. }
  1142. static void
  1143. ccp_byteswap(char *data, size_t len)
  1144. {
  1145. size_t i;
  1146. char t;
  1147. len--;
  1148. for (i = 0; i < len; i++, len--) {
  1149. t = data[i];
  1150. data[i] = data[len];
  1151. data[len] = t;
  1152. }
  1153. }
  1154. static void
  1155. ccp_blkcipher_done(struct ccp_queue *qp, struct ccp_session *s, void *vcrp,
  1156. int error)
  1157. {
  1158. struct cryptop *crp;
  1159. explicit_bzero(&s->blkcipher.iv, sizeof(s->blkcipher.iv));
  1160. crp = vcrp;
  1161. s->pending--;
  1162. if (error != 0)
  1163. crp->crp_etype = error;
  1164. DPRINTF(qp->cq_softc->dev, "%s: qp=%p crp=%p\n", __func__, qp, crp);
  1165. crypto_done(crp);
  1166. }
  1167. static void
  1168. ccp_collect_iv(struct cryptop *crp, const struct crypto_session_params *csp,
  1169. char *iv)
  1170. {
  1171. crypto_read_iv(crp, iv);
  1172. /*
  1173. * If the input IV is 12 bytes, append an explicit counter of 1.
  1174. */
  1175. if (csp->csp_cipher_alg == CRYPTO_AES_NIST_GCM_16 &&
  1176. csp->csp_ivlen == 12)
  1177. *(uint32_t *)&iv[12] = htobe32(1);
  1178. if (csp->csp_cipher_alg == CRYPTO_AES_XTS &&
  1179. csp->csp_ivlen < AES_BLOCK_LEN)
  1180. memset(&iv[csp->csp_ivlen], 0, AES_BLOCK_LEN - csp->csp_ivlen);
  1181. /* Reverse order of IV material for HW */
  1182. INSECURE_DEBUG(NULL, "%s: IV: %16D len: %u\n", __func__, iv, " ",
  1183. csp->csp_ivlen);
  1184. /*
  1185. * For unknown reasons, XTS mode expects the IV in the reverse byte
  1186. * order to every other AES mode.
  1187. */
  1188. if (csp->csp_cipher_alg != CRYPTO_AES_XTS)
  1189. ccp_byteswap(iv, AES_BLOCK_LEN);
  1190. }
  1191. static int __must_check
  1192. ccp_do_pst_to_lsb(struct ccp_queue *qp, uint32_t lsbaddr, const void *src,
  1193. size_t len)
  1194. {
  1195. int error;
  1196. sglist_reset(qp->cq_sg_ulptx);
  1197. error = sglist_append(qp->cq_sg_ulptx, __DECONST(void *, src), len);
  1198. if (error != 0)
  1199. return (error);
  1200. error = ccp_passthrough_sgl(qp, lsbaddr, true, qp->cq_sg_ulptx, len,
  1201. false, NULL);
  1202. return (error);
  1203. }
  1204. static int __must_check
  1205. ccp_do_xts(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp,
  1206. enum ccp_cipher_dir dir, const struct ccp_completion_ctx *cctx)
  1207. {
  1208. struct ccp_desc *desc;
  1209. device_t dev;
  1210. unsigned i;
  1211. enum ccp_xts_unitsize usize;
  1212. /* IV and Key data are already loaded */
  1213. dev = qp->cq_softc->dev;
  1214. for (i = 0; i < nitems(ccp_xts_unitsize_map); i++)
  1215. if (ccp_xts_unitsize_map[i].cxu_size ==
  1216. crp->crp_payload_length) {
  1217. usize = ccp_xts_unitsize_map[i].cxu_id;
  1218. break;
  1219. }
  1220. if (i >= nitems(ccp_xts_unitsize_map))
  1221. return (EINVAL);
  1222. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++) {
  1223. struct sglist_seg *seg;
  1224. seg = &qp->cq_sg_ulptx->sg_segs[i];
  1225. desc = &qp->desc_ring[qp->cq_tail];
  1226. desc->engine = CCP_ENGINE_XTS_AES;
  1227. desc->som = (i == 0);
  1228. desc->eom = (i == qp->cq_sg_ulptx->sg_nseg - 1);
  1229. desc->ioc = (desc->eom && cctx != NULL);
  1230. DPRINTF(dev, "%s: XTS %u: som:%d eom:%d ioc:%d dir:%d\n",
  1231. __func__, qp->cq_tail, (int)desc->som, (int)desc->eom,
  1232. (int)desc->ioc, (int)dir);
  1233. if (desc->ioc)
  1234. memcpy(&qp->completions_ring[qp->cq_tail], cctx,
  1235. sizeof(*cctx));
  1236. desc->aes_xts.encrypt = dir;
  1237. desc->aes_xts.type = s->blkcipher.cipher_type;
  1238. desc->aes_xts.size = usize;
  1239. DPRINTF(dev, "XXX %s: XTS %u: type:%u size:%u\n", __func__,
  1240. qp->cq_tail, (unsigned)desc->aes_xts.type,
  1241. (unsigned)desc->aes_xts.size);
  1242. desc->length = seg->ss_len;
  1243. desc->src_lo = (uint32_t)seg->ss_paddr;
  1244. desc->src_hi = (seg->ss_paddr >> 32);
  1245. desc->src_mem = CCP_MEMTYPE_SYSTEM;
  1246. /* Crypt in-place */
  1247. desc->dst_lo = desc->src_lo;
  1248. desc->dst_hi = desc->src_hi;
  1249. desc->dst_mem = desc->src_mem;
  1250. desc->key_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_KEY);
  1251. desc->key_hi = 0;
  1252. desc->key_mem = CCP_MEMTYPE_SB;
  1253. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_IV);
  1254. qp->cq_tail = (qp->cq_tail + 1) %
  1255. (1 << qp->cq_softc->ring_size_order);
  1256. }
  1257. return (0);
  1258. }
  1259. static int __must_check
  1260. ccp_do_blkcipher(struct ccp_queue *qp, struct ccp_session *s,
  1261. struct cryptop *crp, const struct ccp_completion_ctx *cctx)
  1262. {
  1263. const struct crypto_session_params *csp;
  1264. struct ccp_desc *desc;
  1265. char *keydata;
  1266. device_t dev;
  1267. enum ccp_cipher_dir dir;
  1268. int error, iv_len;
  1269. size_t keydata_len;
  1270. unsigned i, j;
  1271. dev = qp->cq_softc->dev;
  1272. if (s->blkcipher.key_len == 0 || crp->crp_payload_length == 0) {
  1273. DPRINTF(dev, "%s: empty\n", __func__);
  1274. return (EINVAL);
  1275. }
  1276. if ((crp->crp_payload_length % AES_BLOCK_LEN) != 0) {
  1277. DPRINTF(dev, "%s: len modulo: %d\n", __func__,
  1278. crp->crp_payload_length);
  1279. return (EINVAL);
  1280. }
  1281. /*
  1282. * Individual segments must be multiples of AES block size for the HW
  1283. * to process it. Non-compliant inputs aren't bogus, just not doable
  1284. * on this hardware.
  1285. */
  1286. for (i = 0; i < qp->cq_sg_crp->sg_nseg; i++)
  1287. if ((qp->cq_sg_crp->sg_segs[i].ss_len % AES_BLOCK_LEN) != 0) {
  1288. DPRINTF(dev, "%s: seg modulo: %zu\n", __func__,
  1289. qp->cq_sg_crp->sg_segs[i].ss_len);
  1290. return (EINVAL);
  1291. }
  1292. /* Gather IV/nonce data */
  1293. csp = crypto_get_params(crp->crp_session);
  1294. ccp_collect_iv(crp, csp, s->blkcipher.iv);
  1295. iv_len = csp->csp_ivlen;
  1296. if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
  1297. iv_len = AES_BLOCK_LEN;
  1298. if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
  1299. dir = CCP_CIPHER_DIR_ENCRYPT;
  1300. else
  1301. dir = CCP_CIPHER_DIR_DECRYPT;
  1302. /* Set up passthrough op(s) to copy IV into LSB */
  1303. error = ccp_do_pst_to_lsb(qp, ccp_queue_lsb_address(qp, LSB_ENTRY_IV),
  1304. s->blkcipher.iv, iv_len);
  1305. if (error != 0)
  1306. return (error);
  1307. /*
  1308. * Initialize keydata and keydata_len for GCC. The default case of the
  1309. * following switch is impossible to reach, but GCC doesn't know that.
  1310. */
  1311. keydata_len = 0;
  1312. keydata = NULL;
  1313. switch (csp->csp_cipher_alg) {
  1314. case CRYPTO_AES_XTS:
  1315. for (j = 0; j < nitems(ccp_xts_unitsize_map); j++)
  1316. if (ccp_xts_unitsize_map[j].cxu_size ==
  1317. crp->crp_payload_length)
  1318. break;
  1319. /* Input buffer must be a supported UnitSize */
  1320. if (j >= nitems(ccp_xts_unitsize_map)) {
  1321. device_printf(dev, "%s: rejected block size: %u\n",
  1322. __func__, crp->crp_payload_length);
  1323. return (EOPNOTSUPP);
  1324. }
  1325. /* FALLTHROUGH */
  1326. case CRYPTO_AES_CBC:
  1327. case CRYPTO_AES_ICM:
  1328. keydata = s->blkcipher.enckey;
  1329. keydata_len = s->blkcipher.key_len;
  1330. break;
  1331. }
  1332. INSECURE_DEBUG(dev, "%s: KEY(%zu): %16D\n", __func__, keydata_len,
  1333. keydata, " ");
  1334. if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
  1335. INSECURE_DEBUG(dev, "%s: KEY(XTS): %64D\n", __func__, keydata, " ");
  1336. /* Reverse order of key material for HW */
  1337. ccp_byteswap(keydata, keydata_len);
  1338. /* Store key material into LSB to avoid page boundaries */
  1339. if (csp->csp_cipher_alg == CRYPTO_AES_XTS) {
  1340. /*
  1341. * XTS mode uses 2 256-bit vectors for the primary key and the
  1342. * tweak key. For 128-bit keys, the vectors are zero-padded.
  1343. *
  1344. * After byteswapping the combined OCF-provided K1:K2 vector
  1345. * above, we need to reverse the order again so the hardware
  1346. * gets the swapped keys in the order K1':K2'.
  1347. */
  1348. error = ccp_do_pst_to_lsb(qp,
  1349. ccp_queue_lsb_address(qp, LSB_ENTRY_KEY + 1), keydata,
  1350. keydata_len / 2);
  1351. if (error != 0)
  1352. return (error);
  1353. error = ccp_do_pst_to_lsb(qp,
  1354. ccp_queue_lsb_address(qp, LSB_ENTRY_KEY),
  1355. keydata + (keydata_len / 2), keydata_len / 2);
  1356. /* Zero-pad 128 bit keys */
  1357. if (keydata_len == 32) {
  1358. if (error != 0)
  1359. return (error);
  1360. error = ccp_do_pst_to_lsb(qp,
  1361. ccp_queue_lsb_address(qp, LSB_ENTRY_KEY) +
  1362. keydata_len / 2, g_zeroes, keydata_len / 2);
  1363. if (error != 0)
  1364. return (error);
  1365. error = ccp_do_pst_to_lsb(qp,
  1366. ccp_queue_lsb_address(qp, LSB_ENTRY_KEY + 1) +
  1367. keydata_len / 2, g_zeroes, keydata_len / 2);
  1368. }
  1369. } else
  1370. error = ccp_do_pst_to_lsb(qp,
  1371. ccp_queue_lsb_address(qp, LSB_ENTRY_KEY), keydata,
  1372. keydata_len);
  1373. if (error != 0)
  1374. return (error);
  1375. /*
  1376. * Point SGLs at the subset of cryptop buffer contents representing the
  1377. * data.
  1378. */
  1379. sglist_reset(qp->cq_sg_ulptx);
  1380. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1381. crp->crp_payload_start, crp->crp_payload_length);
  1382. if (error != 0)
  1383. return (error);
  1384. INSECURE_DEBUG(dev, "%s: Contents: %16D\n", __func__,
  1385. (void *)PHYS_TO_DMAP(qp->cq_sg_ulptx->sg_segs[0].ss_paddr), " ");
  1386. DPRINTF(dev, "%s: starting AES ops @ %u\n", __func__, qp->cq_tail);
  1387. if (ccp_queue_get_ring_space(qp) < qp->cq_sg_ulptx->sg_nseg)
  1388. return (EAGAIN);
  1389. if (csp->csp_cipher_alg == CRYPTO_AES_XTS)
  1390. return (ccp_do_xts(qp, s, crp, dir, cctx));
  1391. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++) {
  1392. struct sglist_seg *seg;
  1393. seg = &qp->cq_sg_ulptx->sg_segs[i];
  1394. desc = &qp->desc_ring[qp->cq_tail];
  1395. desc->engine = CCP_ENGINE_AES;
  1396. desc->som = (i == 0);
  1397. desc->eom = (i == qp->cq_sg_ulptx->sg_nseg - 1);
  1398. desc->ioc = (desc->eom && cctx != NULL);
  1399. DPRINTF(dev, "%s: AES %u: som:%d eom:%d ioc:%d dir:%d\n",
  1400. __func__, qp->cq_tail, (int)desc->som, (int)desc->eom,
  1401. (int)desc->ioc, (int)dir);
  1402. if (desc->ioc)
  1403. memcpy(&qp->completions_ring[qp->cq_tail], cctx,
  1404. sizeof(*cctx));
  1405. desc->aes.encrypt = dir;
  1406. desc->aes.mode = s->blkcipher.cipher_mode;
  1407. desc->aes.type = s->blkcipher.cipher_type;
  1408. if (csp->csp_cipher_alg == CRYPTO_AES_ICM)
  1409. /*
  1410. * Size of CTR value in bits, - 1. ICM mode uses all
  1411. * 128 bits as counter.
  1412. */
  1413. desc->aes.size = 127;
  1414. DPRINTF(dev, "%s: AES %u: mode:%u type:%u size:%u\n", __func__,
  1415. qp->cq_tail, (unsigned)desc->aes.mode,
  1416. (unsigned)desc->aes.type, (unsigned)desc->aes.size);
  1417. desc->length = seg->ss_len;
  1418. desc->src_lo = (uint32_t)seg->ss_paddr;
  1419. desc->src_hi = (seg->ss_paddr >> 32);
  1420. desc->src_mem = CCP_MEMTYPE_SYSTEM;
  1421. /* Crypt in-place */
  1422. desc->dst_lo = desc->src_lo;
  1423. desc->dst_hi = desc->src_hi;
  1424. desc->dst_mem = desc->src_mem;
  1425. desc->key_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_KEY);
  1426. desc->key_hi = 0;
  1427. desc->key_mem = CCP_MEMTYPE_SB;
  1428. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_IV);
  1429. qp->cq_tail = (qp->cq_tail + 1) %
  1430. (1 << qp->cq_softc->ring_size_order);
  1431. }
  1432. return (0);
  1433. }
  1434. int __must_check
  1435. ccp_blkcipher(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp)
  1436. {
  1437. struct ccp_completion_ctx ctx;
  1438. ctx.callback_fn = ccp_blkcipher_done;
  1439. ctx.session = s;
  1440. ctx.callback_arg = crp;
  1441. return (ccp_do_blkcipher(qp, s, crp, &ctx));
  1442. }
  1443. static void
  1444. ccp_authenc_done(struct ccp_queue *qp, struct ccp_session *s, void *vcrp,
  1445. int error)
  1446. {
  1447. struct cryptop *crp;
  1448. explicit_bzero(&s->blkcipher.iv, sizeof(s->blkcipher.iv));
  1449. crp = vcrp;
  1450. ccp_do_hmac_done(qp, s, crp, error);
  1451. }
  1452. int __must_check
  1453. ccp_authenc(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp)
  1454. {
  1455. struct ccp_completion_ctx ctx;
  1456. int error;
  1457. ctx.callback_fn = ccp_authenc_done;
  1458. ctx.session = s;
  1459. ctx.callback_arg = crp;
  1460. /* Perform first operation */
  1461. if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
  1462. error = ccp_do_blkcipher(qp, s, crp, NULL);
  1463. else
  1464. error = ccp_do_hmac(qp, s, crp, NULL);
  1465. if (error != 0)
  1466. return (error);
  1467. /* Perform second operation */
  1468. if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
  1469. error = ccp_do_hmac(qp, s, crp, &ctx);
  1470. else
  1471. error = ccp_do_blkcipher(qp, s, crp, &ctx);
  1472. return (error);
  1473. }
  1474. static int __must_check
  1475. ccp_do_ghash_aad(struct ccp_queue *qp, struct ccp_session *s)
  1476. {
  1477. struct ccp_desc *desc;
  1478. struct sglist_seg *seg;
  1479. unsigned i;
  1480. if (ccp_queue_get_ring_space(qp) < qp->cq_sg_ulptx->sg_nseg)
  1481. return (EAGAIN);
  1482. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++) {
  1483. seg = &qp->cq_sg_ulptx->sg_segs[i];
  1484. desc = &qp->desc_ring[qp->cq_tail];
  1485. desc->engine = CCP_ENGINE_AES;
  1486. desc->aes.mode = CCP_AES_MODE_GHASH;
  1487. desc->aes.type = s->blkcipher.cipher_type;
  1488. desc->aes.encrypt = CCP_AES_MODE_GHASH_AAD;
  1489. desc->som = (i == 0);
  1490. desc->length = seg->ss_len;
  1491. desc->src_lo = (uint32_t)seg->ss_paddr;
  1492. desc->src_hi = (seg->ss_paddr >> 32);
  1493. desc->src_mem = CCP_MEMTYPE_SYSTEM;
  1494. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_IV);
  1495. desc->key_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_KEY);
  1496. desc->key_mem = CCP_MEMTYPE_SB;
  1497. qp->cq_tail = (qp->cq_tail + 1) %
  1498. (1 << qp->cq_softc->ring_size_order);
  1499. }
  1500. return (0);
  1501. }
  1502. static int __must_check
  1503. ccp_do_gctr(struct ccp_queue *qp, struct ccp_session *s,
  1504. enum ccp_cipher_dir dir, struct sglist_seg *seg, bool som, bool eom)
  1505. {
  1506. struct ccp_desc *desc;
  1507. if (ccp_queue_get_ring_space(qp) == 0)
  1508. return (EAGAIN);
  1509. desc = &qp->desc_ring[qp->cq_tail];
  1510. desc->engine = CCP_ENGINE_AES;
  1511. desc->aes.mode = CCP_AES_MODE_GCTR;
  1512. desc->aes.type = s->blkcipher.cipher_type;
  1513. desc->aes.encrypt = dir;
  1514. desc->aes.size = 8 * (seg->ss_len % GMAC_BLOCK_LEN) - 1;
  1515. desc->som = som;
  1516. desc->eom = eom;
  1517. /* Trailing bytes will be masked off by aes.size above. */
  1518. desc->length = roundup2(seg->ss_len, GMAC_BLOCK_LEN);
  1519. desc->dst_lo = desc->src_lo = (uint32_t)seg->ss_paddr;
  1520. desc->dst_hi = desc->src_hi = seg->ss_paddr >> 32;
  1521. desc->dst_mem = desc->src_mem = CCP_MEMTYPE_SYSTEM;
  1522. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_IV);
  1523. desc->key_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_KEY);
  1524. desc->key_mem = CCP_MEMTYPE_SB;
  1525. qp->cq_tail = (qp->cq_tail + 1) %
  1526. (1 << qp->cq_softc->ring_size_order);
  1527. return (0);
  1528. }
  1529. static int __must_check
  1530. ccp_do_ghash_final(struct ccp_queue *qp, struct ccp_session *s)
  1531. {
  1532. struct ccp_desc *desc;
  1533. if (ccp_queue_get_ring_space(qp) == 0)
  1534. return (EAGAIN);
  1535. desc = &qp->desc_ring[qp->cq_tail];
  1536. desc->engine = CCP_ENGINE_AES;
  1537. desc->aes.mode = CCP_AES_MODE_GHASH;
  1538. desc->aes.type = s->blkcipher.cipher_type;
  1539. desc->aes.encrypt = CCP_AES_MODE_GHASH_FINAL;
  1540. desc->length = GMAC_BLOCK_LEN;
  1541. desc->src_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_GHASH_IN);
  1542. desc->src_mem = CCP_MEMTYPE_SB;
  1543. desc->lsb_ctx_id = ccp_queue_lsb_entry(qp, LSB_ENTRY_IV);
  1544. desc->key_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_KEY);
  1545. desc->key_mem = CCP_MEMTYPE_SB;
  1546. desc->dst_lo = ccp_queue_lsb_address(qp, LSB_ENTRY_GHASH);
  1547. desc->dst_mem = CCP_MEMTYPE_SB;
  1548. qp->cq_tail = (qp->cq_tail + 1) %
  1549. (1 << qp->cq_softc->ring_size_order);
  1550. return (0);
  1551. }
  1552. static void
  1553. ccp_gcm_done(struct ccp_queue *qp, struct ccp_session *s, void *vcrp,
  1554. int error)
  1555. {
  1556. char tag[GMAC_DIGEST_LEN];
  1557. struct cryptop *crp;
  1558. crp = vcrp;
  1559. s->pending--;
  1560. if (error != 0) {
  1561. crp->crp_etype = error;
  1562. goto out;
  1563. }
  1564. /* Encrypt is done. Decrypt needs to verify tag. */
  1565. if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
  1566. goto out;
  1567. /* Copy in message tag. */
  1568. crypto_copydata(crp, crp->crp_digest_start, s->gmac.hash_len, tag);
  1569. /* Verify tag against computed GMAC */
  1570. if (timingsafe_bcmp(tag, s->gmac.final_block, s->gmac.hash_len) != 0)
  1571. crp->crp_etype = EBADMSG;
  1572. out:
  1573. explicit_bzero(&s->blkcipher.iv, sizeof(s->blkcipher.iv));
  1574. explicit_bzero(&s->gmac.final_block, sizeof(s->gmac.final_block));
  1575. crypto_done(crp);
  1576. }
  1577. int __must_check
  1578. ccp_gcm(struct ccp_queue *qp, struct ccp_session *s, struct cryptop *crp)
  1579. {
  1580. const struct crypto_session_params *csp;
  1581. struct ccp_completion_ctx ctx;
  1582. enum ccp_cipher_dir dir;
  1583. device_t dev;
  1584. unsigned i;
  1585. int error;
  1586. if (s->blkcipher.key_len == 0)
  1587. return (EINVAL);
  1588. dev = qp->cq_softc->dev;
  1589. if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op))
  1590. dir = CCP_CIPHER_DIR_ENCRYPT;
  1591. else
  1592. dir = CCP_CIPHER_DIR_DECRYPT;
  1593. /* Zero initial GHASH portion of context */
  1594. memset(s->blkcipher.iv, 0, sizeof(s->blkcipher.iv));
  1595. /* Gather IV data */
  1596. csp = crypto_get_params(crp->crp_session);
  1597. ccp_collect_iv(crp, csp, s->blkcipher.iv);
  1598. /* Reverse order of key material for HW */
  1599. ccp_byteswap(s->blkcipher.enckey, s->blkcipher.key_len);
  1600. /* Prepare input buffer of concatenated lengths for final GHASH */
  1601. be64enc(s->gmac.final_block, (uint64_t)crp->crp_aad_length * 8);
  1602. be64enc(&s->gmac.final_block[8], (uint64_t)crp->crp_payload_length * 8);
  1603. /* Send IV + initial zero GHASH, key data, and lengths buffer to LSB */
  1604. error = ccp_do_pst_to_lsb(qp, ccp_queue_lsb_address(qp, LSB_ENTRY_IV),
  1605. s->blkcipher.iv, 32);
  1606. if (error != 0)
  1607. return (error);
  1608. error = ccp_do_pst_to_lsb(qp, ccp_queue_lsb_address(qp, LSB_ENTRY_KEY),
  1609. s->blkcipher.enckey, s->blkcipher.key_len);
  1610. if (error != 0)
  1611. return (error);
  1612. error = ccp_do_pst_to_lsb(qp,
  1613. ccp_queue_lsb_address(qp, LSB_ENTRY_GHASH_IN), s->gmac.final_block,
  1614. GMAC_BLOCK_LEN);
  1615. if (error != 0)
  1616. return (error);
  1617. /* First step - compute GHASH over AAD */
  1618. if (crp->crp_aad_length != 0) {
  1619. sglist_reset(qp->cq_sg_ulptx);
  1620. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1621. crp->crp_aad_start, crp->crp_aad_length);
  1622. if (error != 0)
  1623. return (error);
  1624. /* This engine cannot process non-block multiple AAD data. */
  1625. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++)
  1626. if ((qp->cq_sg_ulptx->sg_segs[i].ss_len %
  1627. GMAC_BLOCK_LEN) != 0) {
  1628. DPRINTF(dev, "%s: AD seg modulo: %zu\n",
  1629. __func__,
  1630. qp->cq_sg_ulptx->sg_segs[i].ss_len);
  1631. return (EINVAL);
  1632. }
  1633. error = ccp_do_ghash_aad(qp, s);
  1634. if (error != 0)
  1635. return (error);
  1636. }
  1637. /* Feed data piece by piece into GCTR */
  1638. sglist_reset(qp->cq_sg_ulptx);
  1639. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1640. crp->crp_payload_start, crp->crp_payload_length);
  1641. if (error != 0)
  1642. return (error);
  1643. /*
  1644. * All segments except the last must be even multiples of AES block
  1645. * size for the HW to process it. Non-compliant inputs aren't bogus,
  1646. * just not doable on this hardware.
  1647. *
  1648. * XXX: Well, the hardware will produce a valid tag for shorter final
  1649. * segment inputs, but it will still write out a block-sized plaintext
  1650. * or ciphertext chunk. For a typical CRP this tramples trailing data,
  1651. * including the provided message tag. So, reject such inputs for now.
  1652. */
  1653. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++)
  1654. if ((qp->cq_sg_ulptx->sg_segs[i].ss_len % AES_BLOCK_LEN) != 0) {
  1655. DPRINTF(dev, "%s: seg modulo: %zu\n", __func__,
  1656. qp->cq_sg_ulptx->sg_segs[i].ss_len);
  1657. return (EINVAL);
  1658. }
  1659. for (i = 0; i < qp->cq_sg_ulptx->sg_nseg; i++) {
  1660. struct sglist_seg *seg;
  1661. seg = &qp->cq_sg_ulptx->sg_segs[i];
  1662. error = ccp_do_gctr(qp, s, dir, seg,
  1663. (i == 0 && crp->crp_aad_length == 0),
  1664. i == (qp->cq_sg_ulptx->sg_nseg - 1));
  1665. if (error != 0)
  1666. return (error);
  1667. }
  1668. /* Send just initial IV (not GHASH!) to LSB again */
  1669. error = ccp_do_pst_to_lsb(qp, ccp_queue_lsb_address(qp, LSB_ENTRY_IV),
  1670. s->blkcipher.iv, AES_BLOCK_LEN);
  1671. if (error != 0)
  1672. return (error);
  1673. ctx.callback_fn = ccp_gcm_done;
  1674. ctx.session = s;
  1675. ctx.callback_arg = crp;
  1676. /* Compute final hash and copy result back */
  1677. error = ccp_do_ghash_final(qp, s);
  1678. if (error != 0)
  1679. return (error);
  1680. /* When encrypting, copy computed tag out to caller buffer. */
  1681. sglist_reset(qp->cq_sg_ulptx);
  1682. if (dir == CCP_CIPHER_DIR_ENCRYPT)
  1683. error = sglist_append_sglist(qp->cq_sg_ulptx, qp->cq_sg_crp,
  1684. crp->crp_digest_start, s->gmac.hash_len);
  1685. else
  1686. /*
  1687. * For decrypting, copy the computed tag out to our session
  1688. * buffer to verify in our callback.
  1689. */
  1690. error = sglist_append(qp->cq_sg_ulptx, s->gmac.final_block,
  1691. s->gmac.hash_len);
  1692. if (error != 0)
  1693. return (error);
  1694. error = ccp_passthrough_sgl(qp,
  1695. ccp_queue_lsb_address(qp, LSB_ENTRY_GHASH), false, qp->cq_sg_ulptx,
  1696. s->gmac.hash_len, true, &ctx);
  1697. return (error);
  1698. }
  1699. #define MAX_TRNG_RETRIES 10
  1700. u_int
  1701. random_ccp_read(void *v, u_int c)
  1702. {
  1703. uint32_t *buf;
  1704. u_int i, j;
  1705. KASSERT(c % sizeof(*buf) == 0, ("%u not multiple of u_long", c));
  1706. buf = v;
  1707. for (i = c; i > 0; i -= sizeof(*buf)) {
  1708. for (j = 0; j < MAX_TRNG_RETRIES; j++) {
  1709. *buf = ccp_read_4(g_ccp_softc, TRNG_OUT_OFFSET);
  1710. if (*buf != 0)
  1711. break;
  1712. }
  1713. if (j == MAX_TRNG_RETRIES)
  1714. return (0);
  1715. buf++;
  1716. }
  1717. return (c);
  1718. }
  1719. #ifdef DDB
  1720. void
  1721. db_ccp_show_hw(struct ccp_softc *sc)
  1722. {
  1723. db_printf(" queue mask: 0x%x\n",
  1724. ccp_read_4(sc, CMD_QUEUE_MASK_OFFSET));
  1725. db_printf(" queue prio: 0x%x\n",
  1726. ccp_read_4(sc, CMD_QUEUE_PRIO_OFFSET));
  1727. db_printf(" reqid: 0x%x\n", ccp_read_4(sc, CMD_REQID_CONFIG_OFFSET));
  1728. db_printf(" trng output: 0x%x\n", ccp_read_4(sc, TRNG_OUT_OFFSET));
  1729. db_printf(" cmd timeout: 0x%x\n",
  1730. ccp_read_4(sc, CMD_CMD_TIMEOUT_OFFSET));
  1731. db_printf(" lsb public mask lo: 0x%x\n",
  1732. ccp_read_4(sc, LSB_PUBLIC_MASK_LO_OFFSET));
  1733. db_printf(" lsb public mask hi: 0x%x\n",
  1734. ccp_read_4(sc, LSB_PUBLIC_MASK_HI_OFFSET));
  1735. db_printf(" lsb private mask lo: 0x%x\n",
  1736. ccp_read_4(sc, LSB_PRIVATE_MASK_LO_OFFSET));
  1737. db_printf(" lsb private mask hi: 0x%x\n",
  1738. ccp_read_4(sc, LSB_PRIVATE_MASK_HI_OFFSET));
  1739. db_printf(" version: 0x%x\n", ccp_read_4(sc, VERSION_REG));
  1740. }
  1741. void
  1742. db_ccp_show_queue_hw(struct ccp_queue *qp)
  1743. {
  1744. const struct ccp_error_code *ec;
  1745. struct ccp_softc *sc;
  1746. uint32_t status, error, esource, faultblock, headlo, qcontrol;
  1747. unsigned q, i;
  1748. sc = qp->cq_softc;
  1749. q = qp->cq_qindex;
  1750. qcontrol = ccp_read_queue_4(sc, q, CMD_Q_CONTROL_BASE);
  1751. db_printf(" qcontrol: 0x%x%s%s\n", qcontrol,
  1752. (qcontrol & CMD_Q_RUN) ? " RUN" : "",
  1753. (qcontrol & CMD_Q_HALTED) ? " HALTED" : "");
  1754. db_printf(" tail_lo: 0x%x\n",
  1755. ccp_read_queue_4(sc, q, CMD_Q_TAIL_LO_BASE));
  1756. headlo = ccp_read_queue_4(sc, q, CMD_Q_HEAD_LO_BASE);
  1757. db_printf(" head_lo: 0x%x\n", headlo);
  1758. db_printf(" int enable: 0x%x\n",
  1759. ccp_read_queue_4(sc, q, CMD_Q_INT_ENABLE_BASE));
  1760. db_printf(" interrupt status: 0x%x\n",
  1761. ccp_read_queue_4(sc, q, CMD_Q_INTERRUPT_STATUS_BASE));
  1762. status = ccp_read_queue_4(sc, q, CMD_Q_STATUS_BASE);
  1763. db_printf(" status: 0x%x\n", status);
  1764. db_printf(" int stats: 0x%x\n",
  1765. ccp_read_queue_4(sc, q, CMD_Q_INT_STATUS_BASE));
  1766. error = status & STATUS_ERROR_MASK;
  1767. if (error == 0)
  1768. return;
  1769. esource = (status >> STATUS_ERRORSOURCE_SHIFT) &
  1770. STATUS_ERRORSOURCE_MASK;
  1771. faultblock = (status >> STATUS_VLSB_FAULTBLOCK_SHIFT) &
  1772. STATUS_VLSB_FAULTBLOCK_MASK;
  1773. ec = NULL;
  1774. for (i = 0; i < nitems(ccp_error_codes); i++)
  1775. if (ccp_error_codes[i].ce_code == error)
  1776. break;
  1777. if (i < nitems(ccp_error_codes))
  1778. ec = &ccp_error_codes[i];
  1779. db_printf(" Error: %s (%u) Source: %u Faulting LSB block: %u\n",
  1780. (ec != NULL) ? ec->ce_name : "(reserved)", error, esource,
  1781. faultblock);
  1782. if (ec != NULL)
  1783. db_printf(" Error description: %s\n", ec->ce_desc);
  1784. i = (headlo - (uint32_t)qp->desc_ring_bus_addr) / Q_DESC_SIZE;
  1785. db_printf(" Bad descriptor idx: %u contents:\n %32D\n", i,
  1786. (void *)&qp->desc_ring[i], " ");
  1787. }
  1788. #endif