ldc.c 49 KB

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