user_sdma.c 48 KB

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