ixp4xx_hss.c 35 KB

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