rio_cm.c 54 KB

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