ixp4xx_hss.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * Intel IXP4xx HSS (synchronous serial port) driver for Linux
  3. *
  4. * Copyright (C) 2007-2008 Krzysztof Hałasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/cdev.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/fs.h>
  15. #include <linux/hdlc.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/poll.h>
  20. #include <mach/npe.h>
  21. #include <mach/qmgr.h>
  22. #define DEBUG_DESC 0
  23. #define DEBUG_RX 0
  24. #define DEBUG_TX 0
  25. #define DEBUG_PKT_BYTES 0
  26. #define DEBUG_CLOSE 0
  27. #define DRV_NAME "ixp4xx_hss"
  28. #define PKT_EXTRA_FLAGS 0 /* orig 1 */
  29. #define PKT_NUM_PIPES 1 /* 1, 2 or 4 */
  30. #define PKT_PIPE_FIFO_SIZEW 4 /* total 4 dwords per HSS */
  31. #define RX_DESCS 16 /* also length of all RX queues */
  32. #define TX_DESCS 16 /* also length of all TX queues */
  33. #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
  34. #define RX_SIZE (HDLC_MAX_MRU + 4) /* NPE needs more space */
  35. #define MAX_CLOSE_WAIT 1000 /* microseconds */
  36. #define HSS_COUNT 2
  37. #define FRAME_SIZE 256 /* doesn't matter at this point */
  38. #define FRAME_OFFSET 0
  39. #define MAX_CHANNELS (FRAME_SIZE / 8)
  40. #define NAPI_WEIGHT 16
  41. /* Queue IDs */
  42. #define HSS0_CHL_RXTRIG_QUEUE 12 /* orig size = 32 dwords */
  43. #define HSS0_PKT_RX_QUEUE 13 /* orig size = 32 dwords */
  44. #define HSS0_PKT_TX0_QUEUE 14 /* orig size = 16 dwords */
  45. #define HSS0_PKT_TX1_QUEUE 15
  46. #define HSS0_PKT_TX2_QUEUE 16
  47. #define HSS0_PKT_TX3_QUEUE 17
  48. #define HSS0_PKT_RXFREE0_QUEUE 18 /* orig size = 16 dwords */
  49. #define HSS0_PKT_RXFREE1_QUEUE 19
  50. #define HSS0_PKT_RXFREE2_QUEUE 20
  51. #define HSS0_PKT_RXFREE3_QUEUE 21
  52. #define HSS0_PKT_TXDONE_QUEUE 22 /* orig size = 64 dwords */
  53. #define HSS1_CHL_RXTRIG_QUEUE 10
  54. #define HSS1_PKT_RX_QUEUE 0
  55. #define HSS1_PKT_TX0_QUEUE 5
  56. #define HSS1_PKT_TX1_QUEUE 6
  57. #define HSS1_PKT_TX2_QUEUE 7
  58. #define HSS1_PKT_TX3_QUEUE 8
  59. #define HSS1_PKT_RXFREE0_QUEUE 1
  60. #define HSS1_PKT_RXFREE1_QUEUE 2
  61. #define HSS1_PKT_RXFREE2_QUEUE 3
  62. #define HSS1_PKT_RXFREE3_QUEUE 4
  63. #define HSS1_PKT_TXDONE_QUEUE 9
  64. #define NPE_PKT_MODE_HDLC 0
  65. #define NPE_PKT_MODE_RAW 1
  66. #define NPE_PKT_MODE_56KMODE 2
  67. #define NPE_PKT_MODE_56KENDIAN_MSB 4
  68. /* PKT_PIPE_HDLC_CFG_WRITE flags */
  69. #define PKT_HDLC_IDLE_ONES 0x1 /* default = flags */
  70. #define PKT_HDLC_CRC_32 0x2 /* default = CRC-16 */
  71. #define PKT_HDLC_MSB_ENDIAN 0x4 /* default = LE */
  72. /* hss_config, PCRs */
  73. /* Frame sync sampling, default = active low */
  74. #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
  75. #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
  76. #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
  77. /* Frame sync pin: input (default) or output generated off a given clk edge */
  78. #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
  79. #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
  80. /* Frame and data clock sampling on edge, default = falling */
  81. #define PCR_FCLK_EDGE_RISING 0x08000000
  82. #define PCR_DCLK_EDGE_RISING 0x04000000
  83. /* Clock direction, default = input */
  84. #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
  85. /* Generate/Receive frame pulses, default = enabled */
  86. #define PCR_FRM_PULSE_DISABLED 0x01000000
  87. /* Data rate is full (default) or half the configured clk speed */
  88. #define PCR_HALF_CLK_RATE 0x00200000
  89. /* Invert data between NPE and HSS FIFOs? (default = no) */
  90. #define PCR_DATA_POLARITY_INVERT 0x00100000
  91. /* TX/RX endianness, default = LSB */
  92. #define PCR_MSB_ENDIAN 0x00080000
  93. /* Normal (default) / open drain mode (TX only) */
  94. #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
  95. /* No framing bit transmitted and expected on RX? (default = framing bit) */
  96. #define PCR_SOF_NO_FBIT 0x00020000
  97. /* Drive data pins? */
  98. #define PCR_TX_DATA_ENABLE 0x00010000
  99. /* Voice 56k type: drive the data pins low (default), high, high Z */
  100. #define PCR_TX_V56K_HIGH 0x00002000
  101. #define PCR_TX_V56K_HIGH_IMP 0x00004000
  102. /* Unassigned type: drive the data pins low (default), high, high Z */
  103. #define PCR_TX_UNASS_HIGH 0x00000800
  104. #define PCR_TX_UNASS_HIGH_IMP 0x00001000
  105. /* T1 @ 1.544MHz only: Fbit dictated in FIFO (default) or high Z */
  106. #define PCR_TX_FB_HIGH_IMP 0x00000400
  107. /* 56k data endiannes - which bit unused: high (default) or low */
  108. #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
  109. /* 56k data transmission type: 32/8 bit data (default) or 56K data */
  110. #define PCR_TX_56KS_56K_DATA 0x00000100
  111. /* hss_config, cCR */
  112. /* Number of packetized clients, default = 1 */
  113. #define CCR_NPE_HFIFO_2_HDLC 0x04000000
  114. #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
  115. /* default = no loopback */
  116. #define CCR_LOOPBACK 0x02000000
  117. /* HSS number, default = 0 (first) */
  118. #define CCR_SECOND_HSS 0x01000000
  119. /* hss_config, clkCR: main:10, num:10, denom:12 */
  120. #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15) /*65 KHz*/
  121. #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
  122. #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
  123. #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
  124. #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
  125. #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
  126. #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
  127. #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
  128. #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
  129. #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
  130. #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
  131. #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
  132. #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
  133. /* hss_config, LUT entries */
  134. #define TDMMAP_UNASSIGNED 0
  135. #define TDMMAP_HDLC 1 /* HDLC - packetized */
  136. #define TDMMAP_VOICE56K 2 /* Voice56K - 7-bit channelized */
  137. #define TDMMAP_VOICE64K 3 /* Voice64K - 8-bit channelized */
  138. /* offsets into HSS config */
  139. #define HSS_CONFIG_TX_PCR 0x00 /* port configuration registers */
  140. #define HSS_CONFIG_RX_PCR 0x04
  141. #define HSS_CONFIG_CORE_CR 0x08 /* loopback control, HSS# */
  142. #define HSS_CONFIG_CLOCK_CR 0x0C /* clock generator control */
  143. #define HSS_CONFIG_TX_FCR 0x10 /* frame configuration registers */
  144. #define HSS_CONFIG_RX_FCR 0x14
  145. #define HSS_CONFIG_TX_LUT 0x18 /* channel look-up tables */
  146. #define HSS_CONFIG_RX_LUT 0x38
  147. /* NPE command codes */
  148. /* writes the ConfigWord value to the location specified by offset */
  149. #define PORT_CONFIG_WRITE 0x40
  150. /* triggers the NPE to load the contents of the configuration table */
  151. #define PORT_CONFIG_LOAD 0x41
  152. /* triggers the NPE to return an HssErrorReadResponse message */
  153. #define PORT_ERROR_READ 0x42
  154. /* triggers the NPE to reset internal status and enable the HssPacketized
  155. operation for the flow specified by pPipe */
  156. #define PKT_PIPE_FLOW_ENABLE 0x50
  157. #define PKT_PIPE_FLOW_DISABLE 0x51
  158. #define PKT_NUM_PIPES_WRITE 0x52
  159. #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
  160. #define PKT_PIPE_HDLC_CFG_WRITE 0x54
  161. #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
  162. #define PKT_PIPE_RX_SIZE_WRITE 0x56
  163. #define PKT_PIPE_MODE_WRITE 0x57
  164. /* HDLC packet status values - desc->status */
  165. #define ERR_SHUTDOWN 1 /* stop or shutdown occurrance */
  166. #define ERR_HDLC_ALIGN 2 /* HDLC alignment error */
  167. #define ERR_HDLC_FCS 3 /* HDLC Frame Check Sum error */
  168. #define ERR_RXFREE_Q_EMPTY 4 /* RX-free queue became empty while receiving
  169. this packet (if buf_len < pkt_len) */
  170. #define ERR_HDLC_TOO_LONG 5 /* HDLC frame size too long */
  171. #define ERR_HDLC_ABORT 6 /* abort sequence received */
  172. #define ERR_DISCONNECTING 7 /* disconnect is in progress */
  173. #ifdef __ARMEB__
  174. typedef struct sk_buff buffer_t;
  175. #define free_buffer dev_kfree_skb
  176. #define free_buffer_irq dev_kfree_skb_irq
  177. #else
  178. typedef void buffer_t;
  179. #define free_buffer kfree
  180. #define free_buffer_irq kfree
  181. #endif
  182. struct port {
  183. struct device *dev;
  184. struct npe *npe;
  185. struct net_device *netdev;
  186. struct napi_struct napi;
  187. struct hss_plat_info *plat;
  188. buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
  189. struct desc *desc_tab; /* coherent */
  190. u32 desc_tab_phys;
  191. unsigned int id;
  192. unsigned int clock_type, clock_rate, loopback;
  193. unsigned int initialized, carrier;
  194. u8 hdlc_cfg;
  195. };
  196. /* NPE message structure */
  197. struct msg {
  198. #ifdef __ARMEB__
  199. u8 cmd, unused, hss_port, index;
  200. union {
  201. struct { u8 data8a, data8b, data8c, data8d; };
  202. struct { u16 data16a, data16b; };
  203. struct { u32 data32; };
  204. };
  205. #else
  206. u8 index, hss_port, unused, cmd;
  207. union {
  208. struct { u8 data8d, data8c, data8b, data8a; };
  209. struct { u16 data16b, data16a; };
  210. struct { u32 data32; };
  211. };
  212. #endif
  213. };
  214. /* HDLC packet descriptor */
  215. struct desc {
  216. u32 next; /* pointer to next buffer, unused */
  217. #ifdef __ARMEB__
  218. u16 buf_len; /* buffer length */
  219. u16 pkt_len; /* packet length */
  220. u32 data; /* pointer to data buffer in RAM */
  221. u8 status;
  222. u8 error_count;
  223. u16 __reserved;
  224. #else
  225. u16 pkt_len; /* packet length */
  226. u16 buf_len; /* buffer length */
  227. u32 data; /* pointer to data buffer in RAM */
  228. u16 __reserved;
  229. u8 error_count;
  230. u8 status;
  231. #endif
  232. u32 __reserved1[4];
  233. };
  234. #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
  235. (n) * sizeof(struct desc))
  236. #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
  237. #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
  238. ((n) + RX_DESCS) * sizeof(struct desc))
  239. #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
  240. /*****************************************************************************
  241. * global variables
  242. ****************************************************************************/
  243. static int ports_open;
  244. static struct dma_pool *dma_pool;
  245. static spinlock_t npe_lock;
  246. static const struct {
  247. int tx, txdone, rx, rxfree;
  248. }queue_ids[2] = {{HSS0_PKT_TX0_QUEUE, HSS0_PKT_TXDONE_QUEUE, HSS0_PKT_RX_QUEUE,
  249. HSS0_PKT_RXFREE0_QUEUE},
  250. {HSS1_PKT_TX0_QUEUE, HSS1_PKT_TXDONE_QUEUE, HSS1_PKT_RX_QUEUE,
  251. HSS1_PKT_RXFREE0_QUEUE},
  252. };
  253. /*****************************************************************************
  254. * utility functions
  255. ****************************************************************************/
  256. static inline struct port* dev_to_port(struct net_device *dev)
  257. {
  258. return dev_to_hdlc(dev)->priv;
  259. }
  260. #ifndef __ARMEB__
  261. static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
  262. {
  263. int i;
  264. for (i = 0; i < cnt; i++)
  265. dest[i] = swab32(src[i]);
  266. }
  267. #endif
  268. /*****************************************************************************
  269. * HSS access
  270. ****************************************************************************/
  271. static void hss_npe_send(struct port *port, struct msg *msg, const char* what)
  272. {
  273. u32 *val = (u32*)msg;
  274. if (npe_send_message(port->npe, msg, what)) {
  275. printk(KERN_CRIT "HSS-%i: unable to send command [%08X:%08X]"
  276. " to %s\n", port->id, val[0], val[1],
  277. npe_name(port->npe));
  278. BUG();
  279. }
  280. }
  281. static void hss_config_set_lut(struct port *port)
  282. {
  283. struct msg msg;
  284. int ch;
  285. memset(&msg, 0, sizeof(msg));
  286. msg.cmd = PORT_CONFIG_WRITE;
  287. msg.hss_port = port->id;
  288. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  289. msg.data32 >>= 2;
  290. msg.data32 |= TDMMAP_HDLC << 30;
  291. if (ch % 16 == 15) {
  292. msg.index = HSS_CONFIG_TX_LUT + ((ch / 4) & ~3);
  293. hss_npe_send(port, &msg, "HSS_SET_TX_LUT");
  294. msg.index += HSS_CONFIG_RX_LUT - HSS_CONFIG_TX_LUT;
  295. hss_npe_send(port, &msg, "HSS_SET_RX_LUT");
  296. }
  297. }
  298. }
  299. static void hss_config(struct port *port)
  300. {
  301. struct msg msg;
  302. memset(&msg, 0, sizeof(msg));
  303. msg.cmd = PORT_CONFIG_WRITE;
  304. msg.hss_port = port->id;
  305. msg.index = HSS_CONFIG_TX_PCR;
  306. msg.data32 = PCR_FRM_SYNC_OUTPUT_RISING | PCR_MSB_ENDIAN |
  307. PCR_TX_DATA_ENABLE | PCR_SOF_NO_FBIT;
  308. if (port->clock_type == CLOCK_INT)
  309. msg.data32 |= PCR_SYNC_CLK_DIR_OUTPUT;
  310. hss_npe_send(port, &msg, "HSS_SET_TX_PCR");
  311. msg.index = HSS_CONFIG_RX_PCR;
  312. msg.data32 ^= PCR_TX_DATA_ENABLE | PCR_DCLK_EDGE_RISING;
  313. hss_npe_send(port, &msg, "HSS_SET_RX_PCR");
  314. memset(&msg, 0, sizeof(msg));
  315. msg.cmd = PORT_CONFIG_WRITE;
  316. msg.hss_port = port->id;
  317. msg.index = HSS_CONFIG_CORE_CR;
  318. msg.data32 = (port->loopback ? CCR_LOOPBACK : 0) |
  319. (port->id ? CCR_SECOND_HSS : 0);
  320. hss_npe_send(port, &msg, "HSS_SET_CORE_CR");
  321. memset(&msg, 0, sizeof(msg));
  322. msg.cmd = PORT_CONFIG_WRITE;
  323. msg.hss_port = port->id;
  324. msg.index = HSS_CONFIG_CLOCK_CR;
  325. msg.data32 = CLK42X_SPEED_2048KHZ /* FIXME */;
  326. hss_npe_send(port, &msg, "HSS_SET_CLOCK_CR");
  327. memset(&msg, 0, sizeof(msg));
  328. msg.cmd = PORT_CONFIG_WRITE;
  329. msg.hss_port = port->id;
  330. msg.index = HSS_CONFIG_TX_FCR;
  331. msg.data16a = FRAME_OFFSET;
  332. msg.data16b = FRAME_SIZE - 1;
  333. hss_npe_send(port, &msg, "HSS_SET_TX_FCR");
  334. memset(&msg, 0, sizeof(msg));
  335. msg.cmd = PORT_CONFIG_WRITE;
  336. msg.hss_port = port->id;
  337. msg.index = HSS_CONFIG_RX_FCR;
  338. msg.data16a = FRAME_OFFSET;
  339. msg.data16b = FRAME_SIZE - 1;
  340. hss_npe_send(port, &msg, "HSS_SET_RX_FCR");
  341. hss_config_set_lut(port);
  342. memset(&msg, 0, sizeof(msg));
  343. msg.cmd = PORT_CONFIG_LOAD;
  344. msg.hss_port = port->id;
  345. hss_npe_send(port, &msg, "HSS_LOAD_CONFIG");
  346. if (npe_recv_message(port->npe, &msg, "HSS_LOAD_CONFIG") ||
  347. /* HSS_LOAD_CONFIG for port #1 returns port_id = #4 */
  348. msg.cmd != PORT_CONFIG_LOAD || msg.data32) {
  349. printk(KERN_CRIT "HSS-%i: HSS_LOAD_CONFIG failed\n",
  350. port->id);
  351. BUG();
  352. }
  353. /* HDLC may stop working without this - check FIXME */
  354. npe_recv_message(port->npe, &msg, "FLUSH_IT");
  355. }
  356. static void hss_set_hdlc_cfg(struct port *port)
  357. {
  358. struct msg msg;
  359. memset(&msg, 0, sizeof(msg));
  360. msg.cmd = PKT_PIPE_HDLC_CFG_WRITE;
  361. msg.hss_port = port->id;
  362. msg.data8a = port->hdlc_cfg; /* rx_cfg */
  363. msg.data8b = port->hdlc_cfg | (PKT_EXTRA_FLAGS << 3); /* tx_cfg */
  364. hss_npe_send(port, &msg, "HSS_SET_HDLC_CFG");
  365. }
  366. static u32 hss_get_status(struct port *port)
  367. {
  368. struct msg msg;
  369. memset(&msg, 0, sizeof(msg));
  370. msg.cmd = PORT_ERROR_READ;
  371. msg.hss_port = port->id;
  372. hss_npe_send(port, &msg, "PORT_ERROR_READ");
  373. if (npe_recv_message(port->npe, &msg, "PORT_ERROR_READ")) {
  374. printk(KERN_CRIT "HSS-%i: unable to read HSS status\n",
  375. port->id);
  376. BUG();
  377. }
  378. return msg.data32;
  379. }
  380. static void hss_start_hdlc(struct port *port)
  381. {
  382. struct msg msg;
  383. memset(&msg, 0, sizeof(msg));
  384. msg.cmd = PKT_PIPE_FLOW_ENABLE;
  385. msg.hss_port = port->id;
  386. msg.data32 = 0;
  387. hss_npe_send(port, &msg, "HSS_ENABLE_PKT_PIPE");
  388. }
  389. static void hss_stop_hdlc(struct port *port)
  390. {
  391. struct msg msg;
  392. memset(&msg, 0, sizeof(msg));
  393. msg.cmd = PKT_PIPE_FLOW_DISABLE;
  394. msg.hss_port = port->id;
  395. hss_npe_send(port, &msg, "HSS_DISABLE_PKT_PIPE");
  396. hss_get_status(port); /* make sure it's halted */
  397. }
  398. static int hss_load_firmware(struct port *port)
  399. {
  400. struct msg msg;
  401. int err;
  402. if (port->initialized)
  403. return 0;
  404. if (!npe_running(port->npe) &&
  405. (err = npe_load_firmware(port->npe, npe_name(port->npe),
  406. port->dev)))
  407. return err;
  408. /* HDLC mode configuration */
  409. memset(&msg, 0, sizeof(msg));
  410. msg.cmd = PKT_NUM_PIPES_WRITE;
  411. msg.hss_port = port->id;
  412. msg.data8a = PKT_NUM_PIPES;
  413. hss_npe_send(port, &msg, "HSS_SET_PKT_PIPES");
  414. msg.cmd = PKT_PIPE_FIFO_SIZEW_WRITE;
  415. msg.data8a = PKT_PIPE_FIFO_SIZEW;
  416. hss_npe_send(port, &msg, "HSS_SET_PKT_FIFO");
  417. msg.cmd = PKT_PIPE_MODE_WRITE;
  418. msg.data8a = NPE_PKT_MODE_HDLC;
  419. /* msg.data8b = inv_mask */
  420. /* msg.data8c = or_mask */
  421. hss_npe_send(port, &msg, "HSS_SET_PKT_MODE");
  422. msg.cmd = PKT_PIPE_RX_SIZE_WRITE;
  423. msg.data16a = HDLC_MAX_MRU; /* including CRC */
  424. hss_npe_send(port, &msg, "HSS_SET_PKT_RX_SIZE");
  425. msg.cmd = PKT_PIPE_IDLE_PATTERN_WRITE;
  426. msg.data32 = 0x7F7F7F7F; /* ??? FIXME */
  427. hss_npe_send(port, &msg, "HSS_SET_PKT_IDLE");
  428. port->initialized = 1;
  429. return 0;
  430. }
  431. /*****************************************************************************
  432. * packetized (HDLC) operation
  433. ****************************************************************************/
  434. static inline void debug_pkt(struct net_device *dev, const char *func,
  435. u8 *data, int len)
  436. {
  437. #if DEBUG_PKT_BYTES
  438. int i;
  439. printk(KERN_DEBUG "%s: %s(%i)", dev->name, func, len);
  440. for (i = 0; i < len; i++) {
  441. if (i >= DEBUG_PKT_BYTES)
  442. break;
  443. printk("%s%02X", !(i % 4) ? " " : "", data[i]);
  444. }
  445. printk("\n");
  446. #endif
  447. }
  448. static inline void debug_desc(u32 phys, struct desc *desc)
  449. {
  450. #if DEBUG_DESC
  451. printk(KERN_DEBUG "%X: %X %3X %3X %08X %X %X\n",
  452. phys, desc->next, desc->buf_len, desc->pkt_len,
  453. desc->data, desc->status, desc->error_count);
  454. #endif
  455. }
  456. static inline int queue_get_desc(unsigned int queue, struct port *port,
  457. int is_tx)
  458. {
  459. u32 phys, tab_phys, n_desc;
  460. struct desc *tab;
  461. if (!(phys = qmgr_get_entry(queue)))
  462. return -1;
  463. BUG_ON(phys & 0x1F);
  464. tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
  465. tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
  466. n_desc = (phys - tab_phys) / sizeof(struct desc);
  467. BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
  468. debug_desc(phys, &tab[n_desc]);
  469. BUG_ON(tab[n_desc].next);
  470. return n_desc;
  471. }
  472. static inline void queue_put_desc(unsigned int queue, u32 phys,
  473. struct desc *desc)
  474. {
  475. debug_desc(phys, desc);
  476. BUG_ON(phys & 0x1F);
  477. qmgr_put_entry(queue, phys);
  478. BUG_ON(qmgr_stat_overflow(queue));
  479. }
  480. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  481. {
  482. #ifdef __ARMEB__
  483. dma_unmap_single(&port->netdev->dev, desc->data,
  484. desc->buf_len, DMA_TO_DEVICE);
  485. #else
  486. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  487. ALIGN((desc->data & 3) + desc->buf_len, 4),
  488. DMA_TO_DEVICE);
  489. #endif
  490. }
  491. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  492. {
  493. struct net_device *netdev = pdev;
  494. struct port *port = dev_to_port(netdev);
  495. unsigned long flags;
  496. spin_lock_irqsave(&npe_lock, flags);
  497. port->carrier = carrier;
  498. if (!port->loopback) {
  499. if (carrier)
  500. netif_carrier_on(netdev);
  501. else
  502. netif_carrier_off(netdev);
  503. }
  504. spin_unlock_irqrestore(&npe_lock, flags);
  505. }
  506. static void hss_hdlc_rx_irq(void *pdev)
  507. {
  508. struct net_device *dev = pdev;
  509. struct port *port = dev_to_port(dev);
  510. #if DEBUG_RX
  511. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  512. #endif
  513. qmgr_disable_irq(queue_ids[port->id].rx);
  514. netif_rx_schedule(dev, &port->napi);
  515. }
  516. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  517. {
  518. struct port *port = container_of(napi, struct port, napi);
  519. struct net_device *dev = port->netdev;
  520. unsigned int rxq = queue_ids[port->id].rx;
  521. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  522. int received = 0;
  523. #if DEBUG_RX
  524. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  525. #endif
  526. while (received < budget) {
  527. struct sk_buff *skb;
  528. struct desc *desc;
  529. int n;
  530. #ifdef __ARMEB__
  531. struct sk_buff *temp;
  532. u32 phys;
  533. #endif
  534. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  535. #if DEBUG_RX
  536. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  537. " netif_rx_complete\n", dev->name);
  538. #endif
  539. netif_rx_complete(dev, napi);
  540. qmgr_enable_irq(rxq);
  541. if (!qmgr_stat_empty(rxq) &&
  542. netif_rx_reschedule(napi)) {
  543. #if DEBUG_RX
  544. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  545. " netif_rx_reschedule succeeded\n",
  546. dev->name);
  547. #endif
  548. qmgr_disable_irq(rxq);
  549. continue;
  550. }
  551. #if DEBUG_RX
  552. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  553. dev->name);
  554. #endif
  555. return received; /* all work done */
  556. }
  557. desc = rx_desc_ptr(port, n);
  558. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  559. if (desc->error_count)
  560. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  561. " errors %u\n", dev->name, desc->status,
  562. desc->error_count);
  563. #endif
  564. skb = NULL;
  565. switch (desc->status) {
  566. case 0:
  567. #ifdef __ARMEB__
  568. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  569. phys = dma_map_single(&dev->dev, skb->data,
  570. RX_SIZE,
  571. DMA_FROM_DEVICE);
  572. if (dma_mapping_error(&dev->dev, phys)) {
  573. dev_kfree_skb(skb);
  574. skb = NULL;
  575. }
  576. }
  577. #else
  578. skb = netdev_alloc_skb(dev, desc->pkt_len);
  579. #endif
  580. if (!skb)
  581. dev->stats.rx_dropped++;
  582. break;
  583. case ERR_HDLC_ALIGN:
  584. case ERR_HDLC_ABORT:
  585. dev->stats.rx_frame_errors++;
  586. dev->stats.rx_errors++;
  587. break;
  588. case ERR_HDLC_FCS:
  589. dev->stats.rx_crc_errors++;
  590. dev->stats.rx_errors++;
  591. break;
  592. case ERR_HDLC_TOO_LONG:
  593. dev->stats.rx_length_errors++;
  594. dev->stats.rx_errors++;
  595. break;
  596. default: /* FIXME - remove printk */
  597. printk(KERN_ERR "%s: hss_hdlc_poll: status 0x%02X"
  598. " errors %u\n", dev->name, desc->status,
  599. desc->error_count);
  600. dev->stats.rx_errors++;
  601. }
  602. if (!skb) {
  603. /* put the desc back on RX-ready queue */
  604. desc->buf_len = RX_SIZE;
  605. desc->pkt_len = desc->status = 0;
  606. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  607. continue;
  608. }
  609. /* process received frame */
  610. #ifdef __ARMEB__
  611. temp = skb;
  612. skb = port->rx_buff_tab[n];
  613. dma_unmap_single(&dev->dev, desc->data,
  614. RX_SIZE, DMA_FROM_DEVICE);
  615. #else
  616. dma_sync_single(&dev->dev, desc->data,
  617. RX_SIZE, DMA_FROM_DEVICE);
  618. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  619. ALIGN(desc->pkt_len, 4) / 4);
  620. #endif
  621. skb_put(skb, desc->pkt_len);
  622. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  623. skb->protocol = hdlc_type_trans(skb, dev);
  624. dev->stats.rx_packets++;
  625. dev->stats.rx_bytes += skb->len;
  626. netif_receive_skb(skb);
  627. /* put the new buffer on RX-free queue */
  628. #ifdef __ARMEB__
  629. port->rx_buff_tab[n] = temp;
  630. desc->data = phys;
  631. #endif
  632. desc->buf_len = RX_SIZE;
  633. desc->pkt_len = 0;
  634. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  635. received++;
  636. }
  637. #if DEBUG_RX
  638. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  639. #endif
  640. return received; /* not all work done */
  641. }
  642. static void hss_hdlc_txdone_irq(void *pdev)
  643. {
  644. struct net_device *dev = pdev;
  645. struct port *port = dev_to_port(dev);
  646. int n_desc;
  647. #if DEBUG_TX
  648. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  649. #endif
  650. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  651. port, 1)) >= 0) {
  652. struct desc *desc;
  653. int start;
  654. desc = tx_desc_ptr(port, n_desc);
  655. dev->stats.tx_packets++;
  656. dev->stats.tx_bytes += desc->pkt_len;
  657. dma_unmap_tx(port, desc);
  658. #if DEBUG_TX
  659. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  660. dev->name, port->tx_buff_tab[n_desc]);
  661. #endif
  662. free_buffer_irq(port->tx_buff_tab[n_desc]);
  663. port->tx_buff_tab[n_desc] = NULL;
  664. start = qmgr_stat_empty(port->plat->txreadyq);
  665. queue_put_desc(port->plat->txreadyq,
  666. tx_desc_phys(port, n_desc), desc);
  667. if (start) {
  668. #if DEBUG_TX
  669. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  670. " ready\n", dev->name);
  671. #endif
  672. netif_wake_queue(dev);
  673. }
  674. }
  675. }
  676. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  677. {
  678. struct port *port = dev_to_port(dev);
  679. unsigned int txreadyq = port->plat->txreadyq;
  680. int len, offset, bytes, n;
  681. void *mem;
  682. u32 phys;
  683. struct desc *desc;
  684. #if DEBUG_TX
  685. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  686. #endif
  687. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  688. dev_kfree_skb(skb);
  689. dev->stats.tx_errors++;
  690. return NETDEV_TX_OK;
  691. }
  692. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  693. len = skb->len;
  694. #ifdef __ARMEB__
  695. offset = 0; /* no need to keep alignment */
  696. bytes = len;
  697. mem = skb->data;
  698. #else
  699. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  700. bytes = ALIGN(offset + len, 4);
  701. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  702. dev_kfree_skb(skb);
  703. dev->stats.tx_dropped++;
  704. return NETDEV_TX_OK;
  705. }
  706. memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
  707. dev_kfree_skb(skb);
  708. #endif
  709. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  710. if (dma_mapping_error(&dev->dev, phys)) {
  711. #ifdef __ARMEB__
  712. dev_kfree_skb(skb);
  713. #else
  714. kfree(mem);
  715. #endif
  716. dev->stats.tx_dropped++;
  717. return NETDEV_TX_OK;
  718. }
  719. n = queue_get_desc(txreadyq, port, 1);
  720. BUG_ON(n < 0);
  721. desc = tx_desc_ptr(port, n);
  722. #ifdef __ARMEB__
  723. port->tx_buff_tab[n] = skb;
  724. #else
  725. port->tx_buff_tab[n] = mem;
  726. #endif
  727. desc->data = phys + offset;
  728. desc->buf_len = desc->pkt_len = len;
  729. wmb();
  730. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  731. dev->trans_start = jiffies;
  732. if (qmgr_stat_empty(txreadyq)) {
  733. #if DEBUG_TX
  734. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  735. #endif
  736. netif_stop_queue(dev);
  737. /* we could miss TX ready interrupt */
  738. if (!qmgr_stat_empty(txreadyq)) {
  739. #if DEBUG_TX
  740. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  741. dev->name);
  742. #endif
  743. netif_wake_queue(dev);
  744. }
  745. }
  746. #if DEBUG_TX
  747. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  748. #endif
  749. return NETDEV_TX_OK;
  750. }
  751. static int request_hdlc_queues(struct port *port)
  752. {
  753. int err;
  754. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  755. "%s:RX-free", port->netdev->name);
  756. if (err)
  757. return err;
  758. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  759. "%s:RX", port->netdev->name);
  760. if (err)
  761. goto rel_rxfree;
  762. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  763. "%s:TX", port->netdev->name);
  764. if (err)
  765. goto rel_rx;
  766. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  767. "%s:TX-ready", port->netdev->name);
  768. if (err)
  769. goto rel_tx;
  770. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  771. "%s:TX-done", port->netdev->name);
  772. if (err)
  773. goto rel_txready;
  774. return 0;
  775. rel_txready:
  776. qmgr_release_queue(port->plat->txreadyq);
  777. rel_tx:
  778. qmgr_release_queue(queue_ids[port->id].tx);
  779. rel_rx:
  780. qmgr_release_queue(queue_ids[port->id].rx);
  781. rel_rxfree:
  782. qmgr_release_queue(queue_ids[port->id].rxfree);
  783. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  784. port->netdev->name);
  785. return err;
  786. }
  787. static void release_hdlc_queues(struct port *port)
  788. {
  789. qmgr_release_queue(queue_ids[port->id].rxfree);
  790. qmgr_release_queue(queue_ids[port->id].rx);
  791. qmgr_release_queue(queue_ids[port->id].txdone);
  792. qmgr_release_queue(queue_ids[port->id].tx);
  793. qmgr_release_queue(port->plat->txreadyq);
  794. }
  795. static int init_hdlc_queues(struct port *port)
  796. {
  797. int i;
  798. if (!ports_open)
  799. if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
  800. POOL_ALLOC_SIZE, 32, 0)))
  801. return -ENOMEM;
  802. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  803. &port->desc_tab_phys)))
  804. return -ENOMEM;
  805. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  806. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  807. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  808. /* Setup RX buffers */
  809. for (i = 0; i < RX_DESCS; i++) {
  810. struct desc *desc = rx_desc_ptr(port, i);
  811. buffer_t *buff;
  812. void *data;
  813. #ifdef __ARMEB__
  814. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  815. return -ENOMEM;
  816. data = buff->data;
  817. #else
  818. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  819. return -ENOMEM;
  820. data = buff;
  821. #endif
  822. desc->buf_len = RX_SIZE;
  823. desc->data = dma_map_single(&port->netdev->dev, data,
  824. RX_SIZE, DMA_FROM_DEVICE);
  825. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  826. free_buffer(buff);
  827. return -EIO;
  828. }
  829. port->rx_buff_tab[i] = buff;
  830. }
  831. return 0;
  832. }
  833. static void destroy_hdlc_queues(struct port *port)
  834. {
  835. int i;
  836. if (port->desc_tab) {
  837. for (i = 0; i < RX_DESCS; i++) {
  838. struct desc *desc = rx_desc_ptr(port, i);
  839. buffer_t *buff = port->rx_buff_tab[i];
  840. if (buff) {
  841. dma_unmap_single(&port->netdev->dev,
  842. desc->data, RX_SIZE,
  843. DMA_FROM_DEVICE);
  844. free_buffer(buff);
  845. }
  846. }
  847. for (i = 0; i < TX_DESCS; i++) {
  848. struct desc *desc = tx_desc_ptr(port, i);
  849. buffer_t *buff = port->tx_buff_tab[i];
  850. if (buff) {
  851. dma_unmap_tx(port, desc);
  852. free_buffer(buff);
  853. }
  854. }
  855. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  856. port->desc_tab = NULL;
  857. }
  858. if (!ports_open && dma_pool) {
  859. dma_pool_destroy(dma_pool);
  860. dma_pool = NULL;
  861. }
  862. }
  863. static int hss_hdlc_open(struct net_device *dev)
  864. {
  865. struct port *port = dev_to_port(dev);
  866. unsigned long flags;
  867. int i, err = 0;
  868. if ((err = hdlc_open(dev)))
  869. return err;
  870. if ((err = hss_load_firmware(port)))
  871. goto err_hdlc_close;
  872. if ((err = request_hdlc_queues(port)))
  873. goto err_hdlc_close;
  874. if ((err = init_hdlc_queues(port)))
  875. goto err_destroy_queues;
  876. spin_lock_irqsave(&npe_lock, flags);
  877. if (port->plat->open)
  878. if ((err = port->plat->open(port->id, dev,
  879. hss_hdlc_set_carrier)))
  880. goto err_unlock;
  881. spin_unlock_irqrestore(&npe_lock, flags);
  882. /* Populate queues with buffers, no failure after this point */
  883. for (i = 0; i < TX_DESCS; i++)
  884. queue_put_desc(port->plat->txreadyq,
  885. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  886. for (i = 0; i < RX_DESCS; i++)
  887. queue_put_desc(queue_ids[port->id].rxfree,
  888. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  889. napi_enable(&port->napi);
  890. netif_start_queue(dev);
  891. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  892. hss_hdlc_rx_irq, dev);
  893. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  894. hss_hdlc_txdone_irq, dev);
  895. qmgr_enable_irq(queue_ids[port->id].txdone);
  896. ports_open++;
  897. hss_set_hdlc_cfg(port);
  898. hss_config(port);
  899. hss_start_hdlc(port);
  900. /* we may already have RX data, enables IRQ */
  901. netif_rx_schedule(dev, &port->napi);
  902. return 0;
  903. err_unlock:
  904. spin_unlock_irqrestore(&npe_lock, flags);
  905. err_destroy_queues:
  906. destroy_hdlc_queues(port);
  907. release_hdlc_queues(port);
  908. err_hdlc_close:
  909. hdlc_close(dev);
  910. return err;
  911. }
  912. static int hss_hdlc_close(struct net_device *dev)
  913. {
  914. struct port *port = dev_to_port(dev);
  915. unsigned long flags;
  916. int i, buffs = RX_DESCS; /* allocated RX buffers */
  917. spin_lock_irqsave(&npe_lock, flags);
  918. ports_open--;
  919. qmgr_disable_irq(queue_ids[port->id].rx);
  920. netif_stop_queue(dev);
  921. napi_disable(&port->napi);
  922. hss_stop_hdlc(port);
  923. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  924. buffs--;
  925. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  926. buffs--;
  927. if (buffs)
  928. printk(KERN_CRIT "%s: unable to drain RX queue, %i buffer(s)"
  929. " left in NPE\n", dev->name, buffs);
  930. buffs = TX_DESCS;
  931. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  932. buffs--; /* cancel TX */
  933. i = 0;
  934. do {
  935. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  936. buffs--;
  937. if (!buffs)
  938. break;
  939. } while (++i < MAX_CLOSE_WAIT);
  940. if (buffs)
  941. printk(KERN_CRIT "%s: unable to drain TX queue, %i buffer(s) "
  942. "left in NPE\n", dev->name, buffs);
  943. #if DEBUG_CLOSE
  944. if (!buffs)
  945. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  946. #endif
  947. qmgr_disable_irq(queue_ids[port->id].txdone);
  948. if (port->plat->close)
  949. port->plat->close(port->id, dev);
  950. spin_unlock_irqrestore(&npe_lock, flags);
  951. destroy_hdlc_queues(port);
  952. release_hdlc_queues(port);
  953. hdlc_close(dev);
  954. return 0;
  955. }
  956. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  957. unsigned short parity)
  958. {
  959. struct port *port = dev_to_port(dev);
  960. if (encoding != ENCODING_NRZ)
  961. return -EINVAL;
  962. switch(parity) {
  963. case PARITY_CRC16_PR1_CCITT:
  964. port->hdlc_cfg = 0;
  965. return 0;
  966. case PARITY_CRC32_PR1_CCITT:
  967. port->hdlc_cfg = PKT_HDLC_CRC_32;
  968. return 0;
  969. default:
  970. return -EINVAL;
  971. }
  972. }
  973. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  974. {
  975. const size_t size = sizeof(sync_serial_settings);
  976. sync_serial_settings new_line;
  977. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  978. struct port *port = dev_to_port(dev);
  979. unsigned long flags;
  980. int clk;
  981. if (cmd != SIOCWANDEV)
  982. return hdlc_ioctl(dev, ifr, cmd);
  983. switch(ifr->ifr_settings.type) {
  984. case IF_GET_IFACE:
  985. ifr->ifr_settings.type = IF_IFACE_V35;
  986. if (ifr->ifr_settings.size < size) {
  987. ifr->ifr_settings.size = size; /* data size wanted */
  988. return -ENOBUFS;
  989. }
  990. memset(&new_line, 0, sizeof(new_line));
  991. new_line.clock_type = port->clock_type;
  992. new_line.clock_rate = 2048000; /* FIXME */
  993. new_line.loopback = port->loopback;
  994. if (copy_to_user(line, &new_line, size))
  995. return -EFAULT;
  996. return 0;
  997. case IF_IFACE_SYNC_SERIAL:
  998. case IF_IFACE_V35:
  999. if(!capable(CAP_NET_ADMIN))
  1000. return -EPERM;
  1001. if (copy_from_user(&new_line, line, size))
  1002. return -EFAULT;
  1003. clk = new_line.clock_type;
  1004. if (port->plat->set_clock)
  1005. clk = port->plat->set_clock(port->id, clk);
  1006. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1007. return -EINVAL; /* No such clock setting */
  1008. if (new_line.loopback != 0 && new_line.loopback != 1)
  1009. return -EINVAL;
  1010. port->clock_type = clk; /* Update settings */
  1011. /* FIXME port->clock_rate = new_line.clock_rate */;
  1012. port->loopback = new_line.loopback;
  1013. spin_lock_irqsave(&npe_lock, flags);
  1014. if (dev->flags & IFF_UP)
  1015. hss_config(port);
  1016. if (port->loopback || port->carrier)
  1017. netif_carrier_on(port->netdev);
  1018. else
  1019. netif_carrier_off(port->netdev);
  1020. spin_unlock_irqrestore(&npe_lock, flags);
  1021. return 0;
  1022. default:
  1023. return hdlc_ioctl(dev, ifr, cmd);
  1024. }
  1025. }
  1026. /*****************************************************************************
  1027. * initialization
  1028. ****************************************************************************/
  1029. static int __devinit hss_init_one(struct platform_device *pdev)
  1030. {
  1031. struct port *port;
  1032. struct net_device *dev;
  1033. hdlc_device *hdlc;
  1034. int err;
  1035. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1036. return -ENOMEM;
  1037. if ((port->npe = npe_request(0)) == NULL) {
  1038. err = -ENOSYS;
  1039. goto err_free;
  1040. }
  1041. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1042. err = -ENOMEM;
  1043. goto err_plat;
  1044. }
  1045. SET_NETDEV_DEV(dev, &pdev->dev);
  1046. hdlc = dev_to_hdlc(dev);
  1047. hdlc->attach = hss_hdlc_attach;
  1048. hdlc->xmit = hss_hdlc_xmit;
  1049. dev->open = hss_hdlc_open;
  1050. dev->stop = hss_hdlc_close;
  1051. dev->do_ioctl = hss_hdlc_ioctl;
  1052. dev->tx_queue_len = 100;
  1053. port->clock_type = CLOCK_EXT;
  1054. port->clock_rate = 2048000;
  1055. port->id = pdev->id;
  1056. port->dev = &pdev->dev;
  1057. port->plat = pdev->dev.platform_data;
  1058. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1059. if ((err = register_hdlc_device(dev)))
  1060. goto err_free_netdev;
  1061. platform_set_drvdata(pdev, port);
  1062. printk(KERN_INFO "%s: HSS-%i\n", dev->name, port->id);
  1063. return 0;
  1064. err_free_netdev:
  1065. free_netdev(dev);
  1066. err_plat:
  1067. npe_release(port->npe);
  1068. err_free:
  1069. kfree(port);
  1070. return err;
  1071. }
  1072. static int __devexit hss_remove_one(struct platform_device *pdev)
  1073. {
  1074. struct port *port = platform_get_drvdata(pdev);
  1075. unregister_hdlc_device(port->netdev);
  1076. free_netdev(port->netdev);
  1077. npe_release(port->npe);
  1078. platform_set_drvdata(pdev, NULL);
  1079. kfree(port);
  1080. return 0;
  1081. }
  1082. static struct platform_driver ixp4xx_hss_driver = {
  1083. .driver.name = DRV_NAME,
  1084. .probe = hss_init_one,
  1085. .remove = hss_remove_one,
  1086. };
  1087. static int __init hss_init_module(void)
  1088. {
  1089. if ((ixp4xx_read_feature_bits() &
  1090. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1091. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1092. return -ENOSYS;
  1093. spin_lock_init(&npe_lock);
  1094. return platform_driver_register(&ixp4xx_hss_driver);
  1095. }
  1096. static void __exit hss_cleanup_module(void)
  1097. {
  1098. platform_driver_unregister(&ixp4xx_hss_driver);
  1099. }
  1100. MODULE_AUTHOR("Krzysztof Halasa");
  1101. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1102. MODULE_LICENSE("GPL v2");
  1103. MODULE_ALIAS("platform:ixp4xx_hss");
  1104. module_init(hss_init_module);
  1105. module_exit(hss_cleanup_module);