user_sdma.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/mm.h>
  48. #include <linux/types.h>
  49. #include <linux/device.h>
  50. #include <linux/dmapool.h>
  51. #include <linux/slab.h>
  52. #include <linux/list.h>
  53. #include <linux/highmem.h>
  54. #include <linux/io.h>
  55. #include <linux/uio.h>
  56. #include <linux/rbtree.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/delay.h>
  59. #include <linux/kthread.h>
  60. #include <linux/mmu_context.h>
  61. #include <linux/module.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/string.h>
  64. #include "hfi.h"
  65. #include "sdma.h"
  66. #include "user_sdma.h"
  67. #include "verbs.h" /* for the headers */
  68. #include "common.h" /* for struct hfi1_tid_info */
  69. #include "trace.h"
  70. #include "mmu_rb.h"
  71. static uint hfi1_sdma_comp_ring_size = 128;
  72. module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO);
  73. MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128");
  74. /* The maximum number of Data io vectors per message/request */
  75. #define MAX_VECTORS_PER_REQ 8
  76. /*
  77. * Maximum number of packet to send from each message/request
  78. * before moving to the next one.
  79. */
  80. #define MAX_PKTS_PER_QUEUE 16
  81. #define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
  82. #define req_opcode(x) \
  83. (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
  84. #define req_version(x) \
  85. (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
  86. #define req_iovcnt(x) \
  87. (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
  88. /* Number of BTH.PSN bits used for sequence number in expected rcvs */
  89. #define BTH_SEQ_MASK 0x7ffull
  90. /*
  91. * Define fields in the KDETH header so we can update the header
  92. * template.
  93. */
  94. #define KDETH_OFFSET_SHIFT 0
  95. #define KDETH_OFFSET_MASK 0x7fff
  96. #define KDETH_OM_SHIFT 15
  97. #define KDETH_OM_MASK 0x1
  98. #define KDETH_TID_SHIFT 16
  99. #define KDETH_TID_MASK 0x3ff
  100. #define KDETH_TIDCTRL_SHIFT 26
  101. #define KDETH_TIDCTRL_MASK 0x3
  102. #define KDETH_INTR_SHIFT 28
  103. #define KDETH_INTR_MASK 0x1
  104. #define KDETH_SH_SHIFT 29
  105. #define KDETH_SH_MASK 0x1
  106. #define KDETH_HCRC_UPPER_SHIFT 16
  107. #define KDETH_HCRC_UPPER_MASK 0xff
  108. #define KDETH_HCRC_LOWER_SHIFT 24
  109. #define KDETH_HCRC_LOWER_MASK 0xff
  110. #define AHG_KDETH_INTR_SHIFT 12
  111. #define AHG_KDETH_SH_SHIFT 13
  112. #define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
  113. #define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
  114. #define KDETH_GET(val, field) \
  115. (((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK)
  116. #define KDETH_SET(dw, field, val) do { \
  117. u32 dwval = le32_to_cpu(dw); \
  118. dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \
  119. dwval |= (((val) & KDETH_##field##_MASK) << \
  120. KDETH_##field##_SHIFT); \
  121. dw = cpu_to_le32(dwval); \
  122. } while (0)
  123. #define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \
  124. do { \
  125. if ((idx) < ARRAY_SIZE((arr))) \
  126. (arr)[(idx++)] = sdma_build_ahg_descriptor( \
  127. (__force u16)(value), (dw), (bit), \
  128. (width)); \
  129. else \
  130. return -ERANGE; \
  131. } while (0)
  132. /* KDETH OM multipliers and switch over point */
  133. #define KDETH_OM_SMALL 4
  134. #define KDETH_OM_LARGE 64
  135. #define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
  136. /* Tx request flag bits */
  137. #define TXREQ_FLAGS_REQ_ACK BIT(0) /* Set the ACK bit in the header */
  138. #define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
  139. /* SDMA request flag bits */
  140. #define SDMA_REQ_FOR_THREAD 1
  141. #define SDMA_REQ_SEND_DONE 2
  142. #define SDMA_REQ_HAVE_AHG 3
  143. #define SDMA_REQ_HAS_ERROR 4
  144. #define SDMA_REQ_DONE_ERROR 5
  145. #define SDMA_PKT_Q_INACTIVE BIT(0)
  146. #define SDMA_PKT_Q_ACTIVE BIT(1)
  147. #define SDMA_PKT_Q_DEFERRED BIT(2)
  148. /*
  149. * Maximum retry attempts to submit a TX request
  150. * before putting the process to sleep.
  151. */
  152. #define MAX_DEFER_RETRY_COUNT 1
  153. static unsigned initial_pkt_count = 8;
  154. #define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
  155. struct sdma_mmu_node;
  156. struct user_sdma_iovec {
  157. struct list_head list;
  158. struct iovec iov;
  159. /* number of pages in this vector */
  160. unsigned npages;
  161. /* array of pinned pages for this vector */
  162. struct page **pages;
  163. /*
  164. * offset into the virtual address space of the vector at
  165. * which we last left off.
  166. */
  167. u64 offset;
  168. struct sdma_mmu_node *node;
  169. };
  170. struct sdma_mmu_node {
  171. struct mmu_rb_node rb;
  172. struct hfi1_user_sdma_pkt_q *pq;
  173. atomic_t refcount;
  174. struct page **pages;
  175. unsigned npages;
  176. };
  177. /* evict operation argument */
  178. struct evict_data {
  179. u32 cleared; /* count evicted so far */
  180. u32 target; /* target count to evict */
  181. };
  182. struct user_sdma_request {
  183. struct sdma_req_info info;
  184. struct hfi1_user_sdma_pkt_q *pq;
  185. struct hfi1_user_sdma_comp_q *cq;
  186. /* This is the original header from user space */
  187. struct hfi1_pkt_header hdr;
  188. /*
  189. * Pointer to the SDMA engine for this request.
  190. * Since different request could be on different VLs,
  191. * each request will need it's own engine pointer.
  192. */
  193. struct sdma_engine *sde;
  194. u8 ahg_idx;
  195. u32 ahg[9];
  196. /*
  197. * KDETH.Offset (Eager) field
  198. * We need to remember the initial value so the headers
  199. * can be updated properly.
  200. */
  201. u32 koffset;
  202. /*
  203. * KDETH.OFFSET (TID) field
  204. * The offset can cover multiple packets, depending on the
  205. * size of the TID entry.
  206. */
  207. u32 tidoffset;
  208. /*
  209. * KDETH.OM
  210. * Remember this because the header template always sets it
  211. * to 0.
  212. */
  213. u8 omfactor;
  214. /*
  215. * We copy the iovs for this request (based on
  216. * info.iovcnt). These are only the data vectors
  217. */
  218. unsigned data_iovs;
  219. /* total length of the data in the request */
  220. u32 data_len;
  221. /* progress index moving along the iovs array */
  222. unsigned iov_idx;
  223. struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
  224. /* number of elements copied to the tids array */
  225. u16 n_tids;
  226. /* TID array values copied from the tid_iov vector */
  227. u32 *tids;
  228. u16 tididx;
  229. u32 sent;
  230. u64 seqnum;
  231. u64 seqcomp;
  232. u64 seqsubmitted;
  233. struct list_head txps;
  234. unsigned long flags;
  235. /* status of the last txreq completed */
  236. int status;
  237. };
  238. /*
  239. * A single txreq could span up to 3 physical pages when the MTU
  240. * is sufficiently large (> 4K). Each of the IOV pointers also
  241. * needs it's own set of flags so the vector has been handled
  242. * independently of each other.
  243. */
  244. struct user_sdma_txreq {
  245. /* Packet header for the txreq */
  246. struct hfi1_pkt_header hdr;
  247. struct sdma_txreq txreq;
  248. struct list_head list;
  249. struct user_sdma_request *req;
  250. u16 flags;
  251. unsigned busycount;
  252. u64 seqnum;
  253. };
  254. #define SDMA_DBG(req, fmt, ...) \
  255. hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
  256. (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
  257. ##__VA_ARGS__)
  258. #define SDMA_Q_DBG(pq, fmt, ...) \
  259. hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
  260. (pq)->subctxt, ##__VA_ARGS__)
  261. static int user_sdma_send_pkts(struct user_sdma_request *, unsigned);
  262. static int num_user_pages(const struct iovec *);
  263. static void user_sdma_txreq_cb(struct sdma_txreq *, int);
  264. static inline void pq_update(struct hfi1_user_sdma_pkt_q *);
  265. static void user_sdma_free_request(struct user_sdma_request *, bool);
  266. static int pin_vector_pages(struct user_sdma_request *,
  267. struct user_sdma_iovec *);
  268. static void unpin_vector_pages(struct mm_struct *, struct page **, unsigned,
  269. unsigned);
  270. static int check_header_template(struct user_sdma_request *,
  271. struct hfi1_pkt_header *, u32, u32);
  272. static int set_txreq_header(struct user_sdma_request *,
  273. struct user_sdma_txreq *, u32);
  274. static int set_txreq_header_ahg(struct user_sdma_request *,
  275. struct user_sdma_txreq *, u32);
  276. static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *,
  277. struct hfi1_user_sdma_comp_q *,
  278. u16, enum hfi1_sdma_comp_state, int);
  279. static inline u32 set_pkt_bth_psn(__be32, u8, u32);
  280. static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
  281. static int defer_packet_queue(
  282. struct sdma_engine *,
  283. struct iowait *,
  284. struct sdma_txreq *,
  285. unsigned seq);
  286. static void activate_packet_queue(struct iowait *, int);
  287. static bool sdma_rb_filter(struct mmu_rb_node *, unsigned long, unsigned long);
  288. static int sdma_rb_insert(void *, struct mmu_rb_node *);
  289. static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
  290. void *arg2, bool *stop);
  291. static void sdma_rb_remove(void *, struct mmu_rb_node *);
  292. static int sdma_rb_invalidate(void *, struct mmu_rb_node *);
  293. static struct mmu_rb_ops sdma_rb_ops = {
  294. .filter = sdma_rb_filter,
  295. .insert = sdma_rb_insert,
  296. .evict = sdma_rb_evict,
  297. .remove = sdma_rb_remove,
  298. .invalidate = sdma_rb_invalidate
  299. };
  300. static int defer_packet_queue(
  301. struct sdma_engine *sde,
  302. struct iowait *wait,
  303. struct sdma_txreq *txreq,
  304. unsigned seq)
  305. {
  306. struct hfi1_user_sdma_pkt_q *pq =
  307. container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
  308. struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
  309. struct user_sdma_txreq *tx =
  310. container_of(txreq, struct user_sdma_txreq, txreq);
  311. if (sdma_progress(sde, seq, txreq)) {
  312. if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
  313. goto eagain;
  314. }
  315. /*
  316. * We are assuming that if the list is enqueued somewhere, it
  317. * is to the dmawait list since that is the only place where
  318. * it is supposed to be enqueued.
  319. */
  320. xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
  321. write_seqlock(&dev->iowait_lock);
  322. if (list_empty(&pq->busy.list))
  323. list_add_tail(&pq->busy.list, &sde->dmawait);
  324. write_sequnlock(&dev->iowait_lock);
  325. return -EBUSY;
  326. eagain:
  327. return -EAGAIN;
  328. }
  329. static void activate_packet_queue(struct iowait *wait, int reason)
  330. {
  331. struct hfi1_user_sdma_pkt_q *pq =
  332. container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
  333. xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
  334. wake_up(&wait->wait_dma);
  335. };
  336. static void sdma_kmem_cache_ctor(void *obj)
  337. {
  338. struct user_sdma_txreq *tx = obj;
  339. memset(tx, 0, sizeof(*tx));
  340. }
  341. int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, struct file *fp)
  342. {
  343. struct hfi1_filedata *fd;
  344. int ret = 0;
  345. unsigned memsize;
  346. char buf[64];
  347. struct hfi1_devdata *dd;
  348. struct hfi1_user_sdma_comp_q *cq;
  349. struct hfi1_user_sdma_pkt_q *pq;
  350. unsigned long flags;
  351. if (!uctxt || !fp) {
  352. ret = -EBADF;
  353. goto done;
  354. }
  355. fd = fp->private_data;
  356. if (!hfi1_sdma_comp_ring_size) {
  357. ret = -EINVAL;
  358. goto done;
  359. }
  360. dd = uctxt->dd;
  361. pq = kzalloc(sizeof(*pq), GFP_KERNEL);
  362. if (!pq)
  363. goto pq_nomem;
  364. memsize = sizeof(*pq->reqs) * hfi1_sdma_comp_ring_size;
  365. pq->reqs = kzalloc(memsize, GFP_KERNEL);
  366. if (!pq->reqs)
  367. goto pq_reqs_nomem;
  368. memsize = BITS_TO_LONGS(hfi1_sdma_comp_ring_size) * sizeof(long);
  369. pq->req_in_use = kzalloc(memsize, GFP_KERNEL);
  370. if (!pq->req_in_use)
  371. goto pq_reqs_no_in_use;
  372. INIT_LIST_HEAD(&pq->list);
  373. pq->dd = dd;
  374. pq->ctxt = uctxt->ctxt;
  375. pq->subctxt = fd->subctxt;
  376. pq->n_max_reqs = hfi1_sdma_comp_ring_size;
  377. pq->state = SDMA_PKT_Q_INACTIVE;
  378. atomic_set(&pq->n_reqs, 0);
  379. init_waitqueue_head(&pq->wait);
  380. atomic_set(&pq->n_locked, 0);
  381. pq->mm = fd->mm;
  382. iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
  383. activate_packet_queue, NULL);
  384. pq->reqidx = 0;
  385. snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
  386. fd->subctxt);
  387. pq->txreq_cache = kmem_cache_create(buf,
  388. sizeof(struct user_sdma_txreq),
  389. L1_CACHE_BYTES,
  390. SLAB_HWCACHE_ALIGN,
  391. sdma_kmem_cache_ctor);
  392. if (!pq->txreq_cache) {
  393. dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
  394. uctxt->ctxt);
  395. goto pq_txreq_nomem;
  396. }
  397. fd->pq = pq;
  398. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  399. if (!cq)
  400. goto cq_nomem;
  401. memsize = PAGE_ALIGN(sizeof(*cq->comps) * hfi1_sdma_comp_ring_size);
  402. cq->comps = vmalloc_user(memsize);
  403. if (!cq->comps)
  404. goto cq_comps_nomem;
  405. cq->nentries = hfi1_sdma_comp_ring_size;
  406. fd->cq = cq;
  407. ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
  408. &pq->handler);
  409. if (ret) {
  410. dd_dev_err(dd, "Failed to register with MMU %d", ret);
  411. goto done;
  412. }
  413. spin_lock_irqsave(&uctxt->sdma_qlock, flags);
  414. list_add(&pq->list, &uctxt->sdma_queues);
  415. spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
  416. goto done;
  417. cq_comps_nomem:
  418. kfree(cq);
  419. cq_nomem:
  420. kmem_cache_destroy(pq->txreq_cache);
  421. pq_txreq_nomem:
  422. kfree(pq->req_in_use);
  423. pq_reqs_no_in_use:
  424. kfree(pq->reqs);
  425. pq_reqs_nomem:
  426. kfree(pq);
  427. fd->pq = NULL;
  428. pq_nomem:
  429. ret = -ENOMEM;
  430. done:
  431. return ret;
  432. }
  433. int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd)
  434. {
  435. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  436. struct hfi1_user_sdma_pkt_q *pq;
  437. unsigned long flags;
  438. hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
  439. uctxt->ctxt, fd->subctxt);
  440. pq = fd->pq;
  441. if (pq) {
  442. if (pq->handler)
  443. hfi1_mmu_rb_unregister(pq->handler);
  444. spin_lock_irqsave(&uctxt->sdma_qlock, flags);
  445. if (!list_empty(&pq->list))
  446. list_del_init(&pq->list);
  447. spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
  448. iowait_sdma_drain(&pq->busy);
  449. /* Wait until all requests have been freed. */
  450. wait_event_interruptible(
  451. pq->wait,
  452. (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
  453. kfree(pq->reqs);
  454. kfree(pq->req_in_use);
  455. kmem_cache_destroy(pq->txreq_cache);
  456. kfree(pq);
  457. fd->pq = NULL;
  458. }
  459. if (fd->cq) {
  460. vfree(fd->cq->comps);
  461. kfree(fd->cq);
  462. fd->cq = NULL;
  463. }
  464. return 0;
  465. }
  466. static u8 dlid_to_selector(u16 dlid)
  467. {
  468. static u8 mapping[256];
  469. static int initialized;
  470. static u8 next;
  471. int hash;
  472. if (!initialized) {
  473. memset(mapping, 0xFF, 256);
  474. initialized = 1;
  475. }
  476. hash = ((dlid >> 8) ^ dlid) & 0xFF;
  477. if (mapping[hash] == 0xFF) {
  478. mapping[hash] = next;
  479. next = (next + 1) & 0x7F;
  480. }
  481. return mapping[hash];
  482. }
  483. int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec,
  484. unsigned long dim, unsigned long *count)
  485. {
  486. int ret = 0, i;
  487. struct hfi1_filedata *fd = fp->private_data;
  488. struct hfi1_ctxtdata *uctxt = fd->uctxt;
  489. struct hfi1_user_sdma_pkt_q *pq = fd->pq;
  490. struct hfi1_user_sdma_comp_q *cq = fd->cq;
  491. struct hfi1_devdata *dd = pq->dd;
  492. unsigned long idx = 0;
  493. u8 pcount = initial_pkt_count;
  494. struct sdma_req_info info;
  495. struct user_sdma_request *req;
  496. u8 opcode, sc, vl;
  497. int req_queued = 0;
  498. u16 dlid;
  499. u32 selector;
  500. if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
  501. hfi1_cdbg(
  502. SDMA,
  503. "[%u:%u:%u] First vector not big enough for header %lu/%lu",
  504. dd->unit, uctxt->ctxt, fd->subctxt,
  505. iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
  506. return -EINVAL;
  507. }
  508. ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
  509. if (ret) {
  510. hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
  511. dd->unit, uctxt->ctxt, fd->subctxt, ret);
  512. return -EFAULT;
  513. }
  514. trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
  515. (u16 *)&info);
  516. if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
  517. hfi1_cdbg(SDMA,
  518. "[%u:%u:%u:%u] Invalid comp index",
  519. dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
  520. return -EINVAL;
  521. }
  522. /*
  523. * Sanity check the header io vector count. Need at least 1 vector
  524. * (header) and cannot be larger than the actual io vector count.
  525. */
  526. if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
  527. hfi1_cdbg(SDMA,
  528. "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
  529. dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
  530. req_iovcnt(info.ctrl), dim);
  531. return -EINVAL;
  532. }
  533. if (!info.fragsize) {
  534. hfi1_cdbg(SDMA,
  535. "[%u:%u:%u:%u] Request does not specify fragsize",
  536. dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
  537. return -EINVAL;
  538. }
  539. /* Try to claim the request. */
  540. if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
  541. hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
  542. dd->unit, uctxt->ctxt, fd->subctxt,
  543. info.comp_idx);
  544. return -EBADSLT;
  545. }
  546. /*
  547. * All safety checks have been done and this request has been claimed.
  548. */
  549. hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
  550. uctxt->ctxt, fd->subctxt, info.comp_idx);
  551. req = pq->reqs + info.comp_idx;
  552. memset(req, 0, sizeof(*req));
  553. req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
  554. req->pq = pq;
  555. req->cq = cq;
  556. req->status = -1;
  557. INIT_LIST_HEAD(&req->txps);
  558. memcpy(&req->info, &info, sizeof(info));
  559. if (req_opcode(info.ctrl) == EXPECTED) {
  560. /* expected must have a TID info and at least one data vector */
  561. if (req->data_iovs < 2) {
  562. SDMA_DBG(req,
  563. "Not enough vectors for expected request");
  564. ret = -EINVAL;
  565. goto free_req;
  566. }
  567. req->data_iovs--;
  568. }
  569. if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
  570. SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
  571. MAX_VECTORS_PER_REQ);
  572. ret = -EINVAL;
  573. goto free_req;
  574. }
  575. /* Copy the header from the user buffer */
  576. ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
  577. sizeof(req->hdr));
  578. if (ret) {
  579. SDMA_DBG(req, "Failed to copy header template (%d)", ret);
  580. ret = -EFAULT;
  581. goto free_req;
  582. }
  583. /* If Static rate control is not enabled, sanitize the header. */
  584. if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
  585. req->hdr.pbc[2] = 0;
  586. /* Validate the opcode. Do not trust packets from user space blindly. */
  587. opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
  588. if ((opcode & USER_OPCODE_CHECK_MASK) !=
  589. USER_OPCODE_CHECK_VAL) {
  590. SDMA_DBG(req, "Invalid opcode (%d)", opcode);
  591. ret = -EINVAL;
  592. goto free_req;
  593. }
  594. /*
  595. * Validate the vl. Do not trust packets from user space blindly.
  596. * VL comes from PBC, SC comes from LRH, and the VL needs to
  597. * match the SC look up.
  598. */
  599. vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
  600. sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
  601. (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
  602. if (vl >= dd->pport->vls_operational ||
  603. vl != sc_to_vlt(dd, sc)) {
  604. SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
  605. ret = -EINVAL;
  606. goto free_req;
  607. }
  608. /* Checking P_KEY for requests from user-space */
  609. if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc,
  610. PKEY_CHECK_INVALID)) {
  611. ret = -EINVAL;
  612. goto free_req;
  613. }
  614. /*
  615. * Also should check the BTH.lnh. If it says the next header is GRH then
  616. * the RXE parsing will be off and will land in the middle of the KDETH
  617. * or miss it entirely.
  618. */
  619. if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
  620. SDMA_DBG(req, "User tried to pass in a GRH");
  621. ret = -EINVAL;
  622. goto free_req;
  623. }
  624. req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
  625. /*
  626. * Calculate the initial TID offset based on the values of
  627. * KDETH.OFFSET and KDETH.OM that are passed in.
  628. */
  629. req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
  630. (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
  631. KDETH_OM_LARGE : KDETH_OM_SMALL);
  632. SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
  633. idx++;
  634. /* Save all the IO vector structures */
  635. for (i = 0; i < req->data_iovs; i++) {
  636. INIT_LIST_HEAD(&req->iovs[i].list);
  637. memcpy(&req->iovs[i].iov, iovec + idx++, sizeof(struct iovec));
  638. ret = pin_vector_pages(req, &req->iovs[i]);
  639. if (ret) {
  640. req->status = ret;
  641. goto free_req;
  642. }
  643. req->data_len += req->iovs[i].iov.iov_len;
  644. }
  645. SDMA_DBG(req, "total data length %u", req->data_len);
  646. if (pcount > req->info.npkts)
  647. pcount = req->info.npkts;
  648. /*
  649. * Copy any TID info
  650. * User space will provide the TID info only when the
  651. * request type is EXPECTED. This is true even if there is
  652. * only one packet in the request and the header is already
  653. * setup. The reason for the singular TID case is that the
  654. * driver needs to perform safety checks.
  655. */
  656. if (req_opcode(req->info.ctrl) == EXPECTED) {
  657. u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
  658. u32 *tmp;
  659. if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
  660. ret = -EINVAL;
  661. goto free_req;
  662. }
  663. /*
  664. * We have to copy all of the tids because they may vary
  665. * in size and, therefore, the TID count might not be
  666. * equal to the pkt count. However, there is no way to
  667. * tell at this point.
  668. */
  669. tmp = memdup_user(iovec[idx].iov_base,
  670. ntids * sizeof(*req->tids));
  671. if (IS_ERR(tmp)) {
  672. ret = PTR_ERR(tmp);
  673. SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
  674. ntids, ret);
  675. goto free_req;
  676. }
  677. req->tids = tmp;
  678. req->n_tids = ntids;
  679. idx++;
  680. }
  681. dlid = be16_to_cpu(req->hdr.lrh[1]);
  682. selector = dlid_to_selector(dlid);
  683. selector += uctxt->ctxt + fd->subctxt;
  684. req->sde = sdma_select_user_engine(dd, selector, vl);
  685. if (!req->sde || !sdma_running(req->sde)) {
  686. ret = -ECOMM;
  687. goto free_req;
  688. }
  689. /* We don't need an AHG entry if the request contains only one packet */
  690. if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG)) {
  691. int ahg = sdma_ahg_alloc(req->sde);
  692. if (likely(ahg >= 0)) {
  693. req->ahg_idx = (u8)ahg;
  694. set_bit(SDMA_REQ_HAVE_AHG, &req->flags);
  695. }
  696. }
  697. set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
  698. atomic_inc(&pq->n_reqs);
  699. req_queued = 1;
  700. /* Send the first N packets in the request to buy us some time */
  701. ret = user_sdma_send_pkts(req, pcount);
  702. if (unlikely(ret < 0 && ret != -EBUSY)) {
  703. req->status = ret;
  704. goto free_req;
  705. }
  706. /*
  707. * It is possible that the SDMA engine would have processed all the
  708. * submitted packets by the time we get here. Therefore, only set
  709. * packet queue state to ACTIVE if there are still uncompleted
  710. * requests.
  711. */
  712. if (atomic_read(&pq->n_reqs))
  713. xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
  714. /*
  715. * This is a somewhat blocking send implementation.
  716. * The driver will block the caller until all packets of the
  717. * request have been submitted to the SDMA engine. However, it
  718. * will not wait for send completions.
  719. */
  720. while (!test_bit(SDMA_REQ_SEND_DONE, &req->flags)) {
  721. ret = user_sdma_send_pkts(req, pcount);
  722. if (ret < 0) {
  723. if (ret != -EBUSY) {
  724. req->status = ret;
  725. set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
  726. if (ACCESS_ONCE(req->seqcomp) ==
  727. req->seqsubmitted - 1)
  728. goto free_req;
  729. return ret;
  730. }
  731. wait_event_interruptible_timeout(
  732. pq->busy.wait_dma,
  733. (pq->state == SDMA_PKT_Q_ACTIVE),
  734. msecs_to_jiffies(
  735. SDMA_IOWAIT_TIMEOUT));
  736. }
  737. }
  738. *count += idx;
  739. return 0;
  740. free_req:
  741. user_sdma_free_request(req, true);
  742. if (req_queued)
  743. pq_update(pq);
  744. set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
  745. return ret;
  746. }
  747. static inline u32 compute_data_length(struct user_sdma_request *req,
  748. struct user_sdma_txreq *tx)
  749. {
  750. /*
  751. * Determine the proper size of the packet data.
  752. * The size of the data of the first packet is in the header
  753. * template. However, it includes the header and ICRC, which need
  754. * to be subtracted.
  755. * The minimum representable packet data length in a header is 4 bytes,
  756. * therefore, when the data length request is less than 4 bytes, there's
  757. * only one packet, and the packet data length is equal to that of the
  758. * request data length.
  759. * The size of the remaining packets is the minimum of the frag
  760. * size (MTU) or remaining data in the request.
  761. */
  762. u32 len;
  763. if (!req->seqnum) {
  764. if (req->data_len < sizeof(u32))
  765. len = req->data_len;
  766. else
  767. len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
  768. (sizeof(tx->hdr) - 4));
  769. } else if (req_opcode(req->info.ctrl) == EXPECTED) {
  770. u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
  771. PAGE_SIZE;
  772. /*
  773. * Get the data length based on the remaining space in the
  774. * TID pair.
  775. */
  776. len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
  777. /* If we've filled up the TID pair, move to the next one. */
  778. if (unlikely(!len) && ++req->tididx < req->n_tids &&
  779. req->tids[req->tididx]) {
  780. tidlen = EXP_TID_GET(req->tids[req->tididx],
  781. LEN) * PAGE_SIZE;
  782. req->tidoffset = 0;
  783. len = min_t(u32, tidlen, req->info.fragsize);
  784. }
  785. /*
  786. * Since the TID pairs map entire pages, make sure that we
  787. * are not going to try to send more data that we have
  788. * remaining.
  789. */
  790. len = min(len, req->data_len - req->sent);
  791. } else {
  792. len = min(req->data_len - req->sent, (u32)req->info.fragsize);
  793. }
  794. SDMA_DBG(req, "Data Length = %u", len);
  795. return len;
  796. }
  797. static inline u32 pad_len(u32 len)
  798. {
  799. if (len & (sizeof(u32) - 1))
  800. len += sizeof(u32) - (len & (sizeof(u32) - 1));
  801. return len;
  802. }
  803. static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
  804. {
  805. /* (Size of complete header - size of PBC) + 4B ICRC + data length */
  806. return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
  807. }
  808. static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
  809. {
  810. int ret = 0, count;
  811. unsigned npkts = 0;
  812. struct user_sdma_txreq *tx = NULL;
  813. struct hfi1_user_sdma_pkt_q *pq = NULL;
  814. struct user_sdma_iovec *iovec = NULL;
  815. if (!req->pq)
  816. return -EINVAL;
  817. pq = req->pq;
  818. /* If tx completion has reported an error, we are done. */
  819. if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
  820. set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
  821. return -EFAULT;
  822. }
  823. /*
  824. * Check if we might have sent the entire request already
  825. */
  826. if (unlikely(req->seqnum == req->info.npkts)) {
  827. if (!list_empty(&req->txps))
  828. goto dosend;
  829. return ret;
  830. }
  831. if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
  832. maxpkts = req->info.npkts - req->seqnum;
  833. while (npkts < maxpkts) {
  834. u32 datalen = 0, queued = 0, data_sent = 0;
  835. u64 iov_offset = 0;
  836. /*
  837. * Check whether any of the completions have come back
  838. * with errors. If so, we are not going to process any
  839. * more packets from this request.
  840. */
  841. if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
  842. set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
  843. return -EFAULT;
  844. }
  845. tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
  846. if (!tx)
  847. return -ENOMEM;
  848. tx->flags = 0;
  849. tx->req = req;
  850. tx->busycount = 0;
  851. INIT_LIST_HEAD(&tx->list);
  852. /*
  853. * For the last packet set the ACK request
  854. * and disable header suppression.
  855. */
  856. if (req->seqnum == req->info.npkts - 1)
  857. tx->flags |= (TXREQ_FLAGS_REQ_ACK |
  858. TXREQ_FLAGS_REQ_DISABLE_SH);
  859. /*
  860. * Calculate the payload size - this is min of the fragment
  861. * (MTU) size or the remaining bytes in the request but only
  862. * if we have payload data.
  863. */
  864. if (req->data_len) {
  865. iovec = &req->iovs[req->iov_idx];
  866. if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
  867. if (++req->iov_idx == req->data_iovs) {
  868. ret = -EFAULT;
  869. goto free_txreq;
  870. }
  871. iovec = &req->iovs[req->iov_idx];
  872. WARN_ON(iovec->offset);
  873. }
  874. datalen = compute_data_length(req, tx);
  875. /*
  876. * Disable header suppression for the payload <= 8DWS.
  877. * If there is an uncorrectable error in the receive
  878. * data FIFO when the received payload size is less than
  879. * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
  880. * not reported.There is set RHF.EccErr if the header
  881. * is not suppressed.
  882. */
  883. if (!datalen) {
  884. SDMA_DBG(req,
  885. "Request has data but pkt len is 0");
  886. ret = -EFAULT;
  887. goto free_tx;
  888. } else if (datalen <= 32) {
  889. tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
  890. }
  891. }
  892. if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags)) {
  893. if (!req->seqnum) {
  894. u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
  895. u32 lrhlen = get_lrh_len(req->hdr,
  896. pad_len(datalen));
  897. /*
  898. * Copy the request header into the tx header
  899. * because the HW needs a cacheline-aligned
  900. * address.
  901. * This copy can be optimized out if the hdr
  902. * member of user_sdma_request were also
  903. * cacheline aligned.
  904. */
  905. memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
  906. if (PBC2LRH(pbclen) != lrhlen) {
  907. pbclen = (pbclen & 0xf000) |
  908. LRH2PBC(lrhlen);
  909. tx->hdr.pbc[0] = cpu_to_le16(pbclen);
  910. }
  911. ret = check_header_template(req, &tx->hdr,
  912. lrhlen, datalen);
  913. if (ret)
  914. goto free_tx;
  915. ret = sdma_txinit_ahg(&tx->txreq,
  916. SDMA_TXREQ_F_AHG_COPY,
  917. sizeof(tx->hdr) + datalen,
  918. req->ahg_idx, 0, NULL, 0,
  919. user_sdma_txreq_cb);
  920. if (ret)
  921. goto free_tx;
  922. ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
  923. &tx->hdr,
  924. sizeof(tx->hdr));
  925. if (ret)
  926. goto free_txreq;
  927. } else {
  928. int changes;
  929. changes = set_txreq_header_ahg(req, tx,
  930. datalen);
  931. if (changes < 0)
  932. goto free_tx;
  933. sdma_txinit_ahg(&tx->txreq,
  934. SDMA_TXREQ_F_USE_AHG,
  935. datalen, req->ahg_idx, changes,
  936. req->ahg, sizeof(req->hdr),
  937. user_sdma_txreq_cb);
  938. }
  939. } else {
  940. ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
  941. datalen, user_sdma_txreq_cb);
  942. if (ret)
  943. goto free_tx;
  944. /*
  945. * Modify the header for this packet. This only needs
  946. * to be done if we are not going to use AHG. Otherwise,
  947. * the HW will do it based on the changes we gave it
  948. * during sdma_txinit_ahg().
  949. */
  950. ret = set_txreq_header(req, tx, datalen);
  951. if (ret)
  952. goto free_txreq;
  953. }
  954. /*
  955. * If the request contains any data vectors, add up to
  956. * fragsize bytes to the descriptor.
  957. */
  958. while (queued < datalen &&
  959. (req->sent + data_sent) < req->data_len) {
  960. unsigned long base, offset;
  961. unsigned pageidx, len;
  962. base = (unsigned long)iovec->iov.iov_base;
  963. offset = offset_in_page(base + iovec->offset +
  964. iov_offset);
  965. pageidx = (((iovec->offset + iov_offset +
  966. base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
  967. len = offset + req->info.fragsize > PAGE_SIZE ?
  968. PAGE_SIZE - offset : req->info.fragsize;
  969. len = min((datalen - queued), len);
  970. ret = sdma_txadd_page(pq->dd, &tx->txreq,
  971. iovec->pages[pageidx],
  972. offset, len);
  973. if (ret) {
  974. SDMA_DBG(req, "SDMA txreq add page failed %d\n",
  975. ret);
  976. goto free_txreq;
  977. }
  978. iov_offset += len;
  979. queued += len;
  980. data_sent += len;
  981. if (unlikely(queued < datalen &&
  982. pageidx == iovec->npages &&
  983. req->iov_idx < req->data_iovs - 1)) {
  984. iovec->offset += iov_offset;
  985. iovec = &req->iovs[++req->iov_idx];
  986. iov_offset = 0;
  987. }
  988. }
  989. /*
  990. * The txreq was submitted successfully so we can update
  991. * the counters.
  992. */
  993. req->koffset += datalen;
  994. if (req_opcode(req->info.ctrl) == EXPECTED)
  995. req->tidoffset += datalen;
  996. req->sent += data_sent;
  997. if (req->data_len)
  998. iovec->offset += iov_offset;
  999. list_add_tail(&tx->txreq.list, &req->txps);
  1000. /*
  1001. * It is important to increment this here as it is used to
  1002. * generate the BTH.PSN and, therefore, can't be bulk-updated
  1003. * outside of the loop.
  1004. */
  1005. tx->seqnum = req->seqnum++;
  1006. npkts++;
  1007. }
  1008. dosend:
  1009. ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
  1010. req->seqsubmitted += count;
  1011. if (req->seqsubmitted == req->info.npkts) {
  1012. set_bit(SDMA_REQ_SEND_DONE, &req->flags);
  1013. /*
  1014. * The txreq has already been submitted to the HW queue
  1015. * so we can free the AHG entry now. Corruption will not
  1016. * happen due to the sequential manner in which
  1017. * descriptors are processed.
  1018. */
  1019. if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags))
  1020. sdma_ahg_free(req->sde, req->ahg_idx);
  1021. }
  1022. return ret;
  1023. free_txreq:
  1024. sdma_txclean(pq->dd, &tx->txreq);
  1025. free_tx:
  1026. kmem_cache_free(pq->txreq_cache, tx);
  1027. return ret;
  1028. }
  1029. /*
  1030. * How many pages in this iovec element?
  1031. */
  1032. static inline int num_user_pages(const struct iovec *iov)
  1033. {
  1034. const unsigned long addr = (unsigned long)iov->iov_base;
  1035. const unsigned long len = iov->iov_len;
  1036. const unsigned long spage = addr & PAGE_MASK;
  1037. const unsigned long epage = (addr + len - 1) & PAGE_MASK;
  1038. return 1 + ((epage - spage) >> PAGE_SHIFT);
  1039. }
  1040. static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
  1041. {
  1042. struct evict_data evict_data;
  1043. evict_data.cleared = 0;
  1044. evict_data.target = npages;
  1045. hfi1_mmu_rb_evict(pq->handler, &evict_data);
  1046. return evict_data.cleared;
  1047. }
  1048. static int pin_vector_pages(struct user_sdma_request *req,
  1049. struct user_sdma_iovec *iovec)
  1050. {
  1051. int ret = 0, pinned, npages, cleared;
  1052. struct page **pages;
  1053. struct hfi1_user_sdma_pkt_q *pq = req->pq;
  1054. struct sdma_mmu_node *node = NULL;
  1055. struct mmu_rb_node *rb_node;
  1056. rb_node = hfi1_mmu_rb_extract(pq->handler,
  1057. (unsigned long)iovec->iov.iov_base,
  1058. iovec->iov.iov_len);
  1059. if (rb_node)
  1060. node = container_of(rb_node, struct sdma_mmu_node, rb);
  1061. else
  1062. rb_node = NULL;
  1063. if (!node) {
  1064. node = kzalloc(sizeof(*node), GFP_KERNEL);
  1065. if (!node)
  1066. return -ENOMEM;
  1067. node->rb.addr = (unsigned long)iovec->iov.iov_base;
  1068. node->pq = pq;
  1069. atomic_set(&node->refcount, 0);
  1070. }
  1071. npages = num_user_pages(&iovec->iov);
  1072. if (node->npages < npages) {
  1073. pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
  1074. if (!pages) {
  1075. SDMA_DBG(req, "Failed page array alloc");
  1076. ret = -ENOMEM;
  1077. goto bail;
  1078. }
  1079. memcpy(pages, node->pages, node->npages * sizeof(*pages));
  1080. npages -= node->npages;
  1081. retry:
  1082. if (!hfi1_can_pin_pages(pq->dd, pq->mm,
  1083. atomic_read(&pq->n_locked), npages)) {
  1084. cleared = sdma_cache_evict(pq, npages);
  1085. if (cleared >= npages)
  1086. goto retry;
  1087. }
  1088. pinned = hfi1_acquire_user_pages(pq->mm,
  1089. ((unsigned long)iovec->iov.iov_base +
  1090. (node->npages * PAGE_SIZE)), npages, 0,
  1091. pages + node->npages);
  1092. if (pinned < 0) {
  1093. kfree(pages);
  1094. ret = pinned;
  1095. goto bail;
  1096. }
  1097. if (pinned != npages) {
  1098. unpin_vector_pages(pq->mm, pages, node->npages,
  1099. pinned);
  1100. ret = -EFAULT;
  1101. goto bail;
  1102. }
  1103. kfree(node->pages);
  1104. node->rb.len = iovec->iov.iov_len;
  1105. node->pages = pages;
  1106. node->npages += pinned;
  1107. npages = node->npages;
  1108. atomic_add(pinned, &pq->n_locked);
  1109. }
  1110. iovec->pages = node->pages;
  1111. iovec->npages = npages;
  1112. iovec->node = node;
  1113. ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
  1114. if (ret) {
  1115. atomic_sub(node->npages, &pq->n_locked);
  1116. iovec->node = NULL;
  1117. goto bail;
  1118. }
  1119. return 0;
  1120. bail:
  1121. if (rb_node)
  1122. unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
  1123. kfree(node);
  1124. return ret;
  1125. }
  1126. static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
  1127. unsigned start, unsigned npages)
  1128. {
  1129. hfi1_release_user_pages(mm, pages + start, npages, false);
  1130. kfree(pages);
  1131. }
  1132. static int check_header_template(struct user_sdma_request *req,
  1133. struct hfi1_pkt_header *hdr, u32 lrhlen,
  1134. u32 datalen)
  1135. {
  1136. /*
  1137. * Perform safety checks for any type of packet:
  1138. * - transfer size is multiple of 64bytes
  1139. * - packet length is multiple of 4 bytes
  1140. * - packet length is not larger than MTU size
  1141. *
  1142. * These checks are only done for the first packet of the
  1143. * transfer since the header is "given" to us by user space.
  1144. * For the remainder of the packets we compute the values.
  1145. */
  1146. if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
  1147. lrhlen > get_lrh_len(*hdr, req->info.fragsize))
  1148. return -EINVAL;
  1149. if (req_opcode(req->info.ctrl) == EXPECTED) {
  1150. /*
  1151. * The header is checked only on the first packet. Furthermore,
  1152. * we ensure that at least one TID entry is copied when the
  1153. * request is submitted. Therefore, we don't have to verify that
  1154. * tididx points to something sane.
  1155. */
  1156. u32 tidval = req->tids[req->tididx],
  1157. tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
  1158. tididx = EXP_TID_GET(tidval, IDX),
  1159. tidctrl = EXP_TID_GET(tidval, CTRL),
  1160. tidoff;
  1161. __le32 kval = hdr->kdeth.ver_tid_offset;
  1162. tidoff = KDETH_GET(kval, OFFSET) *
  1163. (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
  1164. KDETH_OM_LARGE : KDETH_OM_SMALL);
  1165. /*
  1166. * Expected receive packets have the following
  1167. * additional checks:
  1168. * - offset is not larger than the TID size
  1169. * - TIDCtrl values match between header and TID array
  1170. * - TID indexes match between header and TID array
  1171. */
  1172. if ((tidoff + datalen > tidlen) ||
  1173. KDETH_GET(kval, TIDCTRL) != tidctrl ||
  1174. KDETH_GET(kval, TID) != tididx)
  1175. return -EINVAL;
  1176. }
  1177. return 0;
  1178. }
  1179. /*
  1180. * Correctly set the BTH.PSN field based on type of
  1181. * transfer - eager packets can just increment the PSN but
  1182. * expected packets encode generation and sequence in the
  1183. * BTH.PSN field so just incrementing will result in errors.
  1184. */
  1185. static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
  1186. {
  1187. u32 val = be32_to_cpu(bthpsn),
  1188. mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
  1189. 0xffffffull),
  1190. psn = val & mask;
  1191. if (expct)
  1192. psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
  1193. else
  1194. psn = psn + frags;
  1195. return psn & mask;
  1196. }
  1197. static int set_txreq_header(struct user_sdma_request *req,
  1198. struct user_sdma_txreq *tx, u32 datalen)
  1199. {
  1200. struct hfi1_user_sdma_pkt_q *pq = req->pq;
  1201. struct hfi1_pkt_header *hdr = &tx->hdr;
  1202. u16 pbclen;
  1203. int ret;
  1204. u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
  1205. /* Copy the header template to the request before modification */
  1206. memcpy(hdr, &req->hdr, sizeof(*hdr));
  1207. /*
  1208. * Check if the PBC and LRH length are mismatched. If so
  1209. * adjust both in the header.
  1210. */
  1211. pbclen = le16_to_cpu(hdr->pbc[0]);
  1212. if (PBC2LRH(pbclen) != lrhlen) {
  1213. pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
  1214. hdr->pbc[0] = cpu_to_le16(pbclen);
  1215. hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
  1216. /*
  1217. * Third packet
  1218. * This is the first packet in the sequence that has
  1219. * a "static" size that can be used for the rest of
  1220. * the packets (besides the last one).
  1221. */
  1222. if (unlikely(req->seqnum == 2)) {
  1223. /*
  1224. * From this point on the lengths in both the
  1225. * PBC and LRH are the same until the last
  1226. * packet.
  1227. * Adjust the template so we don't have to update
  1228. * every packet
  1229. */
  1230. req->hdr.pbc[0] = hdr->pbc[0];
  1231. req->hdr.lrh[2] = hdr->lrh[2];
  1232. }
  1233. }
  1234. /*
  1235. * We only have to modify the header if this is not the
  1236. * first packet in the request. Otherwise, we use the
  1237. * header given to us.
  1238. */
  1239. if (unlikely(!req->seqnum)) {
  1240. ret = check_header_template(req, hdr, lrhlen, datalen);
  1241. if (ret)
  1242. return ret;
  1243. goto done;
  1244. }
  1245. hdr->bth[2] = cpu_to_be32(
  1246. set_pkt_bth_psn(hdr->bth[2],
  1247. (req_opcode(req->info.ctrl) == EXPECTED),
  1248. req->seqnum));
  1249. /* Set ACK request on last packet */
  1250. if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
  1251. hdr->bth[2] |= cpu_to_be32(1UL << 31);
  1252. /* Set the new offset */
  1253. hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
  1254. /* Expected packets have to fill in the new TID information */
  1255. if (req_opcode(req->info.ctrl) == EXPECTED) {
  1256. tidval = req->tids[req->tididx];
  1257. /*
  1258. * If the offset puts us at the end of the current TID,
  1259. * advance everything.
  1260. */
  1261. if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
  1262. PAGE_SIZE)) {
  1263. req->tidoffset = 0;
  1264. /*
  1265. * Since we don't copy all the TIDs, all at once,
  1266. * we have to check again.
  1267. */
  1268. if (++req->tididx > req->n_tids - 1 ||
  1269. !req->tids[req->tididx]) {
  1270. return -EINVAL;
  1271. }
  1272. tidval = req->tids[req->tididx];
  1273. }
  1274. req->omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
  1275. KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE : KDETH_OM_SMALL;
  1276. /* Set KDETH.TIDCtrl based on value for this TID. */
  1277. KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
  1278. EXP_TID_GET(tidval, CTRL));
  1279. /* Set KDETH.TID based on value for this TID */
  1280. KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
  1281. EXP_TID_GET(tidval, IDX));
  1282. /* Clear KDETH.SH when DISABLE_SH flag is set */
  1283. if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
  1284. KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
  1285. /*
  1286. * Set the KDETH.OFFSET and KDETH.OM based on size of
  1287. * transfer.
  1288. */
  1289. SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
  1290. req->tidoffset, req->tidoffset / req->omfactor,
  1291. req->omfactor != KDETH_OM_SMALL);
  1292. KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
  1293. req->tidoffset / req->omfactor);
  1294. KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
  1295. req->omfactor != KDETH_OM_SMALL);
  1296. }
  1297. done:
  1298. trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
  1299. req->info.comp_idx, hdr, tidval);
  1300. return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
  1301. }
  1302. static int set_txreq_header_ahg(struct user_sdma_request *req,
  1303. struct user_sdma_txreq *tx, u32 len)
  1304. {
  1305. int diff = 0;
  1306. struct hfi1_user_sdma_pkt_q *pq = req->pq;
  1307. struct hfi1_pkt_header *hdr = &req->hdr;
  1308. u16 pbclen = le16_to_cpu(hdr->pbc[0]);
  1309. u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
  1310. if (PBC2LRH(pbclen) != lrhlen) {
  1311. /* PBC.PbcLengthDWs */
  1312. AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
  1313. cpu_to_le16(LRH2PBC(lrhlen)));
  1314. /* LRH.PktLen (we need the full 16 bits due to byte swap) */
  1315. AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
  1316. cpu_to_be16(lrhlen >> 2));
  1317. }
  1318. /*
  1319. * Do the common updates
  1320. */
  1321. /* BTH.PSN and BTH.A */
  1322. val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
  1323. (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
  1324. if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
  1325. val32 |= 1UL << 31;
  1326. AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
  1327. AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
  1328. /* KDETH.Offset */
  1329. AHG_HEADER_SET(req->ahg, diff, 15, 0, 16,
  1330. cpu_to_le16(req->koffset & 0xffff));
  1331. AHG_HEADER_SET(req->ahg, diff, 15, 16, 16,
  1332. cpu_to_le16(req->koffset >> 16));
  1333. if (req_opcode(req->info.ctrl) == EXPECTED) {
  1334. __le16 val;
  1335. tidval = req->tids[req->tididx];
  1336. /*
  1337. * If the offset puts us at the end of the current TID,
  1338. * advance everything.
  1339. */
  1340. if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
  1341. PAGE_SIZE)) {
  1342. req->tidoffset = 0;
  1343. /*
  1344. * Since we don't copy all the TIDs, all at once,
  1345. * we have to check again.
  1346. */
  1347. if (++req->tididx > req->n_tids - 1 ||
  1348. !req->tids[req->tididx]) {
  1349. return -EINVAL;
  1350. }
  1351. tidval = req->tids[req->tididx];
  1352. }
  1353. req->omfactor = ((EXP_TID_GET(tidval, LEN) *
  1354. PAGE_SIZE) >=
  1355. KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE :
  1356. KDETH_OM_SMALL;
  1357. /* KDETH.OM and KDETH.OFFSET (TID) */
  1358. AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
  1359. ((!!(req->omfactor - KDETH_OM_SMALL)) << 15 |
  1360. ((req->tidoffset / req->omfactor) & 0x7fff)));
  1361. /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
  1362. val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
  1363. (EXP_TID_GET(tidval, IDX) & 0x3ff));
  1364. if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
  1365. val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
  1366. INTR) <<
  1367. AHG_KDETH_INTR_SHIFT));
  1368. } else {
  1369. val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
  1370. cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
  1371. cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
  1372. INTR) <<
  1373. AHG_KDETH_INTR_SHIFT));
  1374. }
  1375. AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
  1376. }
  1377. trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
  1378. req->info.comp_idx, req->sde->this_idx,
  1379. req->ahg_idx, req->ahg, diff, tidval);
  1380. return diff;
  1381. }
  1382. /*
  1383. * SDMA tx request completion callback. Called when the SDMA progress
  1384. * state machine gets notification that the SDMA descriptors for this
  1385. * tx request have been processed by the DMA engine. Called in
  1386. * interrupt context.
  1387. */
  1388. static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
  1389. {
  1390. struct user_sdma_txreq *tx =
  1391. container_of(txreq, struct user_sdma_txreq, txreq);
  1392. struct user_sdma_request *req;
  1393. struct hfi1_user_sdma_pkt_q *pq;
  1394. struct hfi1_user_sdma_comp_q *cq;
  1395. u16 idx;
  1396. if (!tx->req)
  1397. return;
  1398. req = tx->req;
  1399. pq = req->pq;
  1400. cq = req->cq;
  1401. if (status != SDMA_TXREQ_S_OK) {
  1402. SDMA_DBG(req, "SDMA completion with error %d",
  1403. status);
  1404. set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
  1405. }
  1406. req->seqcomp = tx->seqnum;
  1407. kmem_cache_free(pq->txreq_cache, tx);
  1408. tx = NULL;
  1409. idx = req->info.comp_idx;
  1410. if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
  1411. if (req->seqcomp == req->info.npkts - 1) {
  1412. req->status = 0;
  1413. user_sdma_free_request(req, false);
  1414. pq_update(pq);
  1415. set_comp_state(pq, cq, idx, COMPLETE, 0);
  1416. }
  1417. } else {
  1418. if (status != SDMA_TXREQ_S_OK)
  1419. req->status = status;
  1420. if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
  1421. (test_bit(SDMA_REQ_SEND_DONE, &req->flags) ||
  1422. test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) {
  1423. user_sdma_free_request(req, false);
  1424. pq_update(pq);
  1425. set_comp_state(pq, cq, idx, ERROR, req->status);
  1426. }
  1427. }
  1428. }
  1429. static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
  1430. {
  1431. if (atomic_dec_and_test(&pq->n_reqs)) {
  1432. xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
  1433. wake_up(&pq->wait);
  1434. }
  1435. }
  1436. static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
  1437. {
  1438. if (!list_empty(&req->txps)) {
  1439. struct sdma_txreq *t, *p;
  1440. list_for_each_entry_safe(t, p, &req->txps, list) {
  1441. struct user_sdma_txreq *tx =
  1442. container_of(t, struct user_sdma_txreq, txreq);
  1443. list_del_init(&t->list);
  1444. sdma_txclean(req->pq->dd, t);
  1445. kmem_cache_free(req->pq->txreq_cache, tx);
  1446. }
  1447. }
  1448. if (req->data_iovs) {
  1449. struct sdma_mmu_node *node;
  1450. int i;
  1451. for (i = 0; i < req->data_iovs; i++) {
  1452. node = req->iovs[i].node;
  1453. if (!node)
  1454. continue;
  1455. if (unpin)
  1456. hfi1_mmu_rb_remove(req->pq->handler,
  1457. &node->rb);
  1458. else
  1459. atomic_dec(&node->refcount);
  1460. }
  1461. }
  1462. kfree(req->tids);
  1463. clear_bit(req->info.comp_idx, req->pq->req_in_use);
  1464. }
  1465. static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
  1466. struct hfi1_user_sdma_comp_q *cq,
  1467. u16 idx, enum hfi1_sdma_comp_state state,
  1468. int ret)
  1469. {
  1470. hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
  1471. pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
  1472. cq->comps[idx].status = state;
  1473. if (state == ERROR)
  1474. cq->comps[idx].errcode = -ret;
  1475. trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
  1476. idx, state, ret);
  1477. }
  1478. static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
  1479. unsigned long len)
  1480. {
  1481. return (bool)(node->addr == addr);
  1482. }
  1483. static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
  1484. {
  1485. struct sdma_mmu_node *node =
  1486. container_of(mnode, struct sdma_mmu_node, rb);
  1487. atomic_inc(&node->refcount);
  1488. return 0;
  1489. }
  1490. /*
  1491. * Return 1 to remove the node from the rb tree and call the remove op.
  1492. *
  1493. * Called with the rb tree lock held.
  1494. */
  1495. static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
  1496. void *evict_arg, bool *stop)
  1497. {
  1498. struct sdma_mmu_node *node =
  1499. container_of(mnode, struct sdma_mmu_node, rb);
  1500. struct evict_data *evict_data = evict_arg;
  1501. /* is this node still being used? */
  1502. if (atomic_read(&node->refcount))
  1503. return 0; /* keep this node */
  1504. /* this node will be evicted, add its pages to our count */
  1505. evict_data->cleared += node->npages;
  1506. /* have enough pages been cleared? */
  1507. if (evict_data->cleared >= evict_data->target)
  1508. *stop = true;
  1509. return 1; /* remove this node */
  1510. }
  1511. static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
  1512. {
  1513. struct sdma_mmu_node *node =
  1514. container_of(mnode, struct sdma_mmu_node, rb);
  1515. atomic_sub(node->npages, &node->pq->n_locked);
  1516. unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
  1517. kfree(node);
  1518. }
  1519. static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
  1520. {
  1521. struct sdma_mmu_node *node =
  1522. container_of(mnode, struct sdma_mmu_node, rb);
  1523. if (!atomic_read(&node->refcount))
  1524. return 1;
  1525. return 0;
  1526. }