layer2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * PCBIT-D low-layer interface
  3. *
  4. * Copyright (C) 1996 Universidade de Lisboa
  5. *
  6. * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
  7. *
  8. * This software may be used and distributed according to the terms of
  9. * the GNU General Public License, incorporated herein by reference.
  10. */
  11. /*
  12. * 19991203 - Fernando Carvalho - takion@superbofh.org
  13. * Hacked to compile with egcs and run with current version of isdn modules
  14. */
  15. /*
  16. * Based on documentation provided by Inesc:
  17. * - "Interface com bus do PC para o PCBIT e PCBIT-D", Inesc, Jan 93
  18. */
  19. /*
  20. * TODO: better handling of errors
  21. * re-write/remove debug printks
  22. */
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/mm.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/isdnif.h>
  33. #include <asm/system.h>
  34. #include <asm/io.h>
  35. #include "pcbit.h"
  36. #include "layer2.h"
  37. #include "edss1.h"
  38. #undef DEBUG_FRAG
  39. /*
  40. * Prototypes
  41. */
  42. static void pcbit_transmit(struct pcbit_dev *dev);
  43. static void pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack);
  44. static void pcbit_l2_error(struct pcbit_dev *dev);
  45. static void pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info);
  46. static void pcbit_l2_err_recover(unsigned long data);
  47. static void pcbit_firmware_bug(struct pcbit_dev *dev);
  48. static __inline__ void
  49. pcbit_sched_delivery(struct pcbit_dev *dev)
  50. {
  51. schedule_work(&dev->qdelivery);
  52. }
  53. /*
  54. * Called from layer3
  55. */
  56. int
  57. pcbit_l2_write(struct pcbit_dev *dev, ulong msg, ushort refnum,
  58. struct sk_buff *skb, unsigned short hdr_len)
  59. {
  60. struct frame_buf *frame,
  61. *ptr;
  62. unsigned long flags;
  63. if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
  64. dev_kfree_skb(skb);
  65. return -1;
  66. }
  67. if ((frame = kmalloc(sizeof(struct frame_buf),
  68. GFP_ATOMIC)) == NULL) {
  69. printk(KERN_WARNING "pcbit_2_write: kmalloc failed\n");
  70. dev_kfree_skb(skb);
  71. return -1;
  72. }
  73. frame->msg = msg;
  74. frame->refnum = refnum;
  75. frame->copied = 0;
  76. frame->hdr_len = hdr_len;
  77. if (skb)
  78. frame->dt_len = skb->len - hdr_len;
  79. else
  80. frame->dt_len = 0;
  81. frame->skb = skb;
  82. frame->next = NULL;
  83. spin_lock_irqsave(&dev->lock, flags);
  84. if (dev->write_queue == NULL) {
  85. dev->write_queue = frame;
  86. spin_unlock_irqrestore(&dev->lock, flags);
  87. pcbit_transmit(dev);
  88. } else {
  89. for (ptr = dev->write_queue; ptr->next; ptr = ptr->next);
  90. ptr->next = frame;
  91. spin_unlock_irqrestore(&dev->lock, flags);
  92. }
  93. return 0;
  94. }
  95. static __inline__ void
  96. pcbit_tx_update(struct pcbit_dev *dev, ushort len)
  97. {
  98. u_char info;
  99. dev->send_seq = (dev->send_seq + 1) % 8;
  100. dev->fsize[dev->send_seq] = len;
  101. info = 0;
  102. info |= dev->rcv_seq << 3;
  103. info |= dev->send_seq;
  104. writeb(info, dev->sh_mem + BANK4);
  105. }
  106. /*
  107. * called by interrupt service routine or by write_2
  108. */
  109. static void
  110. pcbit_transmit(struct pcbit_dev *dev)
  111. {
  112. struct frame_buf *frame = NULL;
  113. unsigned char unacked;
  114. int flen; /* fragment frame length including all headers */
  115. int free;
  116. int count,
  117. cp_len;
  118. unsigned long flags;
  119. unsigned short tt;
  120. if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
  121. return;
  122. unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
  123. spin_lock_irqsave(&dev->lock, flags);
  124. if (dev->free > 16 && dev->write_queue && unacked < 7) {
  125. if (!dev->w_busy)
  126. dev->w_busy = 1;
  127. else {
  128. spin_unlock_irqrestore(&dev->lock, flags);
  129. return;
  130. }
  131. frame = dev->write_queue;
  132. free = dev->free;
  133. spin_unlock_irqrestore(&dev->lock, flags);
  134. if (frame->copied == 0) {
  135. /* Type 0 frame */
  136. ulong msg;
  137. if (frame->skb)
  138. flen = FRAME_HDR_LEN + PREHDR_LEN + frame->skb->len;
  139. else
  140. flen = FRAME_HDR_LEN + PREHDR_LEN;
  141. if (flen > free)
  142. flen = free;
  143. msg = frame->msg;
  144. /*
  145. * Board level 2 header
  146. */
  147. pcbit_writew(dev, flen - FRAME_HDR_LEN);
  148. pcbit_writeb(dev, GET_MSG_CPU(msg));
  149. pcbit_writeb(dev, GET_MSG_PROC(msg));
  150. /* TH */
  151. pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
  152. /* TD */
  153. pcbit_writew(dev, frame->dt_len);
  154. /*
  155. * Board level 3 fixed-header
  156. */
  157. /* LEN = TH */
  158. pcbit_writew(dev, frame->hdr_len + PREHDR_LEN);
  159. /* XX */
  160. pcbit_writew(dev, 0);
  161. /* C + S */
  162. pcbit_writeb(dev, GET_MSG_CMD(msg));
  163. pcbit_writeb(dev, GET_MSG_SCMD(msg));
  164. /* NUM */
  165. pcbit_writew(dev, frame->refnum);
  166. count = FRAME_HDR_LEN + PREHDR_LEN;
  167. } else {
  168. /* Type 1 frame */
  169. flen = 2 + (frame->skb->len - frame->copied);
  170. if (flen > free)
  171. flen = free;
  172. /* TT */
  173. tt = ((ushort) (flen - 2)) | 0x8000U; /* Type 1 */
  174. pcbit_writew(dev, tt);
  175. count = 2;
  176. }
  177. if (frame->skb) {
  178. cp_len = frame->skb->len - frame->copied;
  179. if (cp_len > flen - count)
  180. cp_len = flen - count;
  181. memcpy_topcbit(dev, frame->skb->data + frame->copied,
  182. cp_len);
  183. frame->copied += cp_len;
  184. }
  185. /* bookkeeping */
  186. dev->free -= flen;
  187. pcbit_tx_update(dev, flen);
  188. spin_lock_irqsave(&dev->lock, flags);
  189. if (frame->skb == NULL || frame->copied == frame->skb->len) {
  190. dev->write_queue = frame->next;
  191. if (frame->skb != NULL) {
  192. /* free frame */
  193. dev_kfree_skb(frame->skb);
  194. }
  195. kfree(frame);
  196. }
  197. dev->w_busy = 0;
  198. spin_unlock_irqrestore(&dev->lock, flags);
  199. } else {
  200. spin_unlock_irqrestore(&dev->lock, flags);
  201. #ifdef DEBUG
  202. printk(KERN_DEBUG "unacked %d free %d write_queue %s\n",
  203. unacked, dev->free, dev->write_queue ? "not empty" :
  204. "empty");
  205. #endif
  206. }
  207. }
  208. /*
  209. * deliver a queued frame to the upper layer
  210. */
  211. void
  212. pcbit_deliver(struct work_struct *work)
  213. {
  214. struct frame_buf *frame;
  215. unsigned long flags, msg;
  216. struct pcbit_dev *dev =
  217. container_of(work, struct pcbit_dev, qdelivery);
  218. spin_lock_irqsave(&dev->lock, flags);
  219. while ((frame = dev->read_queue)) {
  220. dev->read_queue = frame->next;
  221. spin_unlock_irqrestore(&dev->lock, flags);
  222. msg = 0;
  223. SET_MSG_CPU(msg, 0);
  224. SET_MSG_PROC(msg, 0);
  225. SET_MSG_CMD(msg, frame->skb->data[2]);
  226. SET_MSG_SCMD(msg, frame->skb->data[3]);
  227. frame->refnum = *((ushort *) frame->skb->data + 4);
  228. frame->msg = *((ulong *) & msg);
  229. skb_pull(frame->skb, 6);
  230. pcbit_l3_receive(dev, frame->msg, frame->skb, frame->hdr_len,
  231. frame->refnum);
  232. kfree(frame);
  233. spin_lock_irqsave(&dev->lock, flags);
  234. }
  235. spin_unlock_irqrestore(&dev->lock, flags);
  236. }
  237. /*
  238. * Reads BANK 2 & Reassembles
  239. */
  240. static void
  241. pcbit_receive(struct pcbit_dev *dev)
  242. {
  243. unsigned short tt;
  244. u_char cpu,
  245. proc;
  246. struct frame_buf *frame = NULL;
  247. unsigned long flags;
  248. u_char type1;
  249. if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING)
  250. return;
  251. tt = pcbit_readw(dev);
  252. if ((tt & 0x7fffU) > 511) {
  253. printk(KERN_INFO "pcbit: invalid frame length -> TT=%04x\n",
  254. tt);
  255. pcbit_l2_error(dev);
  256. return;
  257. }
  258. if (!(tt & 0x8000U)) { /* Type 0 */
  259. type1 = 0;
  260. if (dev->read_frame) {
  261. printk(KERN_DEBUG "pcbit_receive: Type 0 frame and read_frame != NULL\n");
  262. /* discard previous queued frame */
  263. kfree_skb(dev->read_frame->skb);
  264. kfree(dev->read_frame);
  265. dev->read_frame = NULL;
  266. }
  267. frame = kzalloc(sizeof(struct frame_buf), GFP_ATOMIC);
  268. if (frame == NULL) {
  269. printk(KERN_WARNING "kmalloc failed\n");
  270. return;
  271. }
  272. cpu = pcbit_readb(dev);
  273. proc = pcbit_readb(dev);
  274. if (cpu != 0x06 && cpu != 0x02) {
  275. printk(KERN_DEBUG "pcbit: invalid cpu value\n");
  276. kfree(frame);
  277. pcbit_l2_error(dev);
  278. return;
  279. }
  280. /*
  281. * we discard cpu & proc on receiving
  282. * but we read it to update the pointer
  283. */
  284. frame->hdr_len = pcbit_readw(dev);
  285. frame->dt_len = pcbit_readw(dev);
  286. /*
  287. * 0 sized packet
  288. * I don't know if they are an error or not...
  289. * But they are very frequent
  290. * Not documented
  291. */
  292. if (frame->hdr_len == 0) {
  293. kfree(frame);
  294. #ifdef DEBUG
  295. printk(KERN_DEBUG "0 sized frame\n");
  296. #endif
  297. pcbit_firmware_bug(dev);
  298. return;
  299. }
  300. /* sanity check the length values */
  301. if (frame->hdr_len > 1024 || frame->dt_len > 2048) {
  302. #ifdef DEBUG
  303. printk(KERN_DEBUG "length problem: ");
  304. printk(KERN_DEBUG "TH=%04x TD=%04x\n",
  305. frame->hdr_len,
  306. frame->dt_len);
  307. #endif
  308. pcbit_l2_error(dev);
  309. kfree(frame);
  310. return;
  311. }
  312. /* minimum frame read */
  313. frame->skb = dev_alloc_skb(frame->hdr_len + frame->dt_len +
  314. ((frame->hdr_len + 15) & ~15));
  315. if (!frame->skb) {
  316. printk(KERN_DEBUG "pcbit_receive: out of memory\n");
  317. kfree(frame);
  318. return;
  319. }
  320. /* 16 byte alignment for IP */
  321. if (frame->dt_len)
  322. skb_reserve(frame->skb, (frame->hdr_len + 15) & ~15);
  323. } else {
  324. /* Type 1 */
  325. type1 = 1;
  326. tt &= 0x7fffU;
  327. if (!(frame = dev->read_frame)) {
  328. printk("Type 1 frame and no frame queued\n");
  329. /* usually after an error: toss frame */
  330. dev->readptr += tt;
  331. if (dev->readptr > dev->sh_mem + BANK2 + BANKLEN)
  332. dev->readptr -= BANKLEN;
  333. return;
  334. }
  335. }
  336. memcpy_frompcbit(dev, skb_put(frame->skb, tt), tt);
  337. frame->copied += tt;
  338. spin_lock_irqsave(&dev->lock, flags);
  339. if (frame->copied == frame->hdr_len + frame->dt_len) {
  340. if (type1) {
  341. dev->read_frame = NULL;
  342. }
  343. if (dev->read_queue) {
  344. struct frame_buf *ptr;
  345. for (ptr = dev->read_queue; ptr->next; ptr = ptr->next);
  346. ptr->next = frame;
  347. } else
  348. dev->read_queue = frame;
  349. } else {
  350. dev->read_frame = frame;
  351. }
  352. spin_unlock_irqrestore(&dev->lock, flags);
  353. }
  354. /*
  355. * The board sends 0 sized frames
  356. * They are TDATA_CONFs that get messed up somehow
  357. * gotta send a fake acknowledgment to the upper layer somehow
  358. */
  359. static __inline__ void
  360. pcbit_fake_conf(struct pcbit_dev *dev, struct pcbit_chan *chan)
  361. {
  362. isdn_ctrl ictl;
  363. if (chan->queued) {
  364. chan->queued--;
  365. ictl.driver = dev->id;
  366. ictl.command = ISDN_STAT_BSENT;
  367. ictl.arg = chan->id;
  368. dev->dev_if->statcallb(&ictl);
  369. }
  370. }
  371. static void
  372. pcbit_firmware_bug(struct pcbit_dev *dev)
  373. {
  374. struct pcbit_chan *chan;
  375. chan = dev->b1;
  376. if (chan->fsm_state == ST_ACTIVE) {
  377. pcbit_fake_conf(dev, chan);
  378. }
  379. chan = dev->b2;
  380. if (chan->fsm_state == ST_ACTIVE) {
  381. pcbit_fake_conf(dev, chan);
  382. }
  383. }
  384. irqreturn_t
  385. pcbit_irq_handler(int interrupt, void *devptr)
  386. {
  387. struct pcbit_dev *dev;
  388. u_char info,
  389. ack_seq,
  390. read_seq;
  391. dev = (struct pcbit_dev *) devptr;
  392. if (!dev) {
  393. printk(KERN_WARNING "pcbit_irq_handler: wrong device\n");
  394. return IRQ_NONE;
  395. }
  396. if (dev->interrupt) {
  397. printk(KERN_DEBUG "pcbit: reentering interrupt hander\n");
  398. return IRQ_HANDLED;
  399. }
  400. dev->interrupt = 1;
  401. info = readb(dev->sh_mem + BANK3);
  402. if (dev->l2_state == L2_STARTING || dev->l2_state == L2_ERROR) {
  403. pcbit_l2_active_conf(dev, info);
  404. dev->interrupt = 0;
  405. return IRQ_HANDLED;
  406. }
  407. if (info & 0x40U) { /* E bit set */
  408. #ifdef DEBUG
  409. printk(KERN_DEBUG "pcbit_irq_handler: E bit on\n");
  410. #endif
  411. pcbit_l2_error(dev);
  412. dev->interrupt = 0;
  413. return IRQ_HANDLED;
  414. }
  415. if (dev->l2_state != L2_RUNNING && dev->l2_state != L2_LOADING) {
  416. dev->interrupt = 0;
  417. return IRQ_HANDLED;
  418. }
  419. ack_seq = (info >> 3) & 0x07U;
  420. read_seq = (info & 0x07U);
  421. dev->interrupt = 0;
  422. if (read_seq != dev->rcv_seq) {
  423. while (read_seq != dev->rcv_seq) {
  424. pcbit_receive(dev);
  425. dev->rcv_seq = (dev->rcv_seq + 1) % 8;
  426. }
  427. pcbit_sched_delivery(dev);
  428. }
  429. if (ack_seq != dev->unack_seq) {
  430. pcbit_recv_ack(dev, ack_seq);
  431. }
  432. info = dev->rcv_seq << 3;
  433. info |= dev->send_seq;
  434. writeb(info, dev->sh_mem + BANK4);
  435. return IRQ_HANDLED;
  436. }
  437. static void
  438. pcbit_l2_active_conf(struct pcbit_dev *dev, u_char info)
  439. {
  440. u_char state;
  441. state = dev->l2_state;
  442. #ifdef DEBUG
  443. printk(KERN_DEBUG "layer2_active_confirm\n");
  444. #endif
  445. if (info & 0x80U) {
  446. dev->rcv_seq = info & 0x07U;
  447. dev->l2_state = L2_RUNNING;
  448. } else
  449. dev->l2_state = L2_DOWN;
  450. if (state == L2_STARTING)
  451. wake_up_interruptible(&dev->set_running_wq);
  452. if (state == L2_ERROR && dev->l2_state == L2_RUNNING) {
  453. pcbit_transmit(dev);
  454. }
  455. }
  456. static void
  457. pcbit_l2_err_recover(unsigned long data)
  458. {
  459. struct pcbit_dev *dev;
  460. struct frame_buf *frame;
  461. dev = (struct pcbit_dev *) data;
  462. del_timer(&dev->error_recover_timer);
  463. if (dev->w_busy || dev->r_busy) {
  464. init_timer(&dev->error_recover_timer);
  465. dev->error_recover_timer.expires = jiffies + ERRTIME;
  466. add_timer(&dev->error_recover_timer);
  467. return;
  468. }
  469. dev->w_busy = dev->r_busy = 1;
  470. if (dev->read_frame) {
  471. kfree_skb(dev->read_frame->skb);
  472. kfree(dev->read_frame);
  473. dev->read_frame = NULL;
  474. }
  475. if (dev->write_queue) {
  476. frame = dev->write_queue;
  477. #ifdef FREE_ON_ERROR
  478. dev->write_queue = dev->write_queue->next;
  479. if (frame->skb) {
  480. dev_kfree_skb(frame->skb);
  481. }
  482. kfree(frame);
  483. #else
  484. frame->copied = 0;
  485. #endif
  486. }
  487. dev->rcv_seq = dev->send_seq = dev->unack_seq = 0;
  488. dev->free = 511;
  489. dev->l2_state = L2_ERROR;
  490. /* this is an hack... */
  491. pcbit_firmware_bug(dev);
  492. dev->writeptr = dev->sh_mem;
  493. dev->readptr = dev->sh_mem + BANK2;
  494. writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
  495. dev->sh_mem + BANK4);
  496. dev->w_busy = dev->r_busy = 0;
  497. }
  498. static void
  499. pcbit_l2_error(struct pcbit_dev *dev)
  500. {
  501. if (dev->l2_state == L2_RUNNING) {
  502. printk(KERN_INFO "pcbit: layer 2 error\n");
  503. #ifdef DEBUG
  504. log_state(dev);
  505. #endif
  506. dev->l2_state = L2_DOWN;
  507. init_timer(&dev->error_recover_timer);
  508. dev->error_recover_timer.function = &pcbit_l2_err_recover;
  509. dev->error_recover_timer.data = (ulong) dev;
  510. dev->error_recover_timer.expires = jiffies + ERRTIME;
  511. add_timer(&dev->error_recover_timer);
  512. }
  513. }
  514. /*
  515. * Description:
  516. * if board acks frames
  517. * update dev->free
  518. * call pcbit_transmit to write possible queued frames
  519. */
  520. static void
  521. pcbit_recv_ack(struct pcbit_dev *dev, unsigned char ack)
  522. {
  523. int i,
  524. count;
  525. int unacked;
  526. unacked = (dev->send_seq + (8 - dev->unack_seq)) & 0x07;
  527. /* dev->unack_seq < ack <= dev->send_seq; */
  528. if (unacked) {
  529. if (dev->send_seq > dev->unack_seq) {
  530. if (ack <= dev->unack_seq || ack > dev->send_seq) {
  531. printk(KERN_DEBUG
  532. "layer 2 ack unacceptable - dev %d",
  533. dev->id);
  534. pcbit_l2_error(dev);
  535. } else if (ack > dev->send_seq && ack <= dev->unack_seq) {
  536. printk(KERN_DEBUG
  537. "layer 2 ack unacceptable - dev %d",
  538. dev->id);
  539. pcbit_l2_error(dev);
  540. }
  541. }
  542. /* ack is acceptable */
  543. i = dev->unack_seq;
  544. do {
  545. dev->unack_seq = i = (i + 1) % 8;
  546. dev->free += dev->fsize[i];
  547. } while (i != ack);
  548. count = 0;
  549. while (count < 7 && dev->write_queue) {
  550. u8 lsend_seq = dev->send_seq;
  551. pcbit_transmit(dev);
  552. if (dev->send_seq == lsend_seq)
  553. break;
  554. count++;
  555. }
  556. } else
  557. printk(KERN_DEBUG "recv_ack: unacked = 0\n");
  558. }