user_sdma.c 47 KB

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