rio_cm.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * rio_cm - RapidIO Channelized Messaging Driver
  4. *
  5. * Copyright 2013-2016 Integrated Device Technology, Inc.
  6. * Copyright (c) 2015, Prodrive Technologies
  7. * Copyright (c) 2015, RapidIO Trade Association
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/rio.h>
  15. #include <linux/rio_drv.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/cdev.h>
  20. #include <linux/fs.h>
  21. #include <linux/poll.h>
  22. #include <linux/reboot.h>
  23. #include <linux/bitops.h>
  24. #include <linux/printk.h>
  25. #include <linux/rio_cm_cdev.h>
  26. #define DRV_NAME "rio_cm"
  27. #define DRV_VERSION "1.0.0"
  28. #define DRV_AUTHOR "Alexandre Bounine <alexandre.bounine@idt.com>"
  29. #define DRV_DESC "RapidIO Channelized Messaging Driver"
  30. #define DEV_NAME "rio_cm"
  31. /* Debug output filtering masks */
  32. enum {
  33. DBG_NONE = 0,
  34. DBG_INIT = BIT(0), /* driver init */
  35. DBG_EXIT = BIT(1), /* driver exit */
  36. DBG_MPORT = BIT(2), /* mport add/remove */
  37. DBG_RDEV = BIT(3), /* RapidIO device add/remove */
  38. DBG_CHOP = BIT(4), /* channel operations */
  39. DBG_WAIT = BIT(5), /* waiting for events */
  40. DBG_TX = BIT(6), /* message TX */
  41. DBG_TX_EVENT = BIT(7), /* message TX event */
  42. DBG_RX_DATA = BIT(8), /* inbound data messages */
  43. DBG_RX_CMD = BIT(9), /* inbound REQ/ACK/NACK messages */
  44. DBG_ALL = ~0,
  45. };
  46. #ifdef DEBUG
  47. #define riocm_debug(level, fmt, arg...) \
  48. do { \
  49. if (DBG_##level & dbg_level) \
  50. pr_debug(DRV_NAME ": %s " fmt "\n", \
  51. __func__, ##arg); \
  52. } while (0)
  53. #else
  54. #define riocm_debug(level, fmt, arg...) \
  55. no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg)
  56. #endif
  57. #define riocm_warn(fmt, arg...) \
  58. pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg)
  59. #define riocm_error(fmt, arg...) \
  60. pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg)
  61. static int cmbox = 1;
  62. module_param(cmbox, int, S_IRUGO);
  63. MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)");
  64. static int chstart = 256;
  65. module_param(chstart, int, S_IRUGO);
  66. MODULE_PARM_DESC(chstart,
  67. "Start channel number for dynamic allocation (default 256)");
  68. #ifdef DEBUG
  69. static u32 dbg_level = DBG_NONE;
  70. module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
  71. MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
  72. #endif
  73. MODULE_AUTHOR(DRV_AUTHOR);
  74. MODULE_DESCRIPTION(DRV_DESC);
  75. MODULE_LICENSE("GPL");
  76. MODULE_VERSION(DRV_VERSION);
  77. #define RIOCM_TX_RING_SIZE 128
  78. #define RIOCM_RX_RING_SIZE 128
  79. #define RIOCM_CONNECT_TO 3 /* connect response TO (in sec) */
  80. #define RIOCM_MAX_CHNUM 0xffff /* Use full range of u16 field */
  81. #define RIOCM_CHNUM_AUTO 0
  82. #define RIOCM_MAX_EP_COUNT 0x10000 /* Max number of endpoints */
  83. enum rio_cm_state {
  84. RIO_CM_IDLE,
  85. RIO_CM_CONNECT,
  86. RIO_CM_CONNECTED,
  87. RIO_CM_DISCONNECT,
  88. RIO_CM_CHAN_BOUND,
  89. RIO_CM_LISTEN,
  90. RIO_CM_DESTROYING,
  91. };
  92. enum rio_cm_pkt_type {
  93. RIO_CM_SYS = 0xaa,
  94. RIO_CM_CHAN = 0x55,
  95. };
  96. enum rio_cm_chop {
  97. CM_CONN_REQ,
  98. CM_CONN_ACK,
  99. CM_CONN_CLOSE,
  100. CM_DATA_MSG,
  101. };
  102. struct rio_ch_base_bhdr {
  103. u32 src_id;
  104. u32 dst_id;
  105. #define RIO_HDR_LETTER_MASK 0xffff0000
  106. #define RIO_HDR_MBOX_MASK 0x0000ffff
  107. u8 src_mbox;
  108. u8 dst_mbox;
  109. u8 type;
  110. } __attribute__((__packed__));
  111. struct rio_ch_chan_hdr {
  112. struct rio_ch_base_bhdr bhdr;
  113. u8 ch_op;
  114. u16 dst_ch;
  115. u16 src_ch;
  116. u16 msg_len;
  117. u16 rsrvd;
  118. } __attribute__((__packed__));
  119. struct tx_req {
  120. struct list_head node;
  121. struct rio_dev *rdev;
  122. void *buffer;
  123. size_t len;
  124. };
  125. struct cm_dev {
  126. struct list_head list;
  127. struct rio_mport *mport;
  128. void *rx_buf[RIOCM_RX_RING_SIZE];
  129. int rx_slots;
  130. struct mutex rx_lock;
  131. void *tx_buf[RIOCM_TX_RING_SIZE];
  132. int tx_slot;
  133. int tx_cnt;
  134. int tx_ack_slot;
  135. struct list_head tx_reqs;
  136. spinlock_t tx_lock;
  137. struct list_head peers;
  138. u32 npeers;
  139. struct workqueue_struct *rx_wq;
  140. struct work_struct rx_work;
  141. };
  142. struct chan_rx_ring {
  143. void *buf[RIOCM_RX_RING_SIZE];
  144. int head;
  145. int tail;
  146. int count;
  147. /* Tracking RX buffers reported to upper level */
  148. void *inuse[RIOCM_RX_RING_SIZE];
  149. int inuse_cnt;
  150. };
  151. struct rio_channel {
  152. u16 id; /* local channel ID */
  153. struct kref ref; /* channel refcount */
  154. struct file *filp;
  155. struct cm_dev *cmdev; /* associated CM device object */
  156. struct rio_dev *rdev; /* remote RapidIO device */
  157. enum rio_cm_state state;
  158. int error;
  159. spinlock_t lock;
  160. void *context;
  161. u32 loc_destid; /* local destID */
  162. u32 rem_destid; /* remote destID */
  163. u16 rem_channel; /* remote channel ID */
  164. struct list_head accept_queue;
  165. struct list_head ch_node;
  166. struct completion comp;
  167. struct completion comp_close;
  168. struct chan_rx_ring rx_ring;
  169. };
  170. struct cm_peer {
  171. struct list_head node;
  172. struct rio_dev *rdev;
  173. };
  174. struct rio_cm_work {
  175. struct work_struct work;
  176. struct cm_dev *cm;
  177. void *data;
  178. };
  179. struct conn_req {
  180. struct list_head node;
  181. u32 destid; /* requester destID */
  182. u16 chan; /* requester channel ID */
  183. struct cm_dev *cmdev;
  184. };
  185. /*
  186. * A channel_dev structure represents a CM_CDEV
  187. * @cdev Character device
  188. * @dev Associated device object
  189. */
  190. struct channel_dev {
  191. struct cdev cdev;
  192. struct device *dev;
  193. };
  194. static struct rio_channel *riocm_ch_alloc(u16 ch_num);
  195. static void riocm_ch_free(struct kref *ref);
  196. static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
  197. void *buffer, size_t len);
  198. static int riocm_ch_close(struct rio_channel *ch);
  199. static DEFINE_SPINLOCK(idr_lock);
  200. static DEFINE_IDR(ch_idr);
  201. static LIST_HEAD(cm_dev_list);
  202. static DECLARE_RWSEM(rdev_sem);
  203. static const struct class dev_class = {
  204. .name = DRV_NAME,
  205. };
  206. static unsigned int dev_major;
  207. static unsigned int dev_minor_base;
  208. static dev_t dev_number;
  209. static struct channel_dev riocm_cdev;
  210. #define is_msg_capable(src_ops, dst_ops) \
  211. ((src_ops & RIO_SRC_OPS_DATA_MSG) && \
  212. (dst_ops & RIO_DST_OPS_DATA_MSG))
  213. #define dev_cm_capable(dev) \
  214. is_msg_capable(dev->src_ops, dev->dst_ops)
  215. static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp)
  216. {
  217. int ret;
  218. spin_lock_bh(&ch->lock);
  219. ret = (ch->state == cmp);
  220. spin_unlock_bh(&ch->lock);
  221. return ret;
  222. }
  223. static int riocm_cmp_exch(struct rio_channel *ch,
  224. enum rio_cm_state cmp, enum rio_cm_state exch)
  225. {
  226. int ret;
  227. spin_lock_bh(&ch->lock);
  228. ret = (ch->state == cmp);
  229. if (ret)
  230. ch->state = exch;
  231. spin_unlock_bh(&ch->lock);
  232. return ret;
  233. }
  234. static enum rio_cm_state riocm_exch(struct rio_channel *ch,
  235. enum rio_cm_state exch)
  236. {
  237. enum rio_cm_state old;
  238. spin_lock_bh(&ch->lock);
  239. old = ch->state;
  240. ch->state = exch;
  241. spin_unlock_bh(&ch->lock);
  242. return old;
  243. }
  244. static struct rio_channel *riocm_get_channel(u16 nr)
  245. {
  246. struct rio_channel *ch;
  247. spin_lock_bh(&idr_lock);
  248. ch = idr_find(&ch_idr, nr);
  249. if (ch)
  250. kref_get(&ch->ref);
  251. spin_unlock_bh(&idr_lock);
  252. return ch;
  253. }
  254. static void riocm_put_channel(struct rio_channel *ch)
  255. {
  256. kref_put(&ch->ref, riocm_ch_free);
  257. }
  258. static void *riocm_rx_get_msg(struct cm_dev *cm)
  259. {
  260. void *msg;
  261. int i;
  262. msg = rio_get_inb_message(cm->mport, cmbox);
  263. if (msg) {
  264. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  265. if (cm->rx_buf[i] == msg) {
  266. cm->rx_buf[i] = NULL;
  267. cm->rx_slots++;
  268. break;
  269. }
  270. }
  271. if (i == RIOCM_RX_RING_SIZE)
  272. riocm_warn("no record for buffer 0x%p", msg);
  273. }
  274. return msg;
  275. }
  276. /*
  277. * riocm_rx_fill - fills a ring of receive buffers for given cm device
  278. * @cm: cm_dev object
  279. * @nent: max number of entries to fill
  280. *
  281. * Returns: none
  282. */
  283. static void riocm_rx_fill(struct cm_dev *cm, int nent)
  284. {
  285. int i;
  286. if (cm->rx_slots == 0)
  287. return;
  288. for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) {
  289. if (cm->rx_buf[i] == NULL) {
  290. cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL);
  291. if (cm->rx_buf[i] == NULL)
  292. break;
  293. rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]);
  294. cm->rx_slots--;
  295. nent--;
  296. }
  297. }
  298. }
  299. /*
  300. * riocm_rx_free - frees all receive buffers associated with given cm device
  301. * @cm: cm_dev object
  302. *
  303. * Returns: none
  304. */
  305. static void riocm_rx_free(struct cm_dev *cm)
  306. {
  307. int i;
  308. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  309. if (cm->rx_buf[i] != NULL) {
  310. kfree(cm->rx_buf[i]);
  311. cm->rx_buf[i] = NULL;
  312. }
  313. }
  314. }
  315. /*
  316. * riocm_req_handler - connection request handler
  317. * @cm: cm_dev object
  318. * @req_data: pointer to the request packet
  319. *
  320. * Returns: 0 if success, or
  321. * -EINVAL if channel is not in correct state,
  322. * -ENODEV if cannot find a channel with specified ID,
  323. * -ENOMEM if unable to allocate memory to store the request
  324. */
  325. static int riocm_req_handler(struct cm_dev *cm, void *req_data)
  326. {
  327. struct rio_channel *ch;
  328. struct conn_req *req;
  329. struct rio_ch_chan_hdr *hh = req_data;
  330. u16 chnum;
  331. chnum = ntohs(hh->dst_ch);
  332. ch = riocm_get_channel(chnum);
  333. if (!ch)
  334. return -ENODEV;
  335. if (ch->state != RIO_CM_LISTEN) {
  336. riocm_debug(RX_CMD, "channel %d is not in listen state", chnum);
  337. riocm_put_channel(ch);
  338. return -EINVAL;
  339. }
  340. req = kzalloc(sizeof(*req), GFP_KERNEL);
  341. if (!req) {
  342. riocm_put_channel(ch);
  343. return -ENOMEM;
  344. }
  345. req->destid = ntohl(hh->bhdr.src_id);
  346. req->chan = ntohs(hh->src_ch);
  347. req->cmdev = cm;
  348. spin_lock_bh(&ch->lock);
  349. list_add_tail(&req->node, &ch->accept_queue);
  350. spin_unlock_bh(&ch->lock);
  351. complete(&ch->comp);
  352. riocm_put_channel(ch);
  353. return 0;
  354. }
  355. /*
  356. * riocm_resp_handler - response to connection request handler
  357. * @resp_data: pointer to the response packet
  358. *
  359. * Returns: 0 if success, or
  360. * -EINVAL if channel is not in correct state,
  361. * -ENODEV if cannot find a channel with specified ID,
  362. */
  363. static int riocm_resp_handler(void *resp_data)
  364. {
  365. struct rio_channel *ch;
  366. struct rio_ch_chan_hdr *hh = resp_data;
  367. u16 chnum;
  368. chnum = ntohs(hh->dst_ch);
  369. ch = riocm_get_channel(chnum);
  370. if (!ch)
  371. return -ENODEV;
  372. if (ch->state != RIO_CM_CONNECT) {
  373. riocm_put_channel(ch);
  374. return -EINVAL;
  375. }
  376. riocm_exch(ch, RIO_CM_CONNECTED);
  377. ch->rem_channel = ntohs(hh->src_ch);
  378. complete(&ch->comp);
  379. riocm_put_channel(ch);
  380. return 0;
  381. }
  382. /*
  383. * riocm_close_handler - channel close request handler
  384. * @req_data: pointer to the request packet
  385. *
  386. * Returns: 0 if success, or
  387. * -ENODEV if cannot find a channel with specified ID,
  388. * + error codes returned by riocm_ch_close.
  389. */
  390. static int riocm_close_handler(void *data)
  391. {
  392. struct rio_channel *ch;
  393. struct rio_ch_chan_hdr *hh = data;
  394. int ret;
  395. riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch));
  396. spin_lock_bh(&idr_lock);
  397. ch = idr_find(&ch_idr, ntohs(hh->dst_ch));
  398. if (!ch) {
  399. spin_unlock_bh(&idr_lock);
  400. return -ENODEV;
  401. }
  402. idr_remove(&ch_idr, ch->id);
  403. spin_unlock_bh(&idr_lock);
  404. riocm_exch(ch, RIO_CM_DISCONNECT);
  405. ret = riocm_ch_close(ch);
  406. if (ret)
  407. riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret);
  408. return 0;
  409. }
  410. /*
  411. * rio_cm_handler - function that services request (non-data) packets
  412. * @cm: cm_dev object
  413. * @data: pointer to the packet
  414. */
  415. static void rio_cm_handler(struct cm_dev *cm, void *data)
  416. {
  417. struct rio_ch_chan_hdr *hdr;
  418. if (!rio_mport_is_running(cm->mport))
  419. goto out;
  420. hdr = data;
  421. riocm_debug(RX_CMD, "OP=%x for ch=%d from %d",
  422. hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch));
  423. switch (hdr->ch_op) {
  424. case CM_CONN_REQ:
  425. riocm_req_handler(cm, data);
  426. break;
  427. case CM_CONN_ACK:
  428. riocm_resp_handler(data);
  429. break;
  430. case CM_CONN_CLOSE:
  431. riocm_close_handler(data);
  432. break;
  433. default:
  434. riocm_error("Invalid packet header");
  435. break;
  436. }
  437. out:
  438. kfree(data);
  439. }
  440. /*
  441. * rio_rx_data_handler - received data packet handler
  442. * @cm: cm_dev object
  443. * @buf: data packet
  444. *
  445. * Returns: 0 if success, or
  446. * -ENODEV if cannot find a channel with specified ID,
  447. * -EIO if channel is not in CONNECTED state,
  448. * -ENOMEM if channel RX queue is full (packet discarded)
  449. */
  450. static int rio_rx_data_handler(struct cm_dev *cm, void *buf)
  451. {
  452. struct rio_ch_chan_hdr *hdr;
  453. struct rio_channel *ch;
  454. hdr = buf;
  455. riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch));
  456. ch = riocm_get_channel(ntohs(hdr->dst_ch));
  457. if (!ch) {
  458. /* Discard data message for non-existing channel */
  459. kfree(buf);
  460. return -ENODEV;
  461. }
  462. /* Place pointer to the buffer into channel's RX queue */
  463. spin_lock(&ch->lock);
  464. if (ch->state != RIO_CM_CONNECTED) {
  465. /* Channel is not ready to receive data, discard a packet */
  466. riocm_debug(RX_DATA, "ch=%d is in wrong state=%d",
  467. ch->id, ch->state);
  468. spin_unlock(&ch->lock);
  469. kfree(buf);
  470. riocm_put_channel(ch);
  471. return -EIO;
  472. }
  473. if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) {
  474. /* If RX ring is full, discard a packet */
  475. riocm_debug(RX_DATA, "ch=%d is full", ch->id);
  476. spin_unlock(&ch->lock);
  477. kfree(buf);
  478. riocm_put_channel(ch);
  479. return -ENOMEM;
  480. }
  481. ch->rx_ring.buf[ch->rx_ring.head] = buf;
  482. ch->rx_ring.head++;
  483. ch->rx_ring.count++;
  484. ch->rx_ring.head %= RIOCM_RX_RING_SIZE;
  485. complete(&ch->comp);
  486. spin_unlock(&ch->lock);
  487. riocm_put_channel(ch);
  488. return 0;
  489. }
  490. /*
  491. * rio_ibmsg_handler - inbound message packet handler
  492. */
  493. static void rio_ibmsg_handler(struct work_struct *work)
  494. {
  495. struct cm_dev *cm = container_of(work, struct cm_dev, rx_work);
  496. void *data;
  497. struct rio_ch_chan_hdr *hdr;
  498. if (!rio_mport_is_running(cm->mport))
  499. return;
  500. while (1) {
  501. mutex_lock(&cm->rx_lock);
  502. data = riocm_rx_get_msg(cm);
  503. if (data)
  504. riocm_rx_fill(cm, 1);
  505. mutex_unlock(&cm->rx_lock);
  506. if (data == NULL)
  507. break;
  508. hdr = data;
  509. if (hdr->bhdr.type != RIO_CM_CHAN) {
  510. /* For now simply discard packets other than channel */
  511. riocm_error("Unsupported TYPE code (0x%x). Msg dropped",
  512. hdr->bhdr.type);
  513. kfree(data);
  514. continue;
  515. }
  516. /* Process a channel message */
  517. if (hdr->ch_op == CM_DATA_MSG)
  518. rio_rx_data_handler(cm, data);
  519. else
  520. rio_cm_handler(cm, data);
  521. }
  522. }
  523. static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id,
  524. int mbox, int slot)
  525. {
  526. struct cm_dev *cm = dev_id;
  527. if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work))
  528. queue_work(cm->rx_wq, &cm->rx_work);
  529. }
  530. /*
  531. * rio_txcq_handler - TX completion handler
  532. * @cm: cm_dev object
  533. * @slot: TX queue slot
  534. *
  535. * TX completion handler also ensures that pending request packets are placed
  536. * into transmit queue as soon as a free slot becomes available. This is done
  537. * to give higher priority to request packets during high intensity data flow.
  538. */
  539. static void rio_txcq_handler(struct cm_dev *cm, int slot)
  540. {
  541. int ack_slot;
  542. /* ATTN: Add TX completion notification if/when direct buffer
  543. * transfer is implemented. At this moment only correct tracking
  544. * of tx_count is important.
  545. */
  546. riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d",
  547. cm->mport->id, slot, cm->tx_cnt);
  548. spin_lock(&cm->tx_lock);
  549. ack_slot = cm->tx_ack_slot;
  550. if (ack_slot == slot)
  551. riocm_debug(TX_EVENT, "slot == ack_slot");
  552. while (cm->tx_cnt && ((ack_slot != slot) ||
  553. (cm->tx_cnt == RIOCM_TX_RING_SIZE))) {
  554. cm->tx_buf[ack_slot] = NULL;
  555. ++ack_slot;
  556. ack_slot &= (RIOCM_TX_RING_SIZE - 1);
  557. cm->tx_cnt--;
  558. }
  559. if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE)
  560. riocm_error("tx_cnt %d out of sync", cm->tx_cnt);
  561. WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE));
  562. cm->tx_ack_slot = ack_slot;
  563. /*
  564. * If there are pending requests, insert them into transmit queue
  565. */
  566. if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) {
  567. struct tx_req *req, *_req;
  568. int rc;
  569. list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) {
  570. list_del(&req->node);
  571. cm->tx_buf[cm->tx_slot] = req->buffer;
  572. rc = rio_add_outb_message(cm->mport, req->rdev, cmbox,
  573. req->buffer, req->len);
  574. kfree(req->buffer);
  575. kfree(req);
  576. ++cm->tx_cnt;
  577. ++cm->tx_slot;
  578. cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
  579. if (cm->tx_cnt == RIOCM_TX_RING_SIZE)
  580. break;
  581. }
  582. }
  583. spin_unlock(&cm->tx_lock);
  584. }
  585. static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id,
  586. int mbox, int slot)
  587. {
  588. struct cm_dev *cm = dev_id;
  589. if (cm && rio_mport_is_running(cm->mport))
  590. rio_txcq_handler(cm, slot);
  591. }
  592. static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev,
  593. void *buffer, size_t len)
  594. {
  595. unsigned long flags;
  596. struct tx_req *treq;
  597. treq = kzalloc(sizeof(*treq), GFP_KERNEL);
  598. if (treq == NULL)
  599. return -ENOMEM;
  600. treq->rdev = rdev;
  601. treq->buffer = buffer;
  602. treq->len = len;
  603. spin_lock_irqsave(&cm->tx_lock, flags);
  604. list_add_tail(&treq->node, &cm->tx_reqs);
  605. spin_unlock_irqrestore(&cm->tx_lock, flags);
  606. return 0;
  607. }
  608. /*
  609. * riocm_post_send - helper function that places packet into msg TX queue
  610. * @cm: cm_dev object
  611. * @rdev: target RapidIO device object (required by outbound msg interface)
  612. * @buffer: pointer to a packet buffer to send
  613. * @len: length of data to transfer
  614. * @req: request priority flag
  615. *
  616. * Returns: 0 if success, or error code otherwise.
  617. */
  618. static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
  619. void *buffer, size_t len)
  620. {
  621. int rc;
  622. unsigned long flags;
  623. spin_lock_irqsave(&cm->tx_lock, flags);
  624. if (cm->mport == NULL) {
  625. rc = -ENODEV;
  626. goto err_out;
  627. }
  628. if (cm->tx_cnt == RIOCM_TX_RING_SIZE) {
  629. riocm_debug(TX, "Tx Queue is full");
  630. rc = -EBUSY;
  631. goto err_out;
  632. }
  633. cm->tx_buf[cm->tx_slot] = buffer;
  634. rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len);
  635. riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d",
  636. buffer, rdev->destid, cm->tx_slot, cm->tx_cnt);
  637. ++cm->tx_cnt;
  638. ++cm->tx_slot;
  639. cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
  640. err_out:
  641. spin_unlock_irqrestore(&cm->tx_lock, flags);
  642. return rc;
  643. }
  644. /*
  645. * riocm_ch_send - sends a data packet to a remote device
  646. * @ch_id: local channel ID
  647. * @buf: pointer to a data buffer to send (including CM header)
  648. * @len: length of data to transfer (including CM header)
  649. *
  650. * ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET
  651. *
  652. * Returns: 0 if success, or
  653. * -EINVAL if one or more input parameters is/are not valid,
  654. * -ENODEV if cannot find a channel with specified ID,
  655. * -EAGAIN if a channel is not in CONNECTED state,
  656. * + error codes returned by HW send routine.
  657. */
  658. static int riocm_ch_send(u16 ch_id, void *buf, int len)
  659. {
  660. struct rio_channel *ch;
  661. struct rio_ch_chan_hdr *hdr;
  662. int ret;
  663. if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE)
  664. return -EINVAL;
  665. ch = riocm_get_channel(ch_id);
  666. if (!ch) {
  667. riocm_error("%s(%d) ch_%d not found", current->comm,
  668. task_pid_nr(current), ch_id);
  669. return -ENODEV;
  670. }
  671. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  672. ret = -EAGAIN;
  673. goto err_out;
  674. }
  675. /*
  676. * Fill buffer header section with corresponding channel data
  677. */
  678. hdr = buf;
  679. hdr->bhdr.src_id = htonl(ch->loc_destid);
  680. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  681. hdr->bhdr.src_mbox = cmbox;
  682. hdr->bhdr.dst_mbox = cmbox;
  683. hdr->bhdr.type = RIO_CM_CHAN;
  684. hdr->ch_op = CM_DATA_MSG;
  685. hdr->dst_ch = htons(ch->rem_channel);
  686. hdr->src_ch = htons(ch->id);
  687. hdr->msg_len = htons((u16)len);
  688. /* ATTN: the function call below relies on the fact that underlying
  689. * HW-specific add_outb_message() routine copies TX data into its own
  690. * internal transfer buffer (true for all RIONET compatible mport
  691. * drivers). Must be reviewed if mport driver uses the buffer directly.
  692. */
  693. ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
  694. if (ret)
  695. riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
  696. err_out:
  697. riocm_put_channel(ch);
  698. return ret;
  699. }
  700. static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
  701. {
  702. int i, ret = -EINVAL;
  703. spin_lock_bh(&ch->lock);
  704. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  705. if (ch->rx_ring.inuse[i] == buf) {
  706. ch->rx_ring.inuse[i] = NULL;
  707. ch->rx_ring.inuse_cnt--;
  708. ret = 0;
  709. break;
  710. }
  711. }
  712. spin_unlock_bh(&ch->lock);
  713. if (!ret)
  714. kfree(buf);
  715. return ret;
  716. }
  717. /*
  718. * riocm_ch_receive - fetch a data packet received for the specified channel
  719. * @ch: local channel ID
  720. * @buf: pointer to a packet buffer
  721. * @timeout: timeout to wait for incoming packet (in jiffies)
  722. *
  723. * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
  724. * -EAGAIN if a channel is not in CONNECTED state,
  725. * -ENOMEM if in-use tracking queue is full,
  726. * -ETIME if wait timeout expired,
  727. * -EINTR if wait was interrupted.
  728. */
  729. static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
  730. {
  731. void *rxmsg = NULL;
  732. int i, ret = 0;
  733. long wret;
  734. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  735. ret = -EAGAIN;
  736. goto out;
  737. }
  738. if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
  739. /* If we do not have entries to track buffers given to upper
  740. * layer, reject request.
  741. */
  742. ret = -ENOMEM;
  743. goto out;
  744. }
  745. wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
  746. riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
  747. if (!wret)
  748. ret = -ETIME;
  749. else if (wret == -ERESTARTSYS)
  750. ret = -EINTR;
  751. else
  752. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
  753. if (ret)
  754. goto out;
  755. spin_lock_bh(&ch->lock);
  756. rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
  757. ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
  758. ch->rx_ring.count--;
  759. ch->rx_ring.tail++;
  760. ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
  761. ret = -ENOMEM;
  762. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  763. if (ch->rx_ring.inuse[i] == NULL) {
  764. ch->rx_ring.inuse[i] = rxmsg;
  765. ch->rx_ring.inuse_cnt++;
  766. ret = 0;
  767. break;
  768. }
  769. }
  770. if (ret) {
  771. /* We have no entry to store pending message: drop it */
  772. kfree(rxmsg);
  773. rxmsg = NULL;
  774. }
  775. spin_unlock_bh(&ch->lock);
  776. out:
  777. *buf = rxmsg;
  778. return ret;
  779. }
  780. /*
  781. * riocm_ch_connect - sends a connect request to a remote device
  782. * @loc_ch: local channel ID
  783. * @cm: CM device to send connect request
  784. * @peer: target RapidIO device
  785. * @rem_ch: remote channel ID
  786. *
  787. * Returns: 0 if success, or
  788. * -EINVAL if the channel is not in IDLE state,
  789. * -EAGAIN if no connection request available immediately,
  790. * -ETIME if ACK response timeout expired,
  791. * -EINTR if wait for response was interrupted.
  792. */
  793. static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
  794. struct cm_peer *peer, u16 rem_ch)
  795. {
  796. struct rio_channel *ch = NULL;
  797. struct rio_ch_chan_hdr *hdr;
  798. int ret;
  799. long wret;
  800. ch = riocm_get_channel(loc_ch);
  801. if (!ch)
  802. return -ENODEV;
  803. if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
  804. ret = -EINVAL;
  805. goto conn_done;
  806. }
  807. ch->cmdev = cm;
  808. ch->rdev = peer->rdev;
  809. ch->context = NULL;
  810. ch->loc_destid = cm->mport->host_deviceid;
  811. ch->rem_channel = rem_ch;
  812. /*
  813. * Send connect request to the remote RapidIO device
  814. */
  815. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  816. if (hdr == NULL) {
  817. ret = -ENOMEM;
  818. goto conn_done;
  819. }
  820. hdr->bhdr.src_id = htonl(ch->loc_destid);
  821. hdr->bhdr.dst_id = htonl(peer->rdev->destid);
  822. hdr->bhdr.src_mbox = cmbox;
  823. hdr->bhdr.dst_mbox = cmbox;
  824. hdr->bhdr.type = RIO_CM_CHAN;
  825. hdr->ch_op = CM_CONN_REQ;
  826. hdr->dst_ch = htons(rem_ch);
  827. hdr->src_ch = htons(loc_ch);
  828. /* ATTN: the function call below relies on the fact that underlying
  829. * HW-specific add_outb_message() routine copies TX data into its
  830. * internal transfer buffer. Must be reviewed if mport driver uses
  831. * this buffer directly.
  832. */
  833. ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
  834. if (ret != -EBUSY) {
  835. kfree(hdr);
  836. } else {
  837. ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
  838. if (ret)
  839. kfree(hdr);
  840. }
  841. if (ret) {
  842. riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);
  843. goto conn_done;
  844. }
  845. /* Wait for connect response from the remote device */
  846. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  847. RIOCM_CONNECT_TO * HZ);
  848. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  849. if (!wret)
  850. ret = -ETIME;
  851. else if (wret == -ERESTARTSYS)
  852. ret = -EINTR;
  853. else
  854. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1;
  855. conn_done:
  856. riocm_put_channel(ch);
  857. return ret;
  858. }
  859. static int riocm_send_ack(struct rio_channel *ch)
  860. {
  861. struct rio_ch_chan_hdr *hdr;
  862. int ret;
  863. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  864. if (hdr == NULL)
  865. return -ENOMEM;
  866. hdr->bhdr.src_id = htonl(ch->loc_destid);
  867. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  868. hdr->dst_ch = htons(ch->rem_channel);
  869. hdr->src_ch = htons(ch->id);
  870. hdr->bhdr.src_mbox = cmbox;
  871. hdr->bhdr.dst_mbox = cmbox;
  872. hdr->bhdr.type = RIO_CM_CHAN;
  873. hdr->ch_op = CM_CONN_ACK;
  874. /* ATTN: the function call below relies on the fact that underlying
  875. * add_outb_message() routine copies TX data into its internal transfer
  876. * buffer. Review if switching to direct buffer version.
  877. */
  878. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  879. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev,
  880. ch->rdev, hdr, sizeof(*hdr)))
  881. return 0;
  882. kfree(hdr);
  883. if (ret)
  884. riocm_error("send ACK to ch_%d on %s failed (ret=%d)",
  885. ch->id, rio_name(ch->rdev), ret);
  886. return ret;
  887. }
  888. /*
  889. * riocm_ch_accept - accept incoming connection request
  890. * @ch_id: channel ID
  891. * @new_ch_id: local mport device
  892. * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
  893. * request is not available).
  894. *
  895. * Returns: pointer to new channel struct if success, or error-valued pointer:
  896. * -ENODEV - cannot find specified channel or mport,
  897. * -EINVAL - the channel is not in IDLE state,
  898. * -EAGAIN - no connection request available immediately (timeout=0),
  899. * -ENOMEM - unable to allocate new channel,
  900. * -ETIME - wait timeout expired,
  901. * -EINTR - wait was interrupted.
  902. */
  903. static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id,
  904. long timeout)
  905. {
  906. struct rio_channel *ch;
  907. struct rio_channel *new_ch;
  908. struct conn_req *req;
  909. struct cm_peer *peer;
  910. int found = 0;
  911. int err = 0;
  912. long wret;
  913. ch = riocm_get_channel(ch_id);
  914. if (!ch)
  915. return ERR_PTR(-EINVAL);
  916. if (!riocm_cmp(ch, RIO_CM_LISTEN)) {
  917. err = -EINVAL;
  918. goto err_put;
  919. }
  920. /* Don't sleep if this is a non blocking call */
  921. if (!timeout) {
  922. if (!try_wait_for_completion(&ch->comp)) {
  923. err = -EAGAIN;
  924. goto err_put;
  925. }
  926. } else {
  927. riocm_debug(WAIT, "on %d", ch->id);
  928. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  929. timeout);
  930. if (!wret) {
  931. err = -ETIME;
  932. goto err_put;
  933. } else if (wret == -ERESTARTSYS) {
  934. err = -EINTR;
  935. goto err_put;
  936. }
  937. }
  938. spin_lock_bh(&ch->lock);
  939. if (ch->state != RIO_CM_LISTEN) {
  940. err = -ECANCELED;
  941. } else if (list_empty(&ch->accept_queue)) {
  942. riocm_debug(WAIT, "on %d accept_queue is empty on completion",
  943. ch->id);
  944. err = -EIO;
  945. }
  946. spin_unlock_bh(&ch->lock);
  947. if (err) {
  948. riocm_debug(WAIT, "on %d returns %d", ch->id, err);
  949. goto err_put;
  950. }
  951. /* Create new channel for this connection */
  952. new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO);
  953. if (IS_ERR(new_ch)) {
  954. riocm_error("failed to get channel for new req (%ld)",
  955. PTR_ERR(new_ch));
  956. err = -ENOMEM;
  957. goto err_put;
  958. }
  959. spin_lock_bh(&ch->lock);
  960. req = list_first_entry(&ch->accept_queue, struct conn_req, node);
  961. list_del(&req->node);
  962. new_ch->cmdev = ch->cmdev;
  963. new_ch->loc_destid = ch->loc_destid;
  964. new_ch->rem_destid = req->destid;
  965. new_ch->rem_channel = req->chan;
  966. spin_unlock_bh(&ch->lock);
  967. riocm_put_channel(ch);
  968. ch = NULL;
  969. kfree(req);
  970. down_read(&rdev_sem);
  971. /* Find requester's device object */
  972. list_for_each_entry(peer, &new_ch->cmdev->peers, node) {
  973. if (peer->rdev->destid == new_ch->rem_destid) {
  974. riocm_debug(RX_CMD, "found matching device(%s)",
  975. rio_name(peer->rdev));
  976. found = 1;
  977. break;
  978. }
  979. }
  980. up_read(&rdev_sem);
  981. if (!found) {
  982. /* If peer device object not found, simply ignore the request */
  983. err = -ENODEV;
  984. goto err_put_new_ch;
  985. }
  986. new_ch->rdev = peer->rdev;
  987. new_ch->state = RIO_CM_CONNECTED;
  988. spin_lock_init(&new_ch->lock);
  989. /* Acknowledge the connection request. */
  990. riocm_send_ack(new_ch);
  991. *new_ch_id = new_ch->id;
  992. return new_ch;
  993. err_put_new_ch:
  994. spin_lock_bh(&idr_lock);
  995. idr_remove(&ch_idr, new_ch->id);
  996. spin_unlock_bh(&idr_lock);
  997. riocm_put_channel(new_ch);
  998. err_put:
  999. if (ch)
  1000. riocm_put_channel(ch);
  1001. *new_ch_id = 0;
  1002. return ERR_PTR(err);
  1003. }
  1004. /*
  1005. * riocm_ch_listen - puts a channel into LISTEN state
  1006. * @ch_id: channel ID
  1007. *
  1008. * Returns: 0 if success, or
  1009. * -EINVAL if the specified channel does not exists or
  1010. * is not in CHAN_BOUND state.
  1011. */
  1012. static int riocm_ch_listen(u16 ch_id)
  1013. {
  1014. struct rio_channel *ch = NULL;
  1015. int ret = 0;
  1016. riocm_debug(CHOP, "(ch_%d)", ch_id);
  1017. ch = riocm_get_channel(ch_id);
  1018. if (!ch)
  1019. return -EINVAL;
  1020. if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
  1021. ret = -EINVAL;
  1022. riocm_put_channel(ch);
  1023. return ret;
  1024. }
  1025. /*
  1026. * riocm_ch_bind - associate a channel object and an mport device
  1027. * @ch_id: channel ID
  1028. * @mport_id: local mport device ID
  1029. * @context: pointer to the additional caller's context
  1030. *
  1031. * Returns: 0 if success, or
  1032. * -ENODEV if cannot find specified mport,
  1033. * -EINVAL if the specified channel does not exist or
  1034. * is not in IDLE state.
  1035. */
  1036. static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
  1037. {
  1038. struct rio_channel *ch = NULL;
  1039. struct cm_dev *cm;
  1040. int rc = -ENODEV;
  1041. riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
  1042. /* Find matching cm_dev object */
  1043. down_read(&rdev_sem);
  1044. list_for_each_entry(cm, &cm_dev_list, list) {
  1045. if ((cm->mport->id == mport_id) &&
  1046. rio_mport_is_running(cm->mport)) {
  1047. rc = 0;
  1048. break;
  1049. }
  1050. }
  1051. if (rc)
  1052. goto exit;
  1053. ch = riocm_get_channel(ch_id);
  1054. if (!ch) {
  1055. rc = -EINVAL;
  1056. goto exit;
  1057. }
  1058. spin_lock_bh(&ch->lock);
  1059. if (ch->state != RIO_CM_IDLE) {
  1060. spin_unlock_bh(&ch->lock);
  1061. rc = -EINVAL;
  1062. goto err_put;
  1063. }
  1064. ch->cmdev = cm;
  1065. ch->loc_destid = cm->mport->host_deviceid;
  1066. ch->context = context;
  1067. ch->state = RIO_CM_CHAN_BOUND;
  1068. spin_unlock_bh(&ch->lock);
  1069. err_put:
  1070. riocm_put_channel(ch);
  1071. exit:
  1072. up_read(&rdev_sem);
  1073. return rc;
  1074. }
  1075. /*
  1076. * riocm_ch_alloc - channel object allocation helper routine
  1077. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1078. *
  1079. * Return value: pointer to newly created channel object,
  1080. * or error-valued pointer
  1081. */
  1082. static struct rio_channel *riocm_ch_alloc(u16 ch_num)
  1083. {
  1084. int id;
  1085. int start, end;
  1086. struct rio_channel *ch;
  1087. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1088. if (!ch)
  1089. return ERR_PTR(-ENOMEM);
  1090. if (ch_num) {
  1091. /* If requested, try to obtain the specified channel ID */
  1092. start = ch_num;
  1093. end = ch_num + 1;
  1094. } else {
  1095. /* Obtain channel ID from the dynamic allocation range */
  1096. start = chstart;
  1097. end = RIOCM_MAX_CHNUM + 1;
  1098. }
  1099. idr_preload(GFP_KERNEL);
  1100. spin_lock_bh(&idr_lock);
  1101. id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
  1102. spin_unlock_bh(&idr_lock);
  1103. idr_preload_end();
  1104. if (id < 0) {
  1105. kfree(ch);
  1106. return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
  1107. }
  1108. ch->id = (u16)id;
  1109. ch->state = RIO_CM_IDLE;
  1110. spin_lock_init(&ch->lock);
  1111. INIT_LIST_HEAD(&ch->accept_queue);
  1112. INIT_LIST_HEAD(&ch->ch_node);
  1113. init_completion(&ch->comp);
  1114. init_completion(&ch->comp_close);
  1115. kref_init(&ch->ref);
  1116. ch->rx_ring.head = 0;
  1117. ch->rx_ring.tail = 0;
  1118. ch->rx_ring.count = 0;
  1119. ch->rx_ring.inuse_cnt = 0;
  1120. return ch;
  1121. }
  1122. /*
  1123. * riocm_ch_create - creates a new channel object and allocates ID for it
  1124. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1125. *
  1126. * Allocates and initializes a new channel object. If the parameter ch_num > 0
  1127. * and is within the valid range, riocm_ch_create tries to allocate the
  1128. * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
  1129. * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
  1130. * Module parameter 'chstart' defines start of an ID range available for dynamic
  1131. * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
  1132. * Available channel numbers are limited by 16-bit size of channel numbers used
  1133. * in the packet header.
  1134. *
  1135. * Return value: PTR to rio_channel structure if successful (with channel number
  1136. * updated via pointer) or error-valued pointer if error.
  1137. */
  1138. static struct rio_channel *riocm_ch_create(u16 *ch_num)
  1139. {
  1140. struct rio_channel *ch = NULL;
  1141. ch = riocm_ch_alloc(*ch_num);
  1142. if (IS_ERR(ch))
  1143. riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
  1144. *ch_num, PTR_ERR(ch));
  1145. else
  1146. *ch_num = ch->id;
  1147. return ch;
  1148. }
  1149. /*
  1150. * riocm_ch_free - channel object release routine
  1151. * @ref: pointer to a channel's kref structure
  1152. */
  1153. static void riocm_ch_free(struct kref *ref)
  1154. {
  1155. struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
  1156. int i;
  1157. riocm_debug(CHOP, "(ch_%d)", ch->id);
  1158. if (ch->rx_ring.inuse_cnt) {
  1159. for (i = 0;
  1160. i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
  1161. if (ch->rx_ring.inuse[i] != NULL) {
  1162. kfree(ch->rx_ring.inuse[i]);
  1163. ch->rx_ring.inuse_cnt--;
  1164. }
  1165. }
  1166. }
  1167. if (ch->rx_ring.count)
  1168. for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
  1169. if (ch->rx_ring.buf[i] != NULL) {
  1170. kfree(ch->rx_ring.buf[i]);
  1171. ch->rx_ring.count--;
  1172. }
  1173. }
  1174. complete(&ch->comp_close);
  1175. }
  1176. static int riocm_send_close(struct rio_channel *ch)
  1177. {
  1178. struct rio_ch_chan_hdr *hdr;
  1179. int ret;
  1180. /*
  1181. * Send CH_CLOSE notification to the remote RapidIO device
  1182. */
  1183. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  1184. if (hdr == NULL)
  1185. return -ENOMEM;
  1186. hdr->bhdr.src_id = htonl(ch->loc_destid);
  1187. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  1188. hdr->bhdr.src_mbox = cmbox;
  1189. hdr->bhdr.dst_mbox = cmbox;
  1190. hdr->bhdr.type = RIO_CM_CHAN;
  1191. hdr->ch_op = CM_CONN_CLOSE;
  1192. hdr->dst_ch = htons(ch->rem_channel);
  1193. hdr->src_ch = htons(ch->id);
  1194. /* ATTN: the function call below relies on the fact that underlying
  1195. * add_outb_message() routine copies TX data into its internal transfer
  1196. * buffer. Needs to be reviewed if switched to direct buffer mode.
  1197. */
  1198. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  1199. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
  1200. hdr, sizeof(*hdr)))
  1201. return 0;
  1202. kfree(hdr);
  1203. if (ret)
  1204. riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
  1205. return ret;
  1206. }
  1207. /*
  1208. * riocm_ch_close - closes a channel object with specified ID (by local request)
  1209. * @ch: channel to be closed
  1210. */
  1211. static int riocm_ch_close(struct rio_channel *ch)
  1212. {
  1213. unsigned long tmo = msecs_to_jiffies(3000);
  1214. enum rio_cm_state state;
  1215. long wret;
  1216. int ret = 0;
  1217. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1218. ch->id, current->comm, task_pid_nr(current));
  1219. state = riocm_exch(ch, RIO_CM_DESTROYING);
  1220. if (state == RIO_CM_CONNECTED)
  1221. riocm_send_close(ch);
  1222. complete_all(&ch->comp);
  1223. riocm_put_channel(ch);
  1224. wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
  1225. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  1226. if (wret == 0) {
  1227. /* Timeout on wait occurred */
  1228. riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
  1229. current->comm, task_pid_nr(current), ch->id);
  1230. ret = -ETIMEDOUT;
  1231. } else if (wret == -ERESTARTSYS) {
  1232. /* Wait_for_completion was interrupted by a signal */
  1233. riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
  1234. current->comm, task_pid_nr(current), ch->id);
  1235. ret = -EINTR;
  1236. }
  1237. if (!ret) {
  1238. riocm_debug(CHOP, "ch_%d resources released", ch->id);
  1239. kfree(ch);
  1240. } else {
  1241. riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
  1242. }
  1243. return ret;
  1244. }
  1245. /*
  1246. * riocm_cdev_open() - Open character device
  1247. */
  1248. static int riocm_cdev_open(struct inode *inode, struct file *filp)
  1249. {
  1250. riocm_debug(INIT, "by %s(%d) filp=%p ",
  1251. current->comm, task_pid_nr(current), filp);
  1252. if (list_empty(&cm_dev_list))
  1253. return -ENODEV;
  1254. return 0;
  1255. }
  1256. /*
  1257. * riocm_cdev_release() - Release character device
  1258. */
  1259. static int riocm_cdev_release(struct inode *inode, struct file *filp)
  1260. {
  1261. struct rio_channel *ch, *_c;
  1262. unsigned int i;
  1263. LIST_HEAD(list);
  1264. riocm_debug(EXIT, "by %s(%d) filp=%p",
  1265. current->comm, task_pid_nr(current), filp);
  1266. /* Check if there are channels associated with this file descriptor */
  1267. spin_lock_bh(&idr_lock);
  1268. idr_for_each_entry(&ch_idr, ch, i) {
  1269. if (ch && ch->filp == filp) {
  1270. riocm_debug(EXIT, "ch_%d not released by %s(%d)",
  1271. ch->id, current->comm,
  1272. task_pid_nr(current));
  1273. idr_remove(&ch_idr, ch->id);
  1274. list_add(&ch->ch_node, &list);
  1275. }
  1276. }
  1277. spin_unlock_bh(&idr_lock);
  1278. if (!list_empty(&list)) {
  1279. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1280. list_del(&ch->ch_node);
  1281. riocm_ch_close(ch);
  1282. }
  1283. }
  1284. return 0;
  1285. }
  1286. /*
  1287. * cm_ep_get_list_size() - Reports number of endpoints in the network
  1288. */
  1289. static int cm_ep_get_list_size(void __user *arg)
  1290. {
  1291. u32 __user *p = arg;
  1292. u32 mport_id;
  1293. u32 count = 0;
  1294. struct cm_dev *cm;
  1295. if (get_user(mport_id, p))
  1296. return -EFAULT;
  1297. if (mport_id >= RIO_MAX_MPORTS)
  1298. return -EINVAL;
  1299. /* Find a matching cm_dev object */
  1300. down_read(&rdev_sem);
  1301. list_for_each_entry(cm, &cm_dev_list, list) {
  1302. if (cm->mport->id == mport_id) {
  1303. count = cm->npeers;
  1304. up_read(&rdev_sem);
  1305. if (copy_to_user(arg, &count, sizeof(u32)))
  1306. return -EFAULT;
  1307. return 0;
  1308. }
  1309. }
  1310. up_read(&rdev_sem);
  1311. return -ENODEV;
  1312. }
  1313. /*
  1314. * cm_ep_get_list() - Returns list of attached endpoints
  1315. */
  1316. static int cm_ep_get_list(void __user *arg)
  1317. {
  1318. struct cm_dev *cm;
  1319. struct cm_peer *peer;
  1320. u32 info[2];
  1321. void *buf;
  1322. u32 nent;
  1323. u32 *entry_ptr;
  1324. u32 i = 0;
  1325. int ret = 0;
  1326. if (copy_from_user(&info, arg, sizeof(info)))
  1327. return -EFAULT;
  1328. if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
  1329. return -EINVAL;
  1330. /* Find a matching cm_dev object */
  1331. down_read(&rdev_sem);
  1332. list_for_each_entry(cm, &cm_dev_list, list)
  1333. if (cm->mport->id == (u8)info[1])
  1334. goto found;
  1335. up_read(&rdev_sem);
  1336. return -ENODEV;
  1337. found:
  1338. nent = min(info[0], cm->npeers);
  1339. buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
  1340. if (!buf) {
  1341. up_read(&rdev_sem);
  1342. return -ENOMEM;
  1343. }
  1344. entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
  1345. list_for_each_entry(peer, &cm->peers, node) {
  1346. *entry_ptr = (u32)peer->rdev->destid;
  1347. entry_ptr++;
  1348. if (++i == nent)
  1349. break;
  1350. }
  1351. up_read(&rdev_sem);
  1352. ((u32 *)buf)[0] = i; /* report an updated number of entries */
  1353. ((u32 *)buf)[1] = info[1]; /* put back an mport ID */
  1354. if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
  1355. ret = -EFAULT;
  1356. kfree(buf);
  1357. return ret;
  1358. }
  1359. /*
  1360. * cm_mport_get_list() - Returns list of available local mport devices
  1361. */
  1362. static int cm_mport_get_list(void __user *arg)
  1363. {
  1364. int ret = 0;
  1365. u32 entries;
  1366. void *buf;
  1367. struct cm_dev *cm;
  1368. u32 *entry_ptr;
  1369. int count = 0;
  1370. if (copy_from_user(&entries, arg, sizeof(entries)))
  1371. return -EFAULT;
  1372. if (entries == 0 || entries > RIO_MAX_MPORTS)
  1373. return -EINVAL;
  1374. buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
  1375. if (!buf)
  1376. return -ENOMEM;
  1377. /* Scan all registered cm_dev objects */
  1378. entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
  1379. down_read(&rdev_sem);
  1380. list_for_each_entry(cm, &cm_dev_list, list) {
  1381. if (count++ < entries) {
  1382. *entry_ptr = (cm->mport->id << 16) |
  1383. cm->mport->host_deviceid;
  1384. entry_ptr++;
  1385. }
  1386. }
  1387. up_read(&rdev_sem);
  1388. *((u32 *)buf) = count; /* report a real number of entries */
  1389. if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
  1390. ret = -EFAULT;
  1391. kfree(buf);
  1392. return ret;
  1393. }
  1394. /*
  1395. * cm_chan_create() - Create a message exchange channel
  1396. */
  1397. static int cm_chan_create(struct file *filp, void __user *arg)
  1398. {
  1399. u16 __user *p = arg;
  1400. u16 ch_num;
  1401. struct rio_channel *ch;
  1402. if (get_user(ch_num, p))
  1403. return -EFAULT;
  1404. riocm_debug(CHOP, "ch_%d requested by %s(%d)",
  1405. ch_num, current->comm, task_pid_nr(current));
  1406. ch = riocm_ch_create(&ch_num);
  1407. if (IS_ERR(ch))
  1408. return PTR_ERR(ch);
  1409. ch->filp = filp;
  1410. riocm_debug(CHOP, "ch_%d created by %s(%d)",
  1411. ch_num, current->comm, task_pid_nr(current));
  1412. return put_user(ch_num, p);
  1413. }
  1414. /*
  1415. * cm_chan_close() - Close channel
  1416. * @filp: Pointer to file object
  1417. * @arg: Channel to close
  1418. */
  1419. static int cm_chan_close(struct file *filp, void __user *arg)
  1420. {
  1421. u16 __user *p = arg;
  1422. u16 ch_num;
  1423. struct rio_channel *ch;
  1424. if (get_user(ch_num, p))
  1425. return -EFAULT;
  1426. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1427. ch_num, current->comm, task_pid_nr(current));
  1428. spin_lock_bh(&idr_lock);
  1429. ch = idr_find(&ch_idr, ch_num);
  1430. if (!ch) {
  1431. spin_unlock_bh(&idr_lock);
  1432. return 0;
  1433. }
  1434. if (ch->filp != filp) {
  1435. spin_unlock_bh(&idr_lock);
  1436. return -EINVAL;
  1437. }
  1438. idr_remove(&ch_idr, ch->id);
  1439. spin_unlock_bh(&idr_lock);
  1440. return riocm_ch_close(ch);
  1441. }
  1442. /*
  1443. * cm_chan_bind() - Bind channel
  1444. * @arg: Channel number
  1445. */
  1446. static int cm_chan_bind(void __user *arg)
  1447. {
  1448. struct rio_cm_channel chan;
  1449. if (copy_from_user(&chan, arg, sizeof(chan)))
  1450. return -EFAULT;
  1451. if (chan.mport_id >= RIO_MAX_MPORTS)
  1452. return -EINVAL;
  1453. return riocm_ch_bind(chan.id, chan.mport_id, NULL);
  1454. }
  1455. /*
  1456. * cm_chan_listen() - Listen on channel
  1457. * @arg: Channel number
  1458. */
  1459. static int cm_chan_listen(void __user *arg)
  1460. {
  1461. u16 __user *p = arg;
  1462. u16 ch_num;
  1463. if (get_user(ch_num, p))
  1464. return -EFAULT;
  1465. return riocm_ch_listen(ch_num);
  1466. }
  1467. /*
  1468. * cm_chan_accept() - Accept incoming connection
  1469. * @filp: Pointer to file object
  1470. * @arg: Channel number
  1471. */
  1472. static int cm_chan_accept(struct file *filp, void __user *arg)
  1473. {
  1474. struct rio_cm_accept param;
  1475. long accept_to;
  1476. struct rio_channel *ch;
  1477. if (copy_from_user(&param, arg, sizeof(param)))
  1478. return -EFAULT;
  1479. riocm_debug(CHOP, "on ch_%d by %s(%d)",
  1480. param.ch_num, current->comm, task_pid_nr(current));
  1481. accept_to = param.wait_to ?
  1482. msecs_to_jiffies(param.wait_to) : 0;
  1483. ch = riocm_ch_accept(param.ch_num, &param.ch_num, accept_to);
  1484. if (IS_ERR(ch))
  1485. return PTR_ERR(ch);
  1486. ch->filp = filp;
  1487. riocm_debug(CHOP, "new ch_%d for %s(%d)",
  1488. ch->id, current->comm, task_pid_nr(current));
  1489. if (copy_to_user(arg, &param, sizeof(param)))
  1490. return -EFAULT;
  1491. return 0;
  1492. }
  1493. /*
  1494. * cm_chan_connect() - Connect on channel
  1495. * @arg: Channel information
  1496. */
  1497. static int cm_chan_connect(void __user *arg)
  1498. {
  1499. struct rio_cm_channel chan;
  1500. struct cm_dev *cm;
  1501. struct cm_peer *peer;
  1502. int ret = -ENODEV;
  1503. if (copy_from_user(&chan, arg, sizeof(chan)))
  1504. return -EFAULT;
  1505. if (chan.mport_id >= RIO_MAX_MPORTS)
  1506. return -EINVAL;
  1507. down_read(&rdev_sem);
  1508. /* Find matching cm_dev object */
  1509. list_for_each_entry(cm, &cm_dev_list, list) {
  1510. if (cm->mport->id == chan.mport_id) {
  1511. ret = 0;
  1512. break;
  1513. }
  1514. }
  1515. if (ret)
  1516. goto err_out;
  1517. if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
  1518. ret = -EINVAL;
  1519. goto err_out;
  1520. }
  1521. /* Find corresponding RapidIO endpoint device object */
  1522. ret = -ENODEV;
  1523. list_for_each_entry(peer, &cm->peers, node) {
  1524. if (peer->rdev->destid == chan.remote_destid) {
  1525. ret = 0;
  1526. break;
  1527. }
  1528. }
  1529. if (ret)
  1530. goto err_out;
  1531. up_read(&rdev_sem);
  1532. return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
  1533. err_out:
  1534. up_read(&rdev_sem);
  1535. return ret;
  1536. }
  1537. /*
  1538. * cm_chan_msg_send() - Send a message through channel
  1539. * @arg: Outbound message information
  1540. */
  1541. static int cm_chan_msg_send(void __user *arg)
  1542. {
  1543. struct rio_cm_msg msg;
  1544. void *buf;
  1545. int ret;
  1546. if (copy_from_user(&msg, arg, sizeof(msg)))
  1547. return -EFAULT;
  1548. if (msg.size > RIO_MAX_MSG_SIZE)
  1549. return -EINVAL;
  1550. buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
  1551. if (IS_ERR(buf))
  1552. return PTR_ERR(buf);
  1553. ret = riocm_ch_send(msg.ch_num, buf, msg.size);
  1554. kfree(buf);
  1555. return ret;
  1556. }
  1557. /*
  1558. * cm_chan_msg_rcv() - Receive a message through channel
  1559. * @arg: Inbound message information
  1560. */
  1561. static int cm_chan_msg_rcv(void __user *arg)
  1562. {
  1563. struct rio_cm_msg msg;
  1564. struct rio_channel *ch;
  1565. void *buf;
  1566. long rxto;
  1567. int ret = 0, msg_size;
  1568. if (copy_from_user(&msg, arg, sizeof(msg)))
  1569. return -EFAULT;
  1570. if (msg.ch_num == 0 || msg.size == 0)
  1571. return -EINVAL;
  1572. ch = riocm_get_channel(msg.ch_num);
  1573. if (!ch)
  1574. return -ENODEV;
  1575. rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
  1576. ret = riocm_ch_receive(ch, &buf, rxto);
  1577. if (ret)
  1578. goto out;
  1579. msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
  1580. if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
  1581. ret = -EFAULT;
  1582. riocm_ch_free_rxbuf(ch, buf);
  1583. out:
  1584. riocm_put_channel(ch);
  1585. return ret;
  1586. }
  1587. /*
  1588. * riocm_cdev_ioctl() - IOCTL requests handler
  1589. */
  1590. static long
  1591. riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1592. {
  1593. switch (cmd) {
  1594. case RIO_CM_EP_GET_LIST_SIZE:
  1595. return cm_ep_get_list_size((void __user *)arg);
  1596. case RIO_CM_EP_GET_LIST:
  1597. return cm_ep_get_list((void __user *)arg);
  1598. case RIO_CM_CHAN_CREATE:
  1599. return cm_chan_create(filp, (void __user *)arg);
  1600. case RIO_CM_CHAN_CLOSE:
  1601. return cm_chan_close(filp, (void __user *)arg);
  1602. case RIO_CM_CHAN_BIND:
  1603. return cm_chan_bind((void __user *)arg);
  1604. case RIO_CM_CHAN_LISTEN:
  1605. return cm_chan_listen((void __user *)arg);
  1606. case RIO_CM_CHAN_ACCEPT:
  1607. return cm_chan_accept(filp, (void __user *)arg);
  1608. case RIO_CM_CHAN_CONNECT:
  1609. return cm_chan_connect((void __user *)arg);
  1610. case RIO_CM_CHAN_SEND:
  1611. return cm_chan_msg_send((void __user *)arg);
  1612. case RIO_CM_CHAN_RECEIVE:
  1613. return cm_chan_msg_rcv((void __user *)arg);
  1614. case RIO_CM_MPORT_GET_LIST:
  1615. return cm_mport_get_list((void __user *)arg);
  1616. default:
  1617. break;
  1618. }
  1619. return -EINVAL;
  1620. }
  1621. static const struct file_operations riocm_cdev_fops = {
  1622. .owner = THIS_MODULE,
  1623. .open = riocm_cdev_open,
  1624. .release = riocm_cdev_release,
  1625. .unlocked_ioctl = riocm_cdev_ioctl,
  1626. };
  1627. /*
  1628. * riocm_add_dev - add new remote RapidIO device into channel management core
  1629. * @dev: device object associated with RapidIO device
  1630. * @sif: subsystem interface
  1631. *
  1632. * Adds the specified RapidIO device (if applicable) into peers list of
  1633. * the corresponding channel management device (cm_dev).
  1634. */
  1635. static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
  1636. {
  1637. struct cm_peer *peer;
  1638. struct rio_dev *rdev = to_rio_dev(dev);
  1639. struct cm_dev *cm;
  1640. /* Check if the remote device has capabilities required to support CM */
  1641. if (!dev_cm_capable(rdev))
  1642. return 0;
  1643. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1644. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1645. if (!peer)
  1646. return -ENOMEM;
  1647. /* Find a corresponding cm_dev object */
  1648. down_write(&rdev_sem);
  1649. list_for_each_entry(cm, &cm_dev_list, list) {
  1650. if (cm->mport == rdev->net->hport)
  1651. goto found;
  1652. }
  1653. up_write(&rdev_sem);
  1654. kfree(peer);
  1655. return -ENODEV;
  1656. found:
  1657. peer->rdev = rdev;
  1658. list_add_tail(&peer->node, &cm->peers);
  1659. cm->npeers++;
  1660. up_write(&rdev_sem);
  1661. return 0;
  1662. }
  1663. /*
  1664. * riocm_remove_dev - remove remote RapidIO device from channel management core
  1665. * @dev: device object associated with RapidIO device
  1666. * @sif: subsystem interface
  1667. *
  1668. * Removes the specified RapidIO device (if applicable) from peers list of
  1669. * the corresponding channel management device (cm_dev).
  1670. */
  1671. static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
  1672. {
  1673. struct rio_dev *rdev = to_rio_dev(dev);
  1674. struct cm_dev *cm;
  1675. struct cm_peer *peer;
  1676. struct rio_channel *ch, *_c;
  1677. unsigned int i;
  1678. bool found = false;
  1679. LIST_HEAD(list);
  1680. /* Check if the remote device has capabilities required to support CM */
  1681. if (!dev_cm_capable(rdev))
  1682. return;
  1683. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1684. /* Find matching cm_dev object */
  1685. down_write(&rdev_sem);
  1686. list_for_each_entry(cm, &cm_dev_list, list) {
  1687. if (cm->mport == rdev->net->hport) {
  1688. found = true;
  1689. break;
  1690. }
  1691. }
  1692. if (!found) {
  1693. up_write(&rdev_sem);
  1694. return;
  1695. }
  1696. /* Remove remote device from the list of peers */
  1697. found = false;
  1698. list_for_each_entry(peer, &cm->peers, node) {
  1699. if (peer->rdev == rdev) {
  1700. riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
  1701. found = true;
  1702. list_del(&peer->node);
  1703. cm->npeers--;
  1704. kfree(peer);
  1705. break;
  1706. }
  1707. }
  1708. up_write(&rdev_sem);
  1709. if (!found)
  1710. return;
  1711. /*
  1712. * Release channels associated with this peer
  1713. */
  1714. spin_lock_bh(&idr_lock);
  1715. idr_for_each_entry(&ch_idr, ch, i) {
  1716. if (ch && ch->rdev == rdev) {
  1717. if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
  1718. riocm_exch(ch, RIO_CM_DISCONNECT);
  1719. idr_remove(&ch_idr, ch->id);
  1720. list_add(&ch->ch_node, &list);
  1721. }
  1722. }
  1723. spin_unlock_bh(&idr_lock);
  1724. if (!list_empty(&list)) {
  1725. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1726. list_del(&ch->ch_node);
  1727. riocm_ch_close(ch);
  1728. }
  1729. }
  1730. }
  1731. /*
  1732. * riocm_cdev_add() - Create rio_cm char device
  1733. * @devno: device number assigned to device (MAJ + MIN)
  1734. */
  1735. static int riocm_cdev_add(dev_t devno)
  1736. {
  1737. int ret;
  1738. cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
  1739. riocm_cdev.cdev.owner = THIS_MODULE;
  1740. ret = cdev_add(&riocm_cdev.cdev, devno, 1);
  1741. if (ret < 0) {
  1742. riocm_error("Cannot register a device with error %d", ret);
  1743. return ret;
  1744. }
  1745. riocm_cdev.dev = device_create(&dev_class, NULL, devno, NULL, DEV_NAME);
  1746. if (IS_ERR(riocm_cdev.dev)) {
  1747. cdev_del(&riocm_cdev.cdev);
  1748. return PTR_ERR(riocm_cdev.dev);
  1749. }
  1750. riocm_debug(MPORT, "Added %s cdev(%d:%d)",
  1751. DEV_NAME, MAJOR(devno), MINOR(devno));
  1752. return 0;
  1753. }
  1754. /*
  1755. * riocm_add_mport - add new local mport device into channel management core
  1756. * @dev: device object associated with mport
  1757. *
  1758. * When a new mport device is added, CM immediately reserves inbound and
  1759. * outbound RapidIO mailboxes that will be used.
  1760. */
  1761. static int riocm_add_mport(struct device *dev)
  1762. {
  1763. int rc;
  1764. int i;
  1765. struct cm_dev *cm;
  1766. struct rio_mport *mport = to_rio_mport(dev);
  1767. riocm_debug(MPORT, "add mport %s", mport->name);
  1768. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  1769. if (!cm)
  1770. return -ENOMEM;
  1771. cm->mport = mport;
  1772. rc = rio_request_outb_mbox(mport, cm, cmbox,
  1773. RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
  1774. if (rc) {
  1775. riocm_error("failed to allocate OBMBOX_%d on %s",
  1776. cmbox, mport->name);
  1777. kfree(cm);
  1778. return -ENODEV;
  1779. }
  1780. rc = rio_request_inb_mbox(mport, cm, cmbox,
  1781. RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
  1782. if (rc) {
  1783. riocm_error("failed to allocate IBMBOX_%d on %s",
  1784. cmbox, mport->name);
  1785. rio_release_outb_mbox(mport, cmbox);
  1786. kfree(cm);
  1787. return -ENODEV;
  1788. }
  1789. cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
  1790. if (!cm->rx_wq) {
  1791. rio_release_inb_mbox(mport, cmbox);
  1792. rio_release_outb_mbox(mport, cmbox);
  1793. kfree(cm);
  1794. return -ENOMEM;
  1795. }
  1796. /*
  1797. * Allocate and register inbound messaging buffers to be ready
  1798. * to receive channel and system management requests
  1799. */
  1800. for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
  1801. cm->rx_buf[i] = NULL;
  1802. cm->rx_slots = RIOCM_RX_RING_SIZE;
  1803. mutex_init(&cm->rx_lock);
  1804. riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
  1805. INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
  1806. cm->tx_slot = 0;
  1807. cm->tx_cnt = 0;
  1808. cm->tx_ack_slot = 0;
  1809. spin_lock_init(&cm->tx_lock);
  1810. INIT_LIST_HEAD(&cm->peers);
  1811. cm->npeers = 0;
  1812. INIT_LIST_HEAD(&cm->tx_reqs);
  1813. down_write(&rdev_sem);
  1814. list_add_tail(&cm->list, &cm_dev_list);
  1815. up_write(&rdev_sem);
  1816. return 0;
  1817. }
  1818. /*
  1819. * riocm_remove_mport - remove local mport device from channel management core
  1820. * @dev: device object associated with mport
  1821. *
  1822. * Removes a local mport device from the list of registered devices that provide
  1823. * channel management services. Returns an error if the specified mport is not
  1824. * registered with the CM core.
  1825. */
  1826. static void riocm_remove_mport(struct device *dev)
  1827. {
  1828. struct rio_mport *mport = to_rio_mport(dev);
  1829. struct cm_dev *cm;
  1830. struct cm_peer *peer, *temp;
  1831. struct rio_channel *ch, *_c;
  1832. unsigned int i;
  1833. bool found = false;
  1834. LIST_HEAD(list);
  1835. riocm_debug(MPORT, "%s", mport->name);
  1836. /* Find a matching cm_dev object */
  1837. down_write(&rdev_sem);
  1838. list_for_each_entry(cm, &cm_dev_list, list) {
  1839. if (cm->mport == mport) {
  1840. list_del(&cm->list);
  1841. found = true;
  1842. break;
  1843. }
  1844. }
  1845. up_write(&rdev_sem);
  1846. if (!found)
  1847. return;
  1848. flush_workqueue(cm->rx_wq);
  1849. destroy_workqueue(cm->rx_wq);
  1850. /* Release channels bound to this mport */
  1851. spin_lock_bh(&idr_lock);
  1852. idr_for_each_entry(&ch_idr, ch, i) {
  1853. if (ch->cmdev == cm) {
  1854. riocm_debug(RDEV, "%s drop ch_%d",
  1855. mport->name, ch->id);
  1856. idr_remove(&ch_idr, ch->id);
  1857. list_add(&ch->ch_node, &list);
  1858. }
  1859. }
  1860. spin_unlock_bh(&idr_lock);
  1861. if (!list_empty(&list)) {
  1862. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1863. list_del(&ch->ch_node);
  1864. riocm_ch_close(ch);
  1865. }
  1866. }
  1867. rio_release_inb_mbox(mport, cmbox);
  1868. rio_release_outb_mbox(mport, cmbox);
  1869. /* Remove and free peer entries */
  1870. if (!list_empty(&cm->peers))
  1871. riocm_debug(RDEV, "ATTN: peer list not empty");
  1872. list_for_each_entry_safe(peer, temp, &cm->peers, node) {
  1873. riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
  1874. list_del(&peer->node);
  1875. kfree(peer);
  1876. }
  1877. riocm_rx_free(cm);
  1878. kfree(cm);
  1879. riocm_debug(MPORT, "%s done", mport->name);
  1880. }
  1881. static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
  1882. void *unused)
  1883. {
  1884. struct rio_channel *ch;
  1885. unsigned int i;
  1886. LIST_HEAD(list);
  1887. riocm_debug(EXIT, ".");
  1888. /*
  1889. * If there are any channels left in connected state send
  1890. * close notification to the connection partner.
  1891. * First build a list of channels that require a closing
  1892. * notification because function riocm_send_close() should
  1893. * be called outside of spinlock protected code.
  1894. */
  1895. spin_lock_bh(&idr_lock);
  1896. idr_for_each_entry(&ch_idr, ch, i) {
  1897. if (ch->state == RIO_CM_CONNECTED) {
  1898. riocm_debug(EXIT, "close ch %d", ch->id);
  1899. idr_remove(&ch_idr, ch->id);
  1900. list_add(&ch->ch_node, &list);
  1901. }
  1902. }
  1903. spin_unlock_bh(&idr_lock);
  1904. list_for_each_entry(ch, &list, ch_node)
  1905. riocm_send_close(ch);
  1906. return NOTIFY_DONE;
  1907. }
  1908. /*
  1909. * riocm_interface handles addition/removal of remote RapidIO devices
  1910. */
  1911. static struct subsys_interface riocm_interface = {
  1912. .name = "rio_cm",
  1913. .subsys = &rio_bus_type,
  1914. .add_dev = riocm_add_dev,
  1915. .remove_dev = riocm_remove_dev,
  1916. };
  1917. /*
  1918. * rio_mport_interface handles addition/removal local mport devices
  1919. */
  1920. static struct class_interface rio_mport_interface __refdata = {
  1921. .class = &rio_mport_class,
  1922. .add_dev = riocm_add_mport,
  1923. .remove_dev = riocm_remove_mport,
  1924. };
  1925. static struct notifier_block rio_cm_notifier = {
  1926. .notifier_call = rio_cm_shutdown,
  1927. };
  1928. static int __init riocm_init(void)
  1929. {
  1930. int ret;
  1931. /* Create device class needed by udev */
  1932. ret = class_register(&dev_class);
  1933. if (ret) {
  1934. riocm_error("Cannot create " DRV_NAME " class");
  1935. return ret;
  1936. }
  1937. ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
  1938. if (ret) {
  1939. class_unregister(&dev_class);
  1940. return ret;
  1941. }
  1942. dev_major = MAJOR(dev_number);
  1943. dev_minor_base = MINOR(dev_number);
  1944. riocm_debug(INIT, "Registered class with %d major", dev_major);
  1945. /*
  1946. * Register as rapidio_port class interface to get notifications about
  1947. * mport additions and removals.
  1948. */
  1949. ret = class_interface_register(&rio_mport_interface);
  1950. if (ret) {
  1951. riocm_error("class_interface_register error: %d", ret);
  1952. goto err_reg;
  1953. }
  1954. /*
  1955. * Register as RapidIO bus interface to get notifications about
  1956. * addition/removal of remote RapidIO devices.
  1957. */
  1958. ret = subsys_interface_register(&riocm_interface);
  1959. if (ret) {
  1960. riocm_error("subsys_interface_register error: %d", ret);
  1961. goto err_cl;
  1962. }
  1963. ret = register_reboot_notifier(&rio_cm_notifier);
  1964. if (ret) {
  1965. riocm_error("failed to register reboot notifier (err=%d)", ret);
  1966. goto err_sif;
  1967. }
  1968. ret = riocm_cdev_add(dev_number);
  1969. if (ret) {
  1970. unregister_reboot_notifier(&rio_cm_notifier);
  1971. ret = -ENODEV;
  1972. goto err_sif;
  1973. }
  1974. return 0;
  1975. err_sif:
  1976. subsys_interface_unregister(&riocm_interface);
  1977. err_cl:
  1978. class_interface_unregister(&rio_mport_interface);
  1979. err_reg:
  1980. unregister_chrdev_region(dev_number, 1);
  1981. class_unregister(&dev_class);
  1982. return ret;
  1983. }
  1984. static void __exit riocm_exit(void)
  1985. {
  1986. riocm_debug(EXIT, "enter");
  1987. unregister_reboot_notifier(&rio_cm_notifier);
  1988. subsys_interface_unregister(&riocm_interface);
  1989. class_interface_unregister(&rio_mport_interface);
  1990. idr_destroy(&ch_idr);
  1991. device_unregister(riocm_cdev.dev);
  1992. cdev_del(&(riocm_cdev.cdev));
  1993. class_unregister(&dev_class);
  1994. unregister_chrdev_region(dev_number, 1);
  1995. }
  1996. late_initcall(riocm_init);
  1997. module_exit(riocm_exit);