qeth_eddp.c 18 KB

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