ldc.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379
  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. if (mssbuf)
  936. kfree(mssbuf);
  937. out_free_iommu:
  938. ldc_iommu_release(lp);
  939. out_free_ldc:
  940. kfree(lp);
  941. out_err:
  942. return ERR_PTR(err);
  943. }
  944. EXPORT_SYMBOL(ldc_alloc);
  945. void ldc_free(struct ldc_channel *lp)
  946. {
  947. if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
  948. free_irq(lp->cfg.rx_irq, lp);
  949. free_irq(lp->cfg.tx_irq, lp);
  950. }
  951. if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
  952. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  953. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  954. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  955. }
  956. if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
  957. free_queue(lp->tx_num_entries, lp->tx_base);
  958. free_queue(lp->rx_num_entries, lp->rx_base);
  959. lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
  960. }
  961. hlist_del(&lp->list);
  962. if (lp->mssbuf)
  963. kfree(lp->mssbuf);
  964. ldc_iommu_release(lp);
  965. kfree(lp);
  966. }
  967. EXPORT_SYMBOL(ldc_free);
  968. /* Bind the channel. This registers the LDC queues with
  969. * the hypervisor and puts the channel into a pseudo-listening
  970. * state. This does not initiate a handshake, ldc_connect() does
  971. * that.
  972. */
  973. int ldc_bind(struct ldc_channel *lp, const char *name)
  974. {
  975. unsigned long hv_err, flags;
  976. int err = -EINVAL;
  977. if (!name ||
  978. (lp->state != LDC_STATE_INIT))
  979. return -EINVAL;
  980. snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name);
  981. snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name);
  982. err = request_irq(lp->cfg.rx_irq, ldc_rx,
  983. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  984. lp->rx_irq_name, lp);
  985. if (err)
  986. return err;
  987. err = request_irq(lp->cfg.tx_irq, ldc_tx,
  988. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  989. lp->tx_irq_name, lp);
  990. if (err) {
  991. free_irq(lp->cfg.rx_irq, lp);
  992. return err;
  993. }
  994. spin_lock_irqsave(&lp->lock, flags);
  995. enable_irq(lp->cfg.rx_irq);
  996. enable_irq(lp->cfg.tx_irq);
  997. lp->flags |= LDC_FLAG_REGISTERED_IRQS;
  998. err = -ENODEV;
  999. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1000. if (hv_err)
  1001. goto out_free_irqs;
  1002. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1003. if (hv_err)
  1004. goto out_free_irqs;
  1005. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1006. if (hv_err)
  1007. goto out_unmap_tx;
  1008. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1009. if (hv_err)
  1010. goto out_unmap_tx;
  1011. lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
  1012. hv_err = sun4v_ldc_tx_get_state(lp->id,
  1013. &lp->tx_head,
  1014. &lp->tx_tail,
  1015. &lp->chan_state);
  1016. err = -EBUSY;
  1017. if (hv_err)
  1018. goto out_unmap_rx;
  1019. lp->tx_acked = lp->tx_head;
  1020. lp->hs_state = LDC_HS_OPEN;
  1021. ldc_set_state(lp, LDC_STATE_BOUND);
  1022. spin_unlock_irqrestore(&lp->lock, flags);
  1023. return 0;
  1024. out_unmap_rx:
  1025. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  1026. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1027. out_unmap_tx:
  1028. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1029. out_free_irqs:
  1030. lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
  1031. free_irq(lp->cfg.tx_irq, lp);
  1032. free_irq(lp->cfg.rx_irq, lp);
  1033. spin_unlock_irqrestore(&lp->lock, flags);
  1034. return err;
  1035. }
  1036. EXPORT_SYMBOL(ldc_bind);
  1037. int ldc_connect(struct ldc_channel *lp)
  1038. {
  1039. unsigned long flags;
  1040. int err;
  1041. if (lp->cfg.mode == LDC_MODE_RAW)
  1042. return -EINVAL;
  1043. spin_lock_irqsave(&lp->lock, flags);
  1044. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1045. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
  1046. lp->hs_state != LDC_HS_OPEN)
  1047. err = -EINVAL;
  1048. else
  1049. err = start_handshake(lp);
  1050. spin_unlock_irqrestore(&lp->lock, flags);
  1051. return err;
  1052. }
  1053. EXPORT_SYMBOL(ldc_connect);
  1054. int ldc_disconnect(struct ldc_channel *lp)
  1055. {
  1056. unsigned long hv_err, flags;
  1057. int err;
  1058. if (lp->cfg.mode == LDC_MODE_RAW)
  1059. return -EINVAL;
  1060. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1061. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
  1062. return -EINVAL;
  1063. spin_lock_irqsave(&lp->lock, flags);
  1064. err = -ENODEV;
  1065. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1066. if (hv_err)
  1067. goto out_err;
  1068. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1069. if (hv_err)
  1070. goto out_err;
  1071. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1072. if (hv_err)
  1073. goto out_err;
  1074. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1075. if (hv_err)
  1076. goto out_err;
  1077. ldc_set_state(lp, LDC_STATE_BOUND);
  1078. lp->hs_state = LDC_HS_OPEN;
  1079. lp->flags |= LDC_FLAG_RESET;
  1080. spin_unlock_irqrestore(&lp->lock, flags);
  1081. return 0;
  1082. out_err:
  1083. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1084. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1085. free_irq(lp->cfg.tx_irq, lp);
  1086. free_irq(lp->cfg.rx_irq, lp);
  1087. lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
  1088. LDC_FLAG_REGISTERED_QUEUES);
  1089. ldc_set_state(lp, LDC_STATE_INIT);
  1090. spin_unlock_irqrestore(&lp->lock, flags);
  1091. return err;
  1092. }
  1093. EXPORT_SYMBOL(ldc_disconnect);
  1094. int ldc_state(struct ldc_channel *lp)
  1095. {
  1096. return lp->state;
  1097. }
  1098. EXPORT_SYMBOL(ldc_state);
  1099. static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
  1100. {
  1101. struct ldc_packet *p;
  1102. unsigned long new_tail;
  1103. int err;
  1104. if (size > LDC_PACKET_SIZE)
  1105. return -EMSGSIZE;
  1106. p = data_get_tx_packet(lp, &new_tail);
  1107. if (!p)
  1108. return -EAGAIN;
  1109. memcpy(p, buf, size);
  1110. err = send_tx_packet(lp, p, new_tail);
  1111. if (!err)
  1112. err = size;
  1113. return err;
  1114. }
  1115. static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
  1116. {
  1117. struct ldc_packet *p;
  1118. unsigned long hv_err, new;
  1119. int err;
  1120. if (size < LDC_PACKET_SIZE)
  1121. return -EINVAL;
  1122. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1123. &lp->rx_head,
  1124. &lp->rx_tail,
  1125. &lp->chan_state);
  1126. if (hv_err)
  1127. return ldc_abort(lp);
  1128. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1129. lp->chan_state == LDC_CHANNEL_RESETTING)
  1130. return -ECONNRESET;
  1131. if (lp->rx_head == lp->rx_tail)
  1132. return 0;
  1133. p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
  1134. memcpy(buf, p, LDC_PACKET_SIZE);
  1135. new = rx_advance(lp, lp->rx_head);
  1136. lp->rx_head = new;
  1137. err = __set_rx_head(lp, new);
  1138. if (err < 0)
  1139. err = -ECONNRESET;
  1140. else
  1141. err = LDC_PACKET_SIZE;
  1142. return err;
  1143. }
  1144. static const struct ldc_mode_ops raw_ops = {
  1145. .write = write_raw,
  1146. .read = read_raw,
  1147. };
  1148. static int write_nonraw(struct ldc_channel *lp, const void *buf,
  1149. unsigned int size)
  1150. {
  1151. unsigned long hv_err, tail;
  1152. unsigned int copied;
  1153. u32 seq;
  1154. int err;
  1155. hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
  1156. &lp->chan_state);
  1157. if (unlikely(hv_err))
  1158. return -EBUSY;
  1159. if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
  1160. return ldc_abort(lp);
  1161. if (!tx_has_space_for(lp, size))
  1162. return -EAGAIN;
  1163. seq = lp->snd_nxt;
  1164. copied = 0;
  1165. tail = lp->tx_tail;
  1166. while (copied < size) {
  1167. struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
  1168. u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
  1169. p->u.u_data :
  1170. p->u.r.r_data);
  1171. int data_len;
  1172. p->type = LDC_DATA;
  1173. p->stype = LDC_INFO;
  1174. p->ctrl = 0;
  1175. data_len = size - copied;
  1176. if (data_len > lp->mss)
  1177. data_len = lp->mss;
  1178. BUG_ON(data_len > LDC_LEN);
  1179. p->env = (data_len |
  1180. (copied == 0 ? LDC_START : 0) |
  1181. (data_len == size - copied ? LDC_STOP : 0));
  1182. p->seqid = ++seq;
  1183. ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
  1184. p->type,
  1185. p->stype,
  1186. p->ctrl,
  1187. p->env,
  1188. p->seqid);
  1189. memcpy(data, buf, data_len);
  1190. buf += data_len;
  1191. copied += data_len;
  1192. tail = tx_advance(lp, tail);
  1193. }
  1194. err = set_tx_tail(lp, tail);
  1195. if (!err) {
  1196. lp->snd_nxt = seq;
  1197. err = size;
  1198. }
  1199. return err;
  1200. }
  1201. static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
  1202. struct ldc_packet *first_frag)
  1203. {
  1204. int err;
  1205. if (first_frag)
  1206. lp->rcv_nxt = first_frag->seqid - 1;
  1207. err = send_data_nack(lp, p);
  1208. if (err)
  1209. return err;
  1210. err = __set_rx_head(lp, lp->rx_tail);
  1211. if (err < 0)
  1212. return ldc_abort(lp);
  1213. return 0;
  1214. }
  1215. static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
  1216. {
  1217. if (p->stype & LDC_ACK) {
  1218. int err = process_data_ack(lp, p);
  1219. if (err)
  1220. return err;
  1221. }
  1222. if (p->stype & LDC_NACK)
  1223. return ldc_abort(lp);
  1224. return 0;
  1225. }
  1226. static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
  1227. {
  1228. unsigned long dummy;
  1229. int limit = 1000;
  1230. ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
  1231. cur_head, lp->rx_head, lp->rx_tail);
  1232. while (limit-- > 0) {
  1233. unsigned long hv_err;
  1234. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1235. &dummy,
  1236. &lp->rx_tail,
  1237. &lp->chan_state);
  1238. if (hv_err)
  1239. return ldc_abort(lp);
  1240. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1241. lp->chan_state == LDC_CHANNEL_RESETTING)
  1242. return -ECONNRESET;
  1243. if (cur_head != lp->rx_tail) {
  1244. ldcdbg(DATA, "DATA WAIT DONE "
  1245. "head[%lx] tail[%lx] chan_state[%lx]\n",
  1246. dummy, lp->rx_tail, lp->chan_state);
  1247. return 0;
  1248. }
  1249. udelay(1);
  1250. }
  1251. return -EAGAIN;
  1252. }
  1253. static int rx_set_head(struct ldc_channel *lp, unsigned long head)
  1254. {
  1255. int err = __set_rx_head(lp, head);
  1256. if (err < 0)
  1257. return ldc_abort(lp);
  1258. lp->rx_head = head;
  1259. return 0;
  1260. }
  1261. static void send_data_ack(struct ldc_channel *lp)
  1262. {
  1263. unsigned long new_tail;
  1264. struct ldc_packet *p;
  1265. p = data_get_tx_packet(lp, &new_tail);
  1266. if (likely(p)) {
  1267. int err;
  1268. memset(p, 0, sizeof(*p));
  1269. p->type = LDC_DATA;
  1270. p->stype = LDC_ACK;
  1271. p->ctrl = 0;
  1272. p->seqid = lp->snd_nxt + 1;
  1273. p->u.r.ackid = lp->rcv_nxt;
  1274. err = send_tx_packet(lp, p, new_tail);
  1275. if (!err)
  1276. lp->snd_nxt++;
  1277. }
  1278. }
  1279. static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
  1280. {
  1281. struct ldc_packet *first_frag;
  1282. unsigned long hv_err, new;
  1283. int err, copied;
  1284. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1285. &lp->rx_head,
  1286. &lp->rx_tail,
  1287. &lp->chan_state);
  1288. if (hv_err)
  1289. return ldc_abort(lp);
  1290. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1291. lp->chan_state == LDC_CHANNEL_RESETTING)
  1292. return -ECONNRESET;
  1293. if (lp->rx_head == lp->rx_tail)
  1294. return 0;
  1295. first_frag = NULL;
  1296. copied = err = 0;
  1297. new = lp->rx_head;
  1298. while (1) {
  1299. struct ldc_packet *p;
  1300. int pkt_len;
  1301. BUG_ON(new == lp->rx_tail);
  1302. p = lp->rx_base + (new / LDC_PACKET_SIZE);
  1303. ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
  1304. "rcv_nxt[%08x]\n",
  1305. p->type,
  1306. p->stype,
  1307. p->ctrl,
  1308. p->env,
  1309. p->seqid,
  1310. p->u.r.ackid,
  1311. lp->rcv_nxt);
  1312. if (unlikely(!rx_seq_ok(lp, p->seqid))) {
  1313. err = rx_bad_seq(lp, p, first_frag);
  1314. copied = 0;
  1315. break;
  1316. }
  1317. if (p->type & LDC_CTRL) {
  1318. err = process_control_frame(lp, p);
  1319. if (err < 0)
  1320. break;
  1321. err = 0;
  1322. }
  1323. lp->rcv_nxt = p->seqid;
  1324. if (!(p->type & LDC_DATA)) {
  1325. new = rx_advance(lp, new);
  1326. goto no_data;
  1327. }
  1328. if (p->stype & (LDC_ACK | LDC_NACK)) {
  1329. err = data_ack_nack(lp, p);
  1330. if (err)
  1331. break;
  1332. }
  1333. if (!(p->stype & LDC_INFO)) {
  1334. new = rx_advance(lp, new);
  1335. err = rx_set_head(lp, new);
  1336. if (err)
  1337. break;
  1338. goto no_data;
  1339. }
  1340. pkt_len = p->env & LDC_LEN;
  1341. /* Every initial packet starts with the START bit set.
  1342. *
  1343. * Singleton packets will have both START+STOP set.
  1344. *
  1345. * Fragments will have START set in the first frame, STOP
  1346. * set in the last frame, and neither bit set in middle
  1347. * frames of the packet.
  1348. *
  1349. * Therefore if we are at the beginning of a packet and
  1350. * we don't see START, or we are in the middle of a fragmented
  1351. * packet and do see START, we are unsynchronized and should
  1352. * flush the RX queue.
  1353. */
  1354. if ((first_frag == NULL && !(p->env & LDC_START)) ||
  1355. (first_frag != NULL && (p->env & LDC_START))) {
  1356. if (!first_frag)
  1357. new = rx_advance(lp, new);
  1358. err = rx_set_head(lp, new);
  1359. if (err)
  1360. break;
  1361. if (!first_frag)
  1362. goto no_data;
  1363. }
  1364. if (!first_frag)
  1365. first_frag = p;
  1366. if (pkt_len > size - copied) {
  1367. /* User didn't give us a big enough buffer,
  1368. * what to do? This is a pretty serious error.
  1369. *
  1370. * Since we haven't updated the RX ring head to
  1371. * consume any of the packets, signal the error
  1372. * to the user and just leave the RX ring alone.
  1373. *
  1374. * This seems the best behavior because this allows
  1375. * a user of the LDC layer to start with a small
  1376. * RX buffer for ldc_read() calls and use -EMSGSIZE
  1377. * as a cue to enlarge it's read buffer.
  1378. */
  1379. err = -EMSGSIZE;
  1380. break;
  1381. }
  1382. /* Ok, we are gonna eat this one. */
  1383. new = rx_advance(lp, new);
  1384. memcpy(buf,
  1385. (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
  1386. p->u.u_data : p->u.r.r_data), pkt_len);
  1387. buf += pkt_len;
  1388. copied += pkt_len;
  1389. if (p->env & LDC_STOP)
  1390. break;
  1391. no_data:
  1392. if (new == lp->rx_tail) {
  1393. err = rx_data_wait(lp, new);
  1394. if (err)
  1395. break;
  1396. }
  1397. }
  1398. if (!err)
  1399. err = rx_set_head(lp, new);
  1400. if (err && first_frag)
  1401. lp->rcv_nxt = first_frag->seqid - 1;
  1402. if (!err) {
  1403. err = copied;
  1404. if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE)
  1405. send_data_ack(lp);
  1406. }
  1407. return err;
  1408. }
  1409. static const struct ldc_mode_ops nonraw_ops = {
  1410. .write = write_nonraw,
  1411. .read = read_nonraw,
  1412. };
  1413. static int write_stream(struct ldc_channel *lp, const void *buf,
  1414. unsigned int size)
  1415. {
  1416. if (size > lp->cfg.mtu)
  1417. size = lp->cfg.mtu;
  1418. return write_nonraw(lp, buf, size);
  1419. }
  1420. static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
  1421. {
  1422. if (!lp->mssbuf_len) {
  1423. int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu);
  1424. if (err < 0)
  1425. return err;
  1426. lp->mssbuf_len = err;
  1427. lp->mssbuf_off = 0;
  1428. }
  1429. if (size > lp->mssbuf_len)
  1430. size = lp->mssbuf_len;
  1431. memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
  1432. lp->mssbuf_off += size;
  1433. lp->mssbuf_len -= size;
  1434. return size;
  1435. }
  1436. static const struct ldc_mode_ops stream_ops = {
  1437. .write = write_stream,
  1438. .read = read_stream,
  1439. };
  1440. int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
  1441. {
  1442. unsigned long flags;
  1443. int err;
  1444. if (!buf)
  1445. return -EINVAL;
  1446. if (!size)
  1447. return 0;
  1448. spin_lock_irqsave(&lp->lock, flags);
  1449. if (lp->hs_state != LDC_HS_COMPLETE)
  1450. err = -ENOTCONN;
  1451. else
  1452. err = lp->mops->write(lp, buf, size);
  1453. spin_unlock_irqrestore(&lp->lock, flags);
  1454. return err;
  1455. }
  1456. EXPORT_SYMBOL(ldc_write);
  1457. int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
  1458. {
  1459. unsigned long flags;
  1460. int err;
  1461. if (!buf)
  1462. return -EINVAL;
  1463. if (!size)
  1464. return 0;
  1465. spin_lock_irqsave(&lp->lock, flags);
  1466. if (lp->hs_state != LDC_HS_COMPLETE)
  1467. err = -ENOTCONN;
  1468. else
  1469. err = lp->mops->read(lp, buf, size);
  1470. spin_unlock_irqrestore(&lp->lock, flags);
  1471. return err;
  1472. }
  1473. EXPORT_SYMBOL(ldc_read);
  1474. static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages)
  1475. {
  1476. struct iommu_arena *arena = &iommu->arena;
  1477. unsigned long n, i, start, end, limit;
  1478. int pass;
  1479. limit = arena->limit;
  1480. start = arena->hint;
  1481. pass = 0;
  1482. again:
  1483. n = find_next_zero_bit(arena->map, limit, start);
  1484. end = n + npages;
  1485. if (unlikely(end >= limit)) {
  1486. if (likely(pass < 1)) {
  1487. limit = start;
  1488. start = 0;
  1489. pass++;
  1490. goto again;
  1491. } else {
  1492. /* Scanned the whole thing, give up. */
  1493. return -1;
  1494. }
  1495. }
  1496. for (i = n; i < end; i++) {
  1497. if (test_bit(i, arena->map)) {
  1498. start = i + 1;
  1499. goto again;
  1500. }
  1501. }
  1502. for (i = n; i < end; i++)
  1503. __set_bit(i, arena->map);
  1504. arena->hint = end;
  1505. return n;
  1506. }
  1507. #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
  1508. #define COOKIE_PGSZ_CODE_SHIFT 60ULL
  1509. static u64 pagesize_code(void)
  1510. {
  1511. switch (PAGE_SIZE) {
  1512. default:
  1513. case (8ULL * 1024ULL):
  1514. return 0;
  1515. case (64ULL * 1024ULL):
  1516. return 1;
  1517. case (512ULL * 1024ULL):
  1518. return 2;
  1519. case (4ULL * 1024ULL * 1024ULL):
  1520. return 3;
  1521. case (32ULL * 1024ULL * 1024ULL):
  1522. return 4;
  1523. case (256ULL * 1024ULL * 1024ULL):
  1524. return 5;
  1525. }
  1526. }
  1527. static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
  1528. {
  1529. return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
  1530. (index << PAGE_SHIFT) |
  1531. page_offset);
  1532. }
  1533. static u64 cookie_to_index(u64 cookie, unsigned long *shift)
  1534. {
  1535. u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
  1536. cookie &= ~COOKIE_PGSZ_CODE;
  1537. *shift = szcode * 3;
  1538. return (cookie >> (13ULL + (szcode * 3ULL)));
  1539. }
  1540. static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
  1541. unsigned long npages)
  1542. {
  1543. long entry;
  1544. entry = arena_alloc(iommu, npages);
  1545. if (unlikely(entry < 0))
  1546. return NULL;
  1547. return iommu->page_table + entry;
  1548. }
  1549. static u64 perm_to_mte(unsigned int map_perm)
  1550. {
  1551. u64 mte_base;
  1552. mte_base = pagesize_code();
  1553. if (map_perm & LDC_MAP_SHADOW) {
  1554. if (map_perm & LDC_MAP_R)
  1555. mte_base |= LDC_MTE_COPY_R;
  1556. if (map_perm & LDC_MAP_W)
  1557. mte_base |= LDC_MTE_COPY_W;
  1558. }
  1559. if (map_perm & LDC_MAP_DIRECT) {
  1560. if (map_perm & LDC_MAP_R)
  1561. mte_base |= LDC_MTE_READ;
  1562. if (map_perm & LDC_MAP_W)
  1563. mte_base |= LDC_MTE_WRITE;
  1564. if (map_perm & LDC_MAP_X)
  1565. mte_base |= LDC_MTE_EXEC;
  1566. }
  1567. if (map_perm & LDC_MAP_IO) {
  1568. if (map_perm & LDC_MAP_R)
  1569. mte_base |= LDC_MTE_IOMMU_R;
  1570. if (map_perm & LDC_MAP_W)
  1571. mte_base |= LDC_MTE_IOMMU_W;
  1572. }
  1573. return mte_base;
  1574. }
  1575. static int pages_in_region(unsigned long base, long len)
  1576. {
  1577. int count = 0;
  1578. do {
  1579. unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
  1580. len -= (new - base);
  1581. base = new;
  1582. count++;
  1583. } while (len > 0);
  1584. return count;
  1585. }
  1586. struct cookie_state {
  1587. struct ldc_mtable_entry *page_table;
  1588. struct ldc_trans_cookie *cookies;
  1589. u64 mte_base;
  1590. u64 prev_cookie;
  1591. u32 pte_idx;
  1592. u32 nc;
  1593. };
  1594. static void fill_cookies(struct cookie_state *sp, unsigned long pa,
  1595. unsigned long off, unsigned long len)
  1596. {
  1597. do {
  1598. unsigned long tlen, new = pa + PAGE_SIZE;
  1599. u64 this_cookie;
  1600. sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
  1601. tlen = PAGE_SIZE;
  1602. if (off)
  1603. tlen = PAGE_SIZE - off;
  1604. if (tlen > len)
  1605. tlen = len;
  1606. this_cookie = make_cookie(sp->pte_idx,
  1607. pagesize_code(), off);
  1608. off = 0;
  1609. if (this_cookie == sp->prev_cookie) {
  1610. sp->cookies[sp->nc - 1].cookie_size += tlen;
  1611. } else {
  1612. sp->cookies[sp->nc].cookie_addr = this_cookie;
  1613. sp->cookies[sp->nc].cookie_size = tlen;
  1614. sp->nc++;
  1615. }
  1616. sp->prev_cookie = this_cookie + tlen;
  1617. sp->pte_idx++;
  1618. len -= tlen;
  1619. pa = new;
  1620. } while (len > 0);
  1621. }
  1622. static int sg_count_one(struct scatterlist *sg)
  1623. {
  1624. unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT;
  1625. long len = sg->length;
  1626. if ((sg->offset | len) & (8UL - 1))
  1627. return -EFAULT;
  1628. return pages_in_region(base + sg->offset, len);
  1629. }
  1630. static int sg_count_pages(struct scatterlist *sg, int num_sg)
  1631. {
  1632. int count;
  1633. int i;
  1634. count = 0;
  1635. for (i = 0; i < num_sg; i++) {
  1636. int err = sg_count_one(sg + i);
  1637. if (err < 0)
  1638. return err;
  1639. count += err;
  1640. }
  1641. return count;
  1642. }
  1643. int ldc_map_sg(struct ldc_channel *lp,
  1644. struct scatterlist *sg, int num_sg,
  1645. struct ldc_trans_cookie *cookies, int ncookies,
  1646. unsigned int map_perm)
  1647. {
  1648. unsigned long i, npages, flags;
  1649. struct ldc_mtable_entry *base;
  1650. struct cookie_state state;
  1651. struct ldc_iommu *iommu;
  1652. int err;
  1653. if (map_perm & ~LDC_MAP_ALL)
  1654. return -EINVAL;
  1655. err = sg_count_pages(sg, num_sg);
  1656. if (err < 0)
  1657. return err;
  1658. npages = err;
  1659. if (err > ncookies)
  1660. return -EMSGSIZE;
  1661. iommu = &lp->iommu;
  1662. spin_lock_irqsave(&iommu->lock, flags);
  1663. base = alloc_npages(iommu, npages);
  1664. spin_unlock_irqrestore(&iommu->lock, flags);
  1665. if (!base)
  1666. return -ENOMEM;
  1667. state.page_table = iommu->page_table;
  1668. state.cookies = cookies;
  1669. state.mte_base = perm_to_mte(map_perm);
  1670. state.prev_cookie = ~(u64)0;
  1671. state.pte_idx = (base - iommu->page_table);
  1672. state.nc = 0;
  1673. for (i = 0; i < num_sg; i++)
  1674. fill_cookies(&state, page_to_pfn(sg_page(&sg[i])) << PAGE_SHIFT,
  1675. sg[i].offset, sg[i].length);
  1676. return state.nc;
  1677. }
  1678. EXPORT_SYMBOL(ldc_map_sg);
  1679. int ldc_map_single(struct ldc_channel *lp,
  1680. void *buf, unsigned int len,
  1681. struct ldc_trans_cookie *cookies, int ncookies,
  1682. unsigned int map_perm)
  1683. {
  1684. unsigned long npages, pa, flags;
  1685. struct ldc_mtable_entry *base;
  1686. struct cookie_state state;
  1687. struct ldc_iommu *iommu;
  1688. if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
  1689. return -EINVAL;
  1690. pa = __pa(buf);
  1691. if ((pa | len) & (8UL - 1))
  1692. return -EFAULT;
  1693. npages = pages_in_region(pa, len);
  1694. iommu = &lp->iommu;
  1695. spin_lock_irqsave(&iommu->lock, flags);
  1696. base = alloc_npages(iommu, npages);
  1697. spin_unlock_irqrestore(&iommu->lock, flags);
  1698. if (!base)
  1699. return -ENOMEM;
  1700. state.page_table = iommu->page_table;
  1701. state.cookies = cookies;
  1702. state.mte_base = perm_to_mte(map_perm);
  1703. state.prev_cookie = ~(u64)0;
  1704. state.pte_idx = (base - iommu->page_table);
  1705. state.nc = 0;
  1706. fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
  1707. BUG_ON(state.nc != 1);
  1708. return state.nc;
  1709. }
  1710. EXPORT_SYMBOL(ldc_map_single);
  1711. static void free_npages(unsigned long id, struct ldc_iommu *iommu,
  1712. u64 cookie, u64 size)
  1713. {
  1714. struct iommu_arena *arena = &iommu->arena;
  1715. unsigned long i, shift, index, npages;
  1716. struct ldc_mtable_entry *base;
  1717. npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
  1718. index = cookie_to_index(cookie, &shift);
  1719. base = iommu->page_table + index;
  1720. BUG_ON(index > arena->limit ||
  1721. (index + npages) > arena->limit);
  1722. for (i = 0; i < npages; i++) {
  1723. if (base->cookie)
  1724. sun4v_ldc_revoke(id, cookie + (i << shift),
  1725. base->cookie);
  1726. base->mte = 0;
  1727. __clear_bit(index + i, arena->map);
  1728. }
  1729. }
  1730. void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
  1731. int ncookies)
  1732. {
  1733. struct ldc_iommu *iommu = &lp->iommu;
  1734. unsigned long flags;
  1735. int i;
  1736. spin_lock_irqsave(&iommu->lock, flags);
  1737. for (i = 0; i < ncookies; i++) {
  1738. u64 addr = cookies[i].cookie_addr;
  1739. u64 size = cookies[i].cookie_size;
  1740. free_npages(lp->id, iommu, addr, size);
  1741. }
  1742. spin_unlock_irqrestore(&iommu->lock, flags);
  1743. }
  1744. EXPORT_SYMBOL(ldc_unmap);
  1745. int ldc_copy(struct ldc_channel *lp, int copy_dir,
  1746. void *buf, unsigned int len, unsigned long offset,
  1747. struct ldc_trans_cookie *cookies, int ncookies)
  1748. {
  1749. unsigned int orig_len;
  1750. unsigned long ra;
  1751. int i;
  1752. if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
  1753. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
  1754. lp->id, copy_dir);
  1755. return -EINVAL;
  1756. }
  1757. ra = __pa(buf);
  1758. if ((ra | len | offset) & (8UL - 1)) {
  1759. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
  1760. "ra[%lx] len[%x] offset[%lx]\n",
  1761. lp->id, ra, len, offset);
  1762. return -EFAULT;
  1763. }
  1764. if (lp->hs_state != LDC_HS_COMPLETE ||
  1765. (lp->flags & LDC_FLAG_RESET)) {
  1766. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
  1767. "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
  1768. return -ECONNRESET;
  1769. }
  1770. orig_len = len;
  1771. for (i = 0; i < ncookies; i++) {
  1772. unsigned long cookie_raddr = cookies[i].cookie_addr;
  1773. unsigned long this_len = cookies[i].cookie_size;
  1774. unsigned long actual_len;
  1775. if (unlikely(offset)) {
  1776. unsigned long this_off = offset;
  1777. if (this_off > this_len)
  1778. this_off = this_len;
  1779. offset -= this_off;
  1780. this_len -= this_off;
  1781. if (!this_len)
  1782. continue;
  1783. cookie_raddr += this_off;
  1784. }
  1785. if (this_len > len)
  1786. this_len = len;
  1787. while (1) {
  1788. unsigned long hv_err;
  1789. hv_err = sun4v_ldc_copy(lp->id, copy_dir,
  1790. cookie_raddr, ra,
  1791. this_len, &actual_len);
  1792. if (unlikely(hv_err)) {
  1793. printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
  1794. "HV error %lu\n",
  1795. lp->id, hv_err);
  1796. if (lp->hs_state != LDC_HS_COMPLETE ||
  1797. (lp->flags & LDC_FLAG_RESET))
  1798. return -ECONNRESET;
  1799. else
  1800. return -EFAULT;
  1801. }
  1802. cookie_raddr += actual_len;
  1803. ra += actual_len;
  1804. len -= actual_len;
  1805. if (actual_len == this_len)
  1806. break;
  1807. this_len -= actual_len;
  1808. }
  1809. if (!len)
  1810. break;
  1811. }
  1812. /* It is caller policy what to do about short copies.
  1813. * For example, a networking driver can declare the
  1814. * packet a runt and drop it.
  1815. */
  1816. return orig_len - len;
  1817. }
  1818. EXPORT_SYMBOL(ldc_copy);
  1819. void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
  1820. struct ldc_trans_cookie *cookies, int *ncookies,
  1821. unsigned int map_perm)
  1822. {
  1823. void *buf;
  1824. int err;
  1825. if (len & (8UL - 1))
  1826. return ERR_PTR(-EINVAL);
  1827. buf = kzalloc(len, GFP_KERNEL);
  1828. if (!buf)
  1829. return ERR_PTR(-ENOMEM);
  1830. err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
  1831. if (err < 0) {
  1832. kfree(buf);
  1833. return ERR_PTR(err);
  1834. }
  1835. *ncookies = err;
  1836. return buf;
  1837. }
  1838. EXPORT_SYMBOL(ldc_alloc_exp_dring);
  1839. void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
  1840. struct ldc_trans_cookie *cookies, int ncookies)
  1841. {
  1842. ldc_unmap(lp, cookies, ncookies);
  1843. kfree(buf);
  1844. }
  1845. EXPORT_SYMBOL(ldc_free_exp_dring);
  1846. static int __init ldc_init(void)
  1847. {
  1848. unsigned long major, minor;
  1849. struct mdesc_handle *hp;
  1850. const u64 *v;
  1851. int err;
  1852. u64 mp;
  1853. hp = mdesc_grab();
  1854. if (!hp)
  1855. return -ENODEV;
  1856. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  1857. err = -ENODEV;
  1858. if (mp == MDESC_NODE_NULL)
  1859. goto out;
  1860. v = mdesc_get_property(hp, mp, "domaining-enabled", NULL);
  1861. if (!v)
  1862. goto out;
  1863. major = 1;
  1864. minor = 0;
  1865. if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
  1866. printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
  1867. goto out;
  1868. }
  1869. printk(KERN_INFO "%s", version);
  1870. if (!*v) {
  1871. printk(KERN_INFO PFX "Domaining disabled.\n");
  1872. goto out;
  1873. }
  1874. ldom_domaining_enabled = 1;
  1875. err = 0;
  1876. out:
  1877. mdesc_release(hp);
  1878. return err;
  1879. }
  1880. core_initcall(ldc_init);