ntb_transport.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. *
  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. * BSD LICENSE
  14. *
  15. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copy
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * * Neither the name of Intel Corporation nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * Intel PCIe NTB Linux driver
  44. *
  45. * Contact Information:
  46. * Jon Mason <jon.mason@intel.com>
  47. */
  48. #include <linux/debugfs.h>
  49. #include <linux/delay.h>
  50. #include <linux/dma-mapping.h>
  51. #include <linux/errno.h>
  52. #include <linux/export.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/module.h>
  55. #include <linux/pci.h>
  56. #include <linux/slab.h>
  57. #include <linux/types.h>
  58. #include <linux/ntb.h>
  59. #include "ntb_hw.h"
  60. #define NTB_TRANSPORT_VERSION 1
  61. static unsigned int transport_mtu = 0x401E;
  62. module_param(transport_mtu, uint, 0644);
  63. MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
  64. static unsigned char max_num_clients = 2;
  65. module_param(max_num_clients, byte, 0644);
  66. MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
  67. struct ntb_queue_entry {
  68. /* ntb_queue list reference */
  69. struct list_head entry;
  70. /* pointers to data to be transfered */
  71. void *cb_data;
  72. void *buf;
  73. unsigned int len;
  74. unsigned int flags;
  75. };
  76. struct ntb_rx_info {
  77. unsigned int entry;
  78. };
  79. struct ntb_transport_qp {
  80. struct ntb_transport *transport;
  81. struct ntb_device *ndev;
  82. void *cb_data;
  83. bool client_ready;
  84. bool qp_link;
  85. u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
  86. struct ntb_rx_info *rx_info;
  87. struct ntb_rx_info *remote_rx_info;
  88. void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
  89. void *data, int len);
  90. struct list_head tx_free_q;
  91. spinlock_t ntb_tx_free_q_lock;
  92. void *tx_mw;
  93. unsigned int tx_index;
  94. unsigned int tx_max_entry;
  95. unsigned int tx_max_frame;
  96. void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
  97. void *data, int len);
  98. struct tasklet_struct rx_work;
  99. struct list_head rx_pend_q;
  100. struct list_head rx_free_q;
  101. spinlock_t ntb_rx_pend_q_lock;
  102. spinlock_t ntb_rx_free_q_lock;
  103. void *rx_buff;
  104. unsigned int rx_index;
  105. unsigned int rx_max_entry;
  106. unsigned int rx_max_frame;
  107. void (*event_handler) (void *data, int status);
  108. struct delayed_work link_work;
  109. struct work_struct link_cleanup;
  110. struct dentry *debugfs_dir;
  111. struct dentry *debugfs_stats;
  112. /* Stats */
  113. u64 rx_bytes;
  114. u64 rx_pkts;
  115. u64 rx_ring_empty;
  116. u64 rx_err_no_buf;
  117. u64 rx_err_oflow;
  118. u64 rx_err_ver;
  119. u64 tx_bytes;
  120. u64 tx_pkts;
  121. u64 tx_ring_full;
  122. };
  123. struct ntb_transport_mw {
  124. size_t size;
  125. void *virt_addr;
  126. dma_addr_t dma_addr;
  127. };
  128. struct ntb_transport_client_dev {
  129. struct list_head entry;
  130. struct device dev;
  131. };
  132. struct ntb_transport {
  133. struct list_head entry;
  134. struct list_head client_devs;
  135. struct ntb_device *ndev;
  136. struct ntb_transport_mw mw[NTB_NUM_MW];
  137. struct ntb_transport_qp *qps;
  138. unsigned int max_qps;
  139. unsigned long qp_bitmap;
  140. bool transport_link;
  141. struct delayed_work link_work;
  142. struct work_struct link_cleanup;
  143. struct dentry *debugfs_dir;
  144. };
  145. enum {
  146. DESC_DONE_FLAG = 1 << 0,
  147. LINK_DOWN_FLAG = 1 << 1,
  148. };
  149. struct ntb_payload_header {
  150. u64 ver;
  151. unsigned int len;
  152. unsigned int flags;
  153. };
  154. enum {
  155. VERSION = 0,
  156. MW0_SZ,
  157. MW1_SZ,
  158. NUM_QPS,
  159. QP_LINKS,
  160. MAX_SPAD,
  161. };
  162. #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
  163. #define NTB_QP_DEF_NUM_ENTRIES 100
  164. #define NTB_LINK_DOWN_TIMEOUT 10
  165. static int ntb_match_bus(struct device *dev, struct device_driver *drv)
  166. {
  167. return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
  168. }
  169. static int ntb_client_probe(struct device *dev)
  170. {
  171. const struct ntb_client *drv = container_of(dev->driver,
  172. struct ntb_client, driver);
  173. struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
  174. int rc = -EINVAL;
  175. get_device(dev);
  176. if (drv && drv->probe)
  177. rc = drv->probe(pdev);
  178. if (rc)
  179. put_device(dev);
  180. return rc;
  181. }
  182. static int ntb_client_remove(struct device *dev)
  183. {
  184. const struct ntb_client *drv = container_of(dev->driver,
  185. struct ntb_client, driver);
  186. struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
  187. if (drv && drv->remove)
  188. drv->remove(pdev);
  189. put_device(dev);
  190. return 0;
  191. }
  192. static struct bus_type ntb_bus_type = {
  193. .name = "ntb_bus",
  194. .match = ntb_match_bus,
  195. .probe = ntb_client_probe,
  196. .remove = ntb_client_remove,
  197. };
  198. static LIST_HEAD(ntb_transport_list);
  199. static int ntb_bus_init(struct ntb_transport *nt)
  200. {
  201. if (list_empty(&ntb_transport_list)) {
  202. int rc = bus_register(&ntb_bus_type);
  203. if (rc)
  204. return rc;
  205. }
  206. list_add(&nt->entry, &ntb_transport_list);
  207. return 0;
  208. }
  209. static void ntb_bus_remove(struct ntb_transport *nt)
  210. {
  211. struct ntb_transport_client_dev *client_dev, *cd;
  212. list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
  213. dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
  214. dev_name(&client_dev->dev));
  215. list_del(&client_dev->entry);
  216. device_unregister(&client_dev->dev);
  217. }
  218. list_del(&nt->entry);
  219. if (list_empty(&ntb_transport_list))
  220. bus_unregister(&ntb_bus_type);
  221. }
  222. static void ntb_client_release(struct device *dev)
  223. {
  224. struct ntb_transport_client_dev *client_dev;
  225. client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
  226. kfree(client_dev);
  227. }
  228. /**
  229. * ntb_unregister_client_dev - Unregister NTB client device
  230. * @device_name: Name of NTB client device
  231. *
  232. * Unregister an NTB client device with the NTB transport layer
  233. */
  234. void ntb_unregister_client_dev(char *device_name)
  235. {
  236. struct ntb_transport_client_dev *client, *cd;
  237. struct ntb_transport *nt;
  238. list_for_each_entry(nt, &ntb_transport_list, entry)
  239. list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
  240. if (!strncmp(dev_name(&client->dev), device_name,
  241. strlen(device_name))) {
  242. list_del(&client->entry);
  243. device_unregister(&client->dev);
  244. }
  245. }
  246. EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
  247. /**
  248. * ntb_register_client_dev - Register NTB client device
  249. * @device_name: Name of NTB client device
  250. *
  251. * Register an NTB client device with the NTB transport layer
  252. */
  253. int ntb_register_client_dev(char *device_name)
  254. {
  255. struct ntb_transport_client_dev *client_dev;
  256. struct ntb_transport *nt;
  257. int rc;
  258. if (list_empty(&ntb_transport_list))
  259. return -ENODEV;
  260. list_for_each_entry(nt, &ntb_transport_list, entry) {
  261. struct device *dev;
  262. client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
  263. GFP_KERNEL);
  264. if (!client_dev) {
  265. rc = -ENOMEM;
  266. goto err;
  267. }
  268. dev = &client_dev->dev;
  269. /* setup and register client devices */
  270. dev_set_name(dev, "%s", device_name);
  271. dev->bus = &ntb_bus_type;
  272. dev->release = ntb_client_release;
  273. dev->parent = &ntb_query_pdev(nt->ndev)->dev;
  274. rc = device_register(dev);
  275. if (rc) {
  276. kfree(client_dev);
  277. goto err;
  278. }
  279. list_add_tail(&client_dev->entry, &nt->client_devs);
  280. }
  281. return 0;
  282. err:
  283. ntb_unregister_client_dev(device_name);
  284. return rc;
  285. }
  286. EXPORT_SYMBOL_GPL(ntb_register_client_dev);
  287. /**
  288. * ntb_register_client - Register NTB client driver
  289. * @drv: NTB client driver to be registered
  290. *
  291. * Register an NTB client driver with the NTB transport layer
  292. *
  293. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  294. */
  295. int ntb_register_client(struct ntb_client *drv)
  296. {
  297. drv->driver.bus = &ntb_bus_type;
  298. if (list_empty(&ntb_transport_list))
  299. return -ENODEV;
  300. return driver_register(&drv->driver);
  301. }
  302. EXPORT_SYMBOL_GPL(ntb_register_client);
  303. /**
  304. * ntb_unregister_client - Unregister NTB client driver
  305. * @drv: NTB client driver to be unregistered
  306. *
  307. * Unregister an NTB client driver with the NTB transport layer
  308. *
  309. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  310. */
  311. void ntb_unregister_client(struct ntb_client *drv)
  312. {
  313. driver_unregister(&drv->driver);
  314. }
  315. EXPORT_SYMBOL_GPL(ntb_unregister_client);
  316. static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
  317. loff_t *offp)
  318. {
  319. struct ntb_transport_qp *qp;
  320. char *buf;
  321. ssize_t ret, out_offset, out_count;
  322. out_count = 600;
  323. buf = kmalloc(out_count, GFP_KERNEL);
  324. if (!buf)
  325. return -ENOMEM;
  326. qp = filp->private_data;
  327. out_offset = 0;
  328. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  329. "NTB QP stats\n");
  330. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  331. "rx_bytes - \t%llu\n", qp->rx_bytes);
  332. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  333. "rx_pkts - \t%llu\n", qp->rx_pkts);
  334. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  335. "rx_ring_empty - %llu\n", qp->rx_ring_empty);
  336. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  337. "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
  338. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  339. "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
  340. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  341. "rx_err_ver - \t%llu\n", qp->rx_err_ver);
  342. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  343. "rx_buff - \t%p\n", qp->rx_buff);
  344. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  345. "rx_index - \t%u\n", qp->rx_index);
  346. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  347. "rx_max_entry - \t%u\n", qp->rx_max_entry);
  348. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  349. "tx_bytes - \t%llu\n", qp->tx_bytes);
  350. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  351. "tx_pkts - \t%llu\n", qp->tx_pkts);
  352. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  353. "tx_ring_full - \t%llu\n", qp->tx_ring_full);
  354. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  355. "tx_mw - \t%p\n", qp->tx_mw);
  356. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  357. "tx_index - \t%u\n", qp->tx_index);
  358. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  359. "tx_max_entry - \t%u\n", qp->tx_max_entry);
  360. out_offset += snprintf(buf + out_offset, out_count - out_offset,
  361. "\nQP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
  362. "Up" : "Down");
  363. if (out_offset > out_count)
  364. out_offset = out_count;
  365. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  366. kfree(buf);
  367. return ret;
  368. }
  369. static const struct file_operations ntb_qp_debugfs_stats = {
  370. .owner = THIS_MODULE,
  371. .open = simple_open,
  372. .read = debugfs_read,
  373. };
  374. static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
  375. struct list_head *list)
  376. {
  377. unsigned long flags;
  378. spin_lock_irqsave(lock, flags);
  379. list_add_tail(entry, list);
  380. spin_unlock_irqrestore(lock, flags);
  381. }
  382. static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
  383. struct list_head *list)
  384. {
  385. struct ntb_queue_entry *entry;
  386. unsigned long flags;
  387. spin_lock_irqsave(lock, flags);
  388. if (list_empty(list)) {
  389. entry = NULL;
  390. goto out;
  391. }
  392. entry = list_first_entry(list, struct ntb_queue_entry, entry);
  393. list_del(&entry->entry);
  394. out:
  395. spin_unlock_irqrestore(lock, flags);
  396. return entry;
  397. }
  398. static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
  399. unsigned int qp_num)
  400. {
  401. struct ntb_transport_qp *qp = &nt->qps[qp_num];
  402. unsigned int rx_size, num_qps_mw;
  403. u8 mw_num = QP_TO_MW(qp_num);
  404. unsigned int i;
  405. WARN_ON(nt->mw[mw_num].virt_addr == 0);
  406. if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
  407. num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
  408. else
  409. num_qps_mw = nt->max_qps / NTB_NUM_MW;
  410. rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
  411. qp->remote_rx_info = nt->mw[mw_num].virt_addr +
  412. (qp_num / NTB_NUM_MW * rx_size);
  413. rx_size -= sizeof(struct ntb_rx_info);
  414. qp->rx_buff = qp->remote_rx_info + sizeof(struct ntb_rx_info);
  415. qp->rx_max_frame = min(transport_mtu, rx_size);
  416. qp->rx_max_entry = rx_size / qp->rx_max_frame;
  417. qp->rx_index = 0;
  418. qp->remote_rx_info->entry = qp->rx_max_entry;
  419. /* setup the hdr offsets with 0's */
  420. for (i = 0; i < qp->rx_max_entry; i++) {
  421. void *offset = qp->rx_buff + qp->rx_max_frame * (i + 1) -
  422. sizeof(struct ntb_payload_header);
  423. memset(offset, 0, sizeof(struct ntb_payload_header));
  424. }
  425. qp->rx_pkts = 0;
  426. qp->tx_pkts = 0;
  427. }
  428. static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
  429. {
  430. struct ntb_transport_mw *mw = &nt->mw[num_mw];
  431. struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
  432. /* Alloc memory for receiving data. Must be 4k aligned */
  433. mw->size = ALIGN(size, 4096);
  434. mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
  435. GFP_KERNEL);
  436. if (!mw->virt_addr) {
  437. dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
  438. (int) mw->size);
  439. return -ENOMEM;
  440. }
  441. /* Notify HW the memory location of the receive buffer */
  442. ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
  443. return 0;
  444. }
  445. static void ntb_qp_link_cleanup(struct work_struct *work)
  446. {
  447. struct ntb_transport_qp *qp = container_of(work,
  448. struct ntb_transport_qp,
  449. link_cleanup);
  450. struct ntb_transport *nt = qp->transport;
  451. struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
  452. if (qp->qp_link == NTB_LINK_DOWN) {
  453. cancel_delayed_work_sync(&qp->link_work);
  454. return;
  455. }
  456. if (qp->event_handler)
  457. qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
  458. dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
  459. qp->qp_link = NTB_LINK_DOWN;
  460. if (nt->transport_link == NTB_LINK_UP)
  461. schedule_delayed_work(&qp->link_work,
  462. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  463. }
  464. static void ntb_qp_link_down(struct ntb_transport_qp *qp)
  465. {
  466. schedule_work(&qp->link_cleanup);
  467. }
  468. static void ntb_transport_link_cleanup(struct work_struct *work)
  469. {
  470. struct ntb_transport *nt = container_of(work, struct ntb_transport,
  471. link_cleanup);
  472. int i;
  473. if (nt->transport_link == NTB_LINK_DOWN)
  474. cancel_delayed_work_sync(&nt->link_work);
  475. else
  476. nt->transport_link = NTB_LINK_DOWN;
  477. /* Pass along the info to any clients */
  478. for (i = 0; i < nt->max_qps; i++)
  479. if (!test_bit(i, &nt->qp_bitmap))
  480. ntb_qp_link_down(&nt->qps[i]);
  481. /* The scratchpad registers keep the values if the remote side
  482. * goes down, blast them now to give them a sane value the next
  483. * time they are accessed
  484. */
  485. for (i = 0; i < MAX_SPAD; i++)
  486. ntb_write_local_spad(nt->ndev, i, 0);
  487. }
  488. static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
  489. {
  490. struct ntb_transport *nt = data;
  491. switch (event) {
  492. case NTB_EVENT_HW_LINK_UP:
  493. schedule_delayed_work(&nt->link_work, 0);
  494. break;
  495. case NTB_EVENT_HW_LINK_DOWN:
  496. schedule_work(&nt->link_cleanup);
  497. break;
  498. default:
  499. BUG();
  500. }
  501. }
  502. static void ntb_transport_link_work(struct work_struct *work)
  503. {
  504. struct ntb_transport *nt = container_of(work, struct ntb_transport,
  505. link_work.work);
  506. struct ntb_device *ndev = nt->ndev;
  507. struct pci_dev *pdev = ntb_query_pdev(ndev);
  508. u32 val;
  509. int rc, i;
  510. /* send the local info */
  511. rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
  512. if (rc) {
  513. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  514. 0, VERSION);
  515. goto out;
  516. }
  517. rc = ntb_write_remote_spad(ndev, MW0_SZ, ntb_get_mw_size(ndev, 0));
  518. if (rc) {
  519. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  520. (u32) ntb_get_mw_size(ndev, 0), MW0_SZ);
  521. goto out;
  522. }
  523. rc = ntb_write_remote_spad(ndev, MW1_SZ, ntb_get_mw_size(ndev, 1));
  524. if (rc) {
  525. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  526. (u32) ntb_get_mw_size(ndev, 1), MW1_SZ);
  527. goto out;
  528. }
  529. rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
  530. if (rc) {
  531. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  532. nt->max_qps, NUM_QPS);
  533. goto out;
  534. }
  535. rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
  536. if (rc) {
  537. dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
  538. goto out;
  539. }
  540. rc = ntb_write_remote_spad(ndev, QP_LINKS, val);
  541. if (rc) {
  542. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  543. val, QP_LINKS);
  544. goto out;
  545. }
  546. /* Query the remote side for its info */
  547. rc = ntb_read_remote_spad(ndev, VERSION, &val);
  548. if (rc) {
  549. dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
  550. goto out;
  551. }
  552. if (val != NTB_TRANSPORT_VERSION)
  553. goto out;
  554. dev_dbg(&pdev->dev, "Remote version = %d\n", val);
  555. rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
  556. if (rc) {
  557. dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
  558. goto out;
  559. }
  560. if (val != nt->max_qps)
  561. goto out;
  562. dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
  563. rc = ntb_read_remote_spad(ndev, MW0_SZ, &val);
  564. if (rc) {
  565. dev_err(&pdev->dev, "Error reading remote spad %d\n", MW0_SZ);
  566. goto out;
  567. }
  568. if (!val)
  569. goto out;
  570. dev_dbg(&pdev->dev, "Remote MW0 size = %d\n", val);
  571. rc = ntb_set_mw(nt, 0, val);
  572. if (rc)
  573. goto out;
  574. rc = ntb_read_remote_spad(ndev, MW1_SZ, &val);
  575. if (rc) {
  576. dev_err(&pdev->dev, "Error reading remote spad %d\n", MW1_SZ);
  577. goto out;
  578. }
  579. if (!val)
  580. goto out;
  581. dev_dbg(&pdev->dev, "Remote MW1 size = %d\n", val);
  582. rc = ntb_set_mw(nt, 1, val);
  583. if (rc)
  584. goto out;
  585. nt->transport_link = NTB_LINK_UP;
  586. for (i = 0; i < nt->max_qps; i++) {
  587. struct ntb_transport_qp *qp = &nt->qps[i];
  588. ntb_transport_setup_qp_mw(nt, i);
  589. if (qp->client_ready == NTB_LINK_UP)
  590. schedule_delayed_work(&qp->link_work, 0);
  591. }
  592. return;
  593. out:
  594. if (ntb_hw_link_status(ndev))
  595. schedule_delayed_work(&nt->link_work,
  596. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  597. }
  598. static void ntb_qp_link_work(struct work_struct *work)
  599. {
  600. struct ntb_transport_qp *qp = container_of(work,
  601. struct ntb_transport_qp,
  602. link_work.work);
  603. struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
  604. struct ntb_transport *nt = qp->transport;
  605. int rc, val;
  606. WARN_ON(nt->transport_link != NTB_LINK_UP);
  607. rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
  608. if (rc) {
  609. dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
  610. return;
  611. }
  612. rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
  613. if (rc)
  614. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  615. val | 1 << qp->qp_num, QP_LINKS);
  616. /* query remote spad for qp ready bits */
  617. rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
  618. if (rc)
  619. dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
  620. dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
  621. /* See if the remote side is up */
  622. if (1 << qp->qp_num & val) {
  623. qp->qp_link = NTB_LINK_UP;
  624. dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
  625. if (qp->event_handler)
  626. qp->event_handler(qp->cb_data, NTB_LINK_UP);
  627. } else if (nt->transport_link == NTB_LINK_UP)
  628. schedule_delayed_work(&qp->link_work,
  629. msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
  630. }
  631. static void ntb_transport_init_queue(struct ntb_transport *nt,
  632. unsigned int qp_num)
  633. {
  634. struct ntb_transport_qp *qp;
  635. unsigned int num_qps_mw, tx_size;
  636. u8 mw_num = QP_TO_MW(qp_num);
  637. qp = &nt->qps[qp_num];
  638. qp->qp_num = qp_num;
  639. qp->transport = nt;
  640. qp->ndev = nt->ndev;
  641. qp->qp_link = NTB_LINK_DOWN;
  642. qp->client_ready = NTB_LINK_DOWN;
  643. qp->event_handler = NULL;
  644. if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
  645. num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
  646. else
  647. num_qps_mw = nt->max_qps / NTB_NUM_MW;
  648. tx_size = (unsigned int) ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
  649. qp->rx_info = ntb_get_mw_vbase(nt->ndev, mw_num) +
  650. (qp_num / NTB_NUM_MW * tx_size);
  651. tx_size -= sizeof(struct ntb_rx_info);
  652. qp->tx_mw = qp->rx_info + sizeof(struct ntb_rx_info);
  653. qp->tx_max_frame = min(transport_mtu, tx_size);
  654. qp->tx_max_entry = tx_size / qp->tx_max_frame;
  655. qp->tx_index = 0;
  656. if (nt->debugfs_dir) {
  657. char debugfs_name[4];
  658. snprintf(debugfs_name, 4, "qp%d", qp_num);
  659. qp->debugfs_dir = debugfs_create_dir(debugfs_name,
  660. nt->debugfs_dir);
  661. qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
  662. qp->debugfs_dir, qp,
  663. &ntb_qp_debugfs_stats);
  664. }
  665. INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
  666. INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup);
  667. spin_lock_init(&qp->ntb_rx_pend_q_lock);
  668. spin_lock_init(&qp->ntb_rx_free_q_lock);
  669. spin_lock_init(&qp->ntb_tx_free_q_lock);
  670. INIT_LIST_HEAD(&qp->rx_pend_q);
  671. INIT_LIST_HEAD(&qp->rx_free_q);
  672. INIT_LIST_HEAD(&qp->tx_free_q);
  673. }
  674. int ntb_transport_init(struct pci_dev *pdev)
  675. {
  676. struct ntb_transport *nt;
  677. int rc, i;
  678. nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
  679. if (!nt)
  680. return -ENOMEM;
  681. if (debugfs_initialized())
  682. nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  683. else
  684. nt->debugfs_dir = NULL;
  685. nt->ndev = ntb_register_transport(pdev, nt);
  686. if (!nt->ndev) {
  687. rc = -EIO;
  688. goto err;
  689. }
  690. nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
  691. nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
  692. GFP_KERNEL);
  693. if (!nt->qps) {
  694. rc = -ENOMEM;
  695. goto err1;
  696. }
  697. nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
  698. for (i = 0; i < nt->max_qps; i++)
  699. ntb_transport_init_queue(nt, i);
  700. INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
  701. INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup);
  702. rc = ntb_register_event_callback(nt->ndev,
  703. ntb_transport_event_callback);
  704. if (rc)
  705. goto err2;
  706. INIT_LIST_HEAD(&nt->client_devs);
  707. rc = ntb_bus_init(nt);
  708. if (rc)
  709. goto err3;
  710. if (ntb_hw_link_status(nt->ndev))
  711. schedule_delayed_work(&nt->link_work, 0);
  712. return 0;
  713. err3:
  714. ntb_unregister_event_callback(nt->ndev);
  715. err2:
  716. kfree(nt->qps);
  717. err1:
  718. ntb_unregister_transport(nt->ndev);
  719. err:
  720. debugfs_remove_recursive(nt->debugfs_dir);
  721. kfree(nt);
  722. return rc;
  723. }
  724. void ntb_transport_free(void *transport)
  725. {
  726. struct ntb_transport *nt = transport;
  727. struct pci_dev *pdev;
  728. int i;
  729. nt->transport_link = NTB_LINK_DOWN;
  730. /* verify that all the qp's are freed */
  731. for (i = 0; i < nt->max_qps; i++)
  732. if (!test_bit(i, &nt->qp_bitmap))
  733. ntb_transport_free_queue(&nt->qps[i]);
  734. ntb_bus_remove(nt);
  735. cancel_delayed_work_sync(&nt->link_work);
  736. debugfs_remove_recursive(nt->debugfs_dir);
  737. ntb_unregister_event_callback(nt->ndev);
  738. pdev = ntb_query_pdev(nt->ndev);
  739. for (i = 0; i < NTB_NUM_MW; i++)
  740. if (nt->mw[i].virt_addr)
  741. dma_free_coherent(&pdev->dev, nt->mw[i].size,
  742. nt->mw[i].virt_addr,
  743. nt->mw[i].dma_addr);
  744. kfree(nt->qps);
  745. ntb_unregister_transport(nt->ndev);
  746. kfree(nt);
  747. }
  748. static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
  749. struct ntb_queue_entry *entry, void *offset)
  750. {
  751. memcpy(entry->buf, offset, entry->len);
  752. if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
  753. qp->rx_handler(qp, qp->cb_data, entry->cb_data, entry->len);
  754. ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
  755. }
  756. static int ntb_process_rxc(struct ntb_transport_qp *qp)
  757. {
  758. struct ntb_payload_header *hdr;
  759. struct ntb_queue_entry *entry;
  760. void *offset;
  761. offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
  762. hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
  763. entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
  764. if (!entry) {
  765. dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
  766. "no buffer - HDR ver %llu, len %d, flags %x\n",
  767. hdr->ver, hdr->len, hdr->flags);
  768. qp->rx_err_no_buf++;
  769. return -ENOMEM;
  770. }
  771. if (!(hdr->flags & DESC_DONE_FLAG)) {
  772. ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
  773. &qp->rx_pend_q);
  774. qp->rx_ring_empty++;
  775. return -EAGAIN;
  776. }
  777. if (hdr->ver != qp->rx_pkts) {
  778. dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
  779. "qp %d: version mismatch, expected %llu - got %llu\n",
  780. qp->qp_num, qp->rx_pkts, hdr->ver);
  781. ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
  782. &qp->rx_pend_q);
  783. qp->rx_err_ver++;
  784. return -EIO;
  785. }
  786. if (hdr->flags & LINK_DOWN_FLAG) {
  787. ntb_qp_link_down(qp);
  788. ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
  789. &qp->rx_pend_q);
  790. goto out;
  791. }
  792. dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
  793. "rx offset %u, ver %llu - %d payload received, buf size %d\n",
  794. qp->rx_index, hdr->ver, hdr->len, entry->len);
  795. if (hdr->len <= entry->len) {
  796. entry->len = hdr->len;
  797. ntb_rx_copy_task(qp, entry, offset);
  798. } else {
  799. ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
  800. &qp->rx_pend_q);
  801. qp->rx_err_oflow++;
  802. dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
  803. "RX overflow! Wanted %d got %d\n",
  804. hdr->len, entry->len);
  805. }
  806. qp->rx_bytes += hdr->len;
  807. qp->rx_pkts++;
  808. out:
  809. /* Ensure that the data is fully copied out before clearing the flag */
  810. wmb();
  811. hdr->flags = 0;
  812. qp->rx_info->entry = qp->rx_index;
  813. qp->rx_index++;
  814. qp->rx_index %= qp->rx_max_entry;
  815. return 0;
  816. }
  817. static void ntb_transport_rx(unsigned long data)
  818. {
  819. struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
  820. int rc;
  821. do {
  822. rc = ntb_process_rxc(qp);
  823. } while (!rc);
  824. }
  825. static void ntb_transport_rxc_db(void *data, int db_num)
  826. {
  827. struct ntb_transport_qp *qp = data;
  828. dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
  829. __func__, db_num);
  830. tasklet_schedule(&qp->rx_work);
  831. }
  832. static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
  833. struct ntb_queue_entry *entry,
  834. void *offset)
  835. {
  836. struct ntb_payload_header *hdr;
  837. memcpy_toio(offset, entry->buf, entry->len);
  838. hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
  839. hdr->len = entry->len;
  840. hdr->ver = qp->tx_pkts;
  841. /* Ensure that the data is fully copied out before setting the flag */
  842. wmb();
  843. hdr->flags = entry->flags | DESC_DONE_FLAG;
  844. ntb_ring_sdb(qp->ndev, qp->qp_num);
  845. /* The entry length can only be zero if the packet is intended to be a
  846. * "link down" or similar. Since no payload is being sent in these
  847. * cases, there is nothing to add to the completion queue.
  848. */
  849. if (entry->len > 0) {
  850. qp->tx_bytes += entry->len;
  851. if (qp->tx_handler)
  852. qp->tx_handler(qp, qp->cb_data, entry->cb_data,
  853. entry->len);
  854. }
  855. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
  856. }
  857. static int ntb_process_tx(struct ntb_transport_qp *qp,
  858. struct ntb_queue_entry *entry)
  859. {
  860. void *offset;
  861. offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
  862. dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %u, entry len %d flags %x buff %p\n",
  863. qp->tx_pkts, offset, qp->tx_index, entry->len, entry->flags,
  864. entry->buf);
  865. if (qp->tx_index == qp->remote_rx_info->entry) {
  866. qp->tx_ring_full++;
  867. return -EAGAIN;
  868. }
  869. if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
  870. if (qp->tx_handler)
  871. qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
  872. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  873. &qp->tx_free_q);
  874. return 0;
  875. }
  876. ntb_tx_copy_task(qp, entry, offset);
  877. qp->tx_index++;
  878. qp->tx_index %= qp->tx_max_entry;
  879. qp->tx_pkts++;
  880. return 0;
  881. }
  882. static void ntb_send_link_down(struct ntb_transport_qp *qp)
  883. {
  884. struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
  885. struct ntb_queue_entry *entry;
  886. int i, rc;
  887. if (qp->qp_link == NTB_LINK_DOWN)
  888. return;
  889. qp->qp_link = NTB_LINK_DOWN;
  890. dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
  891. for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
  892. entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
  893. if (entry)
  894. break;
  895. msleep(100);
  896. }
  897. if (!entry)
  898. return;
  899. entry->cb_data = NULL;
  900. entry->buf = NULL;
  901. entry->len = 0;
  902. entry->flags = LINK_DOWN_FLAG;
  903. rc = ntb_process_tx(qp, entry);
  904. if (rc)
  905. dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
  906. qp->qp_num);
  907. }
  908. /**
  909. * ntb_transport_create_queue - Create a new NTB transport layer queue
  910. * @rx_handler: receive callback function
  911. * @tx_handler: transmit callback function
  912. * @event_handler: event callback function
  913. *
  914. * Create a new NTB transport layer queue and provide the queue with a callback
  915. * routine for both transmit and receive. The receive callback routine will be
  916. * used to pass up data when the transport has received it on the queue. The
  917. * transmit callback routine will be called when the transport has completed the
  918. * transmission of the data on the queue and the data is ready to be freed.
  919. *
  920. * RETURNS: pointer to newly created ntb_queue, NULL on error.
  921. */
  922. struct ntb_transport_qp *
  923. ntb_transport_create_queue(void *data, struct pci_dev *pdev,
  924. const struct ntb_queue_handlers *handlers)
  925. {
  926. struct ntb_queue_entry *entry;
  927. struct ntb_transport_qp *qp;
  928. struct ntb_transport *nt;
  929. unsigned int free_queue;
  930. int rc, i;
  931. nt = ntb_find_transport(pdev);
  932. if (!nt)
  933. goto err;
  934. free_queue = ffs(nt->qp_bitmap);
  935. if (!free_queue)
  936. goto err;
  937. /* decrement free_queue to make it zero based */
  938. free_queue--;
  939. clear_bit(free_queue, &nt->qp_bitmap);
  940. qp = &nt->qps[free_queue];
  941. qp->cb_data = data;
  942. qp->rx_handler = handlers->rx_handler;
  943. qp->tx_handler = handlers->tx_handler;
  944. qp->event_handler = handlers->event_handler;
  945. for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
  946. entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
  947. if (!entry)
  948. goto err1;
  949. ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
  950. &qp->rx_free_q);
  951. }
  952. for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
  953. entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
  954. if (!entry)
  955. goto err2;
  956. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  957. &qp->tx_free_q);
  958. }
  959. tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
  960. rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
  961. ntb_transport_rxc_db);
  962. if (rc)
  963. goto err3;
  964. dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
  965. return qp;
  966. err3:
  967. tasklet_disable(&qp->rx_work);
  968. err2:
  969. while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
  970. kfree(entry);
  971. err1:
  972. while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
  973. kfree(entry);
  974. set_bit(free_queue, &nt->qp_bitmap);
  975. err:
  976. return NULL;
  977. }
  978. EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
  979. /**
  980. * ntb_transport_free_queue - Frees NTB transport queue
  981. * @qp: NTB queue to be freed
  982. *
  983. * Frees NTB transport queue
  984. */
  985. void ntb_transport_free_queue(struct ntb_transport_qp *qp)
  986. {
  987. struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
  988. struct ntb_queue_entry *entry;
  989. if (!qp)
  990. return;
  991. cancel_delayed_work_sync(&qp->link_work);
  992. ntb_unregister_db_callback(qp->ndev, qp->qp_num);
  993. tasklet_disable(&qp->rx_work);
  994. while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
  995. kfree(entry);
  996. while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
  997. dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
  998. kfree(entry);
  999. }
  1000. while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
  1001. kfree(entry);
  1002. set_bit(qp->qp_num, &qp->transport->qp_bitmap);
  1003. dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
  1004. }
  1005. EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
  1006. /**
  1007. * ntb_transport_rx_remove - Dequeues enqueued rx packet
  1008. * @qp: NTB queue to be freed
  1009. * @len: pointer to variable to write enqueued buffers length
  1010. *
  1011. * Dequeues unused buffers from receive queue. Should only be used during
  1012. * shutdown of qp.
  1013. *
  1014. * RETURNS: NULL error value on error, or void* for success.
  1015. */
  1016. void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
  1017. {
  1018. struct ntb_queue_entry *entry;
  1019. void *buf;
  1020. if (!qp || qp->client_ready == NTB_LINK_UP)
  1021. return NULL;
  1022. entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
  1023. if (!entry)
  1024. return NULL;
  1025. buf = entry->cb_data;
  1026. *len = entry->len;
  1027. ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
  1028. return buf;
  1029. }
  1030. EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
  1031. /**
  1032. * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
  1033. * @qp: NTB transport layer queue the entry is to be enqueued on
  1034. * @cb: per buffer pointer for callback function to use
  1035. * @data: pointer to data buffer that incoming packets will be copied into
  1036. * @len: length of the data buffer
  1037. *
  1038. * Enqueue a new receive buffer onto the transport queue into which a NTB
  1039. * payload can be received into.
  1040. *
  1041. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1042. */
  1043. int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
  1044. unsigned int len)
  1045. {
  1046. struct ntb_queue_entry *entry;
  1047. if (!qp)
  1048. return -EINVAL;
  1049. entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
  1050. if (!entry)
  1051. return -ENOMEM;
  1052. entry->cb_data = cb;
  1053. entry->buf = data;
  1054. entry->len = len;
  1055. ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
  1059. /**
  1060. * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
  1061. * @qp: NTB transport layer queue the entry is to be enqueued on
  1062. * @cb: per buffer pointer for callback function to use
  1063. * @data: pointer to data buffer that will be sent
  1064. * @len: length of the data buffer
  1065. *
  1066. * Enqueue a new transmit buffer onto the transport queue from which a NTB
  1067. * payload will be transmitted. This assumes that a lock is behing held to
  1068. * serialize access to the qp.
  1069. *
  1070. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  1071. */
  1072. int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
  1073. unsigned int len)
  1074. {
  1075. struct ntb_queue_entry *entry;
  1076. int rc;
  1077. if (!qp || qp->qp_link != NTB_LINK_UP || !len)
  1078. return -EINVAL;
  1079. entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
  1080. if (!entry)
  1081. return -ENOMEM;
  1082. entry->cb_data = cb;
  1083. entry->buf = data;
  1084. entry->len = len;
  1085. entry->flags = 0;
  1086. rc = ntb_process_tx(qp, entry);
  1087. if (rc)
  1088. ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
  1089. &qp->tx_free_q);
  1090. return rc;
  1091. }
  1092. EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
  1093. /**
  1094. * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
  1095. * @qp: NTB transport layer queue to be enabled
  1096. *
  1097. * Notify NTB transport layer of client readiness to use queue
  1098. */
  1099. void ntb_transport_link_up(struct ntb_transport_qp *qp)
  1100. {
  1101. if (!qp)
  1102. return;
  1103. qp->client_ready = NTB_LINK_UP;
  1104. if (qp->transport->transport_link == NTB_LINK_UP)
  1105. schedule_delayed_work(&qp->link_work, 0);
  1106. }
  1107. EXPORT_SYMBOL_GPL(ntb_transport_link_up);
  1108. /**
  1109. * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
  1110. * @qp: NTB transport layer queue to be disabled
  1111. *
  1112. * Notify NTB transport layer of client's desire to no longer receive data on
  1113. * transport queue specified. It is the client's responsibility to ensure all
  1114. * entries on queue are purged or otherwise handled appropraitely.
  1115. */
  1116. void ntb_transport_link_down(struct ntb_transport_qp *qp)
  1117. {
  1118. struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
  1119. int rc, val;
  1120. if (!qp)
  1121. return;
  1122. qp->client_ready = NTB_LINK_DOWN;
  1123. rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
  1124. if (rc) {
  1125. dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
  1126. return;
  1127. }
  1128. rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
  1129. val & ~(1 << qp->qp_num));
  1130. if (rc)
  1131. dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
  1132. val & ~(1 << qp->qp_num), QP_LINKS);
  1133. if (qp->qp_link == NTB_LINK_UP)
  1134. ntb_send_link_down(qp);
  1135. else
  1136. cancel_delayed_work_sync(&qp->link_work);
  1137. }
  1138. EXPORT_SYMBOL_GPL(ntb_transport_link_down);
  1139. /**
  1140. * ntb_transport_link_query - Query transport link state
  1141. * @qp: NTB transport layer queue to be queried
  1142. *
  1143. * Query connectivity to the remote system of the NTB transport queue
  1144. *
  1145. * RETURNS: true for link up or false for link down
  1146. */
  1147. bool ntb_transport_link_query(struct ntb_transport_qp *qp)
  1148. {
  1149. return qp->qp_link == NTB_LINK_UP;
  1150. }
  1151. EXPORT_SYMBOL_GPL(ntb_transport_link_query);
  1152. /**
  1153. * ntb_transport_qp_num - Query the qp number
  1154. * @qp: NTB transport layer queue to be queried
  1155. *
  1156. * Query qp number of the NTB transport queue
  1157. *
  1158. * RETURNS: a zero based number specifying the qp number
  1159. */
  1160. unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
  1161. {
  1162. return qp->qp_num;
  1163. }
  1164. EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
  1165. /**
  1166. * ntb_transport_max_size - Query the max payload size of a qp
  1167. * @qp: NTB transport layer queue to be queried
  1168. *
  1169. * Query the maximum payload size permissible on the given qp
  1170. *
  1171. * RETURNS: the max payload size of a qp
  1172. */
  1173. unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
  1174. {
  1175. return qp->tx_max_frame - sizeof(struct ntb_payload_header);
  1176. }
  1177. EXPORT_SYMBOL_GPL(ntb_transport_max_size);