ixp4xx_hss.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. /* Don't check for queue overflow here, we've allocated sufficient
  479. length and queues >= 32 don't support this check anyway. */
  480. }
  481. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  482. {
  483. #ifdef __ARMEB__
  484. dma_unmap_single(&port->netdev->dev, desc->data,
  485. desc->buf_len, DMA_TO_DEVICE);
  486. #else
  487. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  488. ALIGN((desc->data & 3) + desc->buf_len, 4),
  489. DMA_TO_DEVICE);
  490. #endif
  491. }
  492. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  493. {
  494. struct net_device *netdev = pdev;
  495. struct port *port = dev_to_port(netdev);
  496. unsigned long flags;
  497. spin_lock_irqsave(&npe_lock, flags);
  498. port->carrier = carrier;
  499. if (!port->loopback) {
  500. if (carrier)
  501. netif_carrier_on(netdev);
  502. else
  503. netif_carrier_off(netdev);
  504. }
  505. spin_unlock_irqrestore(&npe_lock, flags);
  506. }
  507. static void hss_hdlc_rx_irq(void *pdev)
  508. {
  509. struct net_device *dev = pdev;
  510. struct port *port = dev_to_port(dev);
  511. #if DEBUG_RX
  512. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  513. #endif
  514. qmgr_disable_irq(queue_ids[port->id].rx);
  515. napi_schedule(&port->napi);
  516. }
  517. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  518. {
  519. struct port *port = container_of(napi, struct port, napi);
  520. struct net_device *dev = port->netdev;
  521. unsigned int rxq = queue_ids[port->id].rx;
  522. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  523. int received = 0;
  524. #if DEBUG_RX
  525. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  526. #endif
  527. while (received < budget) {
  528. struct sk_buff *skb;
  529. struct desc *desc;
  530. int n;
  531. #ifdef __ARMEB__
  532. struct sk_buff *temp;
  533. u32 phys;
  534. #endif
  535. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  536. #if DEBUG_RX
  537. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  538. " napi_complete\n", dev->name);
  539. #endif
  540. napi_complete(napi);
  541. qmgr_enable_irq(rxq);
  542. if (!qmgr_stat_empty(rxq) &&
  543. napi_reschedule(napi)) {
  544. #if DEBUG_RX
  545. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  546. " napi_reschedule succeeded\n",
  547. dev->name);
  548. #endif
  549. qmgr_disable_irq(rxq);
  550. continue;
  551. }
  552. #if DEBUG_RX
  553. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  554. dev->name);
  555. #endif
  556. return received; /* all work done */
  557. }
  558. desc = rx_desc_ptr(port, n);
  559. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  560. if (desc->error_count)
  561. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  562. " errors %u\n", dev->name, desc->status,
  563. desc->error_count);
  564. #endif
  565. skb = NULL;
  566. switch (desc->status) {
  567. case 0:
  568. #ifdef __ARMEB__
  569. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  570. phys = dma_map_single(&dev->dev, skb->data,
  571. RX_SIZE,
  572. DMA_FROM_DEVICE);
  573. if (dma_mapping_error(&dev->dev, phys)) {
  574. dev_kfree_skb(skb);
  575. skb = NULL;
  576. }
  577. }
  578. #else
  579. skb = netdev_alloc_skb(dev, desc->pkt_len);
  580. #endif
  581. if (!skb)
  582. dev->stats.rx_dropped++;
  583. break;
  584. case ERR_HDLC_ALIGN:
  585. case ERR_HDLC_ABORT:
  586. dev->stats.rx_frame_errors++;
  587. dev->stats.rx_errors++;
  588. break;
  589. case ERR_HDLC_FCS:
  590. dev->stats.rx_crc_errors++;
  591. dev->stats.rx_errors++;
  592. break;
  593. case ERR_HDLC_TOO_LONG:
  594. dev->stats.rx_length_errors++;
  595. dev->stats.rx_errors++;
  596. break;
  597. default: /* FIXME - remove printk */
  598. printk(KERN_ERR "%s: hss_hdlc_poll: status 0x%02X"
  599. " errors %u\n", dev->name, desc->status,
  600. desc->error_count);
  601. dev->stats.rx_errors++;
  602. }
  603. if (!skb) {
  604. /* put the desc back on RX-ready queue */
  605. desc->buf_len = RX_SIZE;
  606. desc->pkt_len = desc->status = 0;
  607. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  608. continue;
  609. }
  610. /* process received frame */
  611. #ifdef __ARMEB__
  612. temp = skb;
  613. skb = port->rx_buff_tab[n];
  614. dma_unmap_single(&dev->dev, desc->data,
  615. RX_SIZE, DMA_FROM_DEVICE);
  616. #else
  617. dma_sync_single_for_cpu(&dev->dev, desc->data,
  618. RX_SIZE, DMA_FROM_DEVICE);
  619. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  620. ALIGN(desc->pkt_len, 4) / 4);
  621. #endif
  622. skb_put(skb, desc->pkt_len);
  623. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  624. skb->protocol = hdlc_type_trans(skb, dev);
  625. dev->stats.rx_packets++;
  626. dev->stats.rx_bytes += skb->len;
  627. netif_receive_skb(skb);
  628. /* put the new buffer on RX-free queue */
  629. #ifdef __ARMEB__
  630. port->rx_buff_tab[n] = temp;
  631. desc->data = phys;
  632. #endif
  633. desc->buf_len = RX_SIZE;
  634. desc->pkt_len = 0;
  635. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  636. received++;
  637. }
  638. #if DEBUG_RX
  639. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  640. #endif
  641. return received; /* not all work done */
  642. }
  643. static void hss_hdlc_txdone_irq(void *pdev)
  644. {
  645. struct net_device *dev = pdev;
  646. struct port *port = dev_to_port(dev);
  647. int n_desc;
  648. #if DEBUG_TX
  649. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  650. #endif
  651. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  652. port, 1)) >= 0) {
  653. struct desc *desc;
  654. int start;
  655. desc = tx_desc_ptr(port, n_desc);
  656. dev->stats.tx_packets++;
  657. dev->stats.tx_bytes += desc->pkt_len;
  658. dma_unmap_tx(port, desc);
  659. #if DEBUG_TX
  660. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  661. dev->name, port->tx_buff_tab[n_desc]);
  662. #endif
  663. free_buffer_irq(port->tx_buff_tab[n_desc]);
  664. port->tx_buff_tab[n_desc] = NULL;
  665. start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
  666. queue_put_desc(port->plat->txreadyq,
  667. tx_desc_phys(port, n_desc), desc);
  668. if (start) { /* TX-ready queue was empty */
  669. #if DEBUG_TX
  670. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  671. " ready\n", dev->name);
  672. #endif
  673. netif_wake_queue(dev);
  674. }
  675. }
  676. }
  677. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  678. {
  679. struct port *port = dev_to_port(dev);
  680. unsigned int txreadyq = port->plat->txreadyq;
  681. int len, offset, bytes, n;
  682. void *mem;
  683. u32 phys;
  684. struct desc *desc;
  685. #if DEBUG_TX
  686. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  687. #endif
  688. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  689. dev_kfree_skb(skb);
  690. dev->stats.tx_errors++;
  691. return NETDEV_TX_OK;
  692. }
  693. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  694. len = skb->len;
  695. #ifdef __ARMEB__
  696. offset = 0; /* no need to keep alignment */
  697. bytes = len;
  698. mem = skb->data;
  699. #else
  700. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  701. bytes = ALIGN(offset + len, 4);
  702. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  703. dev_kfree_skb(skb);
  704. dev->stats.tx_dropped++;
  705. return NETDEV_TX_OK;
  706. }
  707. memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
  708. dev_kfree_skb(skb);
  709. #endif
  710. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  711. if (dma_mapping_error(&dev->dev, phys)) {
  712. #ifdef __ARMEB__
  713. dev_kfree_skb(skb);
  714. #else
  715. kfree(mem);
  716. #endif
  717. dev->stats.tx_dropped++;
  718. return NETDEV_TX_OK;
  719. }
  720. n = queue_get_desc(txreadyq, port, 1);
  721. BUG_ON(n < 0);
  722. desc = tx_desc_ptr(port, n);
  723. #ifdef __ARMEB__
  724. port->tx_buff_tab[n] = skb;
  725. #else
  726. port->tx_buff_tab[n] = mem;
  727. #endif
  728. desc->data = phys + offset;
  729. desc->buf_len = desc->pkt_len = len;
  730. wmb();
  731. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  732. dev->trans_start = jiffies;
  733. if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
  734. #if DEBUG_TX
  735. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  736. #endif
  737. netif_stop_queue(dev);
  738. /* we could miss TX ready interrupt */
  739. if (!qmgr_stat_below_low_watermark(txreadyq)) {
  740. #if DEBUG_TX
  741. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  742. dev->name);
  743. #endif
  744. netif_wake_queue(dev);
  745. }
  746. }
  747. #if DEBUG_TX
  748. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  749. #endif
  750. return NETDEV_TX_OK;
  751. }
  752. static int request_hdlc_queues(struct port *port)
  753. {
  754. int err;
  755. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  756. "%s:RX-free", port->netdev->name);
  757. if (err)
  758. return err;
  759. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  760. "%s:RX", port->netdev->name);
  761. if (err)
  762. goto rel_rxfree;
  763. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  764. "%s:TX", port->netdev->name);
  765. if (err)
  766. goto rel_rx;
  767. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  768. "%s:TX-ready", port->netdev->name);
  769. if (err)
  770. goto rel_tx;
  771. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  772. "%s:TX-done", port->netdev->name);
  773. if (err)
  774. goto rel_txready;
  775. return 0;
  776. rel_txready:
  777. qmgr_release_queue(port->plat->txreadyq);
  778. rel_tx:
  779. qmgr_release_queue(queue_ids[port->id].tx);
  780. rel_rx:
  781. qmgr_release_queue(queue_ids[port->id].rx);
  782. rel_rxfree:
  783. qmgr_release_queue(queue_ids[port->id].rxfree);
  784. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  785. port->netdev->name);
  786. return err;
  787. }
  788. static void release_hdlc_queues(struct port *port)
  789. {
  790. qmgr_release_queue(queue_ids[port->id].rxfree);
  791. qmgr_release_queue(queue_ids[port->id].rx);
  792. qmgr_release_queue(queue_ids[port->id].txdone);
  793. qmgr_release_queue(queue_ids[port->id].tx);
  794. qmgr_release_queue(port->plat->txreadyq);
  795. }
  796. static int init_hdlc_queues(struct port *port)
  797. {
  798. int i;
  799. if (!ports_open)
  800. if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
  801. POOL_ALLOC_SIZE, 32, 0)))
  802. return -ENOMEM;
  803. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  804. &port->desc_tab_phys)))
  805. return -ENOMEM;
  806. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  807. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  808. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  809. /* Setup RX buffers */
  810. for (i = 0; i < RX_DESCS; i++) {
  811. struct desc *desc = rx_desc_ptr(port, i);
  812. buffer_t *buff;
  813. void *data;
  814. #ifdef __ARMEB__
  815. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  816. return -ENOMEM;
  817. data = buff->data;
  818. #else
  819. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  820. return -ENOMEM;
  821. data = buff;
  822. #endif
  823. desc->buf_len = RX_SIZE;
  824. desc->data = dma_map_single(&port->netdev->dev, data,
  825. RX_SIZE, DMA_FROM_DEVICE);
  826. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  827. free_buffer(buff);
  828. return -EIO;
  829. }
  830. port->rx_buff_tab[i] = buff;
  831. }
  832. return 0;
  833. }
  834. static void destroy_hdlc_queues(struct port *port)
  835. {
  836. int i;
  837. if (port->desc_tab) {
  838. for (i = 0; i < RX_DESCS; i++) {
  839. struct desc *desc = rx_desc_ptr(port, i);
  840. buffer_t *buff = port->rx_buff_tab[i];
  841. if (buff) {
  842. dma_unmap_single(&port->netdev->dev,
  843. desc->data, RX_SIZE,
  844. DMA_FROM_DEVICE);
  845. free_buffer(buff);
  846. }
  847. }
  848. for (i = 0; i < TX_DESCS; i++) {
  849. struct desc *desc = tx_desc_ptr(port, i);
  850. buffer_t *buff = port->tx_buff_tab[i];
  851. if (buff) {
  852. dma_unmap_tx(port, desc);
  853. free_buffer(buff);
  854. }
  855. }
  856. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  857. port->desc_tab = NULL;
  858. }
  859. if (!ports_open && dma_pool) {
  860. dma_pool_destroy(dma_pool);
  861. dma_pool = NULL;
  862. }
  863. }
  864. static int hss_hdlc_open(struct net_device *dev)
  865. {
  866. struct port *port = dev_to_port(dev);
  867. unsigned long flags;
  868. int i, err = 0;
  869. if ((err = hdlc_open(dev)))
  870. return err;
  871. if ((err = hss_load_firmware(port)))
  872. goto err_hdlc_close;
  873. if ((err = request_hdlc_queues(port)))
  874. goto err_hdlc_close;
  875. if ((err = init_hdlc_queues(port)))
  876. goto err_destroy_queues;
  877. spin_lock_irqsave(&npe_lock, flags);
  878. if (port->plat->open)
  879. if ((err = port->plat->open(port->id, dev,
  880. hss_hdlc_set_carrier)))
  881. goto err_unlock;
  882. spin_unlock_irqrestore(&npe_lock, flags);
  883. /* Populate queues with buffers, no failure after this point */
  884. for (i = 0; i < TX_DESCS; i++)
  885. queue_put_desc(port->plat->txreadyq,
  886. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  887. for (i = 0; i < RX_DESCS; i++)
  888. queue_put_desc(queue_ids[port->id].rxfree,
  889. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  890. napi_enable(&port->napi);
  891. netif_start_queue(dev);
  892. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  893. hss_hdlc_rx_irq, dev);
  894. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  895. hss_hdlc_txdone_irq, dev);
  896. qmgr_enable_irq(queue_ids[port->id].txdone);
  897. ports_open++;
  898. hss_set_hdlc_cfg(port);
  899. hss_config(port);
  900. hss_start_hdlc(port);
  901. /* we may already have RX data, enables IRQ */
  902. napi_schedule(&port->napi);
  903. return 0;
  904. err_unlock:
  905. spin_unlock_irqrestore(&npe_lock, flags);
  906. err_destroy_queues:
  907. destroy_hdlc_queues(port);
  908. release_hdlc_queues(port);
  909. err_hdlc_close:
  910. hdlc_close(dev);
  911. return err;
  912. }
  913. static int hss_hdlc_close(struct net_device *dev)
  914. {
  915. struct port *port = dev_to_port(dev);
  916. unsigned long flags;
  917. int i, buffs = RX_DESCS; /* allocated RX buffers */
  918. spin_lock_irqsave(&npe_lock, flags);
  919. ports_open--;
  920. qmgr_disable_irq(queue_ids[port->id].rx);
  921. netif_stop_queue(dev);
  922. napi_disable(&port->napi);
  923. hss_stop_hdlc(port);
  924. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  925. buffs--;
  926. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  927. buffs--;
  928. if (buffs)
  929. printk(KERN_CRIT "%s: unable to drain RX queue, %i buffer(s)"
  930. " left in NPE\n", dev->name, buffs);
  931. buffs = TX_DESCS;
  932. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  933. buffs--; /* cancel TX */
  934. i = 0;
  935. do {
  936. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  937. buffs--;
  938. if (!buffs)
  939. break;
  940. } while (++i < MAX_CLOSE_WAIT);
  941. if (buffs)
  942. printk(KERN_CRIT "%s: unable to drain TX queue, %i buffer(s) "
  943. "left in NPE\n", dev->name, buffs);
  944. #if DEBUG_CLOSE
  945. if (!buffs)
  946. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  947. #endif
  948. qmgr_disable_irq(queue_ids[port->id].txdone);
  949. if (port->plat->close)
  950. port->plat->close(port->id, dev);
  951. spin_unlock_irqrestore(&npe_lock, flags);
  952. destroy_hdlc_queues(port);
  953. release_hdlc_queues(port);
  954. hdlc_close(dev);
  955. return 0;
  956. }
  957. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  958. unsigned short parity)
  959. {
  960. struct port *port = dev_to_port(dev);
  961. if (encoding != ENCODING_NRZ)
  962. return -EINVAL;
  963. switch(parity) {
  964. case PARITY_CRC16_PR1_CCITT:
  965. port->hdlc_cfg = 0;
  966. return 0;
  967. case PARITY_CRC32_PR1_CCITT:
  968. port->hdlc_cfg = PKT_HDLC_CRC_32;
  969. return 0;
  970. default:
  971. return -EINVAL;
  972. }
  973. }
  974. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  975. {
  976. const size_t size = sizeof(sync_serial_settings);
  977. sync_serial_settings new_line;
  978. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  979. struct port *port = dev_to_port(dev);
  980. unsigned long flags;
  981. int clk;
  982. if (cmd != SIOCWANDEV)
  983. return hdlc_ioctl(dev, ifr, cmd);
  984. switch(ifr->ifr_settings.type) {
  985. case IF_GET_IFACE:
  986. ifr->ifr_settings.type = IF_IFACE_V35;
  987. if (ifr->ifr_settings.size < size) {
  988. ifr->ifr_settings.size = size; /* data size wanted */
  989. return -ENOBUFS;
  990. }
  991. memset(&new_line, 0, sizeof(new_line));
  992. new_line.clock_type = port->clock_type;
  993. new_line.clock_rate = 2048000; /* FIXME */
  994. new_line.loopback = port->loopback;
  995. if (copy_to_user(line, &new_line, size))
  996. return -EFAULT;
  997. return 0;
  998. case IF_IFACE_SYNC_SERIAL:
  999. case IF_IFACE_V35:
  1000. if(!capable(CAP_NET_ADMIN))
  1001. return -EPERM;
  1002. if (copy_from_user(&new_line, line, size))
  1003. return -EFAULT;
  1004. clk = new_line.clock_type;
  1005. if (port->plat->set_clock)
  1006. clk = port->plat->set_clock(port->id, clk);
  1007. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1008. return -EINVAL; /* No such clock setting */
  1009. if (new_line.loopback != 0 && new_line.loopback != 1)
  1010. return -EINVAL;
  1011. port->clock_type = clk; /* Update settings */
  1012. /* FIXME port->clock_rate = new_line.clock_rate */;
  1013. port->loopback = new_line.loopback;
  1014. spin_lock_irqsave(&npe_lock, flags);
  1015. if (dev->flags & IFF_UP)
  1016. hss_config(port);
  1017. if (port->loopback || port->carrier)
  1018. netif_carrier_on(port->netdev);
  1019. else
  1020. netif_carrier_off(port->netdev);
  1021. spin_unlock_irqrestore(&npe_lock, flags);
  1022. return 0;
  1023. default:
  1024. return hdlc_ioctl(dev, ifr, cmd);
  1025. }
  1026. }
  1027. /*****************************************************************************
  1028. * initialization
  1029. ****************************************************************************/
  1030. static const struct net_device_ops hss_hdlc_ops = {
  1031. .ndo_open = hss_hdlc_open,
  1032. .ndo_stop = hss_hdlc_close,
  1033. .ndo_change_mtu = hdlc_change_mtu,
  1034. .ndo_start_xmit = hdlc_start_xmit,
  1035. .ndo_do_ioctl = hss_hdlc_ioctl,
  1036. };
  1037. static int __devinit hss_init_one(struct platform_device *pdev)
  1038. {
  1039. struct port *port;
  1040. struct net_device *dev;
  1041. hdlc_device *hdlc;
  1042. int err;
  1043. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1044. return -ENOMEM;
  1045. if ((port->npe = npe_request(0)) == NULL) {
  1046. err = -ENODEV;
  1047. goto err_free;
  1048. }
  1049. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1050. err = -ENOMEM;
  1051. goto err_plat;
  1052. }
  1053. SET_NETDEV_DEV(dev, &pdev->dev);
  1054. hdlc = dev_to_hdlc(dev);
  1055. hdlc->attach = hss_hdlc_attach;
  1056. hdlc->xmit = hss_hdlc_xmit;
  1057. dev->netdev_ops = &hss_hdlc_ops;
  1058. dev->tx_queue_len = 100;
  1059. port->clock_type = CLOCK_EXT;
  1060. port->clock_rate = 2048000;
  1061. port->id = pdev->id;
  1062. port->dev = &pdev->dev;
  1063. port->plat = pdev->dev.platform_data;
  1064. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1065. if ((err = register_hdlc_device(dev)))
  1066. goto err_free_netdev;
  1067. platform_set_drvdata(pdev, port);
  1068. printk(KERN_INFO "%s: HSS-%i\n", dev->name, port->id);
  1069. return 0;
  1070. err_free_netdev:
  1071. free_netdev(dev);
  1072. err_plat:
  1073. npe_release(port->npe);
  1074. err_free:
  1075. kfree(port);
  1076. return err;
  1077. }
  1078. static int __devexit hss_remove_one(struct platform_device *pdev)
  1079. {
  1080. struct port *port = platform_get_drvdata(pdev);
  1081. unregister_hdlc_device(port->netdev);
  1082. free_netdev(port->netdev);
  1083. npe_release(port->npe);
  1084. platform_set_drvdata(pdev, NULL);
  1085. kfree(port);
  1086. return 0;
  1087. }
  1088. static struct platform_driver ixp4xx_hss_driver = {
  1089. .driver.name = DRV_NAME,
  1090. .probe = hss_init_one,
  1091. .remove = hss_remove_one,
  1092. };
  1093. static int __init hss_init_module(void)
  1094. {
  1095. if ((ixp4xx_read_feature_bits() &
  1096. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1097. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1098. return -ENODEV;
  1099. spin_lock_init(&npe_lock);
  1100. return platform_driver_register(&ixp4xx_hss_driver);
  1101. }
  1102. static void __exit hss_cleanup_module(void)
  1103. {
  1104. platform_driver_unregister(&ixp4xx_hss_driver);
  1105. }
  1106. MODULE_AUTHOR("Krzysztof Halasa");
  1107. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1108. MODULE_LICENSE("GPL v2");
  1109. MODULE_ALIAS("platform:ixp4xx_hss");
  1110. module_init(hss_init_module);
  1111. module_exit(hss_cleanup_module);