ntb_transport.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  8. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * BSD LICENSE
  15. *
  16. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  17. * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * * Redistributions in binary form must reproduce the above copy
  26. * notice, this list of conditions and the following disclaimer in
  27. * the documentation and/or other materials provided with the
  28. * distribution.
  29. * * Neither the name of Intel Corporation nor the names of its
  30. * contributors may be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * PCIe NTB Transport Linux driver
  46. *
  47. * Contact Information:
  48. * Jon Mason <jon.mason@intel.com>
  49. */
  50. #include <linux/debugfs.h>
  51. #include <linux/delay.h>
  52. #include <linux/dmaengine.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/errno.h>
  55. #include <linux/export.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/module.h>
  58. #include <linux/pci.h>
  59. #include <linux/slab.h>
  60. #include <linux/types.h>
  61. #include <linux/uaccess.h>
  62. #include "linux/ntb.h"
  63. #include "linux/ntb_transport.h"
  64. #define NTB_TRANSPORT_VERSION 4
  65. #define NTB_TRANSPORT_VER "4"
  66. #define NTB_TRANSPORT_NAME "ntb_transport"
  67. #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
  68. #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
  69. MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
  70. MODULE_VERSION(NTB_TRANSPORT_VER);
  71. MODULE_LICENSE("Dual BSD/GPL");
  72. MODULE_AUTHOR("Intel Corporation");
  73. static unsigned long max_mw_size;
  74. module_param(max_mw_size, ulong, 0644);
  75. MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
  76. static unsigned int transport_mtu = 0x10000;
  77. module_param(transport_mtu, uint, 0644);
  78. MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
  79. static unsigned char max_num_clients;
  80. module_param(max_num_clients, byte, 0644);
  81. MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
  82. static unsigned int copy_bytes = 1024;
  83. module_param(copy_bytes, uint, 0644);
  84. MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
  85. static bool use_dma;
  86. module_param(use_dma, bool, 0644);
  87. MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
  88. static struct dentry *nt_debugfs_dir;
  89. /* Only two-ports NTB devices are supported */
  90. #define PIDX NTB_DEF_PEER_IDX
  91. struct ntb_queue_entry {
  92. /* ntb_queue list reference */
  93. struct list_head entry;
  94. /* pointers to data to be transferred */
  95. void *cb_data;
  96. void *buf;
  97. unsigned int len;
  98. unsigned int flags;
  99. int retries;
  100. int errors;
  101. unsigned int tx_index;
  102. unsigned int rx_index;
  103. struct ntb_transport_qp *qp;
  104. union {
  105. struct ntb_payload_header __iomem *tx_hdr;
  106. struct ntb_payload_header *rx_hdr;
  107. };
  108. };
  109. struct ntb_rx_info {
  110. unsigned int entry;
  111. };
  112. struct ntb_transport_qp {
  113. struct ntb_transport_ctx *transport;
  114. struct ntb_dev *ndev;
  115. void *cb_data;
  116. struct dma_chan *tx_dma_chan;
  117. struct dma_chan *rx_dma_chan;
  118. bool client_ready;
  119. bool link_is_up;
  120. bool active;
  121. u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
  122. u64 qp_bit;
  123. struct ntb_rx_info __iomem *rx_info;
  124. struct ntb_rx_info *remote_rx_info;
  125. void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
  126. void *data, int len);
  127. struct list_head tx_free_q;
  128. spinlock_t ntb_tx_free_q_lock;
  129. void __iomem *tx_mw;
  130. dma_addr_t tx_mw_phys;
  131. unsigned int tx_index;
  132. unsigned int tx_max_entry;
  133. unsigned int tx_max_frame;
  134. void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
  135. void *data, int len);
  136. struct list_head rx_post_q;
  137. struct list_head rx_pend_q;
  138. struct list_head rx_free_q;
  139. /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
  140. spinlock_t ntb_rx_q_lock;
  141. void *rx_buff;
  142. unsigned int rx_index;
  143. unsigned int rx_max_entry;
  144. unsigned int rx_max_frame;
  145. unsigned int rx_alloc_entry;
  146. dma_cookie_t last_cookie;
  147. struct tasklet_struct rxc_db_work;
  148. void (*event_handler)(void *data, int status);
  149. struct delayed_work link_work;
  150. struct work_struct link_cleanup;
  151. struct dentry *debugfs_dir;
  152. struct dentry *debugfs_stats;
  153. /* Stats */
  154. u64 rx_bytes;
  155. u64 rx_pkts;
  156. u64 rx_ring_empty;
  157. u64 rx_err_no_buf;
  158. u64 rx_err_oflow;
  159. u64 rx_err_ver;
  160. u64 rx_memcpy;
  161. u64 rx_async;
  162. u64 tx_bytes;
  163. u64 tx_pkts;
  164. u64 tx_ring_full;
  165. u64 tx_err_no_buf;
  166. u64 tx_memcpy;
  167. u64 tx_async;
  168. };
  169. struct ntb_transport_mw {
  170. phys_addr_t phys_addr;
  171. resource_size_t phys_size;
  172. void __iomem *vbase;
  173. size_t xlat_size;
  174. size_t buff_size;
  175. void *virt_addr;
  176. dma_addr_t dma_addr;
  177. };
  178. struct ntb_transport_client_dev {
  179. struct list_head entry;
  180. struct ntb_transport_ctx *nt;
  181. struct device dev;
  182. };
  183. struct ntb_transport_ctx {
  184. struct list_head entry;
  185. struct list_head client_devs;
  186. struct ntb_dev *ndev;
  187. struct ntb_transport_mw *mw_vec;
  188. struct ntb_transport_qp *qp_vec;
  189. unsigned int mw_count;
  190. unsigned int qp_count;
  191. u64 qp_bitmap;
  192. u64 qp_bitmap_free;
  193. bool link_is_up;
  194. struct delayed_work link_work;
  195. struct work_struct link_cleanup;
  196. struct dentry *debugfs_node_dir;
  197. };
  198. enum {
  199. DESC_DONE_FLAG = BIT(0),
  200. LINK_DOWN_FLAG = BIT(1),
  201. };
  202. struct ntb_payload_header {
  203. unsigned int ver;
  204. unsigned int len;
  205. unsigned int flags;
  206. };
  207. enum {
  208. VERSION = 0,
  209. QP_LINKS,
  210. NUM_QPS,
  211. NUM_MWS,
  212. MW0_SZ_HIGH,
  213. MW0_SZ_LOW,
  214. };
  215. #define dev_client_dev(__dev) \
  216. container_of((__dev), struct ntb_transport_client_dev, dev)
  217. #define drv_client(__drv) \
  218. container_of((__drv), struct ntb_transport_client, driver)
  219. #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
  220. #define NTB_QP_DEF_NUM_ENTRIES 100
  221. #define NTB_LINK_DOWN_TIMEOUT 10
  222. static void ntb_transport_rxc_db(unsigned long data);
  223. static const struct ntb_ctx_ops ntb_transport_ops;
  224. static struct ntb_client ntb_transport_client;
  225. static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
  226. struct ntb_queue_entry *entry);
  227. static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
  228. static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
  229. static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
  230. static int ntb_transport_bus_match(struct device *dev,
  231. struct device_driver *drv)
  232. {
  233. return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
  234. }
  235. static int ntb_transport_bus_probe(struct device *dev)
  236. {
  237. const struct ntb_transport_client *client;
  238. int rc = -EINVAL;
  239. get_device(dev);
  240. client = drv_client(dev->driver);
  241. rc = client->probe(dev);
  242. if (rc)
  243. put_device(dev);
  244. return rc;
  245. }
  246. static int ntb_transport_bus_remove(struct device *dev)
  247. {
  248. const struct ntb_transport_client *client;
  249. client = drv_client(dev->driver);
  250. client->remove(dev);
  251. put_device(dev);
  252. return 0;
  253. }
  254. static struct bus_type ntb_transport_bus = {
  255. .name = "ntb_transport",
  256. .match = ntb_transport_bus_match,
  257. .probe = ntb_transport_bus_probe,
  258. .remove = ntb_transport_bus_remove,
  259. };
  260. static LIST_HEAD(ntb_transport_list);
  261. static int ntb_bus_init(struct ntb_transport_ctx *nt)
  262. {
  263. list_add_tail(&nt->entry, &ntb_transport_list);
  264. return 0;
  265. }
  266. static void ntb_bus_remove(struct ntb_transport_ctx *nt)
  267. {
  268. struct ntb_transport_client_dev *client_dev, *cd;
  269. list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
  270. dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
  271. dev_name(&client_dev->dev));
  272. list_del(&client_dev->entry);
  273. device_unregister(&client_dev->dev);
  274. }
  275. list_del(&nt->entry);
  276. }
  277. static void ntb_transport_client_release(struct device *dev)
  278. {
  279. struct ntb_transport_client_dev *client_dev;
  280. client_dev = dev_client_dev(dev);
  281. kfree(client_dev);
  282. }
  283. /**
  284. * ntb_transport_unregister_client_dev - Unregister NTB client device
  285. * @device_name: Name of NTB client device
  286. *
  287. * Unregister an NTB client device with the NTB transport layer
  288. */
  289. void ntb_transport_unregister_client_dev(char *device_name)
  290. {
  291. struct ntb_transport_client_dev *client, *cd;
  292. struct ntb_transport_ctx *nt;
  293. list_for_each_entry(nt, &ntb_transport_list, entry)
  294. list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
  295. if (!strncmp(dev_name(&client->dev), device_name,
  296. strlen(device_name))) {
  297. list_del(&client->entry);
  298. device_unregister(&client->dev);
  299. }
  300. }
  301. EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
  302. /**
  303. * ntb_transport_register_client_dev - Register NTB client device
  304. * @device_name: Name of NTB client device
  305. *
  306. * Register an NTB client device with the NTB transport layer
  307. */
  308. int ntb_transport_register_client_dev(char *device_name)
  309. {
  310. struct ntb_transport_client_dev *client_dev;
  311. struct ntb_transport_ctx *nt;
  312. int node;
  313. int rc, i = 0;
  314. if (list_empty(&ntb_transport_list))
  315. return -ENODEV;
  316. list_for_each_entry(nt, &ntb_transport_list, entry) {
  317. struct device *dev;
  318. node = dev_to_node(&nt->ndev->dev);
  319. client_dev = kzalloc_node(sizeof(*client_dev),
  320. GFP_KERNEL, node);
  321. if (!client_dev) {
  322. rc = -ENOMEM;
  323. goto err;
  324. }
  325. dev = &client_dev->dev;
  326. /* setup and register client devices */
  327. dev_set_name(dev, "%s%d", device_name, i);
  328. dev->bus = &ntb_transport_bus;
  329. dev->release = ntb_transport_client_release;
  330. dev->parent = &nt->ndev->dev;
  331. rc = device_register(dev);
  332. if (rc) {
  333. kfree(client_dev);
  334. goto err;
  335. }
  336. list_add_tail(&client_dev->entry, &nt->client_devs);
  337. i++;
  338. }
  339. return 0;
  340. err:
  341. ntb_transport_unregister_client_dev(device_name);
  342. return rc;
  343. }
  344. EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
  345. /**
  346. * ntb_transport_register_client - Register NTB client driver
  347. * @drv: NTB client driver to be registered
  348. *
  349. * Register an NTB client driver with the NTB transport layer
  350. *
  351. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  352. */
  353. int ntb_transport_register_client(struct ntb_transport_client *drv)
  354. {
  355. drv->driver.bus = &ntb_transport_bus;
  356. if (list_empty(&ntb_transport_list))
  357. return -ENODEV;
  358. return driver_register(&drv->driver);
  359. }
  360. EXPORT_SYMBOL_GPL(ntb_transport_register_client);
  361. /**
  362. * ntb_transport_unregister_client - Unregister NTB client driver
  363. * @drv: NTB client driver to be unregistered
  364. *
  365. * Unregister an NTB client driver with the NTB transport layer
  366. *
  367. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  368. */
  369. void ntb_transport_unregister_client(struct ntb_transport_client *drv)
  370. {
  371. driver_unregister(&drv->driver);
  372. }
  373. EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
  374. static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
  375. loff_t *offp)
  376. {
  377. struct ntb_transport_qp *qp;
  378. char *buf;
  379. ssize_t ret, out_offset, out_count;
  380. qp = filp->private_data;
  381. if (!qp || !qp->link_is_up)
  382. return 0;
  383. out_count = 1000;
  384. buf = kmalloc(out_count, GFP_KERNEL);
  385. if (!buf)
  386. return -ENOMEM;
  387. out_offset = 0;
  388. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  389. "\nNTB QP stats:\n\n");
  390. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  391. "rx_bytes - \t%llu\n", qp->rx_bytes);
  392. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  393. "rx_pkts - \t%llu\n", qp->rx_pkts);
  394. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  395. "rx_memcpy - \t%llu\n", qp->rx_memcpy);
  396. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  397. "rx_async - \t%llu\n", qp->rx_async);
  398. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  399. "rx_ring_empty - %llu\n", qp->rx_ring_empty);
  400. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  401. "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
  402. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  403. "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
  404. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  405. "rx_err_ver - \t%llu\n", qp->rx_err_ver);
  406. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  407. "rx_buff - \t0x%p\n", qp->rx_buff);
  408. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  409. "rx_index - \t%u\n", qp->rx_index);
  410. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  411. "rx_max_entry - \t%u\n", qp->rx_max_entry);
  412. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  413. "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
  414. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  415. "tx_bytes - \t%llu\n", qp->tx_bytes);
  416. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  417. "tx_pkts - \t%llu\n", qp->tx_pkts);
  418. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  419. "tx_memcpy - \t%llu\n", qp->tx_memcpy);
  420. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  421. "tx_async - \t%llu\n", qp->tx_async);
  422. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  423. "tx_ring_full - \t%llu\n", qp->tx_ring_full);
  424. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  425. "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
  426. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  427. "tx_mw - \t0x%p\n", qp->tx_mw);
  428. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  429. "tx_index (H) - \t%u\n", qp->tx_index);
  430. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  431. "RRI (T) - \t%u\n",
  432. qp->remote_rx_info->entry);
  433. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  434. "tx_max_entry - \t%u\n", qp->tx_max_entry);
  435. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  436. "free tx - \t%u\n",
  437. ntb_transport_tx_free_entry(qp));
  438. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  439. "\n");
  440. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  441. "Using TX DMA - \t%s\n",
  442. qp->tx_dma_chan ? "Yes" : "No");
  443. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  444. "Using RX DMA - \t%s\n",
  445. qp->rx_dma_chan ? "Yes" : "No");
  446. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  447. "QP Link - \t%s\n",
  448. qp->link_is_up ? "Up" : "Down");
  449. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  450. "\n");
  451. if (out_offset > out_count)
  452. out_offset = out_count;
  453. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  454. kfree(buf);
  455. return ret;
  456. }
  457. static const struct file_operations ntb_qp_debugfs_stats = {
  458. .owner = THIS_MODULE,
  459. .open = simple_open,
  460. .read = debugfs_read,
  461. };
  462. static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
  463. struct list_head *list)
  464. {
  465. unsigned long flags;
  466. spin_lock_irqsave(lock, flags);
  467. list_add_tail(entry, list);
  468. spin_unlock_irqrestore(lock, flags);
  469. }
  470. static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
  471. struct list_head *list)
  472. {
  473. struct ntb_queue_entry *entry;
  474. unsigned long flags;
  475. spin_lock_irqsave(lock, flags);
  476. if (list_empty(list)) {
  477. entry = NULL;
  478. goto out;
  479. }
  480. entry = list_first_entry(list, struct ntb_queue_entry, entry);
  481. list_del(&entry->entry);
  482. out:
  483. spin_unlock_irqrestore(lock, flags);
  484. return entry;
  485. }
  486. static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
  487. struct list_head *list,
  488. struct list_head *to_list)
  489. {
  490. struct ntb_queue_entry *entry;
  491. unsigned long flags;
  492. spin_lock_irqsave(lock, flags);
  493. if (list_empty(list)) {
  494. entry = NULL;
  495. } else {
  496. entry = list_first_entry(list, struct ntb_queue_entry, entry);
  497. list_move_tail(&entry->entry, to_list);
  498. }
  499. spin_unlock_irqrestore(lock, flags);
  500. return entry;
  501. }
  502. static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
  503. unsigned int qp_num)
  504. {
  505. struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
  506. struct ntb_transport_mw *mw;
  507. struct ntb_dev *ndev = nt->ndev;
  508. struct ntb_queue_entry *entry;
  509. unsigned int rx_size, num_qps_mw;
  510. unsigned int mw_num, mw_count, qp_count;
  511. unsigned int i;
  512. int node;
  513. mw_count = nt->mw_count;
  514. qp_count = nt->qp_count;
  515. mw_num = QP_TO_MW(nt, qp_num);
  516. mw = &nt->mw_vec[mw_num];
  517. if (!mw->virt_addr)
  518. return -ENOMEM;
  519. if (mw_num < qp_count % mw_count)
  520. num_qps_mw = qp_count / mw_count + 1;
  521. else
  522. num_qps_mw = qp_count / mw_count;
  523. rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
  524. qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
  525. rx_size -= sizeof(struct ntb_rx_info);
  526. qp->remote_rx_info = qp->rx_buff + rx_size;
  527. /* Due to housekeeping, there must be atleast 2 buffs */
  528. qp->rx_max_frame = min(transport_mtu, rx_size / 2);
  529. qp->rx_max_entry = rx_size / qp->rx_max_frame;
  530. qp->rx_index = 0;
  531. /*
  532. * Checking to see if we have more entries than the default.
  533. * We should add additional entries if that is the case so we
  534. * can be in sync with the transport frames.
  535. */
  536. node = dev_to_node(&ndev->dev);
  537. for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
  538. entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
  539. if (!entry)
  540. return -ENOMEM;
  541. entry->qp = qp;
  542. ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
  543. &qp->rx_free_q);
  544. qp->rx_alloc_entry++;
  545. }
  546. qp->remote_rx_info->entry = qp->rx_max_entry - 1;
  547. /* setup the hdr offsets with 0's */
  548. for (i = 0; i < qp->rx_max_entry; i++) {
  549. void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
  550. sizeof(struct ntb_payload_header));
  551. memset(offset, 0, sizeof(struct ntb_payload_header));
  552. }
  553. qp->rx_pkts = 0;
  554. qp->tx_pkts = 0;
  555. qp->tx_index = 0;
  556. return 0;
  557. }
  558. static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
  559. {
  560. struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
  561. struct pci_dev *pdev = nt->ndev->pdev;
  562. if (!mw->virt_addr)
  563. return;
  564. ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
  565. dma_free_coherent(&pdev->dev, mw->buff_size,
  566. mw->virt_addr, mw->dma_addr);
  567. mw->xlat_size = 0;
  568. mw->buff_size = 0;
  569. mw->virt_addr = NULL;
  570. }
  571. static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
  572. resource_size_t size)
  573. {
  574. struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
  575. struct pci_dev *pdev = nt->ndev->pdev;
  576. size_t xlat_size, buff_size;
  577. resource_size_t xlat_align;
  578. resource_size_t xlat_align_size;
  579. int rc;
  580. if (!size)
  581. return -EINVAL;
  582. rc = ntb_mw_get_align(nt->ndev, PIDX, num_mw, &xlat_align,
  583. &xlat_align_size, NULL);
  584. if (rc)
  585. return rc;
  586. xlat_size = round_up(size, xlat_align_size);
  587. buff_size = round_up(size, xlat_align);
  588. /* No need to re-setup */
  589. if (mw->xlat_size == xlat_size)
  590. return 0;
  591. if (mw->buff_size)
  592. ntb_free_mw(nt, num_mw);
  593. /* Alloc memory for receiving data. Must be aligned */
  594. mw->xlat_size = xlat_size;
  595. mw->buff_size = buff_size;
  596. mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
  597. &mw->dma_addr, GFP_KERNEL);
  598. if (!mw->virt_addr) {
  599. mw->xlat_size = 0;
  600. mw->buff_size = 0;
  601. dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
  602. buff_size);
  603. return -ENOMEM;
  604. }
  605. /*
  606. * we must ensure that the memory address allocated is BAR size
  607. * aligned in order for the XLAT register to take the value. This
  608. * is a requirement of the hardware. It is recommended to setup CMA
  609. * for BAR sizes equal or greater than 4MB.
  610. */
  611. if (!IS_ALIGNED(mw->dma_addr, xlat_align)) {
  612. dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
  613. &mw->dma_addr);
  614. ntb_free_mw(nt, num_mw);
  615. return -ENOMEM;
  616. }
  617. /* Notify HW the memory location of the receive buffer */
  618. rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
  619. mw->xlat_size);
  620. if (rc) {
  621. dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
  622. ntb_free_mw(nt, num_mw);
  623. return -EIO;
  624. }
  625. return 0;
  626. }
  627. static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
  628. {
  629. qp->link_is_up = false;
  630. qp->active = false;
  631. qp->tx_index = 0;
  632. qp->rx_index = 0;
  633. qp->rx_bytes = 0;
  634. qp->rx_pkts = 0;
  635. qp->rx_ring_empty = 0;
  636. qp->rx_err_no_buf = 0;
  637. qp->rx_err_oflow = 0;
  638. qp->rx_err_ver = 0;
  639. qp->rx_memcpy = 0;
  640. qp->rx_async = 0;
  641. qp->tx_bytes = 0;
  642. qp->tx_pkts = 0;
  643. qp->tx_ring_full = 0;
  644. qp->tx_err_no_buf = 0;
  645. qp->tx_memcpy = 0;
  646. qp->tx_async = 0;
  647. }
  648. static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
  649. {
  650. struct ntb_transport_ctx *nt = qp->transport;
  651. struct pci_dev *pdev = nt->ndev->pdev;
  652. dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
  653. cancel_delayed_work_sync(&qp->link_work);
  654. ntb_qp_link_down_reset(qp);
  655. if (qp->event_handler)
  656. qp->event_handler(qp->cb_data, qp->link_is_up);
  657. }
  658. static void ntb_qp_link_cleanup_work(struct work_struct *work)
  659. {
  660. struct ntb_transport_qp *qp = container_of(work,
  661. struct ntb_transport_qp,
  662. link_cleanup);
  663. struct ntb_transport_ctx *nt = qp->transport;
  664. ntb_qp_link_cleanup(qp);
  665. if (nt->link_is_up)
  666. schedule_delayed_work(&qp->link_work,
  667. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  668. }
  669. static void ntb_qp_link_down(struct ntb_transport_qp *qp)
  670. {
  671. schedule_work(&qp->link_cleanup);
  672. }
  673. static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
  674. {
  675. struct ntb_transport_qp *qp;
  676. u64 qp_bitmap_alloc;
  677. unsigned int i, count;
  678. qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
  679. /* Pass along the info to any clients */
  680. for (i = 0; i < nt->qp_count; i++)
  681. if (qp_bitmap_alloc & BIT_ULL(i)) {
  682. qp = &nt->qp_vec[i];
  683. ntb_qp_link_cleanup(qp);
  684. cancel_work_sync(&qp->link_cleanup);
  685. cancel_delayed_work_sync(&qp->link_work);
  686. }
  687. if (!nt->link_is_up)
  688. cancel_delayed_work_sync(&nt->link_work);
  689. /* The scratchpad registers keep the values if the remote side
  690. * goes down, blast them now to give them a sane value the next
  691. * time they are accessed
  692. */
  693. count = ntb_spad_count(nt->ndev);
  694. for (i = 0; i < count; i++)
  695. ntb_spad_write(nt->ndev, i, 0);
  696. }
  697. static void ntb_transport_link_cleanup_work(struct work_struct *work)
  698. {
  699. struct ntb_transport_ctx *nt =
  700. container_of(work, struct ntb_transport_ctx, link_cleanup);
  701. ntb_transport_link_cleanup(nt);
  702. }
  703. static void ntb_transport_event_callback(void *data)
  704. {
  705. struct ntb_transport_ctx *nt = data;
  706. if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
  707. schedule_delayed_work(&nt->link_work, 0);
  708. else
  709. schedule_work(&nt->link_cleanup);
  710. }
  711. static void ntb_transport_link_work(struct work_struct *work)
  712. {
  713. struct ntb_transport_ctx *nt =
  714. container_of(work, struct ntb_transport_ctx, link_work.work);
  715. struct ntb_dev *ndev = nt->ndev;
  716. struct pci_dev *pdev = ndev->pdev;
  717. resource_size_t size;
  718. u32 val;
  719. int rc = 0, i, spad;
  720. /* send the local info, in the opposite order of the way we read it */
  721. for (i = 0; i < nt->mw_count; i++) {
  722. size = nt->mw_vec[i].phys_size;
  723. if (max_mw_size && size > max_mw_size)
  724. size = max_mw_size;
  725. spad = MW0_SZ_HIGH + (i * 2);
  726. ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
  727. spad = MW0_SZ_LOW + (i * 2);
  728. ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
  729. }
  730. ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
  731. ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
  732. ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
  733. /* Query the remote side for its info */
  734. val = ntb_spad_read(ndev, VERSION);
  735. dev_dbg(&pdev->dev, "Remote version = %d\n", val);
  736. if (val != NTB_TRANSPORT_VERSION)
  737. goto out;
  738. val = ntb_spad_read(ndev, NUM_QPS);
  739. dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
  740. if (val != nt->qp_count)
  741. goto out;
  742. val = ntb_spad_read(ndev, NUM_MWS);
  743. dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
  744. if (val != nt->mw_count)
  745. goto out;
  746. for (i = 0; i < nt->mw_count; i++) {
  747. u64 val64;
  748. val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
  749. val64 = (u64)val << 32;
  750. val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
  751. val64 |= val;
  752. dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
  753. rc = ntb_set_mw(nt, i, val64);
  754. if (rc)
  755. goto out1;
  756. }
  757. nt->link_is_up = true;
  758. for (i = 0; i < nt->qp_count; i++) {
  759. struct ntb_transport_qp *qp = &nt->qp_vec[i];
  760. ntb_transport_setup_qp_mw(nt, i);
  761. if (qp->client_ready)
  762. schedule_delayed_work(&qp->link_work, 0);
  763. }
  764. return;
  765. out1:
  766. for (i = 0; i < nt->mw_count; i++)
  767. ntb_free_mw(nt, i);
  768. /* if there's an actual failure, we should just bail */
  769. if (rc < 0)
  770. return;
  771. out:
  772. if (ntb_link_is_up(ndev, NULL, NULL) == 1)
  773. schedule_delayed_work(&nt->link_work,
  774. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  775. }
  776. static void ntb_qp_link_work(struct work_struct *work)
  777. {
  778. struct ntb_transport_qp *qp = container_of(work,
  779. struct ntb_transport_qp,
  780. link_work.work);
  781. struct pci_dev *pdev = qp->ndev->pdev;
  782. struct ntb_transport_ctx *nt = qp->transport;
  783. int val;
  784. WARN_ON(!nt->link_is_up);
  785. val = ntb_spad_read(nt->ndev, QP_LINKS);
  786. ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
  787. /* query remote spad for qp ready bits */
  788. dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
  789. /* See if the remote side is up */
  790. if (val & BIT(qp->qp_num)) {
  791. dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
  792. qp->link_is_up = true;
  793. qp->active = true;
  794. if (qp->event_handler)
  795. qp->event_handler(qp->cb_data, qp->link_is_up);
  796. if (qp->active)
  797. tasklet_schedule(&qp->rxc_db_work);
  798. } else if (nt->link_is_up)
  799. schedule_delayed_work(&qp->link_work,
  800. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  801. }
  802. static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
  803. unsigned int qp_num)
  804. {
  805. struct ntb_transport_qp *qp;
  806. phys_addr_t mw_base;
  807. resource_size_t mw_size;
  808. unsigned int num_qps_mw, tx_size;
  809. unsigned int mw_num, mw_count, qp_count;
  810. u64 qp_offset;
  811. mw_count = nt->mw_count;
  812. qp_count = nt->qp_count;
  813. mw_num = QP_TO_MW(nt, qp_num);
  814. qp = &nt->qp_vec[qp_num];
  815. qp->qp_num = qp_num;
  816. qp->transport = nt;
  817. qp->ndev = nt->ndev;
  818. qp->client_ready = false;
  819. qp->event_handler = NULL;
  820. ntb_qp_link_down_reset(qp);
  821. if (mw_num < qp_count % mw_count)
  822. num_qps_mw = qp_count / mw_count + 1;
  823. else
  824. num_qps_mw = qp_count / mw_count;
  825. mw_base = nt->mw_vec[mw_num].phys_addr;
  826. mw_size = nt->mw_vec[mw_num].phys_size;
  827. if (max_mw_size && mw_size > max_mw_size)
  828. mw_size = max_mw_size;
  829. tx_size = (unsigned int)mw_size / num_qps_mw;
  830. qp_offset = tx_size * (qp_num / mw_count);
  831. qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
  832. if (!qp->tx_mw)
  833. return -EINVAL;
  834. qp->tx_mw_phys = mw_base + qp_offset;
  835. if (!qp->tx_mw_phys)
  836. return -EINVAL;
  837. tx_size -= sizeof(struct ntb_rx_info);
  838. qp->rx_info = qp->tx_mw + tx_size;
  839. /* Due to housekeeping, there must be atleast 2 buffs */
  840. qp->tx_max_frame = min(transport_mtu, tx_size / 2);
  841. qp->tx_max_entry = tx_size / qp->tx_max_frame;
  842. if (nt->debugfs_node_dir) {
  843. char debugfs_name[4];
  844. snprintf(debugfs_name, 4, "qp%d", qp_num);
  845. qp->debugfs_dir = debugfs_create_dir(debugfs_name,
  846. nt->debugfs_node_dir);
  847. qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
  848. qp->debugfs_dir, qp,
  849. &ntb_qp_debugfs_stats);
  850. } else {
  851. qp->debugfs_dir = NULL;
  852. qp->debugfs_stats = NULL;
  853. }
  854. INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
  855. INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
  856. spin_lock_init(&qp->ntb_rx_q_lock);
  857. spin_lock_init(&qp->ntb_tx_free_q_lock);
  858. INIT_LIST_HEAD(&qp->rx_post_q);
  859. INIT_LIST_HEAD(&qp->rx_pend_q);
  860. INIT_LIST_HEAD(&qp->rx_free_q);
  861. INIT_LIST_HEAD(&qp->tx_free_q);
  862. tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
  863. (unsigned long)qp);
  864. return 0;
  865. }
  866. static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
  867. {
  868. struct ntb_transport_ctx *nt;
  869. struct ntb_transport_mw *mw;
  870. unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
  871. u64 qp_bitmap;
  872. int node;
  873. int rc, i;
  874. mw_count = ntb_peer_mw_count(ndev);
  875. if (!ndev->ops->mw_set_trans) {
  876. dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
  877. return -EINVAL;
  878. }
  879. if (ntb_db_is_unsafe(ndev))
  880. dev_dbg(&ndev->dev,
  881. "doorbell is unsafe, proceed anyway...\n");
  882. if (ntb_spad_is_unsafe(ndev))
  883. dev_dbg(&ndev->dev,
  884. "scratchpad is unsafe, proceed anyway...\n");
  885. if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
  886. dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
  887. node = dev_to_node(&ndev->dev);
  888. nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
  889. if (!nt)
  890. return -ENOMEM;
  891. nt->ndev = ndev;
  892. spad_count = ntb_spad_count(ndev);
  893. /* Limit the MW's based on the availability of scratchpads */
  894. if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
  895. nt->mw_count = 0;
  896. rc = -EINVAL;
  897. goto err;
  898. }
  899. max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
  900. nt->mw_count = min(mw_count, max_mw_count_for_spads);
  901. nt->mw_vec = kcalloc_node(mw_count, sizeof(*nt->mw_vec),
  902. GFP_KERNEL, node);
  903. if (!nt->mw_vec) {
  904. rc = -ENOMEM;
  905. goto err;
  906. }
  907. for (i = 0; i < mw_count; i++) {
  908. mw = &nt->mw_vec[i];
  909. rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
  910. &mw->phys_size);
  911. if (rc)
  912. goto err1;
  913. mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
  914. if (!mw->vbase) {
  915. rc = -ENOMEM;
  916. goto err1;
  917. }
  918. mw->buff_size = 0;
  919. mw->xlat_size = 0;
  920. mw->virt_addr = NULL;
  921. mw->dma_addr = 0;
  922. }
  923. qp_bitmap = ntb_db_valid_mask(ndev);
  924. qp_count = ilog2(qp_bitmap);
  925. if (max_num_clients && max_num_clients < qp_count)
  926. qp_count = max_num_clients;
  927. else if (nt->mw_count < qp_count)
  928. qp_count = nt->mw_count;
  929. qp_bitmap &= BIT_ULL(qp_count) - 1;
  930. nt->qp_count = qp_count;
  931. nt->qp_bitmap = qp_bitmap;
  932. nt->qp_bitmap_free = qp_bitmap;
  933. nt->qp_vec = kcalloc_node(qp_count, sizeof(*nt->qp_vec),
  934. GFP_KERNEL, node);
  935. if (!nt->qp_vec) {
  936. rc = -ENOMEM;
  937. goto err1;
  938. }
  939. if (nt_debugfs_dir) {
  940. nt->debugfs_node_dir =
  941. debugfs_create_dir(pci_name(ndev->pdev),
  942. nt_debugfs_dir);
  943. }
  944. for (i = 0; i < qp_count; i++) {
  945. rc = ntb_transport_init_queue(nt, i);
  946. if (rc)
  947. goto err2;
  948. }
  949. INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
  950. INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
  951. rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
  952. if (rc)
  953. goto err2;
  954. INIT_LIST_HEAD(&nt->client_devs);
  955. rc = ntb_bus_init(nt);
  956. if (rc)
  957. goto err3;
  958. nt->link_is_up = false;
  959. ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  960. ntb_link_event(ndev);
  961. return 0;
  962. err3:
  963. ntb_clear_ctx(ndev);
  964. err2:
  965. kfree(nt->qp_vec);
  966. err1:
  967. while (i--) {
  968. mw = &nt->mw_vec[i];
  969. iounmap(mw->vbase);
  970. }
  971. kfree(nt->mw_vec);
  972. err:
  973. kfree(nt);
  974. return rc;
  975. }
  976. static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
  977. {
  978. struct ntb_transport_ctx *nt = ndev->ctx;
  979. struct ntb_transport_qp *qp;
  980. u64 qp_bitmap_alloc;
  981. int i;
  982. ntb_transport_link_cleanup(nt);
  983. cancel_work_sync(&nt->link_cleanup);
  984. cancel_delayed_work_sync(&nt->link_work);
  985. qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
  986. /* verify that all the qp's are freed */
  987. for (i = 0; i < nt->qp_count; i++) {
  988. qp = &nt->qp_vec[i];
  989. if (qp_bitmap_alloc & BIT_ULL(i))
  990. ntb_transport_free_queue(qp);
  991. debugfs_remove_recursive(qp->debugfs_dir);
  992. }
  993. ntb_link_disable(ndev);
  994. ntb_clear_ctx(ndev);
  995. ntb_bus_remove(nt);
  996. for (i = nt->mw_count; i--; ) {
  997. ntb_free_mw(nt, i);
  998. iounmap(nt->mw_vec[i].vbase);
  999. }
  1000. kfree(nt->qp_vec);
  1001. kfree(nt->mw_vec);
  1002. kfree(nt);
  1003. }
  1004. static void ntb_complete_rxc(struct ntb_transport_qp *qp)
  1005. {
  1006. struct ntb_queue_entry *entry;
  1007. void *cb_data;
  1008. unsigned int len;
  1009. unsigned long irqflags;
  1010. spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
  1011. while (!list_empty(&qp->rx_post_q)) {
  1012. entry = list_first_entry(&qp->rx_post_q,
  1013. struct ntb_queue_entry, entry);
  1014. if (!(entry->flags & DESC_DONE_FLAG))
  1015. break;
  1016. entry->rx_hdr->flags = 0;
  1017. iowrite32(entry->rx_index, &qp->rx_info->entry);
  1018. cb_data = entry->cb_data;
  1019. len = entry->len;
  1020. list_move_tail(&entry->entry, &qp->rx_free_q);
  1021. spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
  1022. if (qp->rx_handler && qp->client_ready)
  1023. qp->rx_handler(qp, qp->cb_data, cb_data, len);
  1024. spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
  1025. }
  1026. spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
  1027. }
  1028. static void ntb_rx_copy_callback(void *data,
  1029. const struct dmaengine_result *res)
  1030. {
  1031. struct ntb_queue_entry *entry = data;
  1032. /* we need to check DMA results if we are using DMA */
  1033. if (res) {
  1034. enum dmaengine_tx_result dma_err = res->result;
  1035. switch (dma_err) {
  1036. case DMA_TRANS_READ_FAILED:
  1037. case DMA_TRANS_WRITE_FAILED:
  1038. entry->errors++;
  1039. case DMA_TRANS_ABORTED:
  1040. {
  1041. struct ntb_transport_qp *qp = entry->qp;
  1042. void *offset = qp->rx_buff + qp->rx_max_frame *
  1043. qp->rx_index;
  1044. ntb_memcpy_rx(entry, offset);
  1045. qp->rx_memcpy++;
  1046. return;
  1047. }
  1048. case DMA_TRANS_NOERROR:
  1049. default:
  1050. break;
  1051. }
  1052. }
  1053. entry->flags |= DESC_DONE_FLAG;
  1054. ntb_complete_rxc(entry->qp);
  1055. }
  1056. static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
  1057. {
  1058. void *buf = entry->buf;
  1059. size_t len = entry->len;
  1060. memcpy(buf, offset, len);
  1061. /* Ensure that the data is fully copied out before clearing the flag */
  1062. wmb();
  1063. ntb_rx_copy_callback(entry, NULL);
  1064. }
  1065. static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
  1066. {
  1067. struct dma_async_tx_descriptor *txd;
  1068. struct ntb_transport_qp *qp = entry->qp;
  1069. struct dma_chan *chan = qp->rx_dma_chan;
  1070. struct dma_device *device;
  1071. size_t pay_off, buff_off, len;
  1072. struct dmaengine_unmap_data *unmap;
  1073. dma_cookie_t cookie;
  1074. void *buf = entry->buf;
  1075. len = entry->len;
  1076. device = chan->device;
  1077. pay_off = (size_t)offset & ~PAGE_MASK;
  1078. buff_off = (size_t)buf & ~PAGE_MASK;
  1079. if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
  1080. goto err;
  1081. unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
  1082. if (!unmap)
  1083. goto err;
  1084. unmap->len = len;
  1085. unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
  1086. pay_off, len, DMA_TO_DEVICE);
  1087. if (dma_mapping_error(device->dev, unmap->addr[0]))
  1088. goto err_get_unmap;
  1089. unmap->to_cnt = 1;
  1090. unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
  1091. buff_off, len, DMA_FROM_DEVICE);
  1092. if (dma_mapping_error(device->dev, unmap->addr[1]))
  1093. goto err_get_unmap;
  1094. unmap->from_cnt = 1;
  1095. txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
  1096. unmap->addr[0], len,
  1097. DMA_PREP_INTERRUPT);
  1098. if (!txd)
  1099. goto err_get_unmap;
  1100. txd->callback_result = ntb_rx_copy_callback;
  1101. txd->callback_param = entry;
  1102. dma_set_unmap(txd, unmap);
  1103. cookie = dmaengine_submit(txd);
  1104. if (dma_submit_error(cookie))
  1105. goto err_set_unmap;
  1106. dmaengine_unmap_put(unmap);
  1107. qp->last_cookie = cookie;
  1108. qp->rx_async++;
  1109. return 0;
  1110. err_set_unmap:
  1111. dmaengine_unmap_put(unmap);
  1112. err_get_unmap:
  1113. dmaengine_unmap_put(unmap);
  1114. err:
  1115. return -ENXIO;
  1116. }
  1117. static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
  1118. {
  1119. struct ntb_transport_qp *qp = entry->qp;
  1120. struct dma_chan *chan = qp->rx_dma_chan;
  1121. int res;
  1122. if (!chan)
  1123. goto err;
  1124. if (entry->len < copy_bytes)
  1125. goto err;
  1126. res = ntb_async_rx_submit(entry, offset);
  1127. if (res < 0)
  1128. goto err;
  1129. if (!entry->retries)
  1130. qp->rx_async++;
  1131. return;
  1132. err:
  1133. ntb_memcpy_rx(entry, offset);
  1134. qp->rx_memcpy++;
  1135. }
  1136. static int ntb_process_rxc(struct ntb_transport_qp *qp)
  1137. {
  1138. struct ntb_payload_header *hdr;
  1139. struct ntb_queue_entry *entry;
  1140. void *offset;
  1141. offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
  1142. hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
  1143. dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
  1144. qp->qp_num, hdr->ver, hdr->len, hdr->flags);
  1145. if (!(hdr->flags & DESC_DONE_FLAG)) {
  1146. dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
  1147. qp->rx_ring_empty++;
  1148. return -EAGAIN;
  1149. }
  1150. if (hdr->flags & LINK_DOWN_FLAG) {
  1151. dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
  1152. ntb_qp_link_down(qp);
  1153. hdr->flags = 0;
  1154. return -EAGAIN;
  1155. }
  1156. if (hdr->ver != (u32)qp->rx_pkts) {
  1157. dev_dbg(&qp->ndev->pdev->dev,
  1158. "version mismatch, expected %llu - got %u\n",
  1159. qp->rx_pkts, hdr->ver);
  1160. qp->rx_err_ver++;
  1161. return -EIO;
  1162. }
  1163. entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
  1164. if (!entry) {
  1165. dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
  1166. qp->rx_err_no_buf++;
  1167. return -EAGAIN;
  1168. }
  1169. entry->rx_hdr = hdr;
  1170. entry->rx_index = qp->rx_index;
  1171. if (hdr->len > entry->len) {
  1172. dev_dbg(&qp->ndev->pdev->dev,
  1173. "receive buffer overflow! Wanted %d got %d\n",
  1174. hdr->len, entry->len);
  1175. qp->rx_err_oflow++;
  1176. entry->len = -EIO;
  1177. entry->flags |= DESC_DONE_FLAG;
  1178. ntb_complete_rxc(qp);
  1179. } else {
  1180. dev_dbg(&qp->ndev->pdev->dev,
  1181. "RX OK index %u ver %u size %d into buf size %d\n",
  1182. qp->rx_index, hdr->ver, hdr->len, entry->len);
  1183. qp->rx_bytes += hdr->len;
  1184. qp->rx_pkts++;
  1185. entry->len = hdr->len;
  1186. ntb_async_rx(entry, offset);
  1187. }
  1188. qp->rx_index++;
  1189. qp->rx_index %= qp->rx_max_entry;
  1190. return 0;
  1191. }
  1192. static void ntb_transport_rxc_db(unsigned long data)
  1193. {
  1194. struct ntb_transport_qp *qp = (void *)data;
  1195. int rc, i;
  1196. dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
  1197. __func__, qp->qp_num);
  1198. /* Limit the number of packets processed in a single interrupt to
  1199. * provide fairness to others
  1200. */
  1201. for (i = 0; i < qp->rx_max_entry; i++) {
  1202. rc = ntb_process_rxc(qp);
  1203. if (rc)
  1204. break;
  1205. }
  1206. if (i && qp->rx_dma_chan)
  1207. dma_async_issue_pending(qp->rx_dma_chan);
  1208. if (i == qp->rx_max_entry) {
  1209. /* there is more work to do */
  1210. if (qp->active)
  1211. tasklet_schedule(&qp->rxc_db_work);
  1212. } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
  1213. /* the doorbell bit is set: clear it */
  1214. ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
  1215. /* ntb_db_read ensures ntb_db_clear write is committed */
  1216. ntb_db_read(qp->ndev);
  1217. /* an interrupt may have arrived between finishing
  1218. * ntb_process_rxc and clearing the doorbell bit:
  1219. * there might be some more work to do.
  1220. */
  1221. if (qp->active)
  1222. tasklet_schedule(&qp->rxc_db_work);
  1223. }
  1224. }
  1225. static void ntb_tx_copy_callback(void *data,
  1226. const struct dmaengine_result *res)
  1227. {
  1228. struct ntb_queue_entry *entry = data;
  1229. struct ntb_transport_qp *qp = entry->qp;
  1230. struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
  1231. /* we need to check DMA results if we are using DMA */
  1232. if (res) {
  1233. enum dmaengine_tx_result dma_err = res->result;
  1234. switch (dma_err) {
  1235. case DMA_TRANS_READ_FAILED:
  1236. case DMA_TRANS_WRITE_FAILED:
  1237. entry->errors++;
  1238. case DMA_TRANS_ABORTED:
  1239. {
  1240. void __iomem *offset =
  1241. qp->tx_mw + qp->tx_max_frame *
  1242. entry->tx_index;
  1243. /* resubmit via CPU */
  1244. ntb_memcpy_tx(entry, offset);
  1245. qp->tx_memcpy++;
  1246. return;
  1247. }
  1248. case DMA_TRANS_NOERROR:
  1249. default:
  1250. break;
  1251. }
  1252. }
  1253. iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
  1254. ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
  1255. /* The entry length can only be zero if the packet is intended to be a
  1256. * "link down" or similar. Since no payload is being sent in these
  1257. * cases, there is nothing to add to the completion queue.
  1258. */
  1259. if (entry->len > 0) {
  1260. qp->tx_bytes += entry->len;
  1261. if (qp->tx_handler)
  1262. qp->tx_handler(qp, qp->cb_data, entry->cb_data,
  1263. entry->len);
  1264. }
  1265. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
  1266. }
  1267. static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
  1268. {
  1269. #ifdef ARCH_HAS_NOCACHE_UACCESS
  1270. /*
  1271. * Using non-temporal mov to improve performance on non-cached
  1272. * writes, even though we aren't actually copying from user space.
  1273. */
  1274. __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
  1275. #else
  1276. memcpy_toio(offset, entry->buf, entry->len);
  1277. #endif
  1278. /* Ensure that the data is fully copied out before setting the flags */
  1279. wmb();
  1280. ntb_tx_copy_callback(entry, NULL);
  1281. }
  1282. static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
  1283. struct ntb_queue_entry *entry)
  1284. {
  1285. struct dma_async_tx_descriptor *txd;
  1286. struct dma_chan *chan = qp->tx_dma_chan;
  1287. struct dma_device *device;
  1288. size_t len = entry->len;
  1289. void *buf = entry->buf;
  1290. size_t dest_off, buff_off;
  1291. struct dmaengine_unmap_data *unmap;
  1292. dma_addr_t dest;
  1293. dma_cookie_t cookie;
  1294. device = chan->device;
  1295. dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
  1296. buff_off = (size_t)buf & ~PAGE_MASK;
  1297. dest_off = (size_t)dest & ~PAGE_MASK;
  1298. if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
  1299. goto err;
  1300. unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
  1301. if (!unmap)
  1302. goto err;
  1303. unmap->len = len;
  1304. unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
  1305. buff_off, len, DMA_TO_DEVICE);
  1306. if (dma_mapping_error(device->dev, unmap->addr[0]))
  1307. goto err_get_unmap;
  1308. unmap->to_cnt = 1;
  1309. txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
  1310. DMA_PREP_INTERRUPT);
  1311. if (!txd)
  1312. goto err_get_unmap;
  1313. txd->callback_result = ntb_tx_copy_callback;
  1314. txd->callback_param = entry;
  1315. dma_set_unmap(txd, unmap);
  1316. cookie = dmaengine_submit(txd);
  1317. if (dma_submit_error(cookie))
  1318. goto err_set_unmap;
  1319. dmaengine_unmap_put(unmap);
  1320. dma_async_issue_pending(chan);
  1321. return 0;
  1322. err_set_unmap:
  1323. dmaengine_unmap_put(unmap);
  1324. err_get_unmap:
  1325. dmaengine_unmap_put(unmap);
  1326. err:
  1327. return -ENXIO;
  1328. }
  1329. static void ntb_async_tx(struct ntb_transport_qp *qp,
  1330. struct ntb_queue_entry *entry)
  1331. {
  1332. struct ntb_payload_header __iomem *hdr;
  1333. struct dma_chan *chan = qp->tx_dma_chan;
  1334. void __iomem *offset;
  1335. int res;
  1336. entry->tx_index = qp->tx_index;
  1337. offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
  1338. hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
  1339. entry->tx_hdr = hdr;
  1340. iowrite32(entry->len, &hdr->len);
  1341. iowrite32((u32)qp->tx_pkts, &hdr->ver);
  1342. if (!chan)
  1343. goto err;
  1344. if (entry->len < copy_bytes)
  1345. goto err;
  1346. res = ntb_async_tx_submit(qp, entry);
  1347. if (res < 0)
  1348. goto err;
  1349. if (!entry->retries)
  1350. qp->tx_async++;
  1351. return;
  1352. err:
  1353. ntb_memcpy_tx(entry, offset);
  1354. qp->tx_memcpy++;
  1355. }
  1356. static int ntb_process_tx(struct ntb_transport_qp *qp,
  1357. struct ntb_queue_entry *entry)
  1358. {
  1359. if (qp->tx_index == qp->remote_rx_info->entry) {
  1360. qp->tx_ring_full++;
  1361. return -EAGAIN;
  1362. }
  1363. if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
  1364. if (qp->tx_handler)
  1365. qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
  1366. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  1367. &qp->tx_free_q);
  1368. return 0;
  1369. }
  1370. ntb_async_tx(qp, entry);
  1371. qp->tx_index++;
  1372. qp->tx_index %= qp->tx_max_entry;
  1373. qp->tx_pkts++;
  1374. return 0;
  1375. }
  1376. static void ntb_send_link_down(struct ntb_transport_qp *qp)
  1377. {
  1378. struct pci_dev *pdev = qp->ndev->pdev;
  1379. struct ntb_queue_entry *entry;
  1380. int i, rc;
  1381. if (!qp->link_is_up)
  1382. return;
  1383. dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
  1384. for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
  1385. entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
  1386. if (entry)
  1387. break;
  1388. msleep(100);
  1389. }
  1390. if (!entry)
  1391. return;
  1392. entry->cb_data = NULL;
  1393. entry->buf = NULL;
  1394. entry->len = 0;
  1395. entry->flags = LINK_DOWN_FLAG;
  1396. rc = ntb_process_tx(qp, entry);
  1397. if (rc)
  1398. dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
  1399. qp->qp_num);
  1400. ntb_qp_link_down_reset(qp);
  1401. }
  1402. static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
  1403. {
  1404. return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
  1405. }
  1406. /**
  1407. * ntb_transport_create_queue - Create a new NTB transport layer queue
  1408. * @rx_handler: receive callback function
  1409. * @tx_handler: transmit callback function
  1410. * @event_handler: event callback function
  1411. *
  1412. * Create a new NTB transport layer queue and provide the queue with a callback
  1413. * routine for both transmit and receive. The receive callback routine will be
  1414. * used to pass up data when the transport has received it on the queue. The
  1415. * transmit callback routine will be called when the transport has completed the
  1416. * transmission of the data on the queue and the data is ready to be freed.
  1417. *
  1418. * RETURNS: pointer to newly created ntb_queue, NULL on error.
  1419. */
  1420. struct ntb_transport_qp *
  1421. ntb_transport_create_queue(void *data, struct device *client_dev,
  1422. const struct ntb_queue_handlers *handlers)
  1423. {
  1424. struct ntb_dev *ndev;
  1425. struct pci_dev *pdev;
  1426. struct ntb_transport_ctx *nt;
  1427. struct ntb_queue_entry *entry;
  1428. struct ntb_transport_qp *qp;
  1429. u64 qp_bit;
  1430. unsigned int free_queue;
  1431. dma_cap_mask_t dma_mask;
  1432. int node;
  1433. int i;
  1434. ndev = dev_ntb(client_dev->parent);
  1435. pdev = ndev->pdev;
  1436. nt = ndev->ctx;
  1437. node = dev_to_node(&ndev->dev);
  1438. free_queue = ffs(nt->qp_bitmap_free);
  1439. if (!free_queue)
  1440. goto err;
  1441. /* decrement free_queue to make it zero based */
  1442. free_queue--;
  1443. qp = &nt->qp_vec[free_queue];
  1444. qp_bit = BIT_ULL(qp->qp_num);
  1445. nt->qp_bitmap_free &= ~qp_bit;
  1446. qp->cb_data = data;
  1447. qp->rx_handler = handlers->rx_handler;
  1448. qp->tx_handler = handlers->tx_handler;
  1449. qp->event_handler = handlers->event_handler;
  1450. dma_cap_zero(dma_mask);
  1451. dma_cap_set(DMA_MEMCPY, dma_mask);
  1452. if (use_dma) {
  1453. qp->tx_dma_chan =
  1454. dma_request_channel(dma_mask, ntb_dma_filter_fn,
  1455. (void *)(unsigned long)node);
  1456. if (!qp->tx_dma_chan)
  1457. dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
  1458. qp->rx_dma_chan =
  1459. dma_request_channel(dma_mask, ntb_dma_filter_fn,
  1460. (void *)(unsigned long)node);
  1461. if (!qp->rx_dma_chan)
  1462. dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
  1463. } else {
  1464. qp->tx_dma_chan = NULL;
  1465. qp->rx_dma_chan = NULL;
  1466. }
  1467. dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
  1468. qp->tx_dma_chan ? "DMA" : "CPU");
  1469. dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
  1470. qp->rx_dma_chan ? "DMA" : "CPU");
  1471. for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
  1472. entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
  1473. if (!entry)
  1474. goto err1;
  1475. entry->qp = qp;
  1476. ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
  1477. &qp->rx_free_q);
  1478. }
  1479. qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
  1480. for (i = 0; i < qp->tx_max_entry; i++) {
  1481. entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
  1482. if (!entry)
  1483. goto err2;
  1484. entry->qp = qp;
  1485. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  1486. &qp->tx_free_q);
  1487. }
  1488. ntb_db_clear(qp->ndev, qp_bit);
  1489. ntb_db_clear_mask(qp->ndev, qp_bit);
  1490. dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
  1491. return qp;
  1492. err2:
  1493. while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
  1494. kfree(entry);
  1495. err1:
  1496. qp->rx_alloc_entry = 0;
  1497. while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
  1498. kfree(entry);
  1499. if (qp->tx_dma_chan)
  1500. dma_release_channel(qp->tx_dma_chan);
  1501. if (qp->rx_dma_chan)
  1502. dma_release_channel(qp->rx_dma_chan);
  1503. nt->qp_bitmap_free |= qp_bit;
  1504. err:
  1505. return NULL;
  1506. }
  1507. EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
  1508. /**
  1509. * ntb_transport_free_queue - Frees NTB transport queue
  1510. * @qp: NTB queue to be freed
  1511. *
  1512. * Frees NTB transport queue
  1513. */
  1514. void ntb_transport_free_queue(struct ntb_transport_qp *qp)
  1515. {
  1516. struct pci_dev *pdev;
  1517. struct ntb_queue_entry *entry;
  1518. u64 qp_bit;
  1519. if (!qp)
  1520. return;
  1521. pdev = qp->ndev->pdev;
  1522. qp->active = false;
  1523. if (qp->tx_dma_chan) {
  1524. struct dma_chan *chan = qp->tx_dma_chan;
  1525. /* Putting the dma_chan to NULL will force any new traffic to be
  1526. * processed by the CPU instead of the DAM engine
  1527. */
  1528. qp->tx_dma_chan = NULL;
  1529. /* Try to be nice and wait for any queued DMA engine
  1530. * transactions to process before smashing it with a rock
  1531. */
  1532. dma_sync_wait(chan, qp->last_cookie);
  1533. dmaengine_terminate_all(chan);
  1534. dma_release_channel(chan);
  1535. }
  1536. if (qp->rx_dma_chan) {
  1537. struct dma_chan *chan = qp->rx_dma_chan;
  1538. /* Putting the dma_chan to NULL will force any new traffic to be
  1539. * processed by the CPU instead of the DAM engine
  1540. */
  1541. qp->rx_dma_chan = NULL;
  1542. /* Try to be nice and wait for any queued DMA engine
  1543. * transactions to process before smashing it with a rock
  1544. */
  1545. dma_sync_wait(chan, qp->last_cookie);
  1546. dmaengine_terminate_all(chan);
  1547. dma_release_channel(chan);
  1548. }
  1549. qp_bit = BIT_ULL(qp->qp_num);
  1550. ntb_db_set_mask(qp->ndev, qp_bit);
  1551. tasklet_kill(&qp->rxc_db_work);
  1552. cancel_delayed_work_sync(&qp->link_work);
  1553. qp->cb_data = NULL;
  1554. qp->rx_handler = NULL;
  1555. qp->tx_handler = NULL;
  1556. qp->event_handler = NULL;
  1557. while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
  1558. kfree(entry);
  1559. while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
  1560. dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
  1561. kfree(entry);
  1562. }
  1563. while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
  1564. dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
  1565. kfree(entry);
  1566. }
  1567. while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
  1568. kfree(entry);
  1569. qp->transport->qp_bitmap_free |= qp_bit;
  1570. dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
  1571. }
  1572. EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
  1573. /**
  1574. * ntb_transport_rx_remove - Dequeues enqueued rx packet
  1575. * @qp: NTB queue to be freed
  1576. * @len: pointer to variable to write enqueued buffers length
  1577. *
  1578. * Dequeues unused buffers from receive queue. Should only be used during
  1579. * shutdown of qp.
  1580. *
  1581. * RETURNS: NULL error value on error, or void* for success.
  1582. */
  1583. void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
  1584. {
  1585. struct ntb_queue_entry *entry;
  1586. void *buf;
  1587. if (!qp || qp->client_ready)
  1588. return NULL;
  1589. entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
  1590. if (!entry)
  1591. return NULL;
  1592. buf = entry->cb_data;
  1593. *len = entry->len;
  1594. ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
  1595. return buf;
  1596. }
  1597. EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
  1598. /**
  1599. * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
  1600. * @qp: NTB transport layer queue the entry is to be enqueued on
  1601. * @cb: per buffer pointer for callback function to use
  1602. * @data: pointer to data buffer that incoming packets will be copied into
  1603. * @len: length of the data buffer
  1604. *
  1605. * Enqueue a new receive buffer onto the transport queue into which a NTB
  1606. * payload can be received into.
  1607. *
  1608. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1609. */
  1610. int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
  1611. unsigned int len)
  1612. {
  1613. struct ntb_queue_entry *entry;
  1614. if (!qp)
  1615. return -EINVAL;
  1616. entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
  1617. if (!entry)
  1618. return -ENOMEM;
  1619. entry->cb_data = cb;
  1620. entry->buf = data;
  1621. entry->len = len;
  1622. entry->flags = 0;
  1623. entry->retries = 0;
  1624. entry->errors = 0;
  1625. entry->rx_index = 0;
  1626. ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
  1627. if (qp->active)
  1628. tasklet_schedule(&qp->rxc_db_work);
  1629. return 0;
  1630. }
  1631. EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
  1632. /**
  1633. * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
  1634. * @qp: NTB transport layer queue the entry is to be enqueued on
  1635. * @cb: per buffer pointer for callback function to use
  1636. * @data: pointer to data buffer that will be sent
  1637. * @len: length of the data buffer
  1638. *
  1639. * Enqueue a new transmit buffer onto the transport queue from which a NTB
  1640. * payload will be transmitted. This assumes that a lock is being held to
  1641. * serialize access to the qp.
  1642. *
  1643. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1644. */
  1645. int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
  1646. unsigned int len)
  1647. {
  1648. struct ntb_queue_entry *entry;
  1649. int rc;
  1650. if (!qp || !qp->link_is_up || !len)
  1651. return -EINVAL;
  1652. entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
  1653. if (!entry) {
  1654. qp->tx_err_no_buf++;
  1655. return -EBUSY;
  1656. }
  1657. entry->cb_data = cb;
  1658. entry->buf = data;
  1659. entry->len = len;
  1660. entry->flags = 0;
  1661. entry->errors = 0;
  1662. entry->retries = 0;
  1663. entry->tx_index = 0;
  1664. rc = ntb_process_tx(qp, entry);
  1665. if (rc)
  1666. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  1667. &qp->tx_free_q);
  1668. return rc;
  1669. }
  1670. EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
  1671. /**
  1672. * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
  1673. * @qp: NTB transport layer queue to be enabled
  1674. *
  1675. * Notify NTB transport layer of client readiness to use queue
  1676. */
  1677. void ntb_transport_link_up(struct ntb_transport_qp *qp)
  1678. {
  1679. if (!qp)
  1680. return;
  1681. qp->client_ready = true;
  1682. if (qp->transport->link_is_up)
  1683. schedule_delayed_work(&qp->link_work, 0);
  1684. }
  1685. EXPORT_SYMBOL_GPL(ntb_transport_link_up);
  1686. /**
  1687. * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
  1688. * @qp: NTB transport layer queue to be disabled
  1689. *
  1690. * Notify NTB transport layer of client's desire to no longer receive data on
  1691. * transport queue specified. It is the client's responsibility to ensure all
  1692. * entries on queue are purged or otherwise handled appropriately.
  1693. */
  1694. void ntb_transport_link_down(struct ntb_transport_qp *qp)
  1695. {
  1696. int val;
  1697. if (!qp)
  1698. return;
  1699. qp->client_ready = false;
  1700. val = ntb_spad_read(qp->ndev, QP_LINKS);
  1701. ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
  1702. if (qp->link_is_up)
  1703. ntb_send_link_down(qp);
  1704. else
  1705. cancel_delayed_work_sync(&qp->link_work);
  1706. }
  1707. EXPORT_SYMBOL_GPL(ntb_transport_link_down);
  1708. /**
  1709. * ntb_transport_link_query - Query transport link state
  1710. * @qp: NTB transport layer queue to be queried
  1711. *
  1712. * Query connectivity to the remote system of the NTB transport queue
  1713. *
  1714. * RETURNS: true for link up or false for link down
  1715. */
  1716. bool ntb_transport_link_query(struct ntb_transport_qp *qp)
  1717. {
  1718. if (!qp)
  1719. return false;
  1720. return qp->link_is_up;
  1721. }
  1722. EXPORT_SYMBOL_GPL(ntb_transport_link_query);
  1723. /**
  1724. * ntb_transport_qp_num - Query the qp number
  1725. * @qp: NTB transport layer queue to be queried
  1726. *
  1727. * Query qp number of the NTB transport queue
  1728. *
  1729. * RETURNS: a zero based number specifying the qp number
  1730. */
  1731. unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
  1732. {
  1733. if (!qp)
  1734. return 0;
  1735. return qp->qp_num;
  1736. }
  1737. EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
  1738. /**
  1739. * ntb_transport_max_size - Query the max payload size of a qp
  1740. * @qp: NTB transport layer queue to be queried
  1741. *
  1742. * Query the maximum payload size permissible on the given qp
  1743. *
  1744. * RETURNS: the max payload size of a qp
  1745. */
  1746. unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
  1747. {
  1748. unsigned int max_size;
  1749. unsigned int copy_align;
  1750. struct dma_chan *rx_chan, *tx_chan;
  1751. if (!qp)
  1752. return 0;
  1753. rx_chan = qp->rx_dma_chan;
  1754. tx_chan = qp->tx_dma_chan;
  1755. copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
  1756. tx_chan ? tx_chan->device->copy_align : 0);
  1757. /* If DMA engine usage is possible, try to find the max size for that */
  1758. max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
  1759. max_size = round_down(max_size, 1 << copy_align);
  1760. return max_size;
  1761. }
  1762. EXPORT_SYMBOL_GPL(ntb_transport_max_size);
  1763. unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
  1764. {
  1765. unsigned int head = qp->tx_index;
  1766. unsigned int tail = qp->remote_rx_info->entry;
  1767. return tail > head ? tail - head : qp->tx_max_entry + tail - head;
  1768. }
  1769. EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
  1770. static void ntb_transport_doorbell_callback(void *data, int vector)
  1771. {
  1772. struct ntb_transport_ctx *nt = data;
  1773. struct ntb_transport_qp *qp;
  1774. u64 db_bits;
  1775. unsigned int qp_num;
  1776. db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
  1777. ntb_db_vector_mask(nt->ndev, vector));
  1778. while (db_bits) {
  1779. qp_num = __ffs(db_bits);
  1780. qp = &nt->qp_vec[qp_num];
  1781. if (qp->active)
  1782. tasklet_schedule(&qp->rxc_db_work);
  1783. db_bits &= ~BIT_ULL(qp_num);
  1784. }
  1785. }
  1786. static const struct ntb_ctx_ops ntb_transport_ops = {
  1787. .link_event = ntb_transport_event_callback,
  1788. .db_event = ntb_transport_doorbell_callback,
  1789. };
  1790. static struct ntb_client ntb_transport_client = {
  1791. .ops = {
  1792. .probe = ntb_transport_probe,
  1793. .remove = ntb_transport_free,
  1794. },
  1795. };
  1796. static int __init ntb_transport_init(void)
  1797. {
  1798. int rc;
  1799. pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
  1800. if (debugfs_initialized())
  1801. nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1802. rc = bus_register(&ntb_transport_bus);
  1803. if (rc)
  1804. goto err_bus;
  1805. rc = ntb_register_client(&ntb_transport_client);
  1806. if (rc)
  1807. goto err_client;
  1808. return 0;
  1809. err_client:
  1810. bus_unregister(&ntb_transport_bus);
  1811. err_bus:
  1812. debugfs_remove_recursive(nt_debugfs_dir);
  1813. return rc;
  1814. }
  1815. module_init(ntb_transport_init);
  1816. static void __exit ntb_transport_exit(void)
  1817. {
  1818. ntb_unregister_client(&ntb_transport_client);
  1819. bus_unregister(&ntb_transport_bus);
  1820. debugfs_remove_recursive(nt_debugfs_dir);
  1821. }
  1822. module_exit(ntb_transport_exit);