ldc.c 49 KB

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