ldc.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  1. /* ldc.c: Logical Domain Channel link-layer protocol driver.
  2. *
  3. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/delay.h>
  10. #include <linux/errno.h>
  11. #include <linux/string.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/init.h>
  16. #include <asm/hypervisor.h>
  17. #include <asm/iommu.h>
  18. #include <asm/page.h>
  19. #include <asm/ldc.h>
  20. #include <asm/mdesc.h>
  21. #define DRV_MODULE_NAME "ldc"
  22. #define PFX DRV_MODULE_NAME ": "
  23. #define DRV_MODULE_VERSION "1.1"
  24. #define DRV_MODULE_RELDATE "July 22, 2008"
  25. static char version[] __devinitdata =
  26. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  27. #define LDC_PACKET_SIZE 64
  28. /* Packet header layout for unreliable and reliable mode frames.
  29. * When in RAW mode, packets are simply straight 64-byte payloads
  30. * with no headers.
  31. */
  32. struct ldc_packet {
  33. u8 type;
  34. #define LDC_CTRL 0x01
  35. #define LDC_DATA 0x02
  36. #define LDC_ERR 0x10
  37. u8 stype;
  38. #define LDC_INFO 0x01
  39. #define LDC_ACK 0x02
  40. #define LDC_NACK 0x04
  41. u8 ctrl;
  42. #define LDC_VERS 0x01 /* Link Version */
  43. #define LDC_RTS 0x02 /* Request To Send */
  44. #define LDC_RTR 0x03 /* Ready To Receive */
  45. #define LDC_RDX 0x04 /* Ready for Data eXchange */
  46. #define LDC_CTRL_MSK 0x0f
  47. u8 env;
  48. #define LDC_LEN 0x3f
  49. #define LDC_FRAG_MASK 0xc0
  50. #define LDC_START 0x40
  51. #define LDC_STOP 0x80
  52. u32 seqid;
  53. union {
  54. u8 u_data[LDC_PACKET_SIZE - 8];
  55. struct {
  56. u32 pad;
  57. u32 ackid;
  58. u8 r_data[LDC_PACKET_SIZE - 8 - 8];
  59. } r;
  60. } u;
  61. };
  62. struct ldc_version {
  63. u16 major;
  64. u16 minor;
  65. };
  66. /* Ordered from largest major to lowest. */
  67. static struct ldc_version ver_arr[] = {
  68. { .major = 1, .minor = 0 },
  69. };
  70. #define LDC_DEFAULT_MTU (4 * LDC_PACKET_SIZE)
  71. #define LDC_DEFAULT_NUM_ENTRIES (PAGE_SIZE / LDC_PACKET_SIZE)
  72. struct ldc_channel;
  73. struct ldc_mode_ops {
  74. int (*write)(struct ldc_channel *, const void *, unsigned int);
  75. int (*read)(struct ldc_channel *, void *, unsigned int);
  76. };
  77. static const struct ldc_mode_ops raw_ops;
  78. static const struct ldc_mode_ops nonraw_ops;
  79. static const struct ldc_mode_ops stream_ops;
  80. int ldom_domaining_enabled;
  81. struct ldc_iommu {
  82. /* Protects arena alloc/free. */
  83. spinlock_t lock;
  84. struct iommu_arena arena;
  85. struct ldc_mtable_entry *page_table;
  86. };
  87. struct ldc_channel {
  88. /* Protects all operations that depend upon channel state. */
  89. spinlock_t lock;
  90. unsigned long id;
  91. u8 *mssbuf;
  92. u32 mssbuf_len;
  93. u32 mssbuf_off;
  94. struct ldc_packet *tx_base;
  95. unsigned long tx_head;
  96. unsigned long tx_tail;
  97. unsigned long tx_num_entries;
  98. unsigned long tx_ra;
  99. unsigned long tx_acked;
  100. struct ldc_packet *rx_base;
  101. unsigned long rx_head;
  102. unsigned long rx_tail;
  103. unsigned long rx_num_entries;
  104. unsigned long rx_ra;
  105. u32 rcv_nxt;
  106. u32 snd_nxt;
  107. unsigned long chan_state;
  108. struct ldc_channel_config cfg;
  109. void *event_arg;
  110. const struct ldc_mode_ops *mops;
  111. struct ldc_iommu iommu;
  112. struct ldc_version ver;
  113. u8 hs_state;
  114. #define LDC_HS_CLOSED 0x00
  115. #define LDC_HS_OPEN 0x01
  116. #define LDC_HS_GOTVERS 0x02
  117. #define LDC_HS_SENTRTR 0x03
  118. #define LDC_HS_GOTRTR 0x04
  119. #define LDC_HS_COMPLETE 0x10
  120. u8 flags;
  121. #define LDC_FLAG_ALLOCED_QUEUES 0x01
  122. #define LDC_FLAG_REGISTERED_QUEUES 0x02
  123. #define LDC_FLAG_REGISTERED_IRQS 0x04
  124. #define LDC_FLAG_RESET 0x10
  125. u8 mss;
  126. u8 state;
  127. #define LDC_IRQ_NAME_MAX 32
  128. char rx_irq_name[LDC_IRQ_NAME_MAX];
  129. char tx_irq_name[LDC_IRQ_NAME_MAX];
  130. struct hlist_head mh_list;
  131. struct hlist_node list;
  132. };
  133. #define ldcdbg(TYPE, f, a...) \
  134. do { if (lp->cfg.debug & LDC_DEBUG_##TYPE) \
  135. printk(KERN_INFO PFX "ID[%lu] " f, lp->id, ## a); \
  136. } while (0)
  137. static const char *state_to_str(u8 state)
  138. {
  139. switch (state) {
  140. case LDC_STATE_INVALID:
  141. return "INVALID";
  142. case LDC_STATE_INIT:
  143. return "INIT";
  144. case LDC_STATE_BOUND:
  145. return "BOUND";
  146. case LDC_STATE_READY:
  147. return "READY";
  148. case LDC_STATE_CONNECTED:
  149. return "CONNECTED";
  150. default:
  151. return "<UNKNOWN>";
  152. }
  153. }
  154. static void ldc_set_state(struct ldc_channel *lp, u8 state)
  155. {
  156. ldcdbg(STATE, "STATE (%s) --> (%s)\n",
  157. state_to_str(lp->state),
  158. state_to_str(state));
  159. lp->state = state;
  160. }
  161. static unsigned long __advance(unsigned long off, unsigned long num_entries)
  162. {
  163. off += LDC_PACKET_SIZE;
  164. if (off == (num_entries * LDC_PACKET_SIZE))
  165. off = 0;
  166. return off;
  167. }
  168. static unsigned long rx_advance(struct ldc_channel *lp, unsigned long off)
  169. {
  170. return __advance(off, lp->rx_num_entries);
  171. }
  172. static unsigned long tx_advance(struct ldc_channel *lp, unsigned long off)
  173. {
  174. return __advance(off, lp->tx_num_entries);
  175. }
  176. static struct ldc_packet *handshake_get_tx_packet(struct ldc_channel *lp,
  177. unsigned long *new_tail)
  178. {
  179. struct ldc_packet *p;
  180. unsigned long t;
  181. t = tx_advance(lp, lp->tx_tail);
  182. if (t == lp->tx_head)
  183. return NULL;
  184. *new_tail = t;
  185. p = lp->tx_base;
  186. return p + (lp->tx_tail / LDC_PACKET_SIZE);
  187. }
  188. /* When we are in reliable or stream mode, have to track the next packet
  189. * we haven't gotten an ACK for in the TX queue using tx_acked. We have
  190. * to be careful not to stomp over the queue past that point. During
  191. * the handshake, we don't have TX data packets pending in the queue
  192. * and that's why handshake_get_tx_packet() need not be mindful of
  193. * lp->tx_acked.
  194. */
  195. static unsigned long head_for_data(struct ldc_channel *lp)
  196. {
  197. if (lp->cfg.mode == LDC_MODE_STREAM)
  198. return lp->tx_acked;
  199. return lp->tx_head;
  200. }
  201. static int tx_has_space_for(struct ldc_channel *lp, unsigned int size)
  202. {
  203. unsigned long limit, tail, new_tail, diff;
  204. unsigned int mss;
  205. limit = head_for_data(lp);
  206. tail = lp->tx_tail;
  207. new_tail = tx_advance(lp, tail);
  208. if (new_tail == limit)
  209. return 0;
  210. if (limit > new_tail)
  211. diff = limit - new_tail;
  212. else
  213. diff = (limit +
  214. ((lp->tx_num_entries * LDC_PACKET_SIZE) - new_tail));
  215. diff /= LDC_PACKET_SIZE;
  216. mss = lp->mss;
  217. if (diff * mss < size)
  218. return 0;
  219. return 1;
  220. }
  221. static struct ldc_packet *data_get_tx_packet(struct ldc_channel *lp,
  222. unsigned long *new_tail)
  223. {
  224. struct ldc_packet *p;
  225. unsigned long h, t;
  226. h = head_for_data(lp);
  227. t = tx_advance(lp, lp->tx_tail);
  228. if (t == h)
  229. return NULL;
  230. *new_tail = t;
  231. p = lp->tx_base;
  232. return p + (lp->tx_tail / LDC_PACKET_SIZE);
  233. }
  234. static int set_tx_tail(struct ldc_channel *lp, unsigned long tail)
  235. {
  236. unsigned long orig_tail = lp->tx_tail;
  237. int limit = 1000;
  238. lp->tx_tail = tail;
  239. while (limit-- > 0) {
  240. unsigned long err;
  241. err = sun4v_ldc_tx_set_qtail(lp->id, tail);
  242. if (!err)
  243. return 0;
  244. if (err != HV_EWOULDBLOCK) {
  245. lp->tx_tail = orig_tail;
  246. return -EINVAL;
  247. }
  248. udelay(1);
  249. }
  250. lp->tx_tail = orig_tail;
  251. return -EBUSY;
  252. }
  253. /* This just updates the head value in the hypervisor using
  254. * a polling loop with a timeout. The caller takes care of
  255. * upating software state representing the head change, if any.
  256. */
  257. static int __set_rx_head(struct ldc_channel *lp, unsigned long head)
  258. {
  259. int limit = 1000;
  260. while (limit-- > 0) {
  261. unsigned long err;
  262. err = sun4v_ldc_rx_set_qhead(lp->id, head);
  263. if (!err)
  264. return 0;
  265. if (err != HV_EWOULDBLOCK)
  266. return -EINVAL;
  267. udelay(1);
  268. }
  269. return -EBUSY;
  270. }
  271. static int send_tx_packet(struct ldc_channel *lp,
  272. struct ldc_packet *p,
  273. unsigned long new_tail)
  274. {
  275. BUG_ON(p != (lp->tx_base + (lp->tx_tail / LDC_PACKET_SIZE)));
  276. return set_tx_tail(lp, new_tail);
  277. }
  278. static struct ldc_packet *handshake_compose_ctrl(struct ldc_channel *lp,
  279. u8 stype, u8 ctrl,
  280. void *data, int dlen,
  281. unsigned long *new_tail)
  282. {
  283. struct ldc_packet *p = handshake_get_tx_packet(lp, new_tail);
  284. if (p) {
  285. memset(p, 0, sizeof(*p));
  286. p->type = LDC_CTRL;
  287. p->stype = stype;
  288. p->ctrl = ctrl;
  289. if (data)
  290. memcpy(p->u.u_data, data, dlen);
  291. }
  292. return p;
  293. }
  294. static int start_handshake(struct ldc_channel *lp)
  295. {
  296. struct ldc_packet *p;
  297. struct ldc_version *ver;
  298. unsigned long new_tail;
  299. ver = &ver_arr[0];
  300. ldcdbg(HS, "SEND VER INFO maj[%u] min[%u]\n",
  301. ver->major, ver->minor);
  302. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
  303. ver, sizeof(*ver), &new_tail);
  304. if (p) {
  305. int err = send_tx_packet(lp, p, new_tail);
  306. if (!err)
  307. lp->flags &= ~LDC_FLAG_RESET;
  308. return err;
  309. }
  310. return -EBUSY;
  311. }
  312. static int send_version_nack(struct ldc_channel *lp,
  313. u16 major, u16 minor)
  314. {
  315. struct ldc_packet *p;
  316. struct ldc_version ver;
  317. unsigned long new_tail;
  318. ver.major = major;
  319. ver.minor = minor;
  320. p = handshake_compose_ctrl(lp, LDC_NACK, LDC_VERS,
  321. &ver, sizeof(ver), &new_tail);
  322. if (p) {
  323. ldcdbg(HS, "SEND VER NACK maj[%u] min[%u]\n",
  324. ver.major, ver.minor);
  325. return send_tx_packet(lp, p, new_tail);
  326. }
  327. return -EBUSY;
  328. }
  329. static int send_version_ack(struct ldc_channel *lp,
  330. struct ldc_version *vp)
  331. {
  332. struct ldc_packet *p;
  333. unsigned long new_tail;
  334. p = handshake_compose_ctrl(lp, LDC_ACK, LDC_VERS,
  335. vp, sizeof(*vp), &new_tail);
  336. if (p) {
  337. ldcdbg(HS, "SEND VER ACK maj[%u] min[%u]\n",
  338. vp->major, vp->minor);
  339. return send_tx_packet(lp, p, new_tail);
  340. }
  341. return -EBUSY;
  342. }
  343. static int send_rts(struct ldc_channel *lp)
  344. {
  345. struct ldc_packet *p;
  346. unsigned long new_tail;
  347. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTS, NULL, 0,
  348. &new_tail);
  349. if (p) {
  350. p->env = lp->cfg.mode;
  351. p->seqid = 0;
  352. lp->rcv_nxt = 0;
  353. ldcdbg(HS, "SEND RTS env[0x%x] seqid[0x%x]\n",
  354. p->env, p->seqid);
  355. return send_tx_packet(lp, p, new_tail);
  356. }
  357. return -EBUSY;
  358. }
  359. static int send_rtr(struct ldc_channel *lp)
  360. {
  361. struct ldc_packet *p;
  362. unsigned long new_tail;
  363. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTR, NULL, 0,
  364. &new_tail);
  365. if (p) {
  366. p->env = lp->cfg.mode;
  367. p->seqid = 0;
  368. ldcdbg(HS, "SEND RTR env[0x%x] seqid[0x%x]\n",
  369. p->env, p->seqid);
  370. return send_tx_packet(lp, p, new_tail);
  371. }
  372. return -EBUSY;
  373. }
  374. static int send_rdx(struct ldc_channel *lp)
  375. {
  376. struct ldc_packet *p;
  377. unsigned long new_tail;
  378. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RDX, NULL, 0,
  379. &new_tail);
  380. if (p) {
  381. p->env = 0;
  382. p->seqid = ++lp->snd_nxt;
  383. p->u.r.ackid = lp->rcv_nxt;
  384. ldcdbg(HS, "SEND RDX env[0x%x] seqid[0x%x] ackid[0x%x]\n",
  385. p->env, p->seqid, p->u.r.ackid);
  386. return send_tx_packet(lp, p, new_tail);
  387. }
  388. return -EBUSY;
  389. }
  390. static int send_data_nack(struct ldc_channel *lp, struct ldc_packet *data_pkt)
  391. {
  392. struct ldc_packet *p;
  393. unsigned long new_tail;
  394. int err;
  395. p = data_get_tx_packet(lp, &new_tail);
  396. if (!p)
  397. return -EBUSY;
  398. memset(p, 0, sizeof(*p));
  399. p->type = data_pkt->type;
  400. p->stype = LDC_NACK;
  401. p->ctrl = data_pkt->ctrl & LDC_CTRL_MSK;
  402. p->seqid = lp->snd_nxt + 1;
  403. p->u.r.ackid = lp->rcv_nxt;
  404. ldcdbg(HS, "SEND DATA NACK type[0x%x] ctl[0x%x] seq[0x%x] ack[0x%x]\n",
  405. p->type, p->ctrl, p->seqid, p->u.r.ackid);
  406. err = send_tx_packet(lp, p, new_tail);
  407. if (!err)
  408. lp->snd_nxt++;
  409. return err;
  410. }
  411. static int ldc_abort(struct ldc_channel *lp)
  412. {
  413. unsigned long hv_err;
  414. ldcdbg(STATE, "ABORT\n");
  415. /* We report but do not act upon the hypervisor errors because
  416. * there really isn't much we can do if they fail at this point.
  417. */
  418. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  419. if (hv_err)
  420. printk(KERN_ERR PFX "ldc_abort: "
  421. "sun4v_ldc_tx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
  422. lp->id, lp->tx_ra, lp->tx_num_entries, hv_err);
  423. hv_err = sun4v_ldc_tx_get_state(lp->id,
  424. &lp->tx_head,
  425. &lp->tx_tail,
  426. &lp->chan_state);
  427. if (hv_err)
  428. printk(KERN_ERR PFX "ldc_abort: "
  429. "sun4v_ldc_tx_get_state(%lx,...) failed, err=%lu\n",
  430. lp->id, hv_err);
  431. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  432. if (hv_err)
  433. printk(KERN_ERR PFX "ldc_abort: "
  434. "sun4v_ldc_rx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
  435. lp->id, lp->rx_ra, lp->rx_num_entries, hv_err);
  436. /* Refetch the RX queue state as well, because we could be invoked
  437. * here in the queue processing context.
  438. */
  439. hv_err = sun4v_ldc_rx_get_state(lp->id,
  440. &lp->rx_head,
  441. &lp->rx_tail,
  442. &lp->chan_state);
  443. if (hv_err)
  444. printk(KERN_ERR PFX "ldc_abort: "
  445. "sun4v_ldc_rx_get_state(%lx,...) failed, err=%lu\n",
  446. lp->id, hv_err);
  447. return -ECONNRESET;
  448. }
  449. static struct ldc_version *find_by_major(u16 major)
  450. {
  451. struct ldc_version *ret = NULL;
  452. int i;
  453. for (i = 0; i < ARRAY_SIZE(ver_arr); i++) {
  454. struct ldc_version *v = &ver_arr[i];
  455. if (v->major <= major) {
  456. ret = v;
  457. break;
  458. }
  459. }
  460. return ret;
  461. }
  462. static int process_ver_info(struct ldc_channel *lp, struct ldc_version *vp)
  463. {
  464. struct ldc_version *vap;
  465. int err;
  466. ldcdbg(HS, "GOT VERSION INFO major[%x] minor[%x]\n",
  467. vp->major, vp->minor);
  468. if (lp->hs_state == LDC_HS_GOTVERS) {
  469. lp->hs_state = LDC_HS_OPEN;
  470. memset(&lp->ver, 0, sizeof(lp->ver));
  471. }
  472. vap = find_by_major(vp->major);
  473. if (!vap) {
  474. err = send_version_nack(lp, 0, 0);
  475. } else if (vap->major != vp->major) {
  476. err = send_version_nack(lp, vap->major, vap->minor);
  477. } else {
  478. struct ldc_version ver = *vp;
  479. if (ver.minor > vap->minor)
  480. ver.minor = vap->minor;
  481. err = send_version_ack(lp, &ver);
  482. if (!err) {
  483. lp->ver = ver;
  484. lp->hs_state = LDC_HS_GOTVERS;
  485. }
  486. }
  487. if (err)
  488. return ldc_abort(lp);
  489. return 0;
  490. }
  491. static int process_ver_ack(struct ldc_channel *lp, struct ldc_version *vp)
  492. {
  493. ldcdbg(HS, "GOT VERSION ACK major[%x] minor[%x]\n",
  494. vp->major, vp->minor);
  495. if (lp->hs_state == LDC_HS_GOTVERS) {
  496. if (lp->ver.major != vp->major ||
  497. lp->ver.minor != vp->minor)
  498. return ldc_abort(lp);
  499. } else {
  500. lp->ver = *vp;
  501. lp->hs_state = LDC_HS_GOTVERS;
  502. }
  503. if (send_rts(lp))
  504. return ldc_abort(lp);
  505. return 0;
  506. }
  507. static int process_ver_nack(struct ldc_channel *lp, struct ldc_version *vp)
  508. {
  509. struct ldc_version *vap;
  510. struct ldc_packet *p;
  511. unsigned long new_tail;
  512. if (vp->major == 0 && vp->minor == 0)
  513. return ldc_abort(lp);
  514. vap = find_by_major(vp->major);
  515. if (!vap)
  516. return ldc_abort(lp);
  517. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
  518. vap, sizeof(*vap),
  519. &new_tail);
  520. if (!p)
  521. return ldc_abort(lp);
  522. return send_tx_packet(lp, p, new_tail);
  523. }
  524. static int process_version(struct ldc_channel *lp,
  525. struct ldc_packet *p)
  526. {
  527. struct ldc_version *vp;
  528. vp = (struct ldc_version *) p->u.u_data;
  529. switch (p->stype) {
  530. case LDC_INFO:
  531. return process_ver_info(lp, vp);
  532. case LDC_ACK:
  533. return process_ver_ack(lp, vp);
  534. case LDC_NACK:
  535. return process_ver_nack(lp, vp);
  536. default:
  537. return ldc_abort(lp);
  538. }
  539. }
  540. static int process_rts(struct ldc_channel *lp,
  541. struct ldc_packet *p)
  542. {
  543. ldcdbg(HS, "GOT RTS stype[%x] seqid[%x] env[%x]\n",
  544. p->stype, p->seqid, p->env);
  545. if (p->stype != LDC_INFO ||
  546. lp->hs_state != LDC_HS_GOTVERS ||
  547. p->env != lp->cfg.mode)
  548. return ldc_abort(lp);
  549. lp->snd_nxt = p->seqid;
  550. lp->rcv_nxt = p->seqid;
  551. lp->hs_state = LDC_HS_SENTRTR;
  552. if (send_rtr(lp))
  553. return ldc_abort(lp);
  554. return 0;
  555. }
  556. static int process_rtr(struct ldc_channel *lp,
  557. struct ldc_packet *p)
  558. {
  559. ldcdbg(HS, "GOT RTR stype[%x] seqid[%x] env[%x]\n",
  560. p->stype, p->seqid, p->env);
  561. if (p->stype != LDC_INFO ||
  562. p->env != lp->cfg.mode)
  563. return ldc_abort(lp);
  564. lp->snd_nxt = p->seqid;
  565. lp->hs_state = LDC_HS_COMPLETE;
  566. ldc_set_state(lp, LDC_STATE_CONNECTED);
  567. send_rdx(lp);
  568. return LDC_EVENT_UP;
  569. }
  570. static int rx_seq_ok(struct ldc_channel *lp, u32 seqid)
  571. {
  572. return lp->rcv_nxt + 1 == seqid;
  573. }
  574. static int process_rdx(struct ldc_channel *lp,
  575. struct ldc_packet *p)
  576. {
  577. ldcdbg(HS, "GOT RDX stype[%x] seqid[%x] env[%x] ackid[%x]\n",
  578. p->stype, p->seqid, p->env, p->u.r.ackid);
  579. if (p->stype != LDC_INFO ||
  580. !(rx_seq_ok(lp, p->seqid)))
  581. return ldc_abort(lp);
  582. lp->rcv_nxt = p->seqid;
  583. lp->hs_state = LDC_HS_COMPLETE;
  584. ldc_set_state(lp, LDC_STATE_CONNECTED);
  585. return LDC_EVENT_UP;
  586. }
  587. static int process_control_frame(struct ldc_channel *lp,
  588. struct ldc_packet *p)
  589. {
  590. switch (p->ctrl) {
  591. case LDC_VERS:
  592. return process_version(lp, p);
  593. case LDC_RTS:
  594. return process_rts(lp, p);
  595. case LDC_RTR:
  596. return process_rtr(lp, p);
  597. case LDC_RDX:
  598. return process_rdx(lp, p);
  599. default:
  600. return ldc_abort(lp);
  601. }
  602. }
  603. static int process_error_frame(struct ldc_channel *lp,
  604. struct ldc_packet *p)
  605. {
  606. return ldc_abort(lp);
  607. }
  608. static int process_data_ack(struct ldc_channel *lp,
  609. struct ldc_packet *ack)
  610. {
  611. unsigned long head = lp->tx_acked;
  612. u32 ackid = ack->u.r.ackid;
  613. while (1) {
  614. struct ldc_packet *p = lp->tx_base + (head / LDC_PACKET_SIZE);
  615. head = tx_advance(lp, head);
  616. if (p->seqid == ackid) {
  617. lp->tx_acked = head;
  618. return 0;
  619. }
  620. if (head == lp->tx_tail)
  621. return ldc_abort(lp);
  622. }
  623. return 0;
  624. }
  625. static void send_events(struct ldc_channel *lp, unsigned int event_mask)
  626. {
  627. if (event_mask & LDC_EVENT_RESET)
  628. lp->cfg.event(lp->event_arg, LDC_EVENT_RESET);
  629. if (event_mask & LDC_EVENT_UP)
  630. lp->cfg.event(lp->event_arg, LDC_EVENT_UP);
  631. if (event_mask & LDC_EVENT_DATA_READY)
  632. lp->cfg.event(lp->event_arg, LDC_EVENT_DATA_READY);
  633. }
  634. static irqreturn_t ldc_rx(int irq, void *dev_id)
  635. {
  636. struct ldc_channel *lp = dev_id;
  637. unsigned long orig_state, hv_err, flags;
  638. unsigned int event_mask;
  639. spin_lock_irqsave(&lp->lock, flags);
  640. orig_state = lp->chan_state;
  641. hv_err = sun4v_ldc_rx_get_state(lp->id,
  642. &lp->rx_head,
  643. &lp->rx_tail,
  644. &lp->chan_state);
  645. ldcdbg(RX, "RX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
  646. orig_state, lp->chan_state, lp->rx_head, lp->rx_tail);
  647. event_mask = 0;
  648. if (lp->cfg.mode == LDC_MODE_RAW &&
  649. lp->chan_state == LDC_CHANNEL_UP) {
  650. lp->hs_state = LDC_HS_COMPLETE;
  651. ldc_set_state(lp, LDC_STATE_CONNECTED);
  652. event_mask |= LDC_EVENT_UP;
  653. orig_state = lp->chan_state;
  654. }
  655. /* If we are in reset state, flush the RX queue and ignore
  656. * everything.
  657. */
  658. if (lp->flags & LDC_FLAG_RESET) {
  659. (void) __set_rx_head(lp, lp->rx_tail);
  660. goto out;
  661. }
  662. /* Once we finish the handshake, we let the ldc_read()
  663. * paths do all of the control frame and state management.
  664. * Just trigger the callback.
  665. */
  666. if (lp->hs_state == LDC_HS_COMPLETE) {
  667. handshake_complete:
  668. if (lp->chan_state != orig_state) {
  669. unsigned int event = LDC_EVENT_RESET;
  670. if (lp->chan_state == LDC_CHANNEL_UP)
  671. event = LDC_EVENT_UP;
  672. event_mask |= event;
  673. }
  674. if (lp->rx_head != lp->rx_tail)
  675. event_mask |= LDC_EVENT_DATA_READY;
  676. goto out;
  677. }
  678. if (lp->chan_state != orig_state)
  679. goto out;
  680. while (lp->rx_head != lp->rx_tail) {
  681. struct ldc_packet *p;
  682. unsigned long new;
  683. int err;
  684. p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
  685. switch (p->type) {
  686. case LDC_CTRL:
  687. err = process_control_frame(lp, p);
  688. if (err > 0)
  689. event_mask |= err;
  690. break;
  691. case LDC_DATA:
  692. event_mask |= LDC_EVENT_DATA_READY;
  693. err = 0;
  694. break;
  695. case LDC_ERR:
  696. err = process_error_frame(lp, p);
  697. break;
  698. default:
  699. err = ldc_abort(lp);
  700. break;
  701. }
  702. if (err < 0)
  703. break;
  704. new = lp->rx_head;
  705. new += LDC_PACKET_SIZE;
  706. if (new == (lp->rx_num_entries * LDC_PACKET_SIZE))
  707. new = 0;
  708. lp->rx_head = new;
  709. err = __set_rx_head(lp, new);
  710. if (err < 0) {
  711. (void) ldc_abort(lp);
  712. break;
  713. }
  714. if (lp->hs_state == LDC_HS_COMPLETE)
  715. goto handshake_complete;
  716. }
  717. out:
  718. spin_unlock_irqrestore(&lp->lock, flags);
  719. send_events(lp, event_mask);
  720. return IRQ_HANDLED;
  721. }
  722. static irqreturn_t ldc_tx(int irq, void *dev_id)
  723. {
  724. struct ldc_channel *lp = dev_id;
  725. unsigned long flags, hv_err, orig_state;
  726. unsigned int event_mask = 0;
  727. spin_lock_irqsave(&lp->lock, flags);
  728. orig_state = lp->chan_state;
  729. hv_err = sun4v_ldc_tx_get_state(lp->id,
  730. &lp->tx_head,
  731. &lp->tx_tail,
  732. &lp->chan_state);
  733. ldcdbg(TX, " TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
  734. orig_state, lp->chan_state, lp->tx_head, lp->tx_tail);
  735. if (lp->cfg.mode == LDC_MODE_RAW &&
  736. lp->chan_state == LDC_CHANNEL_UP) {
  737. lp->hs_state = LDC_HS_COMPLETE;
  738. ldc_set_state(lp, LDC_STATE_CONNECTED);
  739. event_mask |= LDC_EVENT_UP;
  740. }
  741. spin_unlock_irqrestore(&lp->lock, flags);
  742. send_events(lp, event_mask);
  743. return IRQ_HANDLED;
  744. }
  745. /* XXX ldc_alloc() and ldc_free() needs to run under a mutex so
  746. * XXX that addition and removal from the ldc_channel_list has
  747. * XXX atomicity, otherwise the __ldc_channel_exists() check is
  748. * XXX totally pointless as another thread can slip into ldc_alloc()
  749. * XXX and add a channel with the same ID. There also needs to be
  750. * XXX a spinlock for ldc_channel_list.
  751. */
  752. static HLIST_HEAD(ldc_channel_list);
  753. static int __ldc_channel_exists(unsigned long id)
  754. {
  755. struct ldc_channel *lp;
  756. struct hlist_node *n;
  757. hlist_for_each_entry(lp, n, &ldc_channel_list, list) {
  758. if (lp->id == id)
  759. return 1;
  760. }
  761. return 0;
  762. }
  763. static int alloc_queue(const char *name, unsigned long num_entries,
  764. struct ldc_packet **base, unsigned long *ra)
  765. {
  766. unsigned long size, order;
  767. void *q;
  768. size = num_entries * LDC_PACKET_SIZE;
  769. order = get_order(size);
  770. q = (void *) __get_free_pages(GFP_KERNEL, order);
  771. if (!q) {
  772. printk(KERN_ERR PFX "Alloc of %s queue failed with "
  773. "size=%lu order=%lu\n", name, size, order);
  774. return -ENOMEM;
  775. }
  776. memset(q, 0, PAGE_SIZE << order);
  777. *base = q;
  778. *ra = __pa(q);
  779. return 0;
  780. }
  781. static void free_queue(unsigned long num_entries, struct ldc_packet *q)
  782. {
  783. unsigned long size, order;
  784. if (!q)
  785. return;
  786. size = num_entries * LDC_PACKET_SIZE;
  787. order = get_order(size);
  788. free_pages((unsigned long)q, order);
  789. }
  790. /* XXX Make this configurable... XXX */
  791. #define LDC_IOTABLE_SIZE (8 * 1024)
  792. static int ldc_iommu_init(struct ldc_channel *lp)
  793. {
  794. unsigned long sz, num_tsb_entries, tsbsize, order;
  795. struct ldc_iommu *iommu = &lp->iommu;
  796. struct ldc_mtable_entry *table;
  797. unsigned long hv_err;
  798. int err;
  799. num_tsb_entries = LDC_IOTABLE_SIZE;
  800. tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
  801. spin_lock_init(&iommu->lock);
  802. sz = num_tsb_entries / 8;
  803. sz = (sz + 7UL) & ~7UL;
  804. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  805. if (!iommu->arena.map) {
  806. printk(KERN_ERR PFX "Alloc of arena map failed, sz=%lu\n", sz);
  807. return -ENOMEM;
  808. }
  809. iommu->arena.limit = num_tsb_entries;
  810. order = get_order(tsbsize);
  811. table = (struct ldc_mtable_entry *)
  812. __get_free_pages(GFP_KERNEL, order);
  813. err = -ENOMEM;
  814. if (!table) {
  815. printk(KERN_ERR PFX "Alloc of MTE table failed, "
  816. "size=%lu order=%lu\n", tsbsize, order);
  817. goto out_free_map;
  818. }
  819. memset(table, 0, PAGE_SIZE << order);
  820. iommu->page_table = table;
  821. hv_err = sun4v_ldc_set_map_table(lp->id, __pa(table),
  822. num_tsb_entries);
  823. err = -EINVAL;
  824. if (hv_err)
  825. goto out_free_table;
  826. return 0;
  827. out_free_table:
  828. free_pages((unsigned long) table, order);
  829. iommu->page_table = NULL;
  830. out_free_map:
  831. kfree(iommu->arena.map);
  832. iommu->arena.map = NULL;
  833. return err;
  834. }
  835. static void ldc_iommu_release(struct ldc_channel *lp)
  836. {
  837. struct ldc_iommu *iommu = &lp->iommu;
  838. unsigned long num_tsb_entries, tsbsize, order;
  839. (void) sun4v_ldc_set_map_table(lp->id, 0, 0);
  840. num_tsb_entries = iommu->arena.limit;
  841. tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
  842. order = get_order(tsbsize);
  843. free_pages((unsigned long) iommu->page_table, order);
  844. iommu->page_table = NULL;
  845. kfree(iommu->arena.map);
  846. iommu->arena.map = NULL;
  847. }
  848. struct ldc_channel *ldc_alloc(unsigned long id,
  849. const struct ldc_channel_config *cfgp,
  850. void *event_arg)
  851. {
  852. struct ldc_channel *lp;
  853. const struct ldc_mode_ops *mops;
  854. unsigned long dummy1, dummy2, hv_err;
  855. u8 mss, *mssbuf;
  856. int err;
  857. err = -ENODEV;
  858. if (!ldom_domaining_enabled)
  859. goto out_err;
  860. err = -EINVAL;
  861. if (!cfgp)
  862. goto out_err;
  863. switch (cfgp->mode) {
  864. case LDC_MODE_RAW:
  865. mops = &raw_ops;
  866. mss = LDC_PACKET_SIZE;
  867. break;
  868. case LDC_MODE_UNRELIABLE:
  869. mops = &nonraw_ops;
  870. mss = LDC_PACKET_SIZE - 8;
  871. break;
  872. case LDC_MODE_STREAM:
  873. mops = &stream_ops;
  874. mss = LDC_PACKET_SIZE - 8 - 8;
  875. break;
  876. default:
  877. goto out_err;
  878. }
  879. if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq)
  880. goto out_err;
  881. hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2);
  882. err = -ENODEV;
  883. if (hv_err == HV_ECHANNEL)
  884. goto out_err;
  885. err = -EEXIST;
  886. if (__ldc_channel_exists(id))
  887. goto out_err;
  888. mssbuf = NULL;
  889. lp = kzalloc(sizeof(*lp), GFP_KERNEL);
  890. err = -ENOMEM;
  891. if (!lp)
  892. goto out_err;
  893. spin_lock_init(&lp->lock);
  894. lp->id = id;
  895. err = ldc_iommu_init(lp);
  896. if (err)
  897. goto out_free_ldc;
  898. lp->mops = mops;
  899. lp->mss = mss;
  900. lp->cfg = *cfgp;
  901. if (!lp->cfg.mtu)
  902. lp->cfg.mtu = LDC_DEFAULT_MTU;
  903. if (lp->cfg.mode == LDC_MODE_STREAM) {
  904. mssbuf = kzalloc(lp->cfg.mtu, GFP_KERNEL);
  905. if (!mssbuf) {
  906. err = -ENOMEM;
  907. goto out_free_iommu;
  908. }
  909. lp->mssbuf = mssbuf;
  910. }
  911. lp->event_arg = event_arg;
  912. /* XXX allow setting via ldc_channel_config to override defaults
  913. * XXX or use some formula based upon mtu
  914. */
  915. lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
  916. lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
  917. err = alloc_queue("TX", lp->tx_num_entries,
  918. &lp->tx_base, &lp->tx_ra);
  919. if (err)
  920. goto out_free_mssbuf;
  921. err = alloc_queue("RX", lp->rx_num_entries,
  922. &lp->rx_base, &lp->rx_ra);
  923. if (err)
  924. goto out_free_txq;
  925. lp->flags |= LDC_FLAG_ALLOCED_QUEUES;
  926. lp->hs_state = LDC_HS_CLOSED;
  927. ldc_set_state(lp, LDC_STATE_INIT);
  928. INIT_HLIST_NODE(&lp->list);
  929. hlist_add_head(&lp->list, &ldc_channel_list);
  930. INIT_HLIST_HEAD(&lp->mh_list);
  931. return lp;
  932. out_free_txq:
  933. free_queue(lp->tx_num_entries, lp->tx_base);
  934. out_free_mssbuf:
  935. kfree(mssbuf);
  936. out_free_iommu:
  937. ldc_iommu_release(lp);
  938. out_free_ldc:
  939. kfree(lp);
  940. out_err:
  941. return ERR_PTR(err);
  942. }
  943. EXPORT_SYMBOL(ldc_alloc);
  944. void ldc_free(struct ldc_channel *lp)
  945. {
  946. if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
  947. free_irq(lp->cfg.rx_irq, lp);
  948. free_irq(lp->cfg.tx_irq, lp);
  949. }
  950. if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
  951. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  952. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  953. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  954. }
  955. if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
  956. free_queue(lp->tx_num_entries, lp->tx_base);
  957. free_queue(lp->rx_num_entries, lp->rx_base);
  958. lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
  959. }
  960. hlist_del(&lp->list);
  961. kfree(lp->mssbuf);
  962. ldc_iommu_release(lp);
  963. kfree(lp);
  964. }
  965. EXPORT_SYMBOL(ldc_free);
  966. /* Bind the channel. This registers the LDC queues with
  967. * the hypervisor and puts the channel into a pseudo-listening
  968. * state. This does not initiate a handshake, ldc_connect() does
  969. * that.
  970. */
  971. int ldc_bind(struct ldc_channel *lp, const char *name)
  972. {
  973. unsigned long hv_err, flags;
  974. int err = -EINVAL;
  975. if (!name ||
  976. (lp->state != LDC_STATE_INIT))
  977. return -EINVAL;
  978. snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name);
  979. snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name);
  980. err = request_irq(lp->cfg.rx_irq, ldc_rx,
  981. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  982. lp->rx_irq_name, lp);
  983. if (err)
  984. return err;
  985. err = request_irq(lp->cfg.tx_irq, ldc_tx,
  986. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  987. lp->tx_irq_name, lp);
  988. if (err) {
  989. free_irq(lp->cfg.rx_irq, lp);
  990. return err;
  991. }
  992. spin_lock_irqsave(&lp->lock, flags);
  993. enable_irq(lp->cfg.rx_irq);
  994. enable_irq(lp->cfg.tx_irq);
  995. lp->flags |= LDC_FLAG_REGISTERED_IRQS;
  996. err = -ENODEV;
  997. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  998. if (hv_err)
  999. goto out_free_irqs;
  1000. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1001. if (hv_err)
  1002. goto out_free_irqs;
  1003. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1004. if (hv_err)
  1005. goto out_unmap_tx;
  1006. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1007. if (hv_err)
  1008. goto out_unmap_tx;
  1009. lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
  1010. hv_err = sun4v_ldc_tx_get_state(lp->id,
  1011. &lp->tx_head,
  1012. &lp->tx_tail,
  1013. &lp->chan_state);
  1014. err = -EBUSY;
  1015. if (hv_err)
  1016. goto out_unmap_rx;
  1017. lp->tx_acked = lp->tx_head;
  1018. lp->hs_state = LDC_HS_OPEN;
  1019. ldc_set_state(lp, LDC_STATE_BOUND);
  1020. spin_unlock_irqrestore(&lp->lock, flags);
  1021. return 0;
  1022. out_unmap_rx:
  1023. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  1024. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1025. out_unmap_tx:
  1026. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1027. out_free_irqs:
  1028. lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
  1029. free_irq(lp->cfg.tx_irq, lp);
  1030. free_irq(lp->cfg.rx_irq, lp);
  1031. spin_unlock_irqrestore(&lp->lock, flags);
  1032. return err;
  1033. }
  1034. EXPORT_SYMBOL(ldc_bind);
  1035. int ldc_connect(struct ldc_channel *lp)
  1036. {
  1037. unsigned long flags;
  1038. int err;
  1039. if (lp->cfg.mode == LDC_MODE_RAW)
  1040. return -EINVAL;
  1041. spin_lock_irqsave(&lp->lock, flags);
  1042. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1043. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
  1044. lp->hs_state != LDC_HS_OPEN)
  1045. err = -EINVAL;
  1046. else
  1047. err = start_handshake(lp);
  1048. spin_unlock_irqrestore(&lp->lock, flags);
  1049. return err;
  1050. }
  1051. EXPORT_SYMBOL(ldc_connect);
  1052. int ldc_disconnect(struct ldc_channel *lp)
  1053. {
  1054. unsigned long hv_err, flags;
  1055. int err;
  1056. if (lp->cfg.mode == LDC_MODE_RAW)
  1057. return -EINVAL;
  1058. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1059. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
  1060. return -EINVAL;
  1061. spin_lock_irqsave(&lp->lock, flags);
  1062. err = -ENODEV;
  1063. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1064. if (hv_err)
  1065. goto out_err;
  1066. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1067. if (hv_err)
  1068. goto out_err;
  1069. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1070. if (hv_err)
  1071. goto out_err;
  1072. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1073. if (hv_err)
  1074. goto out_err;
  1075. ldc_set_state(lp, LDC_STATE_BOUND);
  1076. lp->hs_state = LDC_HS_OPEN;
  1077. lp->flags |= LDC_FLAG_RESET;
  1078. spin_unlock_irqrestore(&lp->lock, flags);
  1079. return 0;
  1080. out_err:
  1081. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1082. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1083. free_irq(lp->cfg.tx_irq, lp);
  1084. free_irq(lp->cfg.rx_irq, lp);
  1085. lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
  1086. LDC_FLAG_REGISTERED_QUEUES);
  1087. ldc_set_state(lp, LDC_STATE_INIT);
  1088. spin_unlock_irqrestore(&lp->lock, flags);
  1089. return err;
  1090. }
  1091. EXPORT_SYMBOL(ldc_disconnect);
  1092. int ldc_state(struct ldc_channel *lp)
  1093. {
  1094. return lp->state;
  1095. }
  1096. EXPORT_SYMBOL(ldc_state);
  1097. static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
  1098. {
  1099. struct ldc_packet *p;
  1100. unsigned long new_tail;
  1101. int err;
  1102. if (size > LDC_PACKET_SIZE)
  1103. return -EMSGSIZE;
  1104. p = data_get_tx_packet(lp, &new_tail);
  1105. if (!p)
  1106. return -EAGAIN;
  1107. memcpy(p, buf, size);
  1108. err = send_tx_packet(lp, p, new_tail);
  1109. if (!err)
  1110. err = size;
  1111. return err;
  1112. }
  1113. static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
  1114. {
  1115. struct ldc_packet *p;
  1116. unsigned long hv_err, new;
  1117. int err;
  1118. if (size < LDC_PACKET_SIZE)
  1119. return -EINVAL;
  1120. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1121. &lp->rx_head,
  1122. &lp->rx_tail,
  1123. &lp->chan_state);
  1124. if (hv_err)
  1125. return ldc_abort(lp);
  1126. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1127. lp->chan_state == LDC_CHANNEL_RESETTING)
  1128. return -ECONNRESET;
  1129. if (lp->rx_head == lp->rx_tail)
  1130. return 0;
  1131. p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
  1132. memcpy(buf, p, LDC_PACKET_SIZE);
  1133. new = rx_advance(lp, lp->rx_head);
  1134. lp->rx_head = new;
  1135. err = __set_rx_head(lp, new);
  1136. if (err < 0)
  1137. err = -ECONNRESET;
  1138. else
  1139. err = LDC_PACKET_SIZE;
  1140. return err;
  1141. }
  1142. static const struct ldc_mode_ops raw_ops = {
  1143. .write = write_raw,
  1144. .read = read_raw,
  1145. };
  1146. static int write_nonraw(struct ldc_channel *lp, const void *buf,
  1147. unsigned int size)
  1148. {
  1149. unsigned long hv_err, tail;
  1150. unsigned int copied;
  1151. u32 seq;
  1152. int err;
  1153. hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
  1154. &lp->chan_state);
  1155. if (unlikely(hv_err))
  1156. return -EBUSY;
  1157. if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
  1158. return ldc_abort(lp);
  1159. if (!tx_has_space_for(lp, size))
  1160. return -EAGAIN;
  1161. seq = lp->snd_nxt;
  1162. copied = 0;
  1163. tail = lp->tx_tail;
  1164. while (copied < size) {
  1165. struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
  1166. u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
  1167. p->u.u_data :
  1168. p->u.r.r_data);
  1169. int data_len;
  1170. p->type = LDC_DATA;
  1171. p->stype = LDC_INFO;
  1172. p->ctrl = 0;
  1173. data_len = size - copied;
  1174. if (data_len > lp->mss)
  1175. data_len = lp->mss;
  1176. BUG_ON(data_len > LDC_LEN);
  1177. p->env = (data_len |
  1178. (copied == 0 ? LDC_START : 0) |
  1179. (data_len == size - copied ? LDC_STOP : 0));
  1180. p->seqid = ++seq;
  1181. ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
  1182. p->type,
  1183. p->stype,
  1184. p->ctrl,
  1185. p->env,
  1186. p->seqid);
  1187. memcpy(data, buf, data_len);
  1188. buf += data_len;
  1189. copied += data_len;
  1190. tail = tx_advance(lp, tail);
  1191. }
  1192. err = set_tx_tail(lp, tail);
  1193. if (!err) {
  1194. lp->snd_nxt = seq;
  1195. err = size;
  1196. }
  1197. return err;
  1198. }
  1199. static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
  1200. struct ldc_packet *first_frag)
  1201. {
  1202. int err;
  1203. if (first_frag)
  1204. lp->rcv_nxt = first_frag->seqid - 1;
  1205. err = send_data_nack(lp, p);
  1206. if (err)
  1207. return err;
  1208. err = __set_rx_head(lp, lp->rx_tail);
  1209. if (err < 0)
  1210. return ldc_abort(lp);
  1211. return 0;
  1212. }
  1213. static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
  1214. {
  1215. if (p->stype & LDC_ACK) {
  1216. int err = process_data_ack(lp, p);
  1217. if (err)
  1218. return err;
  1219. }
  1220. if (p->stype & LDC_NACK)
  1221. return ldc_abort(lp);
  1222. return 0;
  1223. }
  1224. static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
  1225. {
  1226. unsigned long dummy;
  1227. int limit = 1000;
  1228. ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
  1229. cur_head, lp->rx_head, lp->rx_tail);
  1230. while (limit-- > 0) {
  1231. unsigned long hv_err;
  1232. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1233. &dummy,
  1234. &lp->rx_tail,
  1235. &lp->chan_state);
  1236. if (hv_err)
  1237. return ldc_abort(lp);
  1238. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1239. lp->chan_state == LDC_CHANNEL_RESETTING)
  1240. return -ECONNRESET;
  1241. if (cur_head != lp->rx_tail) {
  1242. ldcdbg(DATA, "DATA WAIT DONE "
  1243. "head[%lx] tail[%lx] chan_state[%lx]\n",
  1244. dummy, lp->rx_tail, lp->chan_state);
  1245. return 0;
  1246. }
  1247. udelay(1);
  1248. }
  1249. return -EAGAIN;
  1250. }
  1251. static int rx_set_head(struct ldc_channel *lp, unsigned long head)
  1252. {
  1253. int err = __set_rx_head(lp, head);
  1254. if (err < 0)
  1255. return ldc_abort(lp);
  1256. lp->rx_head = head;
  1257. return 0;
  1258. }
  1259. static void send_data_ack(struct ldc_channel *lp)
  1260. {
  1261. unsigned long new_tail;
  1262. struct ldc_packet *p;
  1263. p = data_get_tx_packet(lp, &new_tail);
  1264. if (likely(p)) {
  1265. int err;
  1266. memset(p, 0, sizeof(*p));
  1267. p->type = LDC_DATA;
  1268. p->stype = LDC_ACK;
  1269. p->ctrl = 0;
  1270. p->seqid = lp->snd_nxt + 1;
  1271. p->u.r.ackid = lp->rcv_nxt;
  1272. err = send_tx_packet(lp, p, new_tail);
  1273. if (!err)
  1274. lp->snd_nxt++;
  1275. }
  1276. }
  1277. static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
  1278. {
  1279. struct ldc_packet *first_frag;
  1280. unsigned long hv_err, new;
  1281. int err, copied;
  1282. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1283. &lp->rx_head,
  1284. &lp->rx_tail,
  1285. &lp->chan_state);
  1286. if (hv_err)
  1287. return ldc_abort(lp);
  1288. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1289. lp->chan_state == LDC_CHANNEL_RESETTING)
  1290. return -ECONNRESET;
  1291. if (lp->rx_head == lp->rx_tail)
  1292. return 0;
  1293. first_frag = NULL;
  1294. copied = err = 0;
  1295. new = lp->rx_head;
  1296. while (1) {
  1297. struct ldc_packet *p;
  1298. int pkt_len;
  1299. BUG_ON(new == lp->rx_tail);
  1300. p = lp->rx_base + (new / LDC_PACKET_SIZE);
  1301. ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
  1302. "rcv_nxt[%08x]\n",
  1303. p->type,
  1304. p->stype,
  1305. p->ctrl,
  1306. p->env,
  1307. p->seqid,
  1308. p->u.r.ackid,
  1309. lp->rcv_nxt);
  1310. if (unlikely(!rx_seq_ok(lp, p->seqid))) {
  1311. err = rx_bad_seq(lp, p, first_frag);
  1312. copied = 0;
  1313. break;
  1314. }
  1315. if (p->type & LDC_CTRL) {
  1316. err = process_control_frame(lp, p);
  1317. if (err < 0)
  1318. break;
  1319. err = 0;
  1320. }
  1321. lp->rcv_nxt = p->seqid;
  1322. if (!(p->type & LDC_DATA)) {
  1323. new = rx_advance(lp, new);
  1324. goto no_data;
  1325. }
  1326. if (p->stype & (LDC_ACK | LDC_NACK)) {
  1327. err = data_ack_nack(lp, p);
  1328. if (err)
  1329. break;
  1330. }
  1331. if (!(p->stype & LDC_INFO)) {
  1332. new = rx_advance(lp, new);
  1333. err = rx_set_head(lp, new);
  1334. if (err)
  1335. break;
  1336. goto no_data;
  1337. }
  1338. pkt_len = p->env & LDC_LEN;
  1339. /* Every initial packet starts with the START bit set.
  1340. *
  1341. * Singleton packets will have both START+STOP set.
  1342. *
  1343. * Fragments will have START set in the first frame, STOP
  1344. * set in the last frame, and neither bit set in middle
  1345. * frames of the packet.
  1346. *
  1347. * Therefore if we are at the beginning of a packet and
  1348. * we don't see START, or we are in the middle of a fragmented
  1349. * packet and do see START, we are unsynchronized and should
  1350. * flush the RX queue.
  1351. */
  1352. if ((first_frag == NULL && !(p->env & LDC_START)) ||
  1353. (first_frag != NULL && (p->env & LDC_START))) {
  1354. if (!first_frag)
  1355. new = rx_advance(lp, new);
  1356. err = rx_set_head(lp, new);
  1357. if (err)
  1358. break;
  1359. if (!first_frag)
  1360. goto no_data;
  1361. }
  1362. if (!first_frag)
  1363. first_frag = p;
  1364. if (pkt_len > size - copied) {
  1365. /* User didn't give us a big enough buffer,
  1366. * what to do? This is a pretty serious error.
  1367. *
  1368. * Since we haven't updated the RX ring head to
  1369. * consume any of the packets, signal the error
  1370. * to the user and just leave the RX ring alone.
  1371. *
  1372. * This seems the best behavior because this allows
  1373. * a user of the LDC layer to start with a small
  1374. * RX buffer for ldc_read() calls and use -EMSGSIZE
  1375. * as a cue to enlarge it's read buffer.
  1376. */
  1377. err = -EMSGSIZE;
  1378. break;
  1379. }
  1380. /* Ok, we are gonna eat this one. */
  1381. new = rx_advance(lp, new);
  1382. memcpy(buf,
  1383. (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
  1384. p->u.u_data : p->u.r.r_data), pkt_len);
  1385. buf += pkt_len;
  1386. copied += pkt_len;
  1387. if (p->env & LDC_STOP)
  1388. break;
  1389. no_data:
  1390. if (new == lp->rx_tail) {
  1391. err = rx_data_wait(lp, new);
  1392. if (err)
  1393. break;
  1394. }
  1395. }
  1396. if (!err)
  1397. err = rx_set_head(lp, new);
  1398. if (err && first_frag)
  1399. lp->rcv_nxt = first_frag->seqid - 1;
  1400. if (!err) {
  1401. err = copied;
  1402. if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE)
  1403. send_data_ack(lp);
  1404. }
  1405. return err;
  1406. }
  1407. static const struct ldc_mode_ops nonraw_ops = {
  1408. .write = write_nonraw,
  1409. .read = read_nonraw,
  1410. };
  1411. static int write_stream(struct ldc_channel *lp, const void *buf,
  1412. unsigned int size)
  1413. {
  1414. if (size > lp->cfg.mtu)
  1415. size = lp->cfg.mtu;
  1416. return write_nonraw(lp, buf, size);
  1417. }
  1418. static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
  1419. {
  1420. if (!lp->mssbuf_len) {
  1421. int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu);
  1422. if (err < 0)
  1423. return err;
  1424. lp->mssbuf_len = err;
  1425. lp->mssbuf_off = 0;
  1426. }
  1427. if (size > lp->mssbuf_len)
  1428. size = lp->mssbuf_len;
  1429. memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
  1430. lp->mssbuf_off += size;
  1431. lp->mssbuf_len -= size;
  1432. return size;
  1433. }
  1434. static const struct ldc_mode_ops stream_ops = {
  1435. .write = write_stream,
  1436. .read = read_stream,
  1437. };
  1438. int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
  1439. {
  1440. unsigned long flags;
  1441. int err;
  1442. if (!buf)
  1443. return -EINVAL;
  1444. if (!size)
  1445. return 0;
  1446. spin_lock_irqsave(&lp->lock, flags);
  1447. if (lp->hs_state != LDC_HS_COMPLETE)
  1448. err = -ENOTCONN;
  1449. else
  1450. err = lp->mops->write(lp, buf, size);
  1451. spin_unlock_irqrestore(&lp->lock, flags);
  1452. return err;
  1453. }
  1454. EXPORT_SYMBOL(ldc_write);
  1455. int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
  1456. {
  1457. unsigned long flags;
  1458. int err;
  1459. if (!buf)
  1460. return -EINVAL;
  1461. if (!size)
  1462. return 0;
  1463. spin_lock_irqsave(&lp->lock, flags);
  1464. if (lp->hs_state != LDC_HS_COMPLETE)
  1465. err = -ENOTCONN;
  1466. else
  1467. err = lp->mops->read(lp, buf, size);
  1468. spin_unlock_irqrestore(&lp->lock, flags);
  1469. return err;
  1470. }
  1471. EXPORT_SYMBOL(ldc_read);
  1472. static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages)
  1473. {
  1474. struct iommu_arena *arena = &iommu->arena;
  1475. unsigned long n, i, start, end, limit;
  1476. int pass;
  1477. limit = arena->limit;
  1478. start = arena->hint;
  1479. pass = 0;
  1480. again:
  1481. n = find_next_zero_bit(arena->map, limit, start);
  1482. end = n + npages;
  1483. if (unlikely(end >= limit)) {
  1484. if (likely(pass < 1)) {
  1485. limit = start;
  1486. start = 0;
  1487. pass++;
  1488. goto again;
  1489. } else {
  1490. /* Scanned the whole thing, give up. */
  1491. return -1;
  1492. }
  1493. }
  1494. for (i = n; i < end; i++) {
  1495. if (test_bit(i, arena->map)) {
  1496. start = i + 1;
  1497. goto again;
  1498. }
  1499. }
  1500. for (i = n; i < end; i++)
  1501. __set_bit(i, arena->map);
  1502. arena->hint = end;
  1503. return n;
  1504. }
  1505. #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
  1506. #define COOKIE_PGSZ_CODE_SHIFT 60ULL
  1507. static u64 pagesize_code(void)
  1508. {
  1509. switch (PAGE_SIZE) {
  1510. default:
  1511. case (8ULL * 1024ULL):
  1512. return 0;
  1513. case (64ULL * 1024ULL):
  1514. return 1;
  1515. case (512ULL * 1024ULL):
  1516. return 2;
  1517. case (4ULL * 1024ULL * 1024ULL):
  1518. return 3;
  1519. case (32ULL * 1024ULL * 1024ULL):
  1520. return 4;
  1521. case (256ULL * 1024ULL * 1024ULL):
  1522. return 5;
  1523. }
  1524. }
  1525. static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
  1526. {
  1527. return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
  1528. (index << PAGE_SHIFT) |
  1529. page_offset);
  1530. }
  1531. static u64 cookie_to_index(u64 cookie, unsigned long *shift)
  1532. {
  1533. u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
  1534. cookie &= ~COOKIE_PGSZ_CODE;
  1535. *shift = szcode * 3;
  1536. return (cookie >> (13ULL + (szcode * 3ULL)));
  1537. }
  1538. static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
  1539. unsigned long npages)
  1540. {
  1541. long entry;
  1542. entry = arena_alloc(iommu, npages);
  1543. if (unlikely(entry < 0))
  1544. return NULL;
  1545. return iommu->page_table + entry;
  1546. }
  1547. static u64 perm_to_mte(unsigned int map_perm)
  1548. {
  1549. u64 mte_base;
  1550. mte_base = pagesize_code();
  1551. if (map_perm & LDC_MAP_SHADOW) {
  1552. if (map_perm & LDC_MAP_R)
  1553. mte_base |= LDC_MTE_COPY_R;
  1554. if (map_perm & LDC_MAP_W)
  1555. mte_base |= LDC_MTE_COPY_W;
  1556. }
  1557. if (map_perm & LDC_MAP_DIRECT) {
  1558. if (map_perm & LDC_MAP_R)
  1559. mte_base |= LDC_MTE_READ;
  1560. if (map_perm & LDC_MAP_W)
  1561. mte_base |= LDC_MTE_WRITE;
  1562. if (map_perm & LDC_MAP_X)
  1563. mte_base |= LDC_MTE_EXEC;
  1564. }
  1565. if (map_perm & LDC_MAP_IO) {
  1566. if (map_perm & LDC_MAP_R)
  1567. mte_base |= LDC_MTE_IOMMU_R;
  1568. if (map_perm & LDC_MAP_W)
  1569. mte_base |= LDC_MTE_IOMMU_W;
  1570. }
  1571. return mte_base;
  1572. }
  1573. static int pages_in_region(unsigned long base, long len)
  1574. {
  1575. int count = 0;
  1576. do {
  1577. unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
  1578. len -= (new - base);
  1579. base = new;
  1580. count++;
  1581. } while (len > 0);
  1582. return count;
  1583. }
  1584. struct cookie_state {
  1585. struct ldc_mtable_entry *page_table;
  1586. struct ldc_trans_cookie *cookies;
  1587. u64 mte_base;
  1588. u64 prev_cookie;
  1589. u32 pte_idx;
  1590. u32 nc;
  1591. };
  1592. static void fill_cookies(struct cookie_state *sp, unsigned long pa,
  1593. unsigned long off, unsigned long len)
  1594. {
  1595. do {
  1596. unsigned long tlen, new = pa + PAGE_SIZE;
  1597. u64 this_cookie;
  1598. sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
  1599. tlen = PAGE_SIZE;
  1600. if (off)
  1601. tlen = PAGE_SIZE - off;
  1602. if (tlen > len)
  1603. tlen = len;
  1604. this_cookie = make_cookie(sp->pte_idx,
  1605. pagesize_code(), off);
  1606. off = 0;
  1607. if (this_cookie == sp->prev_cookie) {
  1608. sp->cookies[sp->nc - 1].cookie_size += tlen;
  1609. } else {
  1610. sp->cookies[sp->nc].cookie_addr = this_cookie;
  1611. sp->cookies[sp->nc].cookie_size = tlen;
  1612. sp->nc++;
  1613. }
  1614. sp->prev_cookie = this_cookie + tlen;
  1615. sp->pte_idx++;
  1616. len -= tlen;
  1617. pa = new;
  1618. } while (len > 0);
  1619. }
  1620. static int sg_count_one(struct scatterlist *sg)
  1621. {
  1622. unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT;
  1623. long len = sg->length;
  1624. if ((sg->offset | len) & (8UL - 1))
  1625. return -EFAULT;
  1626. return pages_in_region(base + sg->offset, len);
  1627. }
  1628. static int sg_count_pages(struct scatterlist *sg, int num_sg)
  1629. {
  1630. int count;
  1631. int i;
  1632. count = 0;
  1633. for (i = 0; i < num_sg; i++) {
  1634. int err = sg_count_one(sg + i);
  1635. if (err < 0)
  1636. return err;
  1637. count += err;
  1638. }
  1639. return count;
  1640. }
  1641. int ldc_map_sg(struct ldc_channel *lp,
  1642. struct scatterlist *sg, int num_sg,
  1643. struct ldc_trans_cookie *cookies, int ncookies,
  1644. unsigned int map_perm)
  1645. {
  1646. unsigned long i, npages, flags;
  1647. struct ldc_mtable_entry *base;
  1648. struct cookie_state state;
  1649. struct ldc_iommu *iommu;
  1650. int err;
  1651. if (map_perm & ~LDC_MAP_ALL)
  1652. return -EINVAL;
  1653. err = sg_count_pages(sg, num_sg);
  1654. if (err < 0)
  1655. return err;
  1656. npages = err;
  1657. if (err > ncookies)
  1658. return -EMSGSIZE;
  1659. iommu = &lp->iommu;
  1660. spin_lock_irqsave(&iommu->lock, flags);
  1661. base = alloc_npages(iommu, npages);
  1662. spin_unlock_irqrestore(&iommu->lock, flags);
  1663. if (!base)
  1664. return -ENOMEM;
  1665. state.page_table = iommu->page_table;
  1666. state.cookies = cookies;
  1667. state.mte_base = perm_to_mte(map_perm);
  1668. state.prev_cookie = ~(u64)0;
  1669. state.pte_idx = (base - iommu->page_table);
  1670. state.nc = 0;
  1671. for (i = 0; i < num_sg; i++)
  1672. fill_cookies(&state, page_to_pfn(sg_page(&sg[i])) << PAGE_SHIFT,
  1673. sg[i].offset, sg[i].length);
  1674. return state.nc;
  1675. }
  1676. EXPORT_SYMBOL(ldc_map_sg);
  1677. int ldc_map_single(struct ldc_channel *lp,
  1678. void *buf, unsigned int len,
  1679. struct ldc_trans_cookie *cookies, int ncookies,
  1680. unsigned int map_perm)
  1681. {
  1682. unsigned long npages, pa, flags;
  1683. struct ldc_mtable_entry *base;
  1684. struct cookie_state state;
  1685. struct ldc_iommu *iommu;
  1686. if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
  1687. return -EINVAL;
  1688. pa = __pa(buf);
  1689. if ((pa | len) & (8UL - 1))
  1690. return -EFAULT;
  1691. npages = pages_in_region(pa, len);
  1692. iommu = &lp->iommu;
  1693. spin_lock_irqsave(&iommu->lock, flags);
  1694. base = alloc_npages(iommu, npages);
  1695. spin_unlock_irqrestore(&iommu->lock, flags);
  1696. if (!base)
  1697. return -ENOMEM;
  1698. state.page_table = iommu->page_table;
  1699. state.cookies = cookies;
  1700. state.mte_base = perm_to_mte(map_perm);
  1701. state.prev_cookie = ~(u64)0;
  1702. state.pte_idx = (base - iommu->page_table);
  1703. state.nc = 0;
  1704. fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
  1705. BUG_ON(state.nc != 1);
  1706. return state.nc;
  1707. }
  1708. EXPORT_SYMBOL(ldc_map_single);
  1709. static void free_npages(unsigned long id, struct ldc_iommu *iommu,
  1710. u64 cookie, u64 size)
  1711. {
  1712. struct iommu_arena *arena = &iommu->arena;
  1713. unsigned long i, shift, index, npages;
  1714. struct ldc_mtable_entry *base;
  1715. npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
  1716. index = cookie_to_index(cookie, &shift);
  1717. base = iommu->page_table + index;
  1718. BUG_ON(index > arena->limit ||
  1719. (index + npages) > arena->limit);
  1720. for (i = 0; i < npages; i++) {
  1721. if (base->cookie)
  1722. sun4v_ldc_revoke(id, cookie + (i << shift),
  1723. base->cookie);
  1724. base->mte = 0;
  1725. __clear_bit(index + i, arena->map);
  1726. }
  1727. }
  1728. void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
  1729. int ncookies)
  1730. {
  1731. struct ldc_iommu *iommu = &lp->iommu;
  1732. unsigned long flags;
  1733. int i;
  1734. spin_lock_irqsave(&iommu->lock, flags);
  1735. for (i = 0; i < ncookies; i++) {
  1736. u64 addr = cookies[i].cookie_addr;
  1737. u64 size = cookies[i].cookie_size;
  1738. free_npages(lp->id, iommu, addr, size);
  1739. }
  1740. spin_unlock_irqrestore(&iommu->lock, flags);
  1741. }
  1742. EXPORT_SYMBOL(ldc_unmap);
  1743. int ldc_copy(struct ldc_channel *lp, int copy_dir,
  1744. void *buf, unsigned int len, unsigned long offset,
  1745. struct ldc_trans_cookie *cookies, int ncookies)
  1746. {
  1747. unsigned int orig_len;
  1748. unsigned long ra;
  1749. int i;
  1750. if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
  1751. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
  1752. lp->id, copy_dir);
  1753. return -EINVAL;
  1754. }
  1755. ra = __pa(buf);
  1756. if ((ra | len | offset) & (8UL - 1)) {
  1757. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
  1758. "ra[%lx] len[%x] offset[%lx]\n",
  1759. lp->id, ra, len, offset);
  1760. return -EFAULT;
  1761. }
  1762. if (lp->hs_state != LDC_HS_COMPLETE ||
  1763. (lp->flags & LDC_FLAG_RESET)) {
  1764. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
  1765. "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
  1766. return -ECONNRESET;
  1767. }
  1768. orig_len = len;
  1769. for (i = 0; i < ncookies; i++) {
  1770. unsigned long cookie_raddr = cookies[i].cookie_addr;
  1771. unsigned long this_len = cookies[i].cookie_size;
  1772. unsigned long actual_len;
  1773. if (unlikely(offset)) {
  1774. unsigned long this_off = offset;
  1775. if (this_off > this_len)
  1776. this_off = this_len;
  1777. offset -= this_off;
  1778. this_len -= this_off;
  1779. if (!this_len)
  1780. continue;
  1781. cookie_raddr += this_off;
  1782. }
  1783. if (this_len > len)
  1784. this_len = len;
  1785. while (1) {
  1786. unsigned long hv_err;
  1787. hv_err = sun4v_ldc_copy(lp->id, copy_dir,
  1788. cookie_raddr, ra,
  1789. this_len, &actual_len);
  1790. if (unlikely(hv_err)) {
  1791. printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
  1792. "HV error %lu\n",
  1793. lp->id, hv_err);
  1794. if (lp->hs_state != LDC_HS_COMPLETE ||
  1795. (lp->flags & LDC_FLAG_RESET))
  1796. return -ECONNRESET;
  1797. else
  1798. return -EFAULT;
  1799. }
  1800. cookie_raddr += actual_len;
  1801. ra += actual_len;
  1802. len -= actual_len;
  1803. if (actual_len == this_len)
  1804. break;
  1805. this_len -= actual_len;
  1806. }
  1807. if (!len)
  1808. break;
  1809. }
  1810. /* It is caller policy what to do about short copies.
  1811. * For example, a networking driver can declare the
  1812. * packet a runt and drop it.
  1813. */
  1814. return orig_len - len;
  1815. }
  1816. EXPORT_SYMBOL(ldc_copy);
  1817. void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
  1818. struct ldc_trans_cookie *cookies, int *ncookies,
  1819. unsigned int map_perm)
  1820. {
  1821. void *buf;
  1822. int err;
  1823. if (len & (8UL - 1))
  1824. return ERR_PTR(-EINVAL);
  1825. buf = kzalloc(len, GFP_KERNEL);
  1826. if (!buf)
  1827. return ERR_PTR(-ENOMEM);
  1828. err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
  1829. if (err < 0) {
  1830. kfree(buf);
  1831. return ERR_PTR(err);
  1832. }
  1833. *ncookies = err;
  1834. return buf;
  1835. }
  1836. EXPORT_SYMBOL(ldc_alloc_exp_dring);
  1837. void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
  1838. struct ldc_trans_cookie *cookies, int ncookies)
  1839. {
  1840. ldc_unmap(lp, cookies, ncookies);
  1841. kfree(buf);
  1842. }
  1843. EXPORT_SYMBOL(ldc_free_exp_dring);
  1844. static int __init ldc_init(void)
  1845. {
  1846. unsigned long major, minor;
  1847. struct mdesc_handle *hp;
  1848. const u64 *v;
  1849. int err;
  1850. u64 mp;
  1851. hp = mdesc_grab();
  1852. if (!hp)
  1853. return -ENODEV;
  1854. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  1855. err = -ENODEV;
  1856. if (mp == MDESC_NODE_NULL)
  1857. goto out;
  1858. v = mdesc_get_property(hp, mp, "domaining-enabled", NULL);
  1859. if (!v)
  1860. goto out;
  1861. major = 1;
  1862. minor = 0;
  1863. if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
  1864. printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
  1865. goto out;
  1866. }
  1867. printk(KERN_INFO "%s", version);
  1868. if (!*v) {
  1869. printk(KERN_INFO PFX "Domaining disabled.\n");
  1870. goto out;
  1871. }
  1872. ldom_domaining_enabled = 1;
  1873. err = 0;
  1874. out:
  1875. mdesc_release(hp);
  1876. return err;
  1877. }
  1878. core_initcall(ldc_init);