qeth_eddp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. *
  3. * linux/drivers/s390/net/qeth_eddp.c ($Revision: 1.11 $)
  4. *
  5. * Enhanced Device Driver Packing (EDDP) support for the qeth driver.
  6. *
  7. * Copyright 2004 IBM Corporation
  8. *
  9. * Author(s): Thomas Spatzier <tspat@de.ibm.com>
  10. *
  11. * $Revision: 1.11 $ $Date: 2005/03/24 09:04:18 $
  12. *
  13. */
  14. #include <linux/config.h>
  15. #include <linux/errno.h>
  16. #include <linux/ip.h>
  17. #include <linux/inetdevice.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/kernel.h>
  20. #include <linux/tcp.h>
  21. #include <net/tcp.h>
  22. #include <linux/skbuff.h>
  23. #include <net/ip.h>
  24. #include "qeth.h"
  25. #include "qeth_mpc.h"
  26. #include "qeth_eddp.h"
  27. int
  28. qeth_eddp_check_buffers_for_context(struct qeth_qdio_out_q *queue,
  29. struct qeth_eddp_context *ctx)
  30. {
  31. int index = queue->next_buf_to_fill;
  32. int elements_needed = ctx->num_elements;
  33. int elements_in_buffer;
  34. int skbs_in_buffer;
  35. int buffers_needed = 0;
  36. QETH_DBF_TEXT(trace, 5, "eddpcbfc");
  37. while(elements_needed > 0) {
  38. buffers_needed++;
  39. if (atomic_read(&queue->bufs[index].state) !=
  40. QETH_QDIO_BUF_EMPTY)
  41. return -EBUSY;
  42. elements_in_buffer = QETH_MAX_BUFFER_ELEMENTS(queue->card) -
  43. queue->bufs[index].next_element_to_fill;
  44. skbs_in_buffer = elements_in_buffer / ctx->elements_per_skb;
  45. elements_needed -= skbs_in_buffer * ctx->elements_per_skb;
  46. index = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
  47. }
  48. return buffers_needed;
  49. }
  50. static inline void
  51. qeth_eddp_free_context(struct qeth_eddp_context *ctx)
  52. {
  53. int i;
  54. QETH_DBF_TEXT(trace, 5, "eddpfctx");
  55. for (i = 0; i < ctx->num_pages; ++i)
  56. free_page((unsigned long)ctx->pages[i]);
  57. kfree(ctx->pages);
  58. if (ctx->elements != NULL)
  59. kfree(ctx->elements);
  60. kfree(ctx);
  61. }
  62. static inline void
  63. qeth_eddp_get_context(struct qeth_eddp_context *ctx)
  64. {
  65. atomic_inc(&ctx->refcnt);
  66. }
  67. void
  68. qeth_eddp_put_context(struct qeth_eddp_context *ctx)
  69. {
  70. if (atomic_dec_return(&ctx->refcnt) == 0)
  71. qeth_eddp_free_context(ctx);
  72. }
  73. void
  74. qeth_eddp_buf_release_contexts(struct qeth_qdio_out_buffer *buf)
  75. {
  76. struct qeth_eddp_context_reference *ref;
  77. QETH_DBF_TEXT(trace, 6, "eddprctx");
  78. while (!list_empty(&buf->ctx_list)){
  79. ref = list_entry(buf->ctx_list.next,
  80. struct qeth_eddp_context_reference, list);
  81. qeth_eddp_put_context(ref->ctx);
  82. list_del(&ref->list);
  83. kfree(ref);
  84. }
  85. }
  86. static inline int
  87. qeth_eddp_buf_ref_context(struct qeth_qdio_out_buffer *buf,
  88. struct qeth_eddp_context *ctx)
  89. {
  90. struct qeth_eddp_context_reference *ref;
  91. QETH_DBF_TEXT(trace, 6, "eddprfcx");
  92. ref = kmalloc(sizeof(struct qeth_eddp_context_reference), GFP_ATOMIC);
  93. if (ref == NULL)
  94. return -ENOMEM;
  95. qeth_eddp_get_context(ctx);
  96. ref->ctx = ctx;
  97. list_add_tail(&ref->list, &buf->ctx_list);
  98. return 0;
  99. }
  100. int
  101. qeth_eddp_fill_buffer(struct qeth_qdio_out_q *queue,
  102. struct qeth_eddp_context *ctx,
  103. int index)
  104. {
  105. struct qeth_qdio_out_buffer *buf = NULL;
  106. struct qdio_buffer *buffer;
  107. int elements = ctx->num_elements;
  108. int element = 0;
  109. int flush_cnt = 0;
  110. int must_refcnt = 1;
  111. int i;
  112. QETH_DBF_TEXT(trace, 5, "eddpfibu");
  113. while (elements > 0) {
  114. buf = &queue->bufs[index];
  115. if (atomic_read(&buf->state) != QETH_QDIO_BUF_EMPTY){
  116. /* normally this should not happen since we checked for
  117. * available elements in qeth_check_elements_for_context
  118. */
  119. if (element == 0)
  120. return -EBUSY;
  121. else {
  122. PRINT_WARN("could only partially fill eddp "
  123. "buffer!\n");
  124. goto out;
  125. }
  126. }
  127. /* check if the whole next skb fits into current buffer */
  128. if ((QETH_MAX_BUFFER_ELEMENTS(queue->card) -
  129. buf->next_element_to_fill)
  130. < ctx->elements_per_skb){
  131. /* no -> go to next buffer */
  132. atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
  133. index = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
  134. flush_cnt++;
  135. /* new buffer, so we have to add ctx to buffer'ctx_list
  136. * and increment ctx's refcnt */
  137. must_refcnt = 1;
  138. continue;
  139. }
  140. if (must_refcnt){
  141. must_refcnt = 0;
  142. if (qeth_eddp_buf_ref_context(buf, ctx)){
  143. PRINT_WARN("no memory to create eddp context "
  144. "reference\n");
  145. goto out_check;
  146. }
  147. }
  148. buffer = buf->buffer;
  149. /* fill one skb into buffer */
  150. for (i = 0; i < ctx->elements_per_skb; ++i){
  151. buffer->element[buf->next_element_to_fill].addr =
  152. ctx->elements[element].addr;
  153. buffer->element[buf->next_element_to_fill].length =
  154. ctx->elements[element].length;
  155. buffer->element[buf->next_element_to_fill].flags =
  156. ctx->elements[element].flags;
  157. buf->next_element_to_fill++;
  158. element++;
  159. elements--;
  160. }
  161. }
  162. out_check:
  163. if (!queue->do_pack) {
  164. QETH_DBF_TEXT(trace, 6, "fillbfnp");
  165. /* set state to PRIMED -> will be flushed */
  166. if (buf->next_element_to_fill > 0){
  167. atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
  168. flush_cnt++;
  169. }
  170. } else {
  171. #ifdef CONFIG_QETH_PERF_STATS
  172. queue->card->perf_stats.skbs_sent_pack++;
  173. #endif
  174. QETH_DBF_TEXT(trace, 6, "fillbfpa");
  175. if (buf->next_element_to_fill >=
  176. QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
  177. /*
  178. * packed buffer if full -> set state PRIMED
  179. * -> will be flushed
  180. */
  181. atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
  182. flush_cnt++;
  183. }
  184. }
  185. out:
  186. return flush_cnt;
  187. }
  188. static inline int
  189. qeth_get_skb_data_len(struct sk_buff *skb)
  190. {
  191. int len = skb->len;
  192. int i;
  193. for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i)
  194. len -= skb_shinfo(skb)->frags[i].size;
  195. return len;
  196. }
  197. static inline void
  198. qeth_eddp_create_segment_hdrs(struct qeth_eddp_context *ctx,
  199. struct qeth_eddp_data *eddp)
  200. {
  201. u8 *page;
  202. int page_remainder;
  203. int page_offset;
  204. int hdr_len;
  205. struct qeth_eddp_element *element;
  206. QETH_DBF_TEXT(trace, 5, "eddpcrsh");
  207. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  208. page_offset = ctx->offset % PAGE_SIZE;
  209. element = &ctx->elements[ctx->num_elements];
  210. hdr_len = eddp->nhl + eddp->thl;
  211. /* FIXME: layer2 and VLAN !!! */
  212. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
  213. hdr_len += ETH_HLEN;
  214. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
  215. hdr_len += VLAN_HLEN;
  216. /* does complete header fit in current page ? */
  217. page_remainder = PAGE_SIZE - page_offset;
  218. if (page_remainder < (sizeof(struct qeth_hdr) + hdr_len)){
  219. /* no -> go to start of next page */
  220. ctx->offset += page_remainder;
  221. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  222. page_offset = 0;
  223. }
  224. memcpy(page + page_offset, &eddp->qh, sizeof(struct qeth_hdr));
  225. element->addr = page + page_offset;
  226. element->length = sizeof(struct qeth_hdr);
  227. ctx->offset += sizeof(struct qeth_hdr);
  228. page_offset += sizeof(struct qeth_hdr);
  229. /* add mac header (?) */
  230. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2){
  231. memcpy(page + page_offset, &eddp->mac, ETH_HLEN);
  232. element->length += ETH_HLEN;
  233. ctx->offset += ETH_HLEN;
  234. page_offset += ETH_HLEN;
  235. }
  236. /* add VLAN tag */
  237. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)){
  238. memcpy(page + page_offset, &eddp->vlan, VLAN_HLEN);
  239. element->length += VLAN_HLEN;
  240. ctx->offset += VLAN_HLEN;
  241. page_offset += VLAN_HLEN;
  242. }
  243. /* add network header */
  244. memcpy(page + page_offset, (u8 *)&eddp->nh, eddp->nhl);
  245. element->length += eddp->nhl;
  246. eddp->nh_in_ctx = page + page_offset;
  247. ctx->offset += eddp->nhl;
  248. page_offset += eddp->nhl;
  249. /* add transport header */
  250. memcpy(page + page_offset, (u8 *)&eddp->th, eddp->thl);
  251. element->length += eddp->thl;
  252. eddp->th_in_ctx = page + page_offset;
  253. ctx->offset += eddp->thl;
  254. }
  255. static inline void
  256. qeth_eddp_copy_data_tcp(char *dst, struct qeth_eddp_data *eddp, int len,
  257. u32 *hcsum)
  258. {
  259. struct skb_frag_struct *frag;
  260. int left_in_frag;
  261. int copy_len;
  262. u8 *src;
  263. QETH_DBF_TEXT(trace, 5, "eddpcdtc");
  264. if (skb_shinfo(eddp->skb)->nr_frags == 0) {
  265. memcpy(dst, eddp->skb->data + eddp->skb_offset, len);
  266. *hcsum = csum_partial(eddp->skb->data + eddp->skb_offset, len,
  267. *hcsum);
  268. eddp->skb_offset += len;
  269. } else {
  270. while (len > 0) {
  271. if (eddp->frag < 0) {
  272. /* we're in skb->data */
  273. left_in_frag = qeth_get_skb_data_len(eddp->skb)
  274. - eddp->skb_offset;
  275. src = eddp->skb->data + eddp->skb_offset;
  276. } else {
  277. frag = &skb_shinfo(eddp->skb)->
  278. frags[eddp->frag];
  279. left_in_frag = frag->size - eddp->frag_offset;
  280. src = (u8 *)(
  281. (page_to_pfn(frag->page) << PAGE_SHIFT)+
  282. frag->page_offset + eddp->frag_offset);
  283. }
  284. if (left_in_frag <= 0) {
  285. eddp->frag++;
  286. eddp->frag_offset = 0;
  287. continue;
  288. }
  289. copy_len = min(left_in_frag, len);
  290. memcpy(dst, src, copy_len);
  291. *hcsum = csum_partial(src, copy_len, *hcsum);
  292. dst += copy_len;
  293. eddp->frag_offset += copy_len;
  294. eddp->skb_offset += copy_len;
  295. len -= copy_len;
  296. }
  297. }
  298. }
  299. static inline void
  300. qeth_eddp_create_segment_data_tcp(struct qeth_eddp_context *ctx,
  301. struct qeth_eddp_data *eddp, int data_len,
  302. u32 hcsum)
  303. {
  304. u8 *page;
  305. int page_remainder;
  306. int page_offset;
  307. struct qeth_eddp_element *element;
  308. int first_lap = 1;
  309. QETH_DBF_TEXT(trace, 5, "eddpcsdt");
  310. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  311. page_offset = ctx->offset % PAGE_SIZE;
  312. element = &ctx->elements[ctx->num_elements];
  313. while (data_len){
  314. page_remainder = PAGE_SIZE - page_offset;
  315. if (page_remainder < data_len){
  316. qeth_eddp_copy_data_tcp(page + page_offset, eddp,
  317. page_remainder, &hcsum);
  318. element->length += page_remainder;
  319. if (first_lap)
  320. element->flags = SBAL_FLAGS_FIRST_FRAG;
  321. else
  322. element->flags = SBAL_FLAGS_MIDDLE_FRAG;
  323. ctx->num_elements++;
  324. element++;
  325. data_len -= page_remainder;
  326. ctx->offset += page_remainder;
  327. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  328. page_offset = 0;
  329. element->addr = page + page_offset;
  330. } else {
  331. qeth_eddp_copy_data_tcp(page + page_offset, eddp,
  332. data_len, &hcsum);
  333. element->length += data_len;
  334. if (!first_lap)
  335. element->flags = SBAL_FLAGS_LAST_FRAG;
  336. ctx->num_elements++;
  337. ctx->offset += data_len;
  338. data_len = 0;
  339. }
  340. first_lap = 0;
  341. }
  342. ((struct tcphdr *)eddp->th_in_ctx)->check = csum_fold(hcsum);
  343. }
  344. static inline u32
  345. qeth_eddp_check_tcp4_hdr(struct qeth_eddp_data *eddp, int data_len)
  346. {
  347. u32 phcsum; /* pseudo header checksum */
  348. QETH_DBF_TEXT(trace, 5, "eddpckt4");
  349. eddp->th.tcp.h.check = 0;
  350. /* compute pseudo header checksum */
  351. phcsum = csum_tcpudp_nofold(eddp->nh.ip4.h.saddr, eddp->nh.ip4.h.daddr,
  352. eddp->thl + data_len, IPPROTO_TCP, 0);
  353. /* compute checksum of tcp header */
  354. return csum_partial((u8 *)&eddp->th, eddp->thl, phcsum);
  355. }
  356. static inline u32
  357. qeth_eddp_check_tcp6_hdr(struct qeth_eddp_data *eddp, int data_len)
  358. {
  359. u32 proto;
  360. u32 phcsum; /* pseudo header checksum */
  361. QETH_DBF_TEXT(trace, 5, "eddpckt6");
  362. eddp->th.tcp.h.check = 0;
  363. /* compute pseudo header checksum */
  364. phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.saddr,
  365. sizeof(struct in6_addr), 0);
  366. phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.daddr,
  367. sizeof(struct in6_addr), phcsum);
  368. proto = htonl(IPPROTO_TCP);
  369. phcsum = csum_partial((u8 *)&proto, sizeof(u32), phcsum);
  370. return phcsum;
  371. }
  372. static inline struct qeth_eddp_data *
  373. qeth_eddp_create_eddp_data(struct qeth_hdr *qh, u8 *nh, u8 nhl, u8 *th, u8 thl)
  374. {
  375. struct qeth_eddp_data *eddp;
  376. QETH_DBF_TEXT(trace, 5, "eddpcrda");
  377. eddp = kmalloc(sizeof(struct qeth_eddp_data), GFP_ATOMIC);
  378. if (eddp){
  379. memset(eddp, 0, sizeof(struct qeth_eddp_data));
  380. eddp->nhl = nhl;
  381. eddp->thl = thl;
  382. memcpy(&eddp->qh, qh, sizeof(struct qeth_hdr));
  383. memcpy(&eddp->nh, nh, nhl);
  384. memcpy(&eddp->th, th, thl);
  385. eddp->frag = -1; /* initially we're in skb->data */
  386. }
  387. return eddp;
  388. }
  389. static inline void
  390. __qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
  391. struct qeth_eddp_data *eddp)
  392. {
  393. struct tcphdr *tcph;
  394. int data_len;
  395. u32 hcsum;
  396. QETH_DBF_TEXT(trace, 5, "eddpftcp");
  397. eddp->skb_offset = sizeof(struct qeth_hdr) + eddp->nhl + eddp->thl;
  398. tcph = eddp->skb->h.th;
  399. while (eddp->skb_offset < eddp->skb->len) {
  400. data_len = min((int)skb_shinfo(eddp->skb)->tso_size,
  401. (int)(eddp->skb->len - eddp->skb_offset));
  402. /* prepare qdio hdr */
  403. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2){
  404. eddp->qh.hdr.l2.pkt_length = data_len + ETH_HLEN +
  405. eddp->nhl + eddp->thl -
  406. sizeof(struct qeth_hdr);
  407. #ifdef CONFIG_QETH_VLAN
  408. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
  409. eddp->qh.hdr.l2.pkt_length += VLAN_HLEN;
  410. #endif /* CONFIG_QETH_VLAN */
  411. } else
  412. eddp->qh.hdr.l3.length = data_len + eddp->nhl +
  413. eddp->thl;
  414. /* prepare ip hdr */
  415. if (eddp->skb->protocol == ETH_P_IP){
  416. eddp->nh.ip4.h.tot_len = data_len + eddp->nhl +
  417. eddp->thl;
  418. eddp->nh.ip4.h.check = 0;
  419. eddp->nh.ip4.h.check =
  420. ip_fast_csum((u8 *)&eddp->nh.ip4.h,
  421. eddp->nh.ip4.h.ihl);
  422. } else
  423. eddp->nh.ip6.h.payload_len = data_len + eddp->thl;
  424. /* prepare tcp hdr */
  425. if (data_len == (eddp->skb->len - eddp->skb_offset)){
  426. /* last segment -> set FIN and PSH flags */
  427. eddp->th.tcp.h.fin = tcph->fin;
  428. eddp->th.tcp.h.psh = tcph->psh;
  429. }
  430. if (eddp->skb->protocol == ETH_P_IP)
  431. hcsum = qeth_eddp_check_tcp4_hdr(eddp, data_len);
  432. else
  433. hcsum = qeth_eddp_check_tcp6_hdr(eddp, data_len);
  434. /* fill the next segment into the context */
  435. qeth_eddp_create_segment_hdrs(ctx, eddp);
  436. qeth_eddp_create_segment_data_tcp(ctx, eddp, data_len, hcsum);
  437. if (eddp->skb_offset >= eddp->skb->len)
  438. break;
  439. /* prepare headers for next round */
  440. if (eddp->skb->protocol == ETH_P_IP)
  441. eddp->nh.ip4.h.id++;
  442. eddp->th.tcp.h.seq += data_len;
  443. }
  444. }
  445. static inline int
  446. qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
  447. struct sk_buff *skb, struct qeth_hdr *qhdr)
  448. {
  449. struct qeth_eddp_data *eddp = NULL;
  450. QETH_DBF_TEXT(trace, 5, "eddpficx");
  451. /* create our segmentation headers and copy original headers */
  452. if (skb->protocol == ETH_P_IP)
  453. eddp = qeth_eddp_create_eddp_data(qhdr, (u8 *)skb->nh.iph,
  454. skb->nh.iph->ihl*4,
  455. (u8 *)skb->h.th, skb->h.th->doff*4);
  456. else
  457. eddp = qeth_eddp_create_eddp_data(qhdr, (u8 *)skb->nh.ipv6h,
  458. sizeof(struct ipv6hdr),
  459. (u8 *)skb->h.th, skb->h.th->doff*4);
  460. if (eddp == NULL) {
  461. QETH_DBF_TEXT(trace, 2, "eddpfcnm");
  462. return -ENOMEM;
  463. }
  464. if (qhdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
  465. memcpy(&eddp->mac, eth_hdr(skb), ETH_HLEN);
  466. #ifdef CONFIG_QETH_VLAN
  467. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)) {
  468. eddp->vlan[0] = __constant_htons(skb->protocol);
  469. eddp->vlan[1] = htons(vlan_tx_tag_get(skb));
  470. }
  471. #endif /* CONFIG_QETH_VLAN */
  472. }
  473. /* the next flags will only be set on the last segment */
  474. eddp->th.tcp.h.fin = 0;
  475. eddp->th.tcp.h.psh = 0;
  476. eddp->skb = skb;
  477. /* begin segmentation and fill context */
  478. __qeth_eddp_fill_context_tcp(ctx, eddp);
  479. kfree(eddp);
  480. return 0;
  481. }
  482. static inline void
  483. qeth_eddp_calc_num_pages(struct qeth_eddp_context *ctx, struct sk_buff *skb,
  484. int hdr_len)
  485. {
  486. int skbs_per_page;
  487. QETH_DBF_TEXT(trace, 5, "eddpcanp");
  488. /* can we put multiple skbs in one page? */
  489. skbs_per_page = PAGE_SIZE / (skb_shinfo(skb)->tso_size + hdr_len);
  490. if (skbs_per_page > 1){
  491. ctx->num_pages = (skb_shinfo(skb)->tso_segs + 1) /
  492. skbs_per_page + 1;
  493. ctx->elements_per_skb = 1;
  494. } else {
  495. /* no -> how many elements per skb? */
  496. ctx->elements_per_skb = (skb_shinfo(skb)->tso_size + hdr_len +
  497. PAGE_SIZE) >> PAGE_SHIFT;
  498. ctx->num_pages = ctx->elements_per_skb *
  499. (skb_shinfo(skb)->tso_segs + 1);
  500. }
  501. ctx->num_elements = ctx->elements_per_skb *
  502. (skb_shinfo(skb)->tso_segs + 1);
  503. }
  504. static inline struct qeth_eddp_context *
  505. qeth_eddp_create_context_generic(struct qeth_card *card, struct sk_buff *skb,
  506. int hdr_len)
  507. {
  508. struct qeth_eddp_context *ctx = NULL;
  509. u8 *addr;
  510. int i;
  511. QETH_DBF_TEXT(trace, 5, "creddpcg");
  512. /* create the context and allocate pages */
  513. ctx = kmalloc(sizeof(struct qeth_eddp_context), GFP_ATOMIC);
  514. if (ctx == NULL){
  515. QETH_DBF_TEXT(trace, 2, "ceddpcn1");
  516. return NULL;
  517. }
  518. memset(ctx, 0, sizeof(struct qeth_eddp_context));
  519. ctx->type = QETH_LARGE_SEND_EDDP;
  520. qeth_eddp_calc_num_pages(ctx, skb, hdr_len);
  521. if (ctx->elements_per_skb > QETH_MAX_BUFFER_ELEMENTS(card)){
  522. QETH_DBF_TEXT(trace, 2, "ceddpcis");
  523. kfree(ctx);
  524. return NULL;
  525. }
  526. ctx->pages = kmalloc(ctx->num_pages * sizeof(u8 *), GFP_ATOMIC);
  527. if (ctx->pages == NULL){
  528. QETH_DBF_TEXT(trace, 2, "ceddpcn2");
  529. kfree(ctx);
  530. return NULL;
  531. }
  532. memset(ctx->pages, 0, ctx->num_pages * sizeof(u8 *));
  533. for (i = 0; i < ctx->num_pages; ++i){
  534. addr = (u8 *)__get_free_page(GFP_ATOMIC);
  535. if (addr == NULL){
  536. QETH_DBF_TEXT(trace, 2, "ceddpcn3");
  537. ctx->num_pages = i;
  538. qeth_eddp_free_context(ctx);
  539. return NULL;
  540. }
  541. memset(addr, 0, PAGE_SIZE);
  542. ctx->pages[i] = addr;
  543. }
  544. ctx->elements = kmalloc(ctx->num_elements *
  545. sizeof(struct qeth_eddp_element), GFP_ATOMIC);
  546. if (ctx->elements == NULL){
  547. QETH_DBF_TEXT(trace, 2, "ceddpcn4");
  548. qeth_eddp_free_context(ctx);
  549. return NULL;
  550. }
  551. memset(ctx->elements, 0,
  552. ctx->num_elements * sizeof(struct qeth_eddp_element));
  553. /* reset num_elements; will be incremented again in fill_buffer to
  554. * reflect number of actually used elements */
  555. ctx->num_elements = 0;
  556. return ctx;
  557. }
  558. static inline struct qeth_eddp_context *
  559. qeth_eddp_create_context_tcp(struct qeth_card *card, struct sk_buff *skb,
  560. struct qeth_hdr *qhdr)
  561. {
  562. struct qeth_eddp_context *ctx = NULL;
  563. QETH_DBF_TEXT(trace, 5, "creddpct");
  564. if (skb->protocol == ETH_P_IP)
  565. ctx = qeth_eddp_create_context_generic(card, skb,
  566. sizeof(struct qeth_hdr) + skb->nh.iph->ihl*4 +
  567. skb->h.th->doff*4);
  568. else if (skb->protocol == ETH_P_IPV6)
  569. ctx = qeth_eddp_create_context_generic(card, skb,
  570. sizeof(struct qeth_hdr) + sizeof(struct ipv6hdr) +
  571. skb->h.th->doff*4);
  572. else
  573. QETH_DBF_TEXT(trace, 2, "cetcpinv");
  574. if (ctx == NULL) {
  575. QETH_DBF_TEXT(trace, 2, "creddpnl");
  576. return NULL;
  577. }
  578. if (qeth_eddp_fill_context_tcp(ctx, skb, qhdr)){
  579. QETH_DBF_TEXT(trace, 2, "ceddptfe");
  580. qeth_eddp_free_context(ctx);
  581. return NULL;
  582. }
  583. atomic_set(&ctx->refcnt, 1);
  584. return ctx;
  585. }
  586. struct qeth_eddp_context *
  587. qeth_eddp_create_context(struct qeth_card *card, struct sk_buff *skb,
  588. struct qeth_hdr *qhdr)
  589. {
  590. QETH_DBF_TEXT(trace, 5, "creddpc");
  591. switch (skb->sk->sk_protocol){
  592. case IPPROTO_TCP:
  593. return qeth_eddp_create_context_tcp(card, skb, qhdr);
  594. default:
  595. QETH_DBF_TEXT(trace, 2, "eddpinvp");
  596. }
  597. return NULL;
  598. }