rio_cm.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  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. if (len < sizeof(struct rio_ch_chan_hdr))
  666. return -EINVAL; /* insufficient data from user */
  667. ch = riocm_get_channel(ch_id);
  668. if (!ch) {
  669. riocm_error("%s(%d) ch_%d not found", current->comm,
  670. task_pid_nr(current), ch_id);
  671. return -ENODEV;
  672. }
  673. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  674. ret = -EAGAIN;
  675. goto err_out;
  676. }
  677. /*
  678. * Fill buffer header section with corresponding channel data
  679. */
  680. hdr = buf;
  681. hdr->bhdr.src_id = htonl(ch->loc_destid);
  682. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  683. hdr->bhdr.src_mbox = cmbox;
  684. hdr->bhdr.dst_mbox = cmbox;
  685. hdr->bhdr.type = RIO_CM_CHAN;
  686. hdr->ch_op = CM_DATA_MSG;
  687. hdr->dst_ch = htons(ch->rem_channel);
  688. hdr->src_ch = htons(ch->id);
  689. hdr->msg_len = htons((u16)len);
  690. /* ATTN: the function call below relies on the fact that underlying
  691. * HW-specific add_outb_message() routine copies TX data into its own
  692. * internal transfer buffer (true for all RIONET compatible mport
  693. * drivers). Must be reviewed if mport driver uses the buffer directly.
  694. */
  695. ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
  696. if (ret)
  697. riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
  698. err_out:
  699. riocm_put_channel(ch);
  700. return ret;
  701. }
  702. static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
  703. {
  704. int i, ret = -EINVAL;
  705. spin_lock_bh(&ch->lock);
  706. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  707. if (ch->rx_ring.inuse[i] == buf) {
  708. ch->rx_ring.inuse[i] = NULL;
  709. ch->rx_ring.inuse_cnt--;
  710. ret = 0;
  711. break;
  712. }
  713. }
  714. spin_unlock_bh(&ch->lock);
  715. if (!ret)
  716. kfree(buf);
  717. return ret;
  718. }
  719. /*
  720. * riocm_ch_receive - fetch a data packet received for the specified channel
  721. * @ch: local channel ID
  722. * @buf: pointer to a packet buffer
  723. * @timeout: timeout to wait for incoming packet (in jiffies)
  724. *
  725. * Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
  726. * -EAGAIN if a channel is not in CONNECTED state,
  727. * -ENOMEM if in-use tracking queue is full,
  728. * -ETIME if wait timeout expired,
  729. * -EINTR if wait was interrupted.
  730. */
  731. static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
  732. {
  733. void *rxmsg = NULL;
  734. int i, ret = 0;
  735. long wret;
  736. if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
  737. ret = -EAGAIN;
  738. goto out;
  739. }
  740. if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
  741. /* If we do not have entries to track buffers given to upper
  742. * layer, reject request.
  743. */
  744. ret = -ENOMEM;
  745. goto out;
  746. }
  747. wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
  748. riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
  749. if (!wret)
  750. ret = -ETIME;
  751. else if (wret == -ERESTARTSYS)
  752. ret = -EINTR;
  753. else
  754. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
  755. if (ret)
  756. goto out;
  757. spin_lock_bh(&ch->lock);
  758. rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
  759. ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
  760. ch->rx_ring.count--;
  761. ch->rx_ring.tail++;
  762. ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
  763. ret = -ENOMEM;
  764. for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
  765. if (ch->rx_ring.inuse[i] == NULL) {
  766. ch->rx_ring.inuse[i] = rxmsg;
  767. ch->rx_ring.inuse_cnt++;
  768. ret = 0;
  769. break;
  770. }
  771. }
  772. if (ret) {
  773. /* We have no entry to store pending message: drop it */
  774. kfree(rxmsg);
  775. rxmsg = NULL;
  776. }
  777. spin_unlock_bh(&ch->lock);
  778. out:
  779. *buf = rxmsg;
  780. return ret;
  781. }
  782. /*
  783. * riocm_ch_connect - sends a connect request to a remote device
  784. * @loc_ch: local channel ID
  785. * @cm: CM device to send connect request
  786. * @peer: target RapidIO device
  787. * @rem_ch: remote channel ID
  788. *
  789. * Returns: 0 if success, or
  790. * -EINVAL if the channel is not in IDLE state,
  791. * -EAGAIN if no connection request available immediately,
  792. * -ETIME if ACK response timeout expired,
  793. * -EINTR if wait for response was interrupted.
  794. */
  795. static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
  796. struct cm_peer *peer, u16 rem_ch)
  797. {
  798. struct rio_channel *ch = NULL;
  799. struct rio_ch_chan_hdr *hdr;
  800. int ret;
  801. long wret;
  802. ch = riocm_get_channel(loc_ch);
  803. if (!ch)
  804. return -ENODEV;
  805. if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
  806. ret = -EINVAL;
  807. goto conn_done;
  808. }
  809. ch->cmdev = cm;
  810. ch->rdev = peer->rdev;
  811. ch->context = NULL;
  812. ch->loc_destid = cm->mport->host_deviceid;
  813. ch->rem_channel = rem_ch;
  814. /*
  815. * Send connect request to the remote RapidIO device
  816. */
  817. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  818. if (hdr == NULL) {
  819. ret = -ENOMEM;
  820. goto conn_done;
  821. }
  822. hdr->bhdr.src_id = htonl(ch->loc_destid);
  823. hdr->bhdr.dst_id = htonl(peer->rdev->destid);
  824. hdr->bhdr.src_mbox = cmbox;
  825. hdr->bhdr.dst_mbox = cmbox;
  826. hdr->bhdr.type = RIO_CM_CHAN;
  827. hdr->ch_op = CM_CONN_REQ;
  828. hdr->dst_ch = htons(rem_ch);
  829. hdr->src_ch = htons(loc_ch);
  830. /* ATTN: the function call below relies on the fact that underlying
  831. * HW-specific add_outb_message() routine copies TX data into its
  832. * internal transfer buffer. Must be reviewed if mport driver uses
  833. * this buffer directly.
  834. */
  835. ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
  836. if (ret != -EBUSY) {
  837. kfree(hdr);
  838. } else {
  839. ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
  840. if (ret)
  841. kfree(hdr);
  842. }
  843. if (ret) {
  844. riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);
  845. goto conn_done;
  846. }
  847. /* Wait for connect response from the remote device */
  848. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  849. RIOCM_CONNECT_TO * HZ);
  850. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  851. if (!wret)
  852. ret = -ETIME;
  853. else if (wret == -ERESTARTSYS)
  854. ret = -EINTR;
  855. else
  856. ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -1;
  857. conn_done:
  858. riocm_put_channel(ch);
  859. return ret;
  860. }
  861. static int riocm_send_ack(struct rio_channel *ch)
  862. {
  863. struct rio_ch_chan_hdr *hdr;
  864. int ret;
  865. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  866. if (hdr == NULL)
  867. return -ENOMEM;
  868. hdr->bhdr.src_id = htonl(ch->loc_destid);
  869. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  870. hdr->dst_ch = htons(ch->rem_channel);
  871. hdr->src_ch = htons(ch->id);
  872. hdr->bhdr.src_mbox = cmbox;
  873. hdr->bhdr.dst_mbox = cmbox;
  874. hdr->bhdr.type = RIO_CM_CHAN;
  875. hdr->ch_op = CM_CONN_ACK;
  876. /* ATTN: the function call below relies on the fact that underlying
  877. * add_outb_message() routine copies TX data into its internal transfer
  878. * buffer. Review if switching to direct buffer version.
  879. */
  880. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  881. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev,
  882. ch->rdev, hdr, sizeof(*hdr)))
  883. return 0;
  884. kfree(hdr);
  885. if (ret)
  886. riocm_error("send ACK to ch_%d on %s failed (ret=%d)",
  887. ch->id, rio_name(ch->rdev), ret);
  888. return ret;
  889. }
  890. /*
  891. * riocm_ch_accept - accept incoming connection request
  892. * @ch_id: channel ID
  893. * @new_ch_id: local mport device
  894. * @timeout: wait timeout (if 0 non-blocking call, do not wait if connection
  895. * request is not available).
  896. *
  897. * Returns: pointer to new channel struct if success, or error-valued pointer:
  898. * -ENODEV - cannot find specified channel or mport,
  899. * -EINVAL - the channel is not in IDLE state,
  900. * -EAGAIN - no connection request available immediately (timeout=0),
  901. * -ENOMEM - unable to allocate new channel,
  902. * -ETIME - wait timeout expired,
  903. * -EINTR - wait was interrupted.
  904. */
  905. static struct rio_channel *riocm_ch_accept(u16 ch_id, u16 *new_ch_id,
  906. long timeout)
  907. {
  908. struct rio_channel *ch;
  909. struct rio_channel *new_ch;
  910. struct conn_req *req;
  911. struct cm_peer *peer;
  912. int found = 0;
  913. int err = 0;
  914. long wret;
  915. ch = riocm_get_channel(ch_id);
  916. if (!ch)
  917. return ERR_PTR(-EINVAL);
  918. if (!riocm_cmp(ch, RIO_CM_LISTEN)) {
  919. err = -EINVAL;
  920. goto err_put;
  921. }
  922. /* Don't sleep if this is a non blocking call */
  923. if (!timeout) {
  924. if (!try_wait_for_completion(&ch->comp)) {
  925. err = -EAGAIN;
  926. goto err_put;
  927. }
  928. } else {
  929. riocm_debug(WAIT, "on %d", ch->id);
  930. wret = wait_for_completion_interruptible_timeout(&ch->comp,
  931. timeout);
  932. if (!wret) {
  933. err = -ETIME;
  934. goto err_put;
  935. } else if (wret == -ERESTARTSYS) {
  936. err = -EINTR;
  937. goto err_put;
  938. }
  939. }
  940. spin_lock_bh(&ch->lock);
  941. if (ch->state != RIO_CM_LISTEN) {
  942. err = -ECANCELED;
  943. } else if (list_empty(&ch->accept_queue)) {
  944. riocm_debug(WAIT, "on %d accept_queue is empty on completion",
  945. ch->id);
  946. err = -EIO;
  947. }
  948. spin_unlock_bh(&ch->lock);
  949. if (err) {
  950. riocm_debug(WAIT, "on %d returns %d", ch->id, err);
  951. goto err_put;
  952. }
  953. /* Create new channel for this connection */
  954. new_ch = riocm_ch_alloc(RIOCM_CHNUM_AUTO);
  955. if (IS_ERR(new_ch)) {
  956. riocm_error("failed to get channel for new req (%ld)",
  957. PTR_ERR(new_ch));
  958. err = -ENOMEM;
  959. goto err_put;
  960. }
  961. spin_lock_bh(&ch->lock);
  962. req = list_first_entry(&ch->accept_queue, struct conn_req, node);
  963. list_del(&req->node);
  964. new_ch->cmdev = ch->cmdev;
  965. new_ch->loc_destid = ch->loc_destid;
  966. new_ch->rem_destid = req->destid;
  967. new_ch->rem_channel = req->chan;
  968. spin_unlock_bh(&ch->lock);
  969. riocm_put_channel(ch);
  970. ch = NULL;
  971. kfree(req);
  972. down_read(&rdev_sem);
  973. /* Find requester's device object */
  974. list_for_each_entry(peer, &new_ch->cmdev->peers, node) {
  975. if (peer->rdev->destid == new_ch->rem_destid) {
  976. riocm_debug(RX_CMD, "found matching device(%s)",
  977. rio_name(peer->rdev));
  978. found = 1;
  979. break;
  980. }
  981. }
  982. up_read(&rdev_sem);
  983. if (!found) {
  984. /* If peer device object not found, simply ignore the request */
  985. err = -ENODEV;
  986. goto err_put_new_ch;
  987. }
  988. new_ch->rdev = peer->rdev;
  989. new_ch->state = RIO_CM_CONNECTED;
  990. spin_lock_init(&new_ch->lock);
  991. /* Acknowledge the connection request. */
  992. riocm_send_ack(new_ch);
  993. *new_ch_id = new_ch->id;
  994. return new_ch;
  995. err_put_new_ch:
  996. spin_lock_bh(&idr_lock);
  997. idr_remove(&ch_idr, new_ch->id);
  998. spin_unlock_bh(&idr_lock);
  999. riocm_put_channel(new_ch);
  1000. err_put:
  1001. if (ch)
  1002. riocm_put_channel(ch);
  1003. *new_ch_id = 0;
  1004. return ERR_PTR(err);
  1005. }
  1006. /*
  1007. * riocm_ch_listen - puts a channel into LISTEN state
  1008. * @ch_id: channel ID
  1009. *
  1010. * Returns: 0 if success, or
  1011. * -EINVAL if the specified channel does not exists or
  1012. * is not in CHAN_BOUND state.
  1013. */
  1014. static int riocm_ch_listen(u16 ch_id)
  1015. {
  1016. struct rio_channel *ch = NULL;
  1017. int ret = 0;
  1018. riocm_debug(CHOP, "(ch_%d)", ch_id);
  1019. ch = riocm_get_channel(ch_id);
  1020. if (!ch)
  1021. return -EINVAL;
  1022. if (!riocm_cmp_exch(ch, RIO_CM_CHAN_BOUND, RIO_CM_LISTEN))
  1023. ret = -EINVAL;
  1024. riocm_put_channel(ch);
  1025. return ret;
  1026. }
  1027. /*
  1028. * riocm_ch_bind - associate a channel object and an mport device
  1029. * @ch_id: channel ID
  1030. * @mport_id: local mport device ID
  1031. * @context: pointer to the additional caller's context
  1032. *
  1033. * Returns: 0 if success, or
  1034. * -ENODEV if cannot find specified mport,
  1035. * -EINVAL if the specified channel does not exist or
  1036. * is not in IDLE state.
  1037. */
  1038. static int riocm_ch_bind(u16 ch_id, u8 mport_id, void *context)
  1039. {
  1040. struct rio_channel *ch = NULL;
  1041. struct cm_dev *cm;
  1042. int rc = -ENODEV;
  1043. riocm_debug(CHOP, "ch_%d to mport_%d", ch_id, mport_id);
  1044. /* Find matching cm_dev object */
  1045. down_read(&rdev_sem);
  1046. list_for_each_entry(cm, &cm_dev_list, list) {
  1047. if ((cm->mport->id == mport_id) &&
  1048. rio_mport_is_running(cm->mport)) {
  1049. rc = 0;
  1050. break;
  1051. }
  1052. }
  1053. if (rc)
  1054. goto exit;
  1055. ch = riocm_get_channel(ch_id);
  1056. if (!ch) {
  1057. rc = -EINVAL;
  1058. goto exit;
  1059. }
  1060. spin_lock_bh(&ch->lock);
  1061. if (ch->state != RIO_CM_IDLE) {
  1062. spin_unlock_bh(&ch->lock);
  1063. rc = -EINVAL;
  1064. goto err_put;
  1065. }
  1066. ch->cmdev = cm;
  1067. ch->loc_destid = cm->mport->host_deviceid;
  1068. ch->context = context;
  1069. ch->state = RIO_CM_CHAN_BOUND;
  1070. spin_unlock_bh(&ch->lock);
  1071. err_put:
  1072. riocm_put_channel(ch);
  1073. exit:
  1074. up_read(&rdev_sem);
  1075. return rc;
  1076. }
  1077. /*
  1078. * riocm_ch_alloc - channel object allocation helper routine
  1079. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1080. *
  1081. * Return value: pointer to newly created channel object,
  1082. * or error-valued pointer
  1083. */
  1084. static struct rio_channel *riocm_ch_alloc(u16 ch_num)
  1085. {
  1086. int id;
  1087. int start, end;
  1088. struct rio_channel *ch;
  1089. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1090. if (!ch)
  1091. return ERR_PTR(-ENOMEM);
  1092. if (ch_num) {
  1093. /* If requested, try to obtain the specified channel ID */
  1094. start = ch_num;
  1095. end = ch_num + 1;
  1096. } else {
  1097. /* Obtain channel ID from the dynamic allocation range */
  1098. start = chstart;
  1099. end = RIOCM_MAX_CHNUM + 1;
  1100. }
  1101. idr_preload(GFP_KERNEL);
  1102. spin_lock_bh(&idr_lock);
  1103. id = idr_alloc_cyclic(&ch_idr, ch, start, end, GFP_NOWAIT);
  1104. spin_unlock_bh(&idr_lock);
  1105. idr_preload_end();
  1106. if (id < 0) {
  1107. kfree(ch);
  1108. return ERR_PTR(id == -ENOSPC ? -EBUSY : id);
  1109. }
  1110. ch->id = (u16)id;
  1111. ch->state = RIO_CM_IDLE;
  1112. spin_lock_init(&ch->lock);
  1113. INIT_LIST_HEAD(&ch->accept_queue);
  1114. INIT_LIST_HEAD(&ch->ch_node);
  1115. init_completion(&ch->comp);
  1116. init_completion(&ch->comp_close);
  1117. kref_init(&ch->ref);
  1118. ch->rx_ring.head = 0;
  1119. ch->rx_ring.tail = 0;
  1120. ch->rx_ring.count = 0;
  1121. ch->rx_ring.inuse_cnt = 0;
  1122. return ch;
  1123. }
  1124. /*
  1125. * riocm_ch_create - creates a new channel object and allocates ID for it
  1126. * @ch_num: channel ID (1 ... RIOCM_MAX_CHNUM, 0 = automatic)
  1127. *
  1128. * Allocates and initializes a new channel object. If the parameter ch_num > 0
  1129. * and is within the valid range, riocm_ch_create tries to allocate the
  1130. * specified ID for the new channel. If ch_num = 0, channel ID will be assigned
  1131. * automatically from the range (chstart ... RIOCM_MAX_CHNUM).
  1132. * Module parameter 'chstart' defines start of an ID range available for dynamic
  1133. * allocation. Range below 'chstart' is reserved for pre-defined ID numbers.
  1134. * Available channel numbers are limited by 16-bit size of channel numbers used
  1135. * in the packet header.
  1136. *
  1137. * Return value: PTR to rio_channel structure if successful (with channel number
  1138. * updated via pointer) or error-valued pointer if error.
  1139. */
  1140. static struct rio_channel *riocm_ch_create(u16 *ch_num)
  1141. {
  1142. struct rio_channel *ch = NULL;
  1143. ch = riocm_ch_alloc(*ch_num);
  1144. if (IS_ERR(ch))
  1145. riocm_debug(CHOP, "Failed to allocate channel %d (err=%ld)",
  1146. *ch_num, PTR_ERR(ch));
  1147. else
  1148. *ch_num = ch->id;
  1149. return ch;
  1150. }
  1151. /*
  1152. * riocm_ch_free - channel object release routine
  1153. * @ref: pointer to a channel's kref structure
  1154. */
  1155. static void riocm_ch_free(struct kref *ref)
  1156. {
  1157. struct rio_channel *ch = container_of(ref, struct rio_channel, ref);
  1158. int i;
  1159. riocm_debug(CHOP, "(ch_%d)", ch->id);
  1160. if (ch->rx_ring.inuse_cnt) {
  1161. for (i = 0;
  1162. i < RIOCM_RX_RING_SIZE && ch->rx_ring.inuse_cnt; i++) {
  1163. if (ch->rx_ring.inuse[i] != NULL) {
  1164. kfree(ch->rx_ring.inuse[i]);
  1165. ch->rx_ring.inuse_cnt--;
  1166. }
  1167. }
  1168. }
  1169. if (ch->rx_ring.count)
  1170. for (i = 0; i < RIOCM_RX_RING_SIZE && ch->rx_ring.count; i++) {
  1171. if (ch->rx_ring.buf[i] != NULL) {
  1172. kfree(ch->rx_ring.buf[i]);
  1173. ch->rx_ring.count--;
  1174. }
  1175. }
  1176. complete(&ch->comp_close);
  1177. }
  1178. static int riocm_send_close(struct rio_channel *ch)
  1179. {
  1180. struct rio_ch_chan_hdr *hdr;
  1181. int ret;
  1182. /*
  1183. * Send CH_CLOSE notification to the remote RapidIO device
  1184. */
  1185. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  1186. if (hdr == NULL)
  1187. return -ENOMEM;
  1188. hdr->bhdr.src_id = htonl(ch->loc_destid);
  1189. hdr->bhdr.dst_id = htonl(ch->rem_destid);
  1190. hdr->bhdr.src_mbox = cmbox;
  1191. hdr->bhdr.dst_mbox = cmbox;
  1192. hdr->bhdr.type = RIO_CM_CHAN;
  1193. hdr->ch_op = CM_CONN_CLOSE;
  1194. hdr->dst_ch = htons(ch->rem_channel);
  1195. hdr->src_ch = htons(ch->id);
  1196. /* ATTN: the function call below relies on the fact that underlying
  1197. * add_outb_message() routine copies TX data into its internal transfer
  1198. * buffer. Needs to be reviewed if switched to direct buffer mode.
  1199. */
  1200. ret = riocm_post_send(ch->cmdev, ch->rdev, hdr, sizeof(*hdr));
  1201. if (ret == -EBUSY && !riocm_queue_req(ch->cmdev, ch->rdev,
  1202. hdr, sizeof(*hdr)))
  1203. return 0;
  1204. kfree(hdr);
  1205. if (ret)
  1206. riocm_error("ch(%d) send CLOSE failed (ret=%d)", ch->id, ret);
  1207. return ret;
  1208. }
  1209. /*
  1210. * riocm_ch_close - closes a channel object with specified ID (by local request)
  1211. * @ch: channel to be closed
  1212. */
  1213. static int riocm_ch_close(struct rio_channel *ch)
  1214. {
  1215. unsigned long tmo = msecs_to_jiffies(3000);
  1216. enum rio_cm_state state;
  1217. long wret;
  1218. int ret = 0;
  1219. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1220. ch->id, current->comm, task_pid_nr(current));
  1221. state = riocm_exch(ch, RIO_CM_DESTROYING);
  1222. if (state == RIO_CM_CONNECTED)
  1223. riocm_send_close(ch);
  1224. complete_all(&ch->comp);
  1225. riocm_put_channel(ch);
  1226. wret = wait_for_completion_interruptible_timeout(&ch->comp_close, tmo);
  1227. riocm_debug(WAIT, "wait on %d returns %ld", ch->id, wret);
  1228. if (wret == 0) {
  1229. /* Timeout on wait occurred */
  1230. riocm_debug(CHOP, "%s(%d) timed out waiting for ch %d",
  1231. current->comm, task_pid_nr(current), ch->id);
  1232. ret = -ETIMEDOUT;
  1233. } else if (wret == -ERESTARTSYS) {
  1234. /* Wait_for_completion was interrupted by a signal */
  1235. riocm_debug(CHOP, "%s(%d) wait for ch %d was interrupted",
  1236. current->comm, task_pid_nr(current), ch->id);
  1237. ret = -EINTR;
  1238. }
  1239. if (!ret) {
  1240. riocm_debug(CHOP, "ch_%d resources released", ch->id);
  1241. kfree(ch);
  1242. } else {
  1243. riocm_debug(CHOP, "failed to release ch_%d resources", ch->id);
  1244. }
  1245. return ret;
  1246. }
  1247. /*
  1248. * riocm_cdev_open() - Open character device
  1249. */
  1250. static int riocm_cdev_open(struct inode *inode, struct file *filp)
  1251. {
  1252. riocm_debug(INIT, "by %s(%d) filp=%p ",
  1253. current->comm, task_pid_nr(current), filp);
  1254. if (list_empty(&cm_dev_list))
  1255. return -ENODEV;
  1256. return 0;
  1257. }
  1258. /*
  1259. * riocm_cdev_release() - Release character device
  1260. */
  1261. static int riocm_cdev_release(struct inode *inode, struct file *filp)
  1262. {
  1263. struct rio_channel *ch, *_c;
  1264. unsigned int i;
  1265. LIST_HEAD(list);
  1266. riocm_debug(EXIT, "by %s(%d) filp=%p",
  1267. current->comm, task_pid_nr(current), filp);
  1268. /* Check if there are channels associated with this file descriptor */
  1269. spin_lock_bh(&idr_lock);
  1270. idr_for_each_entry(&ch_idr, ch, i) {
  1271. if (ch && ch->filp == filp) {
  1272. riocm_debug(EXIT, "ch_%d not released by %s(%d)",
  1273. ch->id, current->comm,
  1274. task_pid_nr(current));
  1275. idr_remove(&ch_idr, ch->id);
  1276. list_add(&ch->ch_node, &list);
  1277. }
  1278. }
  1279. spin_unlock_bh(&idr_lock);
  1280. if (!list_empty(&list)) {
  1281. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1282. list_del(&ch->ch_node);
  1283. riocm_ch_close(ch);
  1284. }
  1285. }
  1286. return 0;
  1287. }
  1288. /*
  1289. * cm_ep_get_list_size() - Reports number of endpoints in the network
  1290. */
  1291. static int cm_ep_get_list_size(void __user *arg)
  1292. {
  1293. u32 __user *p = arg;
  1294. u32 mport_id;
  1295. u32 count = 0;
  1296. struct cm_dev *cm;
  1297. if (get_user(mport_id, p))
  1298. return -EFAULT;
  1299. if (mport_id >= RIO_MAX_MPORTS)
  1300. return -EINVAL;
  1301. /* Find a matching cm_dev object */
  1302. down_read(&rdev_sem);
  1303. list_for_each_entry(cm, &cm_dev_list, list) {
  1304. if (cm->mport->id == mport_id) {
  1305. count = cm->npeers;
  1306. up_read(&rdev_sem);
  1307. if (copy_to_user(arg, &count, sizeof(u32)))
  1308. return -EFAULT;
  1309. return 0;
  1310. }
  1311. }
  1312. up_read(&rdev_sem);
  1313. return -ENODEV;
  1314. }
  1315. /*
  1316. * cm_ep_get_list() - Returns list of attached endpoints
  1317. */
  1318. static int cm_ep_get_list(void __user *arg)
  1319. {
  1320. struct cm_dev *cm;
  1321. struct cm_peer *peer;
  1322. u32 info[2];
  1323. void *buf;
  1324. u32 nent;
  1325. u32 *entry_ptr;
  1326. u32 i = 0;
  1327. int ret = 0;
  1328. if (copy_from_user(&info, arg, sizeof(info)))
  1329. return -EFAULT;
  1330. if (info[1] >= RIO_MAX_MPORTS || info[0] > RIOCM_MAX_EP_COUNT)
  1331. return -EINVAL;
  1332. /* Find a matching cm_dev object */
  1333. down_read(&rdev_sem);
  1334. list_for_each_entry(cm, &cm_dev_list, list)
  1335. if (cm->mport->id == (u8)info[1])
  1336. goto found;
  1337. up_read(&rdev_sem);
  1338. return -ENODEV;
  1339. found:
  1340. nent = min(info[0], cm->npeers);
  1341. buf = kcalloc(nent + 2, sizeof(u32), GFP_KERNEL);
  1342. if (!buf) {
  1343. up_read(&rdev_sem);
  1344. return -ENOMEM;
  1345. }
  1346. entry_ptr = (u32 *)((uintptr_t)buf + 2*sizeof(u32));
  1347. list_for_each_entry(peer, &cm->peers, node) {
  1348. *entry_ptr = (u32)peer->rdev->destid;
  1349. entry_ptr++;
  1350. if (++i == nent)
  1351. break;
  1352. }
  1353. up_read(&rdev_sem);
  1354. ((u32 *)buf)[0] = i; /* report an updated number of entries */
  1355. ((u32 *)buf)[1] = info[1]; /* put back an mport ID */
  1356. if (copy_to_user(arg, buf, sizeof(u32) * (info[0] + 2)))
  1357. ret = -EFAULT;
  1358. kfree(buf);
  1359. return ret;
  1360. }
  1361. /*
  1362. * cm_mport_get_list() - Returns list of available local mport devices
  1363. */
  1364. static int cm_mport_get_list(void __user *arg)
  1365. {
  1366. int ret = 0;
  1367. u32 entries;
  1368. void *buf;
  1369. struct cm_dev *cm;
  1370. u32 *entry_ptr;
  1371. int count = 0;
  1372. if (copy_from_user(&entries, arg, sizeof(entries)))
  1373. return -EFAULT;
  1374. if (entries == 0 || entries > RIO_MAX_MPORTS)
  1375. return -EINVAL;
  1376. buf = kcalloc(entries + 1, sizeof(u32), GFP_KERNEL);
  1377. if (!buf)
  1378. return -ENOMEM;
  1379. /* Scan all registered cm_dev objects */
  1380. entry_ptr = (u32 *)((uintptr_t)buf + sizeof(u32));
  1381. down_read(&rdev_sem);
  1382. list_for_each_entry(cm, &cm_dev_list, list) {
  1383. if (count++ < entries) {
  1384. *entry_ptr = (cm->mport->id << 16) |
  1385. cm->mport->host_deviceid;
  1386. entry_ptr++;
  1387. }
  1388. }
  1389. up_read(&rdev_sem);
  1390. *((u32 *)buf) = count; /* report a real number of entries */
  1391. if (copy_to_user(arg, buf, sizeof(u32) * (count + 1)))
  1392. ret = -EFAULT;
  1393. kfree(buf);
  1394. return ret;
  1395. }
  1396. /*
  1397. * cm_chan_create() - Create a message exchange channel
  1398. */
  1399. static int cm_chan_create(struct file *filp, void __user *arg)
  1400. {
  1401. u16 __user *p = arg;
  1402. u16 ch_num;
  1403. struct rio_channel *ch;
  1404. if (get_user(ch_num, p))
  1405. return -EFAULT;
  1406. riocm_debug(CHOP, "ch_%d requested by %s(%d)",
  1407. ch_num, current->comm, task_pid_nr(current));
  1408. ch = riocm_ch_create(&ch_num);
  1409. if (IS_ERR(ch))
  1410. return PTR_ERR(ch);
  1411. ch->filp = filp;
  1412. riocm_debug(CHOP, "ch_%d created by %s(%d)",
  1413. ch_num, current->comm, task_pid_nr(current));
  1414. return put_user(ch_num, p);
  1415. }
  1416. /*
  1417. * cm_chan_close() - Close channel
  1418. * @filp: Pointer to file object
  1419. * @arg: Channel to close
  1420. */
  1421. static int cm_chan_close(struct file *filp, void __user *arg)
  1422. {
  1423. u16 __user *p = arg;
  1424. u16 ch_num;
  1425. struct rio_channel *ch;
  1426. if (get_user(ch_num, p))
  1427. return -EFAULT;
  1428. riocm_debug(CHOP, "ch_%d by %s(%d)",
  1429. ch_num, current->comm, task_pid_nr(current));
  1430. spin_lock_bh(&idr_lock);
  1431. ch = idr_find(&ch_idr, ch_num);
  1432. if (!ch) {
  1433. spin_unlock_bh(&idr_lock);
  1434. return 0;
  1435. }
  1436. if (ch->filp != filp) {
  1437. spin_unlock_bh(&idr_lock);
  1438. return -EINVAL;
  1439. }
  1440. idr_remove(&ch_idr, ch->id);
  1441. spin_unlock_bh(&idr_lock);
  1442. return riocm_ch_close(ch);
  1443. }
  1444. /*
  1445. * cm_chan_bind() - Bind channel
  1446. * @arg: Channel number
  1447. */
  1448. static int cm_chan_bind(void __user *arg)
  1449. {
  1450. struct rio_cm_channel chan;
  1451. if (copy_from_user(&chan, arg, sizeof(chan)))
  1452. return -EFAULT;
  1453. if (chan.mport_id >= RIO_MAX_MPORTS)
  1454. return -EINVAL;
  1455. return riocm_ch_bind(chan.id, chan.mport_id, NULL);
  1456. }
  1457. /*
  1458. * cm_chan_listen() - Listen on channel
  1459. * @arg: Channel number
  1460. */
  1461. static int cm_chan_listen(void __user *arg)
  1462. {
  1463. u16 __user *p = arg;
  1464. u16 ch_num;
  1465. if (get_user(ch_num, p))
  1466. return -EFAULT;
  1467. return riocm_ch_listen(ch_num);
  1468. }
  1469. /*
  1470. * cm_chan_accept() - Accept incoming connection
  1471. * @filp: Pointer to file object
  1472. * @arg: Channel number
  1473. */
  1474. static int cm_chan_accept(struct file *filp, void __user *arg)
  1475. {
  1476. struct rio_cm_accept param;
  1477. long accept_to;
  1478. struct rio_channel *ch;
  1479. if (copy_from_user(&param, arg, sizeof(param)))
  1480. return -EFAULT;
  1481. riocm_debug(CHOP, "on ch_%d by %s(%d)",
  1482. param.ch_num, current->comm, task_pid_nr(current));
  1483. accept_to = param.wait_to ?
  1484. msecs_to_jiffies(param.wait_to) : 0;
  1485. ch = riocm_ch_accept(param.ch_num, &param.ch_num, accept_to);
  1486. if (IS_ERR(ch))
  1487. return PTR_ERR(ch);
  1488. ch->filp = filp;
  1489. riocm_debug(CHOP, "new ch_%d for %s(%d)",
  1490. ch->id, current->comm, task_pid_nr(current));
  1491. if (copy_to_user(arg, &param, sizeof(param)))
  1492. return -EFAULT;
  1493. return 0;
  1494. }
  1495. /*
  1496. * cm_chan_connect() - Connect on channel
  1497. * @arg: Channel information
  1498. */
  1499. static int cm_chan_connect(void __user *arg)
  1500. {
  1501. struct rio_cm_channel chan;
  1502. struct cm_dev *cm;
  1503. struct cm_peer *peer;
  1504. int ret = -ENODEV;
  1505. if (copy_from_user(&chan, arg, sizeof(chan)))
  1506. return -EFAULT;
  1507. if (chan.mport_id >= RIO_MAX_MPORTS)
  1508. return -EINVAL;
  1509. down_read(&rdev_sem);
  1510. /* Find matching cm_dev object */
  1511. list_for_each_entry(cm, &cm_dev_list, list) {
  1512. if (cm->mport->id == chan.mport_id) {
  1513. ret = 0;
  1514. break;
  1515. }
  1516. }
  1517. if (ret)
  1518. goto err_out;
  1519. if (chan.remote_destid >= RIO_ANY_DESTID(cm->mport->sys_size)) {
  1520. ret = -EINVAL;
  1521. goto err_out;
  1522. }
  1523. /* Find corresponding RapidIO endpoint device object */
  1524. ret = -ENODEV;
  1525. list_for_each_entry(peer, &cm->peers, node) {
  1526. if (peer->rdev->destid == chan.remote_destid) {
  1527. ret = 0;
  1528. break;
  1529. }
  1530. }
  1531. if (ret)
  1532. goto err_out;
  1533. up_read(&rdev_sem);
  1534. return riocm_ch_connect(chan.id, cm, peer, chan.remote_channel);
  1535. err_out:
  1536. up_read(&rdev_sem);
  1537. return ret;
  1538. }
  1539. /*
  1540. * cm_chan_msg_send() - Send a message through channel
  1541. * @arg: Outbound message information
  1542. */
  1543. static int cm_chan_msg_send(void __user *arg)
  1544. {
  1545. struct rio_cm_msg msg;
  1546. void *buf;
  1547. int ret;
  1548. if (copy_from_user(&msg, arg, sizeof(msg)))
  1549. return -EFAULT;
  1550. if (msg.size > RIO_MAX_MSG_SIZE)
  1551. return -EINVAL;
  1552. buf = memdup_user((void __user *)(uintptr_t)msg.msg, msg.size);
  1553. if (IS_ERR(buf))
  1554. return PTR_ERR(buf);
  1555. ret = riocm_ch_send(msg.ch_num, buf, msg.size);
  1556. kfree(buf);
  1557. return ret;
  1558. }
  1559. /*
  1560. * cm_chan_msg_rcv() - Receive a message through channel
  1561. * @arg: Inbound message information
  1562. */
  1563. static int cm_chan_msg_rcv(void __user *arg)
  1564. {
  1565. struct rio_cm_msg msg;
  1566. struct rio_channel *ch;
  1567. void *buf;
  1568. long rxto;
  1569. int ret = 0, msg_size;
  1570. if (copy_from_user(&msg, arg, sizeof(msg)))
  1571. return -EFAULT;
  1572. if (msg.ch_num == 0 || msg.size == 0)
  1573. return -EINVAL;
  1574. ch = riocm_get_channel(msg.ch_num);
  1575. if (!ch)
  1576. return -ENODEV;
  1577. rxto = msg.rxto ? msecs_to_jiffies(msg.rxto) : MAX_SCHEDULE_TIMEOUT;
  1578. ret = riocm_ch_receive(ch, &buf, rxto);
  1579. if (ret)
  1580. goto out;
  1581. msg_size = min(msg.size, (u16)(RIO_MAX_MSG_SIZE));
  1582. if (copy_to_user((void __user *)(uintptr_t)msg.msg, buf, msg_size))
  1583. ret = -EFAULT;
  1584. riocm_ch_free_rxbuf(ch, buf);
  1585. out:
  1586. riocm_put_channel(ch);
  1587. return ret;
  1588. }
  1589. /*
  1590. * riocm_cdev_ioctl() - IOCTL requests handler
  1591. */
  1592. static long
  1593. riocm_cdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1594. {
  1595. switch (cmd) {
  1596. case RIO_CM_EP_GET_LIST_SIZE:
  1597. return cm_ep_get_list_size((void __user *)arg);
  1598. case RIO_CM_EP_GET_LIST:
  1599. return cm_ep_get_list((void __user *)arg);
  1600. case RIO_CM_CHAN_CREATE:
  1601. return cm_chan_create(filp, (void __user *)arg);
  1602. case RIO_CM_CHAN_CLOSE:
  1603. return cm_chan_close(filp, (void __user *)arg);
  1604. case RIO_CM_CHAN_BIND:
  1605. return cm_chan_bind((void __user *)arg);
  1606. case RIO_CM_CHAN_LISTEN:
  1607. return cm_chan_listen((void __user *)arg);
  1608. case RIO_CM_CHAN_ACCEPT:
  1609. return cm_chan_accept(filp, (void __user *)arg);
  1610. case RIO_CM_CHAN_CONNECT:
  1611. return cm_chan_connect((void __user *)arg);
  1612. case RIO_CM_CHAN_SEND:
  1613. return cm_chan_msg_send((void __user *)arg);
  1614. case RIO_CM_CHAN_RECEIVE:
  1615. return cm_chan_msg_rcv((void __user *)arg);
  1616. case RIO_CM_MPORT_GET_LIST:
  1617. return cm_mport_get_list((void __user *)arg);
  1618. default:
  1619. break;
  1620. }
  1621. return -EINVAL;
  1622. }
  1623. static const struct file_operations riocm_cdev_fops = {
  1624. .owner = THIS_MODULE,
  1625. .open = riocm_cdev_open,
  1626. .release = riocm_cdev_release,
  1627. .unlocked_ioctl = riocm_cdev_ioctl,
  1628. };
  1629. /*
  1630. * riocm_add_dev - add new remote RapidIO device into channel management core
  1631. * @dev: device object associated with RapidIO device
  1632. * @sif: subsystem interface
  1633. *
  1634. * Adds the specified RapidIO device (if applicable) into peers list of
  1635. * the corresponding channel management device (cm_dev).
  1636. */
  1637. static int riocm_add_dev(struct device *dev, struct subsys_interface *sif)
  1638. {
  1639. struct cm_peer *peer;
  1640. struct rio_dev *rdev = to_rio_dev(dev);
  1641. struct cm_dev *cm;
  1642. /* Check if the remote device has capabilities required to support CM */
  1643. if (!dev_cm_capable(rdev))
  1644. return 0;
  1645. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1646. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1647. if (!peer)
  1648. return -ENOMEM;
  1649. /* Find a corresponding cm_dev object */
  1650. down_write(&rdev_sem);
  1651. list_for_each_entry(cm, &cm_dev_list, list) {
  1652. if (cm->mport == rdev->net->hport)
  1653. goto found;
  1654. }
  1655. up_write(&rdev_sem);
  1656. kfree(peer);
  1657. return -ENODEV;
  1658. found:
  1659. peer->rdev = rdev;
  1660. list_add_tail(&peer->node, &cm->peers);
  1661. cm->npeers++;
  1662. up_write(&rdev_sem);
  1663. return 0;
  1664. }
  1665. /*
  1666. * riocm_remove_dev - remove remote RapidIO device from channel management core
  1667. * @dev: device object associated with RapidIO device
  1668. * @sif: subsystem interface
  1669. *
  1670. * Removes the specified RapidIO device (if applicable) from peers list of
  1671. * the corresponding channel management device (cm_dev).
  1672. */
  1673. static void riocm_remove_dev(struct device *dev, struct subsys_interface *sif)
  1674. {
  1675. struct rio_dev *rdev = to_rio_dev(dev);
  1676. struct cm_dev *cm;
  1677. struct cm_peer *peer;
  1678. struct rio_channel *ch, *_c;
  1679. unsigned int i;
  1680. bool found = false;
  1681. LIST_HEAD(list);
  1682. /* Check if the remote device has capabilities required to support CM */
  1683. if (!dev_cm_capable(rdev))
  1684. return;
  1685. riocm_debug(RDEV, "(%s)", rio_name(rdev));
  1686. /* Find matching cm_dev object */
  1687. down_write(&rdev_sem);
  1688. list_for_each_entry(cm, &cm_dev_list, list) {
  1689. if (cm->mport == rdev->net->hport) {
  1690. found = true;
  1691. break;
  1692. }
  1693. }
  1694. if (!found) {
  1695. up_write(&rdev_sem);
  1696. return;
  1697. }
  1698. /* Remove remote device from the list of peers */
  1699. found = false;
  1700. list_for_each_entry(peer, &cm->peers, node) {
  1701. if (peer->rdev == rdev) {
  1702. riocm_debug(RDEV, "removing peer %s", rio_name(rdev));
  1703. found = true;
  1704. list_del(&peer->node);
  1705. cm->npeers--;
  1706. kfree(peer);
  1707. break;
  1708. }
  1709. }
  1710. up_write(&rdev_sem);
  1711. if (!found)
  1712. return;
  1713. /*
  1714. * Release channels associated with this peer
  1715. */
  1716. spin_lock_bh(&idr_lock);
  1717. idr_for_each_entry(&ch_idr, ch, i) {
  1718. if (ch && ch->rdev == rdev) {
  1719. if (atomic_read(&rdev->state) != RIO_DEVICE_SHUTDOWN)
  1720. riocm_exch(ch, RIO_CM_DISCONNECT);
  1721. idr_remove(&ch_idr, ch->id);
  1722. list_add(&ch->ch_node, &list);
  1723. }
  1724. }
  1725. spin_unlock_bh(&idr_lock);
  1726. if (!list_empty(&list)) {
  1727. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1728. list_del(&ch->ch_node);
  1729. riocm_ch_close(ch);
  1730. }
  1731. }
  1732. }
  1733. /*
  1734. * riocm_cdev_add() - Create rio_cm char device
  1735. * @devno: device number assigned to device (MAJ + MIN)
  1736. */
  1737. static int riocm_cdev_add(dev_t devno)
  1738. {
  1739. int ret;
  1740. cdev_init(&riocm_cdev.cdev, &riocm_cdev_fops);
  1741. riocm_cdev.cdev.owner = THIS_MODULE;
  1742. ret = cdev_add(&riocm_cdev.cdev, devno, 1);
  1743. if (ret < 0) {
  1744. riocm_error("Cannot register a device with error %d", ret);
  1745. return ret;
  1746. }
  1747. riocm_cdev.dev = device_create(&dev_class, NULL, devno, NULL, DEV_NAME);
  1748. if (IS_ERR(riocm_cdev.dev)) {
  1749. cdev_del(&riocm_cdev.cdev);
  1750. return PTR_ERR(riocm_cdev.dev);
  1751. }
  1752. riocm_debug(MPORT, "Added %s cdev(%d:%d)",
  1753. DEV_NAME, MAJOR(devno), MINOR(devno));
  1754. return 0;
  1755. }
  1756. /*
  1757. * riocm_add_mport - add new local mport device into channel management core
  1758. * @dev: device object associated with mport
  1759. *
  1760. * When a new mport device is added, CM immediately reserves inbound and
  1761. * outbound RapidIO mailboxes that will be used.
  1762. */
  1763. static int riocm_add_mport(struct device *dev)
  1764. {
  1765. int rc;
  1766. int i;
  1767. struct cm_dev *cm;
  1768. struct rio_mport *mport = to_rio_mport(dev);
  1769. riocm_debug(MPORT, "add mport %s", mport->name);
  1770. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  1771. if (!cm)
  1772. return -ENOMEM;
  1773. cm->mport = mport;
  1774. rc = rio_request_outb_mbox(mport, cm, cmbox,
  1775. RIOCM_TX_RING_SIZE, riocm_outb_msg_event);
  1776. if (rc) {
  1777. riocm_error("failed to allocate OBMBOX_%d on %s",
  1778. cmbox, mport->name);
  1779. kfree(cm);
  1780. return -ENODEV;
  1781. }
  1782. rc = rio_request_inb_mbox(mport, cm, cmbox,
  1783. RIOCM_RX_RING_SIZE, riocm_inb_msg_event);
  1784. if (rc) {
  1785. riocm_error("failed to allocate IBMBOX_%d on %s",
  1786. cmbox, mport->name);
  1787. rio_release_outb_mbox(mport, cmbox);
  1788. kfree(cm);
  1789. return -ENODEV;
  1790. }
  1791. cm->rx_wq = create_workqueue(DRV_NAME "/rxq");
  1792. if (!cm->rx_wq) {
  1793. rio_release_inb_mbox(mport, cmbox);
  1794. rio_release_outb_mbox(mport, cmbox);
  1795. kfree(cm);
  1796. return -ENOMEM;
  1797. }
  1798. /*
  1799. * Allocate and register inbound messaging buffers to be ready
  1800. * to receive channel and system management requests
  1801. */
  1802. for (i = 0; i < RIOCM_RX_RING_SIZE; i++)
  1803. cm->rx_buf[i] = NULL;
  1804. cm->rx_slots = RIOCM_RX_RING_SIZE;
  1805. mutex_init(&cm->rx_lock);
  1806. riocm_rx_fill(cm, RIOCM_RX_RING_SIZE);
  1807. INIT_WORK(&cm->rx_work, rio_ibmsg_handler);
  1808. cm->tx_slot = 0;
  1809. cm->tx_cnt = 0;
  1810. cm->tx_ack_slot = 0;
  1811. spin_lock_init(&cm->tx_lock);
  1812. INIT_LIST_HEAD(&cm->peers);
  1813. cm->npeers = 0;
  1814. INIT_LIST_HEAD(&cm->tx_reqs);
  1815. down_write(&rdev_sem);
  1816. list_add_tail(&cm->list, &cm_dev_list);
  1817. up_write(&rdev_sem);
  1818. return 0;
  1819. }
  1820. /*
  1821. * riocm_remove_mport - remove local mport device from channel management core
  1822. * @dev: device object associated with mport
  1823. *
  1824. * Removes a local mport device from the list of registered devices that provide
  1825. * channel management services. Returns an error if the specified mport is not
  1826. * registered with the CM core.
  1827. */
  1828. static void riocm_remove_mport(struct device *dev)
  1829. {
  1830. struct rio_mport *mport = to_rio_mport(dev);
  1831. struct cm_dev *cm;
  1832. struct cm_peer *peer, *temp;
  1833. struct rio_channel *ch, *_c;
  1834. unsigned int i;
  1835. bool found = false;
  1836. LIST_HEAD(list);
  1837. riocm_debug(MPORT, "%s", mport->name);
  1838. /* Find a matching cm_dev object */
  1839. down_write(&rdev_sem);
  1840. list_for_each_entry(cm, &cm_dev_list, list) {
  1841. if (cm->mport == mport) {
  1842. list_del(&cm->list);
  1843. found = true;
  1844. break;
  1845. }
  1846. }
  1847. up_write(&rdev_sem);
  1848. if (!found)
  1849. return;
  1850. flush_workqueue(cm->rx_wq);
  1851. destroy_workqueue(cm->rx_wq);
  1852. /* Release channels bound to this mport */
  1853. spin_lock_bh(&idr_lock);
  1854. idr_for_each_entry(&ch_idr, ch, i) {
  1855. if (ch->cmdev == cm) {
  1856. riocm_debug(RDEV, "%s drop ch_%d",
  1857. mport->name, ch->id);
  1858. idr_remove(&ch_idr, ch->id);
  1859. list_add(&ch->ch_node, &list);
  1860. }
  1861. }
  1862. spin_unlock_bh(&idr_lock);
  1863. if (!list_empty(&list)) {
  1864. list_for_each_entry_safe(ch, _c, &list, ch_node) {
  1865. list_del(&ch->ch_node);
  1866. riocm_ch_close(ch);
  1867. }
  1868. }
  1869. rio_release_inb_mbox(mport, cmbox);
  1870. rio_release_outb_mbox(mport, cmbox);
  1871. /* Remove and free peer entries */
  1872. if (!list_empty(&cm->peers))
  1873. riocm_debug(RDEV, "ATTN: peer list not empty");
  1874. list_for_each_entry_safe(peer, temp, &cm->peers, node) {
  1875. riocm_debug(RDEV, "removing peer %s", rio_name(peer->rdev));
  1876. list_del(&peer->node);
  1877. kfree(peer);
  1878. }
  1879. riocm_rx_free(cm);
  1880. kfree(cm);
  1881. riocm_debug(MPORT, "%s done", mport->name);
  1882. }
  1883. static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
  1884. void *unused)
  1885. {
  1886. struct rio_channel *ch;
  1887. unsigned int i;
  1888. LIST_HEAD(list);
  1889. riocm_debug(EXIT, ".");
  1890. /*
  1891. * If there are any channels left in connected state send
  1892. * close notification to the connection partner.
  1893. * First build a list of channels that require a closing
  1894. * notification because function riocm_send_close() should
  1895. * be called outside of spinlock protected code.
  1896. */
  1897. spin_lock_bh(&idr_lock);
  1898. idr_for_each_entry(&ch_idr, ch, i) {
  1899. if (ch->state == RIO_CM_CONNECTED) {
  1900. riocm_debug(EXIT, "close ch %d", ch->id);
  1901. idr_remove(&ch_idr, ch->id);
  1902. list_add(&ch->ch_node, &list);
  1903. }
  1904. }
  1905. spin_unlock_bh(&idr_lock);
  1906. list_for_each_entry(ch, &list, ch_node)
  1907. riocm_send_close(ch);
  1908. return NOTIFY_DONE;
  1909. }
  1910. /*
  1911. * riocm_interface handles addition/removal of remote RapidIO devices
  1912. */
  1913. static struct subsys_interface riocm_interface = {
  1914. .name = "rio_cm",
  1915. .subsys = &rio_bus_type,
  1916. .add_dev = riocm_add_dev,
  1917. .remove_dev = riocm_remove_dev,
  1918. };
  1919. /*
  1920. * rio_mport_interface handles addition/removal local mport devices
  1921. */
  1922. static struct class_interface rio_mport_interface __refdata = {
  1923. .class = &rio_mport_class,
  1924. .add_dev = riocm_add_mport,
  1925. .remove_dev = riocm_remove_mport,
  1926. };
  1927. static struct notifier_block rio_cm_notifier = {
  1928. .notifier_call = rio_cm_shutdown,
  1929. };
  1930. static int __init riocm_init(void)
  1931. {
  1932. int ret;
  1933. /* Create device class needed by udev */
  1934. ret = class_register(&dev_class);
  1935. if (ret) {
  1936. riocm_error("Cannot create " DRV_NAME " class");
  1937. return ret;
  1938. }
  1939. ret = alloc_chrdev_region(&dev_number, 0, 1, DRV_NAME);
  1940. if (ret) {
  1941. class_unregister(&dev_class);
  1942. return ret;
  1943. }
  1944. dev_major = MAJOR(dev_number);
  1945. dev_minor_base = MINOR(dev_number);
  1946. riocm_debug(INIT, "Registered class with %d major", dev_major);
  1947. /*
  1948. * Register as rapidio_port class interface to get notifications about
  1949. * mport additions and removals.
  1950. */
  1951. ret = class_interface_register(&rio_mport_interface);
  1952. if (ret) {
  1953. riocm_error("class_interface_register error: %d", ret);
  1954. goto err_reg;
  1955. }
  1956. /*
  1957. * Register as RapidIO bus interface to get notifications about
  1958. * addition/removal of remote RapidIO devices.
  1959. */
  1960. ret = subsys_interface_register(&riocm_interface);
  1961. if (ret) {
  1962. riocm_error("subsys_interface_register error: %d", ret);
  1963. goto err_cl;
  1964. }
  1965. ret = register_reboot_notifier(&rio_cm_notifier);
  1966. if (ret) {
  1967. riocm_error("failed to register reboot notifier (err=%d)", ret);
  1968. goto err_sif;
  1969. }
  1970. ret = riocm_cdev_add(dev_number);
  1971. if (ret) {
  1972. unregister_reboot_notifier(&rio_cm_notifier);
  1973. ret = -ENODEV;
  1974. goto err_sif;
  1975. }
  1976. return 0;
  1977. err_sif:
  1978. subsys_interface_unregister(&riocm_interface);
  1979. err_cl:
  1980. class_interface_unregister(&rio_mport_interface);
  1981. err_reg:
  1982. unregister_chrdev_region(dev_number, 1);
  1983. class_unregister(&dev_class);
  1984. return ret;
  1985. }
  1986. static void __exit riocm_exit(void)
  1987. {
  1988. riocm_debug(EXIT, "enter");
  1989. unregister_reboot_notifier(&rio_cm_notifier);
  1990. subsys_interface_unregister(&riocm_interface);
  1991. class_interface_unregister(&rio_mport_interface);
  1992. idr_destroy(&ch_idr);
  1993. device_unregister(riocm_cdev.dev);
  1994. cdev_del(&(riocm_cdev.cdev));
  1995. class_unregister(&dev_class);
  1996. unregister_chrdev_region(dev_number, 1);
  1997. }
  1998. late_initcall(riocm_init);
  1999. module_exit(riocm_exit);