ldc.c 49 KB

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