layer2.c 14 KB

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