ldc.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  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. if ((vp->major == 0 && vp->minor == 0) ||
  511. !(vap = find_by_major(vp->major))) {
  512. return ldc_abort(lp);
  513. } else {
  514. struct ldc_packet *p;
  515. unsigned long new_tail;
  516. p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
  517. vap, sizeof(*vap),
  518. &new_tail);
  519. if (p)
  520. return send_tx_packet(lp, p, new_tail);
  521. else
  522. return ldc_abort(lp);
  523. }
  524. }
  525. static int process_version(struct ldc_channel *lp,
  526. struct ldc_packet *p)
  527. {
  528. struct ldc_version *vp;
  529. vp = (struct ldc_version *) p->u.u_data;
  530. switch (p->stype) {
  531. case LDC_INFO:
  532. return process_ver_info(lp, vp);
  533. case LDC_ACK:
  534. return process_ver_ack(lp, vp);
  535. case LDC_NACK:
  536. return process_ver_nack(lp, vp);
  537. default:
  538. return ldc_abort(lp);
  539. }
  540. }
  541. static int process_rts(struct ldc_channel *lp,
  542. struct ldc_packet *p)
  543. {
  544. ldcdbg(HS, "GOT RTS stype[%x] seqid[%x] env[%x]\n",
  545. p->stype, p->seqid, p->env);
  546. if (p->stype != LDC_INFO ||
  547. lp->hs_state != LDC_HS_GOTVERS ||
  548. p->env != lp->cfg.mode)
  549. return ldc_abort(lp);
  550. lp->snd_nxt = p->seqid;
  551. lp->rcv_nxt = p->seqid;
  552. lp->hs_state = LDC_HS_SENTRTR;
  553. if (send_rtr(lp))
  554. return ldc_abort(lp);
  555. return 0;
  556. }
  557. static int process_rtr(struct ldc_channel *lp,
  558. struct ldc_packet *p)
  559. {
  560. ldcdbg(HS, "GOT RTR stype[%x] seqid[%x] env[%x]\n",
  561. p->stype, p->seqid, p->env);
  562. if (p->stype != LDC_INFO ||
  563. p->env != lp->cfg.mode)
  564. return ldc_abort(lp);
  565. lp->snd_nxt = p->seqid;
  566. lp->hs_state = LDC_HS_COMPLETE;
  567. ldc_set_state(lp, LDC_STATE_CONNECTED);
  568. send_rdx(lp);
  569. return LDC_EVENT_UP;
  570. }
  571. static int rx_seq_ok(struct ldc_channel *lp, u32 seqid)
  572. {
  573. return lp->rcv_nxt + 1 == seqid;
  574. }
  575. static int process_rdx(struct ldc_channel *lp,
  576. struct ldc_packet *p)
  577. {
  578. ldcdbg(HS, "GOT RDX stype[%x] seqid[%x] env[%x] ackid[%x]\n",
  579. p->stype, p->seqid, p->env, p->u.r.ackid);
  580. if (p->stype != LDC_INFO ||
  581. !(rx_seq_ok(lp, p->seqid)))
  582. return ldc_abort(lp);
  583. lp->rcv_nxt = p->seqid;
  584. lp->hs_state = LDC_HS_COMPLETE;
  585. ldc_set_state(lp, LDC_STATE_CONNECTED);
  586. return LDC_EVENT_UP;
  587. }
  588. static int process_control_frame(struct ldc_channel *lp,
  589. struct ldc_packet *p)
  590. {
  591. switch (p->ctrl) {
  592. case LDC_VERS:
  593. return process_version(lp, p);
  594. case LDC_RTS:
  595. return process_rts(lp, p);
  596. case LDC_RTR:
  597. return process_rtr(lp, p);
  598. case LDC_RDX:
  599. return process_rdx(lp, p);
  600. default:
  601. return ldc_abort(lp);
  602. }
  603. }
  604. static int process_error_frame(struct ldc_channel *lp,
  605. struct ldc_packet *p)
  606. {
  607. return ldc_abort(lp);
  608. }
  609. static int process_data_ack(struct ldc_channel *lp,
  610. struct ldc_packet *ack)
  611. {
  612. unsigned long head = lp->tx_acked;
  613. u32 ackid = ack->u.r.ackid;
  614. while (1) {
  615. struct ldc_packet *p = lp->tx_base + (head / LDC_PACKET_SIZE);
  616. head = tx_advance(lp, head);
  617. if (p->seqid == ackid) {
  618. lp->tx_acked = head;
  619. return 0;
  620. }
  621. if (head == lp->tx_tail)
  622. return ldc_abort(lp);
  623. }
  624. return 0;
  625. }
  626. static void send_events(struct ldc_channel *lp, unsigned int event_mask)
  627. {
  628. if (event_mask & LDC_EVENT_RESET)
  629. lp->cfg.event(lp->event_arg, LDC_EVENT_RESET);
  630. if (event_mask & LDC_EVENT_UP)
  631. lp->cfg.event(lp->event_arg, LDC_EVENT_UP);
  632. if (event_mask & LDC_EVENT_DATA_READY)
  633. lp->cfg.event(lp->event_arg, LDC_EVENT_DATA_READY);
  634. }
  635. static irqreturn_t ldc_rx(int irq, void *dev_id)
  636. {
  637. struct ldc_channel *lp = dev_id;
  638. unsigned long orig_state, hv_err, flags;
  639. unsigned int event_mask;
  640. spin_lock_irqsave(&lp->lock, flags);
  641. orig_state = lp->chan_state;
  642. hv_err = sun4v_ldc_rx_get_state(lp->id,
  643. &lp->rx_head,
  644. &lp->rx_tail,
  645. &lp->chan_state);
  646. ldcdbg(RX, "RX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
  647. orig_state, lp->chan_state, lp->rx_head, lp->rx_tail);
  648. event_mask = 0;
  649. if (lp->cfg.mode == LDC_MODE_RAW &&
  650. lp->chan_state == LDC_CHANNEL_UP) {
  651. lp->hs_state = LDC_HS_COMPLETE;
  652. ldc_set_state(lp, LDC_STATE_CONNECTED);
  653. event_mask |= LDC_EVENT_UP;
  654. orig_state = lp->chan_state;
  655. }
  656. /* If we are in reset state, flush the RX queue and ignore
  657. * everything.
  658. */
  659. if (lp->flags & LDC_FLAG_RESET) {
  660. (void) __set_rx_head(lp, lp->rx_tail);
  661. goto out;
  662. }
  663. /* Once we finish the handshake, we let the ldc_read()
  664. * paths do all of the control frame and state management.
  665. * Just trigger the callback.
  666. */
  667. if (lp->hs_state == LDC_HS_COMPLETE) {
  668. handshake_complete:
  669. if (lp->chan_state != orig_state) {
  670. unsigned int event = LDC_EVENT_RESET;
  671. if (lp->chan_state == LDC_CHANNEL_UP)
  672. event = LDC_EVENT_UP;
  673. event_mask |= event;
  674. }
  675. if (lp->rx_head != lp->rx_tail)
  676. event_mask |= LDC_EVENT_DATA_READY;
  677. goto out;
  678. }
  679. if (lp->chan_state != orig_state)
  680. goto out;
  681. while (lp->rx_head != lp->rx_tail) {
  682. struct ldc_packet *p;
  683. unsigned long new;
  684. int err;
  685. p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
  686. switch (p->type) {
  687. case LDC_CTRL:
  688. err = process_control_frame(lp, p);
  689. if (err > 0)
  690. event_mask |= err;
  691. break;
  692. case LDC_DATA:
  693. event_mask |= LDC_EVENT_DATA_READY;
  694. err = 0;
  695. break;
  696. case LDC_ERR:
  697. err = process_error_frame(lp, p);
  698. break;
  699. default:
  700. err = ldc_abort(lp);
  701. break;
  702. }
  703. if (err < 0)
  704. break;
  705. new = lp->rx_head;
  706. new += LDC_PACKET_SIZE;
  707. if (new == (lp->rx_num_entries * LDC_PACKET_SIZE))
  708. new = 0;
  709. lp->rx_head = new;
  710. err = __set_rx_head(lp, new);
  711. if (err < 0) {
  712. (void) ldc_abort(lp);
  713. break;
  714. }
  715. if (lp->hs_state == LDC_HS_COMPLETE)
  716. goto handshake_complete;
  717. }
  718. out:
  719. spin_unlock_irqrestore(&lp->lock, flags);
  720. send_events(lp, event_mask);
  721. return IRQ_HANDLED;
  722. }
  723. static irqreturn_t ldc_tx(int irq, void *dev_id)
  724. {
  725. struct ldc_channel *lp = dev_id;
  726. unsigned long flags, hv_err, orig_state;
  727. unsigned int event_mask = 0;
  728. spin_lock_irqsave(&lp->lock, flags);
  729. orig_state = lp->chan_state;
  730. hv_err = sun4v_ldc_tx_get_state(lp->id,
  731. &lp->tx_head,
  732. &lp->tx_tail,
  733. &lp->chan_state);
  734. ldcdbg(TX, " TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
  735. orig_state, lp->chan_state, lp->tx_head, lp->tx_tail);
  736. if (lp->cfg.mode == LDC_MODE_RAW &&
  737. lp->chan_state == LDC_CHANNEL_UP) {
  738. lp->hs_state = LDC_HS_COMPLETE;
  739. ldc_set_state(lp, LDC_STATE_CONNECTED);
  740. event_mask |= LDC_EVENT_UP;
  741. }
  742. spin_unlock_irqrestore(&lp->lock, flags);
  743. send_events(lp, event_mask);
  744. return IRQ_HANDLED;
  745. }
  746. /* XXX ldc_alloc() and ldc_free() needs to run under a mutex so
  747. * XXX that addition and removal from the ldc_channel_list has
  748. * XXX atomicity, otherwise the __ldc_channel_exists() check is
  749. * XXX totally pointless as another thread can slip into ldc_alloc()
  750. * XXX and add a channel with the same ID. There also needs to be
  751. * XXX a spinlock for ldc_channel_list.
  752. */
  753. static HLIST_HEAD(ldc_channel_list);
  754. static int __ldc_channel_exists(unsigned long id)
  755. {
  756. struct ldc_channel *lp;
  757. struct hlist_node *n;
  758. hlist_for_each_entry(lp, n, &ldc_channel_list, list) {
  759. if (lp->id == id)
  760. return 1;
  761. }
  762. return 0;
  763. }
  764. static int alloc_queue(const char *name, unsigned long num_entries,
  765. struct ldc_packet **base, unsigned long *ra)
  766. {
  767. unsigned long size, order;
  768. void *q;
  769. size = num_entries * LDC_PACKET_SIZE;
  770. order = get_order(size);
  771. q = (void *) __get_free_pages(GFP_KERNEL, order);
  772. if (!q) {
  773. printk(KERN_ERR PFX "Alloc of %s queue failed with "
  774. "size=%lu order=%lu\n", name, size, order);
  775. return -ENOMEM;
  776. }
  777. memset(q, 0, PAGE_SIZE << order);
  778. *base = q;
  779. *ra = __pa(q);
  780. return 0;
  781. }
  782. static void free_queue(unsigned long num_entries, struct ldc_packet *q)
  783. {
  784. unsigned long size, order;
  785. if (!q)
  786. return;
  787. size = num_entries * LDC_PACKET_SIZE;
  788. order = get_order(size);
  789. free_pages((unsigned long)q, order);
  790. }
  791. /* XXX Make this configurable... XXX */
  792. #define LDC_IOTABLE_SIZE (8 * 1024)
  793. static int ldc_iommu_init(struct ldc_channel *lp)
  794. {
  795. unsigned long sz, num_tsb_entries, tsbsize, order;
  796. struct ldc_iommu *iommu = &lp->iommu;
  797. struct ldc_mtable_entry *table;
  798. unsigned long hv_err;
  799. int err;
  800. num_tsb_entries = LDC_IOTABLE_SIZE;
  801. tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
  802. spin_lock_init(&iommu->lock);
  803. sz = num_tsb_entries / 8;
  804. sz = (sz + 7UL) & ~7UL;
  805. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  806. if (!iommu->arena.map) {
  807. printk(KERN_ERR PFX "Alloc of arena map failed, sz=%lu\n", sz);
  808. return -ENOMEM;
  809. }
  810. iommu->arena.limit = num_tsb_entries;
  811. order = get_order(tsbsize);
  812. table = (struct ldc_mtable_entry *)
  813. __get_free_pages(GFP_KERNEL, order);
  814. err = -ENOMEM;
  815. if (!table) {
  816. printk(KERN_ERR PFX "Alloc of MTE table failed, "
  817. "size=%lu order=%lu\n", tsbsize, order);
  818. goto out_free_map;
  819. }
  820. memset(table, 0, PAGE_SIZE << order);
  821. iommu->page_table = table;
  822. hv_err = sun4v_ldc_set_map_table(lp->id, __pa(table),
  823. num_tsb_entries);
  824. err = -EINVAL;
  825. if (hv_err)
  826. goto out_free_table;
  827. return 0;
  828. out_free_table:
  829. free_pages((unsigned long) table, order);
  830. iommu->page_table = NULL;
  831. out_free_map:
  832. kfree(iommu->arena.map);
  833. iommu->arena.map = NULL;
  834. return err;
  835. }
  836. static void ldc_iommu_release(struct ldc_channel *lp)
  837. {
  838. struct ldc_iommu *iommu = &lp->iommu;
  839. unsigned long num_tsb_entries, tsbsize, order;
  840. (void) sun4v_ldc_set_map_table(lp->id, 0, 0);
  841. num_tsb_entries = iommu->arena.limit;
  842. tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
  843. order = get_order(tsbsize);
  844. free_pages((unsigned long) iommu->page_table, order);
  845. iommu->page_table = NULL;
  846. kfree(iommu->arena.map);
  847. iommu->arena.map = NULL;
  848. }
  849. struct ldc_channel *ldc_alloc(unsigned long id,
  850. const struct ldc_channel_config *cfgp,
  851. void *event_arg)
  852. {
  853. struct ldc_channel *lp;
  854. const struct ldc_mode_ops *mops;
  855. unsigned long dummy1, dummy2, hv_err;
  856. u8 mss, *mssbuf;
  857. int err;
  858. err = -ENODEV;
  859. if (!ldom_domaining_enabled)
  860. goto out_err;
  861. err = -EINVAL;
  862. if (!cfgp)
  863. goto out_err;
  864. switch (cfgp->mode) {
  865. case LDC_MODE_RAW:
  866. mops = &raw_ops;
  867. mss = LDC_PACKET_SIZE;
  868. break;
  869. case LDC_MODE_UNRELIABLE:
  870. mops = &nonraw_ops;
  871. mss = LDC_PACKET_SIZE - 8;
  872. break;
  873. case LDC_MODE_STREAM:
  874. mops = &stream_ops;
  875. mss = LDC_PACKET_SIZE - 8 - 8;
  876. break;
  877. default:
  878. goto out_err;
  879. }
  880. if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq)
  881. goto out_err;
  882. hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2);
  883. err = -ENODEV;
  884. if (hv_err == HV_ECHANNEL)
  885. goto out_err;
  886. err = -EEXIST;
  887. if (__ldc_channel_exists(id))
  888. goto out_err;
  889. mssbuf = NULL;
  890. lp = kzalloc(sizeof(*lp), GFP_KERNEL);
  891. err = -ENOMEM;
  892. if (!lp)
  893. goto out_err;
  894. spin_lock_init(&lp->lock);
  895. lp->id = id;
  896. err = ldc_iommu_init(lp);
  897. if (err)
  898. goto out_free_ldc;
  899. lp->mops = mops;
  900. lp->mss = mss;
  901. lp->cfg = *cfgp;
  902. if (!lp->cfg.mtu)
  903. lp->cfg.mtu = LDC_DEFAULT_MTU;
  904. if (lp->cfg.mode == LDC_MODE_STREAM) {
  905. mssbuf = kzalloc(lp->cfg.mtu, GFP_KERNEL);
  906. if (!mssbuf) {
  907. err = -ENOMEM;
  908. goto out_free_iommu;
  909. }
  910. lp->mssbuf = mssbuf;
  911. }
  912. lp->event_arg = event_arg;
  913. /* XXX allow setting via ldc_channel_config to override defaults
  914. * XXX or use some formula based upon mtu
  915. */
  916. lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
  917. lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
  918. err = alloc_queue("TX", lp->tx_num_entries,
  919. &lp->tx_base, &lp->tx_ra);
  920. if (err)
  921. goto out_free_mssbuf;
  922. err = alloc_queue("RX", lp->rx_num_entries,
  923. &lp->rx_base, &lp->rx_ra);
  924. if (err)
  925. goto out_free_txq;
  926. lp->flags |= LDC_FLAG_ALLOCED_QUEUES;
  927. lp->hs_state = LDC_HS_CLOSED;
  928. ldc_set_state(lp, LDC_STATE_INIT);
  929. INIT_HLIST_NODE(&lp->list);
  930. hlist_add_head(&lp->list, &ldc_channel_list);
  931. INIT_HLIST_HEAD(&lp->mh_list);
  932. return lp;
  933. out_free_txq:
  934. free_queue(lp->tx_num_entries, lp->tx_base);
  935. out_free_mssbuf:
  936. if (mssbuf)
  937. kfree(mssbuf);
  938. out_free_iommu:
  939. ldc_iommu_release(lp);
  940. out_free_ldc:
  941. kfree(lp);
  942. out_err:
  943. return ERR_PTR(err);
  944. }
  945. EXPORT_SYMBOL(ldc_alloc);
  946. void ldc_free(struct ldc_channel *lp)
  947. {
  948. if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
  949. free_irq(lp->cfg.rx_irq, lp);
  950. free_irq(lp->cfg.tx_irq, lp);
  951. }
  952. if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
  953. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  954. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  955. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  956. }
  957. if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
  958. free_queue(lp->tx_num_entries, lp->tx_base);
  959. free_queue(lp->rx_num_entries, lp->rx_base);
  960. lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
  961. }
  962. hlist_del(&lp->list);
  963. if (lp->mssbuf)
  964. kfree(lp->mssbuf);
  965. ldc_iommu_release(lp);
  966. kfree(lp);
  967. }
  968. EXPORT_SYMBOL(ldc_free);
  969. /* Bind the channel. This registers the LDC queues with
  970. * the hypervisor and puts the channel into a pseudo-listening
  971. * state. This does not initiate a handshake, ldc_connect() does
  972. * that.
  973. */
  974. int ldc_bind(struct ldc_channel *lp, const char *name)
  975. {
  976. unsigned long hv_err, flags;
  977. int err = -EINVAL;
  978. if (!name ||
  979. (lp->state != LDC_STATE_INIT))
  980. return -EINVAL;
  981. snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name);
  982. snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name);
  983. err = request_irq(lp->cfg.rx_irq, ldc_rx,
  984. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  985. lp->rx_irq_name, lp);
  986. if (err)
  987. return err;
  988. err = request_irq(lp->cfg.tx_irq, ldc_tx,
  989. IRQF_SAMPLE_RANDOM | IRQF_SHARED,
  990. lp->tx_irq_name, lp);
  991. if (err) {
  992. free_irq(lp->cfg.rx_irq, lp);
  993. return err;
  994. }
  995. spin_lock_irqsave(&lp->lock, flags);
  996. enable_irq(lp->cfg.rx_irq);
  997. enable_irq(lp->cfg.tx_irq);
  998. lp->flags |= LDC_FLAG_REGISTERED_IRQS;
  999. err = -ENODEV;
  1000. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1001. if (hv_err)
  1002. goto out_free_irqs;
  1003. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1004. if (hv_err)
  1005. goto out_free_irqs;
  1006. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1007. if (hv_err)
  1008. goto out_unmap_tx;
  1009. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1010. if (hv_err)
  1011. goto out_unmap_tx;
  1012. lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
  1013. hv_err = sun4v_ldc_tx_get_state(lp->id,
  1014. &lp->tx_head,
  1015. &lp->tx_tail,
  1016. &lp->chan_state);
  1017. err = -EBUSY;
  1018. if (hv_err)
  1019. goto out_unmap_rx;
  1020. lp->tx_acked = lp->tx_head;
  1021. lp->hs_state = LDC_HS_OPEN;
  1022. ldc_set_state(lp, LDC_STATE_BOUND);
  1023. spin_unlock_irqrestore(&lp->lock, flags);
  1024. return 0;
  1025. out_unmap_rx:
  1026. lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
  1027. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1028. out_unmap_tx:
  1029. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1030. out_free_irqs:
  1031. lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
  1032. free_irq(lp->cfg.tx_irq, lp);
  1033. free_irq(lp->cfg.rx_irq, lp);
  1034. spin_unlock_irqrestore(&lp->lock, flags);
  1035. return err;
  1036. }
  1037. EXPORT_SYMBOL(ldc_bind);
  1038. int ldc_connect(struct ldc_channel *lp)
  1039. {
  1040. unsigned long flags;
  1041. int err;
  1042. if (lp->cfg.mode == LDC_MODE_RAW)
  1043. return -EINVAL;
  1044. spin_lock_irqsave(&lp->lock, flags);
  1045. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1046. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
  1047. lp->hs_state != LDC_HS_OPEN)
  1048. err = -EINVAL;
  1049. else
  1050. err = start_handshake(lp);
  1051. spin_unlock_irqrestore(&lp->lock, flags);
  1052. return err;
  1053. }
  1054. EXPORT_SYMBOL(ldc_connect);
  1055. int ldc_disconnect(struct ldc_channel *lp)
  1056. {
  1057. unsigned long hv_err, flags;
  1058. int err;
  1059. if (lp->cfg.mode == LDC_MODE_RAW)
  1060. return -EINVAL;
  1061. if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
  1062. !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
  1063. return -EINVAL;
  1064. spin_lock_irqsave(&lp->lock, flags);
  1065. err = -ENODEV;
  1066. hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1067. if (hv_err)
  1068. goto out_err;
  1069. hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
  1070. if (hv_err)
  1071. goto out_err;
  1072. hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1073. if (hv_err)
  1074. goto out_err;
  1075. hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
  1076. if (hv_err)
  1077. goto out_err;
  1078. ldc_set_state(lp, LDC_STATE_BOUND);
  1079. lp->hs_state = LDC_HS_OPEN;
  1080. lp->flags |= LDC_FLAG_RESET;
  1081. spin_unlock_irqrestore(&lp->lock, flags);
  1082. return 0;
  1083. out_err:
  1084. sun4v_ldc_tx_qconf(lp->id, 0, 0);
  1085. sun4v_ldc_rx_qconf(lp->id, 0, 0);
  1086. free_irq(lp->cfg.tx_irq, lp);
  1087. free_irq(lp->cfg.rx_irq, lp);
  1088. lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
  1089. LDC_FLAG_REGISTERED_QUEUES);
  1090. ldc_set_state(lp, LDC_STATE_INIT);
  1091. spin_unlock_irqrestore(&lp->lock, flags);
  1092. return err;
  1093. }
  1094. EXPORT_SYMBOL(ldc_disconnect);
  1095. int ldc_state(struct ldc_channel *lp)
  1096. {
  1097. return lp->state;
  1098. }
  1099. EXPORT_SYMBOL(ldc_state);
  1100. static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
  1101. {
  1102. struct ldc_packet *p;
  1103. unsigned long new_tail;
  1104. int err;
  1105. if (size > LDC_PACKET_SIZE)
  1106. return -EMSGSIZE;
  1107. p = data_get_tx_packet(lp, &new_tail);
  1108. if (!p)
  1109. return -EAGAIN;
  1110. memcpy(p, buf, size);
  1111. err = send_tx_packet(lp, p, new_tail);
  1112. if (!err)
  1113. err = size;
  1114. return err;
  1115. }
  1116. static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
  1117. {
  1118. struct ldc_packet *p;
  1119. unsigned long hv_err, new;
  1120. int err;
  1121. if (size < LDC_PACKET_SIZE)
  1122. return -EINVAL;
  1123. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1124. &lp->rx_head,
  1125. &lp->rx_tail,
  1126. &lp->chan_state);
  1127. if (hv_err)
  1128. return ldc_abort(lp);
  1129. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1130. lp->chan_state == LDC_CHANNEL_RESETTING)
  1131. return -ECONNRESET;
  1132. if (lp->rx_head == lp->rx_tail)
  1133. return 0;
  1134. p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
  1135. memcpy(buf, p, LDC_PACKET_SIZE);
  1136. new = rx_advance(lp, lp->rx_head);
  1137. lp->rx_head = new;
  1138. err = __set_rx_head(lp, new);
  1139. if (err < 0)
  1140. err = -ECONNRESET;
  1141. else
  1142. err = LDC_PACKET_SIZE;
  1143. return err;
  1144. }
  1145. static const struct ldc_mode_ops raw_ops = {
  1146. .write = write_raw,
  1147. .read = read_raw,
  1148. };
  1149. static int write_nonraw(struct ldc_channel *lp, const void *buf,
  1150. unsigned int size)
  1151. {
  1152. unsigned long hv_err, tail;
  1153. unsigned int copied;
  1154. u32 seq;
  1155. int err;
  1156. hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
  1157. &lp->chan_state);
  1158. if (unlikely(hv_err))
  1159. return -EBUSY;
  1160. if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
  1161. return ldc_abort(lp);
  1162. if (!tx_has_space_for(lp, size))
  1163. return -EAGAIN;
  1164. seq = lp->snd_nxt;
  1165. copied = 0;
  1166. tail = lp->tx_tail;
  1167. while (copied < size) {
  1168. struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
  1169. u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
  1170. p->u.u_data :
  1171. p->u.r.r_data);
  1172. int data_len;
  1173. p->type = LDC_DATA;
  1174. p->stype = LDC_INFO;
  1175. p->ctrl = 0;
  1176. data_len = size - copied;
  1177. if (data_len > lp->mss)
  1178. data_len = lp->mss;
  1179. BUG_ON(data_len > LDC_LEN);
  1180. p->env = (data_len |
  1181. (copied == 0 ? LDC_START : 0) |
  1182. (data_len == size - copied ? LDC_STOP : 0));
  1183. p->seqid = ++seq;
  1184. ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
  1185. p->type,
  1186. p->stype,
  1187. p->ctrl,
  1188. p->env,
  1189. p->seqid);
  1190. memcpy(data, buf, data_len);
  1191. buf += data_len;
  1192. copied += data_len;
  1193. tail = tx_advance(lp, tail);
  1194. }
  1195. err = set_tx_tail(lp, tail);
  1196. if (!err) {
  1197. lp->snd_nxt = seq;
  1198. err = size;
  1199. }
  1200. return err;
  1201. }
  1202. static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
  1203. struct ldc_packet *first_frag)
  1204. {
  1205. int err;
  1206. if (first_frag)
  1207. lp->rcv_nxt = first_frag->seqid - 1;
  1208. err = send_data_nack(lp, p);
  1209. if (err)
  1210. return err;
  1211. err = __set_rx_head(lp, lp->rx_tail);
  1212. if (err < 0)
  1213. return ldc_abort(lp);
  1214. return 0;
  1215. }
  1216. static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
  1217. {
  1218. if (p->stype & LDC_ACK) {
  1219. int err = process_data_ack(lp, p);
  1220. if (err)
  1221. return err;
  1222. }
  1223. if (p->stype & LDC_NACK)
  1224. return ldc_abort(lp);
  1225. return 0;
  1226. }
  1227. static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
  1228. {
  1229. unsigned long dummy;
  1230. int limit = 1000;
  1231. ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
  1232. cur_head, lp->rx_head, lp->rx_tail);
  1233. while (limit-- > 0) {
  1234. unsigned long hv_err;
  1235. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1236. &dummy,
  1237. &lp->rx_tail,
  1238. &lp->chan_state);
  1239. if (hv_err)
  1240. return ldc_abort(lp);
  1241. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1242. lp->chan_state == LDC_CHANNEL_RESETTING)
  1243. return -ECONNRESET;
  1244. if (cur_head != lp->rx_tail) {
  1245. ldcdbg(DATA, "DATA WAIT DONE "
  1246. "head[%lx] tail[%lx] chan_state[%lx]\n",
  1247. dummy, lp->rx_tail, lp->chan_state);
  1248. return 0;
  1249. }
  1250. udelay(1);
  1251. }
  1252. return -EAGAIN;
  1253. }
  1254. static int rx_set_head(struct ldc_channel *lp, unsigned long head)
  1255. {
  1256. int err = __set_rx_head(lp, head);
  1257. if (err < 0)
  1258. return ldc_abort(lp);
  1259. lp->rx_head = head;
  1260. return 0;
  1261. }
  1262. static void send_data_ack(struct ldc_channel *lp)
  1263. {
  1264. unsigned long new_tail;
  1265. struct ldc_packet *p;
  1266. p = data_get_tx_packet(lp, &new_tail);
  1267. if (likely(p)) {
  1268. int err;
  1269. memset(p, 0, sizeof(*p));
  1270. p->type = LDC_DATA;
  1271. p->stype = LDC_ACK;
  1272. p->ctrl = 0;
  1273. p->seqid = lp->snd_nxt + 1;
  1274. p->u.r.ackid = lp->rcv_nxt;
  1275. err = send_tx_packet(lp, p, new_tail);
  1276. if (!err)
  1277. lp->snd_nxt++;
  1278. }
  1279. }
  1280. static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
  1281. {
  1282. struct ldc_packet *first_frag;
  1283. unsigned long hv_err, new;
  1284. int err, copied;
  1285. hv_err = sun4v_ldc_rx_get_state(lp->id,
  1286. &lp->rx_head,
  1287. &lp->rx_tail,
  1288. &lp->chan_state);
  1289. if (hv_err)
  1290. return ldc_abort(lp);
  1291. if (lp->chan_state == LDC_CHANNEL_DOWN ||
  1292. lp->chan_state == LDC_CHANNEL_RESETTING)
  1293. return -ECONNRESET;
  1294. if (lp->rx_head == lp->rx_tail)
  1295. return 0;
  1296. first_frag = NULL;
  1297. copied = err = 0;
  1298. new = lp->rx_head;
  1299. while (1) {
  1300. struct ldc_packet *p;
  1301. int pkt_len;
  1302. BUG_ON(new == lp->rx_tail);
  1303. p = lp->rx_base + (new / LDC_PACKET_SIZE);
  1304. ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
  1305. "rcv_nxt[%08x]\n",
  1306. p->type,
  1307. p->stype,
  1308. p->ctrl,
  1309. p->env,
  1310. p->seqid,
  1311. p->u.r.ackid,
  1312. lp->rcv_nxt);
  1313. if (unlikely(!rx_seq_ok(lp, p->seqid))) {
  1314. err = rx_bad_seq(lp, p, first_frag);
  1315. copied = 0;
  1316. break;
  1317. }
  1318. if (p->type & LDC_CTRL) {
  1319. err = process_control_frame(lp, p);
  1320. if (err < 0)
  1321. break;
  1322. err = 0;
  1323. }
  1324. lp->rcv_nxt = p->seqid;
  1325. if (!(p->type & LDC_DATA)) {
  1326. new = rx_advance(lp, new);
  1327. goto no_data;
  1328. }
  1329. if (p->stype & (LDC_ACK | LDC_NACK)) {
  1330. err = data_ack_nack(lp, p);
  1331. if (err)
  1332. break;
  1333. }
  1334. if (!(p->stype & LDC_INFO)) {
  1335. new = rx_advance(lp, new);
  1336. err = rx_set_head(lp, new);
  1337. if (err)
  1338. break;
  1339. goto no_data;
  1340. }
  1341. pkt_len = p->env & LDC_LEN;
  1342. /* Every initial packet starts with the START bit set.
  1343. *
  1344. * Singleton packets will have both START+STOP set.
  1345. *
  1346. * Fragments will have START set in the first frame, STOP
  1347. * set in the last frame, and neither bit set in middle
  1348. * frames of the packet.
  1349. *
  1350. * Therefore if we are at the beginning of a packet and
  1351. * we don't see START, or we are in the middle of a fragmented
  1352. * packet and do see START, we are unsynchronized and should
  1353. * flush the RX queue.
  1354. */
  1355. if ((first_frag == NULL && !(p->env & LDC_START)) ||
  1356. (first_frag != NULL && (p->env & LDC_START))) {
  1357. if (!first_frag)
  1358. new = rx_advance(lp, new);
  1359. err = rx_set_head(lp, new);
  1360. if (err)
  1361. break;
  1362. if (!first_frag)
  1363. goto no_data;
  1364. }
  1365. if (!first_frag)
  1366. first_frag = p;
  1367. if (pkt_len > size - copied) {
  1368. /* User didn't give us a big enough buffer,
  1369. * what to do? This is a pretty serious error.
  1370. *
  1371. * Since we haven't updated the RX ring head to
  1372. * consume any of the packets, signal the error
  1373. * to the user and just leave the RX ring alone.
  1374. *
  1375. * This seems the best behavior because this allows
  1376. * a user of the LDC layer to start with a small
  1377. * RX buffer for ldc_read() calls and use -EMSGSIZE
  1378. * as a cue to enlarge it's read buffer.
  1379. */
  1380. err = -EMSGSIZE;
  1381. break;
  1382. }
  1383. /* Ok, we are gonna eat this one. */
  1384. new = rx_advance(lp, new);
  1385. memcpy(buf,
  1386. (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
  1387. p->u.u_data : p->u.r.r_data), pkt_len);
  1388. buf += pkt_len;
  1389. copied += pkt_len;
  1390. if (p->env & LDC_STOP)
  1391. break;
  1392. no_data:
  1393. if (new == lp->rx_tail) {
  1394. err = rx_data_wait(lp, new);
  1395. if (err)
  1396. break;
  1397. }
  1398. }
  1399. if (!err)
  1400. err = rx_set_head(lp, new);
  1401. if (err && first_frag)
  1402. lp->rcv_nxt = first_frag->seqid - 1;
  1403. if (!err) {
  1404. err = copied;
  1405. if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE)
  1406. send_data_ack(lp);
  1407. }
  1408. return err;
  1409. }
  1410. static const struct ldc_mode_ops nonraw_ops = {
  1411. .write = write_nonraw,
  1412. .read = read_nonraw,
  1413. };
  1414. static int write_stream(struct ldc_channel *lp, const void *buf,
  1415. unsigned int size)
  1416. {
  1417. if (size > lp->cfg.mtu)
  1418. size = lp->cfg.mtu;
  1419. return write_nonraw(lp, buf, size);
  1420. }
  1421. static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
  1422. {
  1423. if (!lp->mssbuf_len) {
  1424. int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu);
  1425. if (err < 0)
  1426. return err;
  1427. lp->mssbuf_len = err;
  1428. lp->mssbuf_off = 0;
  1429. }
  1430. if (size > lp->mssbuf_len)
  1431. size = lp->mssbuf_len;
  1432. memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
  1433. lp->mssbuf_off += size;
  1434. lp->mssbuf_len -= size;
  1435. return size;
  1436. }
  1437. static const struct ldc_mode_ops stream_ops = {
  1438. .write = write_stream,
  1439. .read = read_stream,
  1440. };
  1441. int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
  1442. {
  1443. unsigned long flags;
  1444. int err;
  1445. if (!buf)
  1446. return -EINVAL;
  1447. if (!size)
  1448. return 0;
  1449. spin_lock_irqsave(&lp->lock, flags);
  1450. if (lp->hs_state != LDC_HS_COMPLETE)
  1451. err = -ENOTCONN;
  1452. else
  1453. err = lp->mops->write(lp, buf, size);
  1454. spin_unlock_irqrestore(&lp->lock, flags);
  1455. return err;
  1456. }
  1457. EXPORT_SYMBOL(ldc_write);
  1458. int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
  1459. {
  1460. unsigned long flags;
  1461. int err;
  1462. if (!buf)
  1463. return -EINVAL;
  1464. if (!size)
  1465. return 0;
  1466. spin_lock_irqsave(&lp->lock, flags);
  1467. if (lp->hs_state != LDC_HS_COMPLETE)
  1468. err = -ENOTCONN;
  1469. else
  1470. err = lp->mops->read(lp, buf, size);
  1471. spin_unlock_irqrestore(&lp->lock, flags);
  1472. return err;
  1473. }
  1474. EXPORT_SYMBOL(ldc_read);
  1475. static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages)
  1476. {
  1477. struct iommu_arena *arena = &iommu->arena;
  1478. unsigned long n, i, start, end, limit;
  1479. int pass;
  1480. limit = arena->limit;
  1481. start = arena->hint;
  1482. pass = 0;
  1483. again:
  1484. n = find_next_zero_bit(arena->map, limit, start);
  1485. end = n + npages;
  1486. if (unlikely(end >= limit)) {
  1487. if (likely(pass < 1)) {
  1488. limit = start;
  1489. start = 0;
  1490. pass++;
  1491. goto again;
  1492. } else {
  1493. /* Scanned the whole thing, give up. */
  1494. return -1;
  1495. }
  1496. }
  1497. for (i = n; i < end; i++) {
  1498. if (test_bit(i, arena->map)) {
  1499. start = i + 1;
  1500. goto again;
  1501. }
  1502. }
  1503. for (i = n; i < end; i++)
  1504. __set_bit(i, arena->map);
  1505. arena->hint = end;
  1506. return n;
  1507. }
  1508. #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
  1509. #define COOKIE_PGSZ_CODE_SHIFT 60ULL
  1510. static u64 pagesize_code(void)
  1511. {
  1512. switch (PAGE_SIZE) {
  1513. default:
  1514. case (8ULL * 1024ULL):
  1515. return 0;
  1516. case (64ULL * 1024ULL):
  1517. return 1;
  1518. case (512ULL * 1024ULL):
  1519. return 2;
  1520. case (4ULL * 1024ULL * 1024ULL):
  1521. return 3;
  1522. case (32ULL * 1024ULL * 1024ULL):
  1523. return 4;
  1524. case (256ULL * 1024ULL * 1024ULL):
  1525. return 5;
  1526. }
  1527. }
  1528. static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
  1529. {
  1530. return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
  1531. (index << PAGE_SHIFT) |
  1532. page_offset);
  1533. }
  1534. static u64 cookie_to_index(u64 cookie, unsigned long *shift)
  1535. {
  1536. u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
  1537. cookie &= ~COOKIE_PGSZ_CODE;
  1538. *shift = szcode * 3;
  1539. return (cookie >> (13ULL + (szcode * 3ULL)));
  1540. }
  1541. static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
  1542. unsigned long npages)
  1543. {
  1544. long entry;
  1545. entry = arena_alloc(iommu, npages);
  1546. if (unlikely(entry < 0))
  1547. return NULL;
  1548. return iommu->page_table + entry;
  1549. }
  1550. static u64 perm_to_mte(unsigned int map_perm)
  1551. {
  1552. u64 mte_base;
  1553. mte_base = pagesize_code();
  1554. if (map_perm & LDC_MAP_SHADOW) {
  1555. if (map_perm & LDC_MAP_R)
  1556. mte_base |= LDC_MTE_COPY_R;
  1557. if (map_perm & LDC_MAP_W)
  1558. mte_base |= LDC_MTE_COPY_W;
  1559. }
  1560. if (map_perm & LDC_MAP_DIRECT) {
  1561. if (map_perm & LDC_MAP_R)
  1562. mte_base |= LDC_MTE_READ;
  1563. if (map_perm & LDC_MAP_W)
  1564. mte_base |= LDC_MTE_WRITE;
  1565. if (map_perm & LDC_MAP_X)
  1566. mte_base |= LDC_MTE_EXEC;
  1567. }
  1568. if (map_perm & LDC_MAP_IO) {
  1569. if (map_perm & LDC_MAP_R)
  1570. mte_base |= LDC_MTE_IOMMU_R;
  1571. if (map_perm & LDC_MAP_W)
  1572. mte_base |= LDC_MTE_IOMMU_W;
  1573. }
  1574. return mte_base;
  1575. }
  1576. static int pages_in_region(unsigned long base, long len)
  1577. {
  1578. int count = 0;
  1579. do {
  1580. unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
  1581. len -= (new - base);
  1582. base = new;
  1583. count++;
  1584. } while (len > 0);
  1585. return count;
  1586. }
  1587. struct cookie_state {
  1588. struct ldc_mtable_entry *page_table;
  1589. struct ldc_trans_cookie *cookies;
  1590. u64 mte_base;
  1591. u64 prev_cookie;
  1592. u32 pte_idx;
  1593. u32 nc;
  1594. };
  1595. static void fill_cookies(struct cookie_state *sp, unsigned long pa,
  1596. unsigned long off, unsigned long len)
  1597. {
  1598. do {
  1599. unsigned long tlen, new = pa + PAGE_SIZE;
  1600. u64 this_cookie;
  1601. sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
  1602. tlen = PAGE_SIZE;
  1603. if (off)
  1604. tlen = PAGE_SIZE - off;
  1605. if (tlen > len)
  1606. tlen = len;
  1607. this_cookie = make_cookie(sp->pte_idx,
  1608. pagesize_code(), off);
  1609. off = 0;
  1610. if (this_cookie == sp->prev_cookie) {
  1611. sp->cookies[sp->nc - 1].cookie_size += tlen;
  1612. } else {
  1613. sp->cookies[sp->nc].cookie_addr = this_cookie;
  1614. sp->cookies[sp->nc].cookie_size = tlen;
  1615. sp->nc++;
  1616. }
  1617. sp->prev_cookie = this_cookie + tlen;
  1618. sp->pte_idx++;
  1619. len -= tlen;
  1620. pa = new;
  1621. } while (len > 0);
  1622. }
  1623. static int sg_count_one(struct scatterlist *sg)
  1624. {
  1625. unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT;
  1626. long len = sg->length;
  1627. if ((sg->offset | len) & (8UL - 1))
  1628. return -EFAULT;
  1629. return pages_in_region(base + sg->offset, len);
  1630. }
  1631. static int sg_count_pages(struct scatterlist *sg, int num_sg)
  1632. {
  1633. int count;
  1634. int i;
  1635. count = 0;
  1636. for (i = 0; i < num_sg; i++) {
  1637. int err = sg_count_one(sg + i);
  1638. if (err < 0)
  1639. return err;
  1640. count += err;
  1641. }
  1642. return count;
  1643. }
  1644. int ldc_map_sg(struct ldc_channel *lp,
  1645. struct scatterlist *sg, int num_sg,
  1646. struct ldc_trans_cookie *cookies, int ncookies,
  1647. unsigned int map_perm)
  1648. {
  1649. unsigned long i, npages, flags;
  1650. struct ldc_mtable_entry *base;
  1651. struct cookie_state state;
  1652. struct ldc_iommu *iommu;
  1653. int err;
  1654. if (map_perm & ~LDC_MAP_ALL)
  1655. return -EINVAL;
  1656. err = sg_count_pages(sg, num_sg);
  1657. if (err < 0)
  1658. return err;
  1659. npages = err;
  1660. if (err > ncookies)
  1661. return -EMSGSIZE;
  1662. iommu = &lp->iommu;
  1663. spin_lock_irqsave(&iommu->lock, flags);
  1664. base = alloc_npages(iommu, npages);
  1665. spin_unlock_irqrestore(&iommu->lock, flags);
  1666. if (!base)
  1667. return -ENOMEM;
  1668. state.page_table = iommu->page_table;
  1669. state.cookies = cookies;
  1670. state.mte_base = perm_to_mte(map_perm);
  1671. state.prev_cookie = ~(u64)0;
  1672. state.pte_idx = (base - iommu->page_table);
  1673. state.nc = 0;
  1674. for (i = 0; i < num_sg; i++)
  1675. fill_cookies(&state, page_to_pfn(sg_page(&sg[i])) << PAGE_SHIFT,
  1676. sg[i].offset, sg[i].length);
  1677. return state.nc;
  1678. }
  1679. EXPORT_SYMBOL(ldc_map_sg);
  1680. int ldc_map_single(struct ldc_channel *lp,
  1681. void *buf, unsigned int len,
  1682. struct ldc_trans_cookie *cookies, int ncookies,
  1683. unsigned int map_perm)
  1684. {
  1685. unsigned long npages, pa, flags;
  1686. struct ldc_mtable_entry *base;
  1687. struct cookie_state state;
  1688. struct ldc_iommu *iommu;
  1689. if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
  1690. return -EINVAL;
  1691. pa = __pa(buf);
  1692. if ((pa | len) & (8UL - 1))
  1693. return -EFAULT;
  1694. npages = pages_in_region(pa, len);
  1695. iommu = &lp->iommu;
  1696. spin_lock_irqsave(&iommu->lock, flags);
  1697. base = alloc_npages(iommu, npages);
  1698. spin_unlock_irqrestore(&iommu->lock, flags);
  1699. if (!base)
  1700. return -ENOMEM;
  1701. state.page_table = iommu->page_table;
  1702. state.cookies = cookies;
  1703. state.mte_base = perm_to_mte(map_perm);
  1704. state.prev_cookie = ~(u64)0;
  1705. state.pte_idx = (base - iommu->page_table);
  1706. state.nc = 0;
  1707. fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
  1708. BUG_ON(state.nc != 1);
  1709. return state.nc;
  1710. }
  1711. EXPORT_SYMBOL(ldc_map_single);
  1712. static void free_npages(unsigned long id, struct ldc_iommu *iommu,
  1713. u64 cookie, u64 size)
  1714. {
  1715. struct iommu_arena *arena = &iommu->arena;
  1716. unsigned long i, shift, index, npages;
  1717. struct ldc_mtable_entry *base;
  1718. npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
  1719. index = cookie_to_index(cookie, &shift);
  1720. base = iommu->page_table + index;
  1721. BUG_ON(index > arena->limit ||
  1722. (index + npages) > arena->limit);
  1723. for (i = 0; i < npages; i++) {
  1724. if (base->cookie)
  1725. sun4v_ldc_revoke(id, cookie + (i << shift),
  1726. base->cookie);
  1727. base->mte = 0;
  1728. __clear_bit(index + i, arena->map);
  1729. }
  1730. }
  1731. void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
  1732. int ncookies)
  1733. {
  1734. struct ldc_iommu *iommu = &lp->iommu;
  1735. unsigned long flags;
  1736. int i;
  1737. spin_lock_irqsave(&iommu->lock, flags);
  1738. for (i = 0; i < ncookies; i++) {
  1739. u64 addr = cookies[i].cookie_addr;
  1740. u64 size = cookies[i].cookie_size;
  1741. free_npages(lp->id, iommu, addr, size);
  1742. }
  1743. spin_unlock_irqrestore(&iommu->lock, flags);
  1744. }
  1745. EXPORT_SYMBOL(ldc_unmap);
  1746. int ldc_copy(struct ldc_channel *lp, int copy_dir,
  1747. void *buf, unsigned int len, unsigned long offset,
  1748. struct ldc_trans_cookie *cookies, int ncookies)
  1749. {
  1750. unsigned int orig_len;
  1751. unsigned long ra;
  1752. int i;
  1753. if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
  1754. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
  1755. lp->id, copy_dir);
  1756. return -EINVAL;
  1757. }
  1758. ra = __pa(buf);
  1759. if ((ra | len | offset) & (8UL - 1)) {
  1760. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
  1761. "ra[%lx] len[%x] offset[%lx]\n",
  1762. lp->id, ra, len, offset);
  1763. return -EFAULT;
  1764. }
  1765. if (lp->hs_state != LDC_HS_COMPLETE ||
  1766. (lp->flags & LDC_FLAG_RESET)) {
  1767. printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
  1768. "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
  1769. return -ECONNRESET;
  1770. }
  1771. orig_len = len;
  1772. for (i = 0; i < ncookies; i++) {
  1773. unsigned long cookie_raddr = cookies[i].cookie_addr;
  1774. unsigned long this_len = cookies[i].cookie_size;
  1775. unsigned long actual_len;
  1776. if (unlikely(offset)) {
  1777. unsigned long this_off = offset;
  1778. if (this_off > this_len)
  1779. this_off = this_len;
  1780. offset -= this_off;
  1781. this_len -= this_off;
  1782. if (!this_len)
  1783. continue;
  1784. cookie_raddr += this_off;
  1785. }
  1786. if (this_len > len)
  1787. this_len = len;
  1788. while (1) {
  1789. unsigned long hv_err;
  1790. hv_err = sun4v_ldc_copy(lp->id, copy_dir,
  1791. cookie_raddr, ra,
  1792. this_len, &actual_len);
  1793. if (unlikely(hv_err)) {
  1794. printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
  1795. "HV error %lu\n",
  1796. lp->id, hv_err);
  1797. if (lp->hs_state != LDC_HS_COMPLETE ||
  1798. (lp->flags & LDC_FLAG_RESET))
  1799. return -ECONNRESET;
  1800. else
  1801. return -EFAULT;
  1802. }
  1803. cookie_raddr += actual_len;
  1804. ra += actual_len;
  1805. len -= actual_len;
  1806. if (actual_len == this_len)
  1807. break;
  1808. this_len -= actual_len;
  1809. }
  1810. if (!len)
  1811. break;
  1812. }
  1813. /* It is caller policy what to do about short copies.
  1814. * For example, a networking driver can declare the
  1815. * packet a runt and drop it.
  1816. */
  1817. return orig_len - len;
  1818. }
  1819. EXPORT_SYMBOL(ldc_copy);
  1820. void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
  1821. struct ldc_trans_cookie *cookies, int *ncookies,
  1822. unsigned int map_perm)
  1823. {
  1824. void *buf;
  1825. int err;
  1826. if (len & (8UL - 1))
  1827. return ERR_PTR(-EINVAL);
  1828. buf = kzalloc(len, GFP_KERNEL);
  1829. if (!buf)
  1830. return ERR_PTR(-ENOMEM);
  1831. err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
  1832. if (err < 0) {
  1833. kfree(buf);
  1834. return ERR_PTR(err);
  1835. }
  1836. *ncookies = err;
  1837. return buf;
  1838. }
  1839. EXPORT_SYMBOL(ldc_alloc_exp_dring);
  1840. void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
  1841. struct ldc_trans_cookie *cookies, int ncookies)
  1842. {
  1843. ldc_unmap(lp, cookies, ncookies);
  1844. kfree(buf);
  1845. }
  1846. EXPORT_SYMBOL(ldc_free_exp_dring);
  1847. static int __init ldc_init(void)
  1848. {
  1849. unsigned long major, minor;
  1850. struct mdesc_handle *hp;
  1851. const u64 *v;
  1852. int err;
  1853. u64 mp;
  1854. hp = mdesc_grab();
  1855. if (!hp)
  1856. return -ENODEV;
  1857. mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
  1858. err = -ENODEV;
  1859. if (mp == MDESC_NODE_NULL)
  1860. goto out;
  1861. v = mdesc_get_property(hp, mp, "domaining-enabled", NULL);
  1862. if (!v)
  1863. goto out;
  1864. major = 1;
  1865. minor = 0;
  1866. if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
  1867. printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
  1868. goto out;
  1869. }
  1870. printk(KERN_INFO "%s", version);
  1871. if (!*v) {
  1872. printk(KERN_INFO PFX "Domaining disabled.\n");
  1873. goto out;
  1874. }
  1875. ldom_domaining_enabled = 1;
  1876. err = 0;
  1877. out:
  1878. mdesc_release(hp);
  1879. return err;
  1880. }
  1881. core_initcall(ldc_init);