qeth_eddp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. if (ctx->elements[element].length != 0) {
  147. buffer->element[buf->next_element_to_fill].
  148. addr = ctx->elements[element].addr;
  149. buffer->element[buf->next_element_to_fill].
  150. length = ctx->elements[element].length;
  151. buffer->element[buf->next_element_to_fill].
  152. flags = ctx->elements[element].flags;
  153. buf->next_element_to_fill++;
  154. }
  155. element++;
  156. elements--;
  157. }
  158. }
  159. out_check:
  160. if (!queue->do_pack) {
  161. QETH_DBF_TEXT(trace, 6, "fillbfnp");
  162. /* set state to PRIMED -> will be flushed */
  163. if (buf->next_element_to_fill > 0){
  164. atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
  165. flush_cnt++;
  166. }
  167. } else {
  168. if (queue->card->options.performance_stats)
  169. queue->card->perf_stats.skbs_sent_pack++;
  170. QETH_DBF_TEXT(trace, 6, "fillbfpa");
  171. if (buf->next_element_to_fill >=
  172. QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
  173. /*
  174. * packed buffer if full -> set state PRIMED
  175. * -> will be flushed
  176. */
  177. atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
  178. flush_cnt++;
  179. }
  180. }
  181. out:
  182. return flush_cnt;
  183. }
  184. static void
  185. qeth_eddp_create_segment_hdrs(struct qeth_eddp_context *ctx,
  186. struct qeth_eddp_data *eddp, int data_len)
  187. {
  188. u8 *page;
  189. int page_remainder;
  190. int page_offset;
  191. int pkt_len;
  192. struct qeth_eddp_element *element;
  193. QETH_DBF_TEXT(trace, 5, "eddpcrsh");
  194. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  195. page_offset = ctx->offset % PAGE_SIZE;
  196. element = &ctx->elements[ctx->num_elements];
  197. pkt_len = eddp->nhl + eddp->thl + data_len;
  198. /* FIXME: layer2 and VLAN !!! */
  199. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
  200. pkt_len += ETH_HLEN;
  201. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
  202. pkt_len += VLAN_HLEN;
  203. /* does complete packet fit in current page ? */
  204. page_remainder = PAGE_SIZE - page_offset;
  205. if (page_remainder < (sizeof(struct qeth_hdr) + pkt_len)){
  206. /* no -> go to start of next page */
  207. ctx->offset += page_remainder;
  208. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  209. page_offset = 0;
  210. }
  211. memcpy(page + page_offset, &eddp->qh, sizeof(struct qeth_hdr));
  212. element->addr = page + page_offset;
  213. element->length = sizeof(struct qeth_hdr);
  214. ctx->offset += sizeof(struct qeth_hdr);
  215. page_offset += sizeof(struct qeth_hdr);
  216. /* add mac header (?) */
  217. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2){
  218. memcpy(page + page_offset, &eddp->mac, ETH_HLEN);
  219. element->length += ETH_HLEN;
  220. ctx->offset += ETH_HLEN;
  221. page_offset += ETH_HLEN;
  222. }
  223. /* add VLAN tag */
  224. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)){
  225. memcpy(page + page_offset, &eddp->vlan, VLAN_HLEN);
  226. element->length += VLAN_HLEN;
  227. ctx->offset += VLAN_HLEN;
  228. page_offset += VLAN_HLEN;
  229. }
  230. /* add network header */
  231. memcpy(page + page_offset, (u8 *)&eddp->nh, eddp->nhl);
  232. element->length += eddp->nhl;
  233. eddp->nh_in_ctx = page + page_offset;
  234. ctx->offset += eddp->nhl;
  235. page_offset += eddp->nhl;
  236. /* add transport header */
  237. memcpy(page + page_offset, (u8 *)&eddp->th, eddp->thl);
  238. element->length += eddp->thl;
  239. eddp->th_in_ctx = page + page_offset;
  240. ctx->offset += eddp->thl;
  241. }
  242. static void
  243. qeth_eddp_copy_data_tcp(char *dst, struct qeth_eddp_data *eddp, int len,
  244. __wsum *hcsum)
  245. {
  246. struct skb_frag_struct *frag;
  247. int left_in_frag;
  248. int copy_len;
  249. u8 *src;
  250. QETH_DBF_TEXT(trace, 5, "eddpcdtc");
  251. if (skb_shinfo(eddp->skb)->nr_frags == 0) {
  252. skb_copy_from_linear_data_offset(eddp->skb, eddp->skb_offset,
  253. dst, len);
  254. *hcsum = csum_partial(eddp->skb->data + eddp->skb_offset, len,
  255. *hcsum);
  256. eddp->skb_offset += len;
  257. } else {
  258. while (len > 0) {
  259. if (eddp->frag < 0) {
  260. /* we're in skb->data */
  261. left_in_frag = (eddp->skb->len - eddp->skb->data_len)
  262. - eddp->skb_offset;
  263. src = eddp->skb->data + eddp->skb_offset;
  264. } else {
  265. frag = &skb_shinfo(eddp->skb)->
  266. frags[eddp->frag];
  267. left_in_frag = frag->size - eddp->frag_offset;
  268. src = (u8 *)(
  269. (page_to_pfn(frag->page) << PAGE_SHIFT)+
  270. frag->page_offset + eddp->frag_offset);
  271. }
  272. if (left_in_frag <= 0) {
  273. eddp->frag++;
  274. eddp->frag_offset = 0;
  275. continue;
  276. }
  277. copy_len = min(left_in_frag, len);
  278. memcpy(dst, src, copy_len);
  279. *hcsum = csum_partial(src, copy_len, *hcsum);
  280. dst += copy_len;
  281. eddp->frag_offset += copy_len;
  282. eddp->skb_offset += copy_len;
  283. len -= copy_len;
  284. }
  285. }
  286. }
  287. static void
  288. qeth_eddp_create_segment_data_tcp(struct qeth_eddp_context *ctx,
  289. struct qeth_eddp_data *eddp, int data_len,
  290. __wsum hcsum)
  291. {
  292. u8 *page;
  293. int page_remainder;
  294. int page_offset;
  295. struct qeth_eddp_element *element;
  296. int first_lap = 1;
  297. QETH_DBF_TEXT(trace, 5, "eddpcsdt");
  298. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  299. page_offset = ctx->offset % PAGE_SIZE;
  300. element = &ctx->elements[ctx->num_elements];
  301. while (data_len){
  302. page_remainder = PAGE_SIZE - page_offset;
  303. if (page_remainder < data_len){
  304. qeth_eddp_copy_data_tcp(page + page_offset, eddp,
  305. page_remainder, &hcsum);
  306. element->length += page_remainder;
  307. if (first_lap)
  308. element->flags = SBAL_FLAGS_FIRST_FRAG;
  309. else
  310. element->flags = SBAL_FLAGS_MIDDLE_FRAG;
  311. ctx->num_elements++;
  312. element++;
  313. data_len -= page_remainder;
  314. ctx->offset += page_remainder;
  315. page = ctx->pages[ctx->offset >> PAGE_SHIFT];
  316. page_offset = 0;
  317. element->addr = page + page_offset;
  318. } else {
  319. qeth_eddp_copy_data_tcp(page + page_offset, eddp,
  320. data_len, &hcsum);
  321. element->length += data_len;
  322. if (!first_lap)
  323. element->flags = SBAL_FLAGS_LAST_FRAG;
  324. ctx->num_elements++;
  325. ctx->offset += data_len;
  326. data_len = 0;
  327. }
  328. first_lap = 0;
  329. }
  330. ((struct tcphdr *)eddp->th_in_ctx)->check = csum_fold(hcsum);
  331. }
  332. static __wsum
  333. qeth_eddp_check_tcp4_hdr(struct qeth_eddp_data *eddp, int data_len)
  334. {
  335. __wsum phcsum; /* pseudo header checksum */
  336. QETH_DBF_TEXT(trace, 5, "eddpckt4");
  337. eddp->th.tcp.h.check = 0;
  338. /* compute pseudo header checksum */
  339. phcsum = csum_tcpudp_nofold(eddp->nh.ip4.h.saddr, eddp->nh.ip4.h.daddr,
  340. eddp->thl + data_len, IPPROTO_TCP, 0);
  341. /* compute checksum of tcp header */
  342. return csum_partial((u8 *)&eddp->th, eddp->thl, phcsum);
  343. }
  344. static __wsum
  345. qeth_eddp_check_tcp6_hdr(struct qeth_eddp_data *eddp, int data_len)
  346. {
  347. __be32 proto;
  348. __wsum phcsum; /* pseudo header checksum */
  349. QETH_DBF_TEXT(trace, 5, "eddpckt6");
  350. eddp->th.tcp.h.check = 0;
  351. /* compute pseudo header checksum */
  352. phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.saddr,
  353. sizeof(struct in6_addr), 0);
  354. phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.daddr,
  355. sizeof(struct in6_addr), phcsum);
  356. proto = htonl(IPPROTO_TCP);
  357. phcsum = csum_partial((u8 *)&proto, sizeof(u32), phcsum);
  358. return phcsum;
  359. }
  360. static struct qeth_eddp_data *
  361. qeth_eddp_create_eddp_data(struct qeth_hdr *qh, u8 *nh, u8 nhl, u8 *th, u8 thl)
  362. {
  363. struct qeth_eddp_data *eddp;
  364. QETH_DBF_TEXT(trace, 5, "eddpcrda");
  365. eddp = kzalloc(sizeof(struct qeth_eddp_data), GFP_ATOMIC);
  366. if (eddp){
  367. eddp->nhl = nhl;
  368. eddp->thl = thl;
  369. memcpy(&eddp->qh, qh, sizeof(struct qeth_hdr));
  370. memcpy(&eddp->nh, nh, nhl);
  371. memcpy(&eddp->th, th, thl);
  372. eddp->frag = -1; /* initially we're in skb->data */
  373. }
  374. return eddp;
  375. }
  376. static void
  377. __qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
  378. struct qeth_eddp_data *eddp)
  379. {
  380. struct tcphdr *tcph;
  381. int data_len;
  382. __wsum hcsum;
  383. QETH_DBF_TEXT(trace, 5, "eddpftcp");
  384. eddp->skb_offset = sizeof(struct qeth_hdr) + eddp->nhl + eddp->thl;
  385. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
  386. eddp->skb_offset += sizeof(struct ethhdr);
  387. #ifdef CONFIG_QETH_VLAN
  388. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
  389. eddp->skb_offset += VLAN_HLEN;
  390. #endif /* CONFIG_QETH_VLAN */
  391. }
  392. tcph = tcp_hdr(eddp->skb);
  393. while (eddp->skb_offset < eddp->skb->len) {
  394. data_len = min((int)skb_shinfo(eddp->skb)->gso_size,
  395. (int)(eddp->skb->len - eddp->skb_offset));
  396. /* prepare qdio hdr */
  397. if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2){
  398. eddp->qh.hdr.l2.pkt_length = data_len + ETH_HLEN +
  399. eddp->nhl + eddp->thl;
  400. #ifdef CONFIG_QETH_VLAN
  401. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
  402. eddp->qh.hdr.l2.pkt_length += VLAN_HLEN;
  403. #endif /* CONFIG_QETH_VLAN */
  404. } else
  405. eddp->qh.hdr.l3.length = data_len + eddp->nhl +
  406. eddp->thl;
  407. /* prepare ip hdr */
  408. if (eddp->skb->protocol == htons(ETH_P_IP)){
  409. eddp->nh.ip4.h.tot_len = htons(data_len + eddp->nhl +
  410. eddp->thl);
  411. eddp->nh.ip4.h.check = 0;
  412. eddp->nh.ip4.h.check =
  413. ip_fast_csum((u8 *)&eddp->nh.ip4.h,
  414. eddp->nh.ip4.h.ihl);
  415. } else
  416. eddp->nh.ip6.h.payload_len = htons(data_len + eddp->thl);
  417. /* prepare tcp hdr */
  418. if (data_len == (eddp->skb->len - eddp->skb_offset)){
  419. /* last segment -> set FIN and PSH flags */
  420. eddp->th.tcp.h.fin = tcph->fin;
  421. eddp->th.tcp.h.psh = tcph->psh;
  422. }
  423. if (eddp->skb->protocol == htons(ETH_P_IP))
  424. hcsum = qeth_eddp_check_tcp4_hdr(eddp, data_len);
  425. else
  426. hcsum = qeth_eddp_check_tcp6_hdr(eddp, data_len);
  427. /* fill the next segment into the context */
  428. qeth_eddp_create_segment_hdrs(ctx, eddp, data_len);
  429. qeth_eddp_create_segment_data_tcp(ctx, eddp, data_len, hcsum);
  430. if (eddp->skb_offset >= eddp->skb->len)
  431. break;
  432. /* prepare headers for next round */
  433. if (eddp->skb->protocol == htons(ETH_P_IP))
  434. eddp->nh.ip4.h.id = htons(ntohs(eddp->nh.ip4.h.id) + 1);
  435. eddp->th.tcp.h.seq = htonl(ntohl(eddp->th.tcp.h.seq) + data_len);
  436. }
  437. }
  438. static int
  439. qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
  440. struct sk_buff *skb, struct qeth_hdr *qhdr)
  441. {
  442. struct qeth_eddp_data *eddp = NULL;
  443. QETH_DBF_TEXT(trace, 5, "eddpficx");
  444. /* create our segmentation headers and copy original headers */
  445. if (skb->protocol == htons(ETH_P_IP))
  446. eddp = qeth_eddp_create_eddp_data(qhdr,
  447. skb_network_header(skb),
  448. ip_hdrlen(skb),
  449. skb_transport_header(skb),
  450. tcp_hdrlen(skb));
  451. else
  452. eddp = qeth_eddp_create_eddp_data(qhdr,
  453. skb_network_header(skb),
  454. sizeof(struct ipv6hdr),
  455. skb_transport_header(skb),
  456. tcp_hdrlen(skb));
  457. if (eddp == NULL) {
  458. QETH_DBF_TEXT(trace, 2, "eddpfcnm");
  459. return -ENOMEM;
  460. }
  461. if (qhdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
  462. skb_set_mac_header(skb, sizeof(struct qeth_hdr));
  463. memcpy(&eddp->mac, eth_hdr(skb), ETH_HLEN);
  464. #ifdef CONFIG_QETH_VLAN
  465. if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)) {
  466. eddp->vlan[0] = skb->protocol;
  467. eddp->vlan[1] = htons(vlan_tx_tag_get(skb));
  468. }
  469. #endif /* CONFIG_QETH_VLAN */
  470. }
  471. /* the next flags will only be set on the last segment */
  472. eddp->th.tcp.h.fin = 0;
  473. eddp->th.tcp.h.psh = 0;
  474. eddp->skb = skb;
  475. /* begin segmentation and fill context */
  476. __qeth_eddp_fill_context_tcp(ctx, eddp);
  477. kfree(eddp);
  478. return 0;
  479. }
  480. static void
  481. qeth_eddp_calc_num_pages(struct qeth_eddp_context *ctx, struct sk_buff *skb,
  482. int hdr_len)
  483. {
  484. int skbs_per_page;
  485. QETH_DBF_TEXT(trace, 5, "eddpcanp");
  486. /* can we put multiple skbs in one page? */
  487. skbs_per_page = PAGE_SIZE / (skb_shinfo(skb)->gso_size + hdr_len);
  488. if (skbs_per_page > 1){
  489. ctx->num_pages = (skb_shinfo(skb)->gso_segs + 1) /
  490. skbs_per_page + 1;
  491. ctx->elements_per_skb = 1;
  492. } else {
  493. /* no -> how many elements per skb? */
  494. ctx->elements_per_skb = (skb_shinfo(skb)->gso_size + hdr_len +
  495. PAGE_SIZE) >> PAGE_SHIFT;
  496. ctx->num_pages = ctx->elements_per_skb *
  497. (skb_shinfo(skb)->gso_segs + 1);
  498. }
  499. ctx->num_elements = ctx->elements_per_skb *
  500. (skb_shinfo(skb)->gso_segs + 1);
  501. }
  502. static struct qeth_eddp_context *
  503. qeth_eddp_create_context_generic(struct qeth_card *card, struct sk_buff *skb,
  504. int hdr_len)
  505. {
  506. struct qeth_eddp_context *ctx = NULL;
  507. u8 *addr;
  508. int i;
  509. QETH_DBF_TEXT(trace, 5, "creddpcg");
  510. /* create the context and allocate pages */
  511. ctx = kzalloc(sizeof(struct qeth_eddp_context), GFP_ATOMIC);
  512. if (ctx == NULL){
  513. QETH_DBF_TEXT(trace, 2, "ceddpcn1");
  514. return NULL;
  515. }
  516. ctx->type = QETH_LARGE_SEND_EDDP;
  517. qeth_eddp_calc_num_pages(ctx, skb, hdr_len);
  518. if (ctx->elements_per_skb > QETH_MAX_BUFFER_ELEMENTS(card)){
  519. QETH_DBF_TEXT(trace, 2, "ceddpcis");
  520. kfree(ctx);
  521. return NULL;
  522. }
  523. ctx->pages = kcalloc(ctx->num_pages, sizeof(u8 *), GFP_ATOMIC);
  524. if (ctx->pages == NULL){
  525. QETH_DBF_TEXT(trace, 2, "ceddpcn2");
  526. kfree(ctx);
  527. return NULL;
  528. }
  529. for (i = 0; i < ctx->num_pages; ++i){
  530. addr = (u8 *)__get_free_page(GFP_ATOMIC);
  531. if (addr == NULL){
  532. QETH_DBF_TEXT(trace, 2, "ceddpcn3");
  533. ctx->num_pages = i;
  534. qeth_eddp_free_context(ctx);
  535. return NULL;
  536. }
  537. memset(addr, 0, PAGE_SIZE);
  538. ctx->pages[i] = addr;
  539. }
  540. ctx->elements = kcalloc(ctx->num_elements,
  541. sizeof(struct qeth_eddp_element), GFP_ATOMIC);
  542. if (ctx->elements == NULL){
  543. QETH_DBF_TEXT(trace, 2, "ceddpcn4");
  544. qeth_eddp_free_context(ctx);
  545. return NULL;
  546. }
  547. /* reset num_elements; will be incremented again in fill_buffer to
  548. * reflect number of actually used elements */
  549. ctx->num_elements = 0;
  550. return ctx;
  551. }
  552. static struct qeth_eddp_context *
  553. qeth_eddp_create_context_tcp(struct qeth_card *card, struct sk_buff *skb,
  554. struct qeth_hdr *qhdr)
  555. {
  556. struct qeth_eddp_context *ctx = NULL;
  557. QETH_DBF_TEXT(trace, 5, "creddpct");
  558. if (skb->protocol == htons(ETH_P_IP))
  559. ctx = qeth_eddp_create_context_generic(card, skb,
  560. (sizeof(struct qeth_hdr) +
  561. ip_hdrlen(skb) +
  562. tcp_hdrlen(skb)));
  563. else if (skb->protocol == htons(ETH_P_IPV6))
  564. ctx = qeth_eddp_create_context_generic(card, skb,
  565. sizeof(struct qeth_hdr) + sizeof(struct ipv6hdr) +
  566. tcp_hdrlen(skb));
  567. else
  568. QETH_DBF_TEXT(trace, 2, "cetcpinv");
  569. if (ctx == NULL) {
  570. QETH_DBF_TEXT(trace, 2, "creddpnl");
  571. return NULL;
  572. }
  573. if (qeth_eddp_fill_context_tcp(ctx, skb, qhdr)){
  574. QETH_DBF_TEXT(trace, 2, "ceddptfe");
  575. qeth_eddp_free_context(ctx);
  576. return NULL;
  577. }
  578. atomic_set(&ctx->refcnt, 1);
  579. return ctx;
  580. }
  581. struct qeth_eddp_context *
  582. qeth_eddp_create_context(struct qeth_card *card, struct sk_buff *skb,
  583. struct qeth_hdr *qhdr, unsigned char sk_protocol)
  584. {
  585. QETH_DBF_TEXT(trace, 5, "creddpc");
  586. switch (sk_protocol) {
  587. case IPPROTO_TCP:
  588. return qeth_eddp_create_context_tcp(card, skb, qhdr);
  589. default:
  590. QETH_DBF_TEXT(trace, 2, "eddpinvp");
  591. }
  592. return NULL;
  593. }