]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/cxgb3i/cxgb3i_ddp.c
[SCSI] cxgb3i: re-initialize ddp settings after chip reset
[linux-2.6-omap-h63xx.git] / drivers / scsi / cxgb3i / cxgb3i_ddp.c
1 /*
2  * cxgb3i_ddp.c: Chelsio S3xx iSCSI DDP Manager.
3  *
4  * Copyright (c) 2008 Chelsio Communications, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  *
10  * Written by: Karen Xie (kxie@chelsio.com)
11  */
12
13 #include <linux/skbuff.h>
14 #include <linux/scatterlist.h>
15
16 /* from cxgb3 LLD */
17 #include "common.h"
18 #include "t3_cpl.h"
19 #include "t3cdev.h"
20 #include "cxgb3_ctl_defs.h"
21 #include "cxgb3_offload.h"
22 #include "firmware_exports.h"
23
24 #include "cxgb3i_ddp.h"
25
26 #define DRV_MODULE_NAME         "cxgb3i_ddp"
27 #define DRV_MODULE_VERSION      "1.0.0"
28 #define DRV_MODULE_RELDATE      "Dec. 1, 2008"
29
30 static char version[] =
31         "Chelsio S3xx iSCSI DDP " DRV_MODULE_NAME
32         " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
33
34 MODULE_AUTHOR("Karen Xie <kxie@chelsio.com>");
35 MODULE_DESCRIPTION("cxgb3i ddp pagepod manager");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
38
39 #define ddp_log_error(fmt...) printk(KERN_ERR "cxgb3i_ddp: ERR! " fmt)
40 #define ddp_log_warn(fmt...)  printk(KERN_WARNING "cxgb3i_ddp: WARN! " fmt)
41 #define ddp_log_info(fmt...)  printk(KERN_INFO "cxgb3i_ddp: " fmt)
42
43 #ifdef __DEBUG_CXGB3I_DDP__
44 #define ddp_log_debug(fmt, args...) \
45         printk(KERN_INFO "cxgb3i_ddp: %s - " fmt, __func__ , ## args)
46 #else
47 #define ddp_log_debug(fmt...)
48 #endif
49
50 /*
51  * iSCSI Direct Data Placement
52  *
53  * T3 h/w can directly place the iSCSI Data-In or Data-Out PDU's payload into
54  * pre-posted final destination host-memory buffers based on the Initiator
55  * Task Tag (ITT) in Data-In or Target Task Tag (TTT) in Data-Out PDUs.
56  *
57  * The host memory address is programmed into h/w in the format of pagepod
58  * entries.
59  * The location of the pagepod entry is encoded into ddp tag which is used or
60  * is the base for ITT/TTT.
61  */
62
63 #define DDP_PGIDX_MAX           4
64 #define DDP_THRESHOLD   2048
65 static unsigned char ddp_page_order[DDP_PGIDX_MAX] = {0, 1, 2, 4};
66 static unsigned char ddp_page_shift[DDP_PGIDX_MAX] = {12, 13, 14, 16};
67 static unsigned char page_idx = DDP_PGIDX_MAX;
68
69 /*
70  * functions to program the pagepod in h/w
71  */
72 static inline void ulp_mem_io_set_hdr(struct sk_buff *skb, unsigned int addr)
73 {
74         struct ulp_mem_io *req = (struct ulp_mem_io *)skb->head;
75
76         req->wr.wr_lo = 0;
77         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_BYPASS));
78         req->cmd_lock_addr = htonl(V_ULP_MEMIO_ADDR(addr >> 5) |
79                                    V_ULPTX_CMD(ULP_MEM_WRITE));
80         req->len = htonl(V_ULP_MEMIO_DATA_LEN(PPOD_SIZE >> 5) |
81                          V_ULPTX_NFLITS((PPOD_SIZE >> 3) + 1));
82 }
83
84 static int set_ddp_map(struct cxgb3i_ddp_info *ddp, struct pagepod_hdr *hdr,
85                        unsigned int idx, unsigned int npods,
86                        struct cxgb3i_gather_list *gl)
87 {
88         unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ddp->llimit;
89         int i;
90
91         for (i = 0; i < npods; i++, idx++, pm_addr += PPOD_SIZE) {
92                 struct sk_buff *skb = ddp->gl_skb[idx];
93                 struct pagepod *ppod;
94                 int j, pidx;
95
96                 /* hold on to the skb until we clear the ddp mapping */
97                 skb_get(skb);
98
99                 ulp_mem_io_set_hdr(skb, pm_addr);
100                 ppod = (struct pagepod *)
101                        (skb->head + sizeof(struct ulp_mem_io));
102                 memcpy(&(ppod->hdr), hdr, sizeof(struct pagepod));
103                 for (pidx = 4 * i, j = 0; j < 5; ++j, ++pidx)
104                         ppod->addr[j] = pidx < gl->nelem ?
105                                      cpu_to_be64(gl->phys_addr[pidx]) : 0UL;
106
107                 skb->priority = CPL_PRIORITY_CONTROL;
108                 cxgb3_ofld_send(ddp->tdev, skb);
109         }
110         return 0;
111 }
112
113 static void clear_ddp_map(struct cxgb3i_ddp_info *ddp, unsigned int tag,
114                          unsigned int idx, unsigned int npods)
115 {
116         unsigned int pm_addr = (idx << PPOD_SIZE_SHIFT) + ddp->llimit;
117         int i;
118
119         for (i = 0; i < npods; i++, idx++, pm_addr += PPOD_SIZE) {
120                 struct sk_buff *skb = ddp->gl_skb[idx];
121
122                 if (!skb) {
123                         ddp_log_error("ddp tag 0x%x, 0x%x, %d/%u, skb NULL.\n",
124                                         tag, idx, i, npods);
125                         continue;
126                 }
127                 ddp->gl_skb[idx] = NULL;
128                 memset((skb->head + sizeof(struct ulp_mem_io)), 0, PPOD_SIZE);
129                 ulp_mem_io_set_hdr(skb, pm_addr);
130                 skb->priority = CPL_PRIORITY_CONTROL;
131                 cxgb3_ofld_send(ddp->tdev, skb);
132         }
133 }
134
135 static inline int ddp_find_unused_entries(struct cxgb3i_ddp_info *ddp,
136                                           int start, int max, int count,
137                                           struct cxgb3i_gather_list *gl)
138 {
139         unsigned int i, j;
140
141         spin_lock(&ddp->map_lock);
142         for (i = start; i <= max;) {
143                 for (j = 0; j < count; j++) {
144                         if (ddp->gl_map[i + j])
145                                 break;
146                 }
147                 if (j == count) {
148                         for (j = 0; j < count; j++)
149                                 ddp->gl_map[i + j] = gl;
150                         spin_unlock(&ddp->map_lock);
151                         return i;
152                 }
153                 i += j + 1;
154         }
155         spin_unlock(&ddp->map_lock);
156         return -EBUSY;
157 }
158
159 static inline void ddp_unmark_entries(struct cxgb3i_ddp_info *ddp,
160                                       int start, int count)
161 {
162         spin_lock(&ddp->map_lock);
163         memset(&ddp->gl_map[start], 0,
164                count * sizeof(struct cxgb3i_gather_list *));
165         spin_unlock(&ddp->map_lock);
166 }
167
168 static inline void ddp_free_gl_skb(struct cxgb3i_ddp_info *ddp,
169                                    int idx, int count)
170 {
171         int i;
172
173         for (i = 0; i < count; i++, idx++)
174                 if (ddp->gl_skb[idx]) {
175                         kfree_skb(ddp->gl_skb[idx]);
176                         ddp->gl_skb[idx] = NULL;
177                 }
178 }
179
180 static inline int ddp_alloc_gl_skb(struct cxgb3i_ddp_info *ddp, int idx,
181                                    int count, gfp_t gfp)
182 {
183         int i;
184
185         for (i = 0; i < count; i++) {
186                 struct sk_buff *skb = alloc_skb(sizeof(struct ulp_mem_io) +
187                                                 PPOD_SIZE, gfp);
188                 if (skb) {
189                         ddp->gl_skb[idx + i] = skb;
190                         skb_put(skb, sizeof(struct ulp_mem_io) + PPOD_SIZE);
191                 } else {
192                         ddp_free_gl_skb(ddp, idx, i);
193                         return -ENOMEM;
194                 }
195         }
196         return 0;
197 }
198
199 /**
200  * cxgb3i_ddp_find_page_index - return ddp page index for a given page size
201  * @pgsz: page size
202  * return the ddp page index, if no match is found return DDP_PGIDX_MAX.
203  */
204 int cxgb3i_ddp_find_page_index(unsigned long pgsz)
205 {
206         int i;
207
208         for (i = 0; i < DDP_PGIDX_MAX; i++) {
209                 if (pgsz == (1UL << ddp_page_shift[i]))
210                         return i;
211         }
212         ddp_log_debug("ddp page size 0x%lx not supported.\n", pgsz);
213         return DDP_PGIDX_MAX;
214 }
215 EXPORT_SYMBOL_GPL(cxgb3i_ddp_find_page_index);
216
217 static inline void ddp_gl_unmap(struct pci_dev *pdev,
218                                 struct cxgb3i_gather_list *gl)
219 {
220         int i;
221
222         for (i = 0; i < gl->nelem; i++)
223                 pci_unmap_page(pdev, gl->phys_addr[i], PAGE_SIZE,
224                                PCI_DMA_FROMDEVICE);
225 }
226
227 static inline int ddp_gl_map(struct pci_dev *pdev,
228                              struct cxgb3i_gather_list *gl)
229 {
230         int i;
231
232         for (i = 0; i < gl->nelem; i++) {
233                 gl->phys_addr[i] = pci_map_page(pdev, gl->pages[i], 0,
234                                                 PAGE_SIZE,
235                                                 PCI_DMA_FROMDEVICE);
236                 if (unlikely(pci_dma_mapping_error(pdev, gl->phys_addr[i])))
237                         goto unmap;
238         }
239
240         return i;
241
242 unmap:
243         if (i) {
244                 unsigned int nelem = gl->nelem;
245
246                 gl->nelem = i;
247                 ddp_gl_unmap(pdev, gl);
248                 gl->nelem = nelem;
249         }
250         return -ENOMEM;
251 }
252
253 /**
254  * cxgb3i_ddp_make_gl - build ddp page buffer list
255  * @xferlen: total buffer length
256  * @sgl: page buffer scatter-gather list
257  * @sgcnt: # of page buffers
258  * @pdev: pci_dev, used for pci map
259  * @gfp: allocation mode
260  *
261  * construct a ddp page buffer list from the scsi scattergather list.
262  * coalesce buffers as much as possible, and obtain dma addresses for
263  * each page.
264  *
265  * Return the cxgb3i_gather_list constructed from the page buffers if the
266  * memory can be used for ddp. Return NULL otherwise.
267  */
268 struct cxgb3i_gather_list *cxgb3i_ddp_make_gl(unsigned int xferlen,
269                                               struct scatterlist *sgl,
270                                               unsigned int sgcnt,
271                                               struct pci_dev *pdev,
272                                               gfp_t gfp)
273 {
274         struct cxgb3i_gather_list *gl;
275         struct scatterlist *sg = sgl;
276         struct page *sgpage = sg_page(sg);
277         unsigned int sglen = sg->length;
278         unsigned int sgoffset = sg->offset;
279         unsigned int npages = (xferlen + sgoffset + PAGE_SIZE - 1) >>
280                               PAGE_SHIFT;
281         int i = 1, j = 0;
282
283         if (xferlen < DDP_THRESHOLD) {
284                 ddp_log_debug("xfer %u < threshold %u, no ddp.\n",
285                               xferlen, DDP_THRESHOLD);
286                 return NULL;
287         }
288
289         gl = kzalloc(sizeof(struct cxgb3i_gather_list) +
290                      npages * (sizeof(dma_addr_t) + sizeof(struct page *)),
291                      gfp);
292         if (!gl)
293                 return NULL;
294
295         gl->pages = (struct page **)&gl->phys_addr[npages];
296         gl->length = xferlen;
297         gl->offset = sgoffset;
298         gl->pages[0] = sgpage;
299
300         sg = sg_next(sg);
301         while (sg) {
302                 struct page *page = sg_page(sg);
303
304                 if (sgpage == page && sg->offset == sgoffset + sglen)
305                         sglen += sg->length;
306                 else {
307                         /* make sure the sgl is fit for ddp:
308                          * each has the same page size, and
309                          * all of the middle pages are used completely
310                          */
311                         if ((j && sgoffset) ||
312                             ((i != sgcnt - 1) &&
313                              ((sglen + sgoffset) & ~PAGE_MASK)))
314                                 goto error_out;
315
316                         j++;
317                         if (j == gl->nelem || sg->offset)
318                                 goto error_out;
319                         gl->pages[j] = page;
320                         sglen = sg->length;
321                         sgoffset = sg->offset;
322                         sgpage = page;
323                 }
324                 i++;
325                 sg = sg_next(sg);
326         }
327         gl->nelem = ++j;
328
329         if (ddp_gl_map(pdev, gl) < 0)
330                 goto error_out;
331
332         return gl;
333
334 error_out:
335         kfree(gl);
336         return NULL;
337 }
338 EXPORT_SYMBOL_GPL(cxgb3i_ddp_make_gl);
339
340 /**
341  * cxgb3i_ddp_release_gl - release a page buffer list
342  * @gl: a ddp page buffer list
343  * @pdev: pci_dev used for pci_unmap
344  * free a ddp page buffer list resulted from cxgb3i_ddp_make_gl().
345  */
346 void cxgb3i_ddp_release_gl(struct cxgb3i_gather_list *gl,
347                            struct pci_dev *pdev)
348 {
349         ddp_gl_unmap(pdev, gl);
350         kfree(gl);
351 }
352 EXPORT_SYMBOL_GPL(cxgb3i_ddp_release_gl);
353
354 /**
355  * cxgb3i_ddp_tag_reserve - set up ddp for a data transfer
356  * @tdev: t3cdev adapter
357  * @tid: connection id
358  * @tformat: tag format
359  * @tagp: contains s/w tag initially, will be updated with ddp/hw tag
360  * @gl: the page momory list
361  * @gfp: allocation mode
362  *
363  * ddp setup for a given page buffer list and construct the ddp tag.
364  * return 0 if success, < 0 otherwise.
365  */
366 int cxgb3i_ddp_tag_reserve(struct t3cdev *tdev, unsigned int tid,
367                            struct cxgb3i_tag_format *tformat, u32 *tagp,
368                            struct cxgb3i_gather_list *gl, gfp_t gfp)
369 {
370         struct cxgb3i_ddp_info *ddp = tdev->ulp_iscsi;
371         struct pagepod_hdr hdr;
372         unsigned int npods;
373         int idx = -1, idx_max;
374         int err = -ENOMEM;
375         u32 sw_tag = *tagp;
376         u32 tag;
377
378         if (page_idx >= DDP_PGIDX_MAX || !ddp || !gl || !gl->nelem ||
379                 gl->length < DDP_THRESHOLD) {
380                 ddp_log_debug("pgidx %u, xfer %u/%u, NO ddp.\n",
381                               page_idx, gl->length, DDP_THRESHOLD);
382                 return -EINVAL;
383         }
384
385         npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
386         idx_max = ddp->nppods - npods + 1;
387
388         if (ddp->idx_last == ddp->nppods)
389                 idx = ddp_find_unused_entries(ddp, 0, idx_max, npods, gl);
390         else {
391                 idx = ddp_find_unused_entries(ddp, ddp->idx_last + 1,
392                                               idx_max, npods, gl);
393                 if (idx < 0 && ddp->idx_last >= npods)
394                         idx = ddp_find_unused_entries(ddp, 0,
395                                                       ddp->idx_last - npods + 1,
396                                                       npods, gl);
397         }
398         if (idx < 0) {
399                 ddp_log_debug("xferlen %u, gl %u, npods %u NO DDP.\n",
400                               gl->length, gl->nelem, npods);
401                 return idx;
402         }
403
404         err = ddp_alloc_gl_skb(ddp, idx, npods, gfp);
405         if (err < 0)
406                 goto unmark_entries;
407
408         tag = cxgb3i_ddp_tag_base(tformat, sw_tag);
409         tag |= idx << PPOD_IDX_SHIFT;
410
411         hdr.rsvd = 0;
412         hdr.vld_tid = htonl(F_PPOD_VALID | V_PPOD_TID(tid));
413         hdr.pgsz_tag_clr = htonl(tag & ddp->rsvd_tag_mask);
414         hdr.maxoffset = htonl(gl->length);
415         hdr.pgoffset = htonl(gl->offset);
416
417         err = set_ddp_map(ddp, &hdr, idx, npods, gl);
418         if (err < 0)
419                 goto free_gl_skb;
420
421         ddp->idx_last = idx;
422         ddp_log_debug("xfer %u, gl %u,%u, tid 0x%x, 0x%x -> 0x%x(%u,%u).\n",
423                       gl->length, gl->nelem, gl->offset, tid, sw_tag, tag,
424                       idx, npods);
425         *tagp = tag;
426         return 0;
427
428 free_gl_skb:
429         ddp_free_gl_skb(ddp, idx, npods);
430 unmark_entries:
431         ddp_unmark_entries(ddp, idx, npods);
432         return err;
433 }
434 EXPORT_SYMBOL_GPL(cxgb3i_ddp_tag_reserve);
435
436 /**
437  * cxgb3i_ddp_tag_release - release a ddp tag
438  * @tdev: t3cdev adapter
439  * @tag: ddp tag
440  * ddp cleanup for a given ddp tag and release all the resources held
441  */
442 void cxgb3i_ddp_tag_release(struct t3cdev *tdev, u32 tag)
443 {
444         struct cxgb3i_ddp_info *ddp = tdev->ulp_iscsi;
445         u32 idx;
446
447         if (!ddp) {
448                 ddp_log_error("release ddp tag 0x%x, ddp NULL.\n", tag);
449                 return;
450         }
451
452         idx = (tag >> PPOD_IDX_SHIFT) & ddp->idx_mask;
453         if (idx < ddp->nppods) {
454                 struct cxgb3i_gather_list *gl = ddp->gl_map[idx];
455                 unsigned int npods;
456
457                 if (!gl || !gl->nelem) {
458                         ddp_log_error("release 0x%x, idx 0x%x, gl 0x%p, %u.\n",
459                                       tag, idx, gl, gl ? gl->nelem : 0);
460                         return;
461                 }
462                 npods = (gl->nelem + PPOD_PAGES_MAX - 1) >> PPOD_PAGES_SHIFT;
463                 ddp_log_debug("ddp tag 0x%x, release idx 0x%x, npods %u.\n",
464                               tag, idx, npods);
465                 clear_ddp_map(ddp, tag, idx, npods);
466                 ddp_unmark_entries(ddp, idx, npods);
467                 cxgb3i_ddp_release_gl(gl, ddp->pdev);
468         } else
469                 ddp_log_error("ddp tag 0x%x, idx 0x%x > max 0x%x.\n",
470                               tag, idx, ddp->nppods);
471 }
472 EXPORT_SYMBOL_GPL(cxgb3i_ddp_tag_release);
473
474 static int setup_conn_pgidx(struct t3cdev *tdev, unsigned int tid, int pg_idx,
475                             int reply)
476 {
477         struct sk_buff *skb = alloc_skb(sizeof(struct cpl_set_tcb_field),
478                                         GFP_KERNEL);
479         struct cpl_set_tcb_field *req;
480         u64 val = pg_idx < DDP_PGIDX_MAX ? pg_idx : 0;
481
482         if (!skb)
483                 return -ENOMEM;
484
485         /* set up ulp submode and page size */
486         req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
487         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
488         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
489         req->reply = V_NO_REPLY(reply ? 0 : 1);
490         req->cpu_idx = 0;
491         req->word = htons(31);
492         req->mask = cpu_to_be64(0xF0000000);
493         req->val = cpu_to_be64(val << 28);
494         skb->priority = CPL_PRIORITY_CONTROL;
495
496         cxgb3_ofld_send(tdev, skb);
497         return 0;
498 }
499
500 /**
501  * cxgb3i_setup_conn_host_pagesize - setup the conn.'s ddp page size
502  * @tdev: t3cdev adapter
503  * @tid: connection id
504  * @reply: request reply from h/w
505  * set up the ddp page size based on the host PAGE_SIZE for a connection
506  * identified by tid
507  */
508 int cxgb3i_setup_conn_host_pagesize(struct t3cdev *tdev, unsigned int tid,
509                                     int reply)
510 {
511         return setup_conn_pgidx(tdev, tid, page_idx, reply);
512 }
513 EXPORT_SYMBOL_GPL(cxgb3i_setup_conn_host_pagesize);
514
515 /**
516  * cxgb3i_setup_conn_pagesize - setup the conn.'s ddp page size
517  * @tdev: t3cdev adapter
518  * @tid: connection id
519  * @reply: request reply from h/w
520  * @pgsz: ddp page size
521  * set up the ddp page size for a connection identified by tid
522  */
523 int cxgb3i_setup_conn_pagesize(struct t3cdev *tdev, unsigned int tid,
524                                 int reply, unsigned long pgsz)
525 {
526         int pgidx = cxgb3i_ddp_find_page_index(pgsz);
527
528         return setup_conn_pgidx(tdev, tid, pgidx, reply);
529 }
530 EXPORT_SYMBOL_GPL(cxgb3i_setup_conn_pagesize);
531
532 /**
533  * cxgb3i_setup_conn_digest - setup conn. digest setting
534  * @tdev: t3cdev adapter
535  * @tid: connection id
536  * @hcrc: header digest enabled
537  * @dcrc: data digest enabled
538  * @reply: request reply from h/w
539  * set up the iscsi digest settings for a connection identified by tid
540  */
541 int cxgb3i_setup_conn_digest(struct t3cdev *tdev, unsigned int tid,
542                              int hcrc, int dcrc, int reply)
543 {
544         struct sk_buff *skb = alloc_skb(sizeof(struct cpl_set_tcb_field),
545                                         GFP_KERNEL);
546         struct cpl_set_tcb_field *req;
547         u64 val = (hcrc ? 1 : 0) | (dcrc ? 2 : 0);
548
549         if (!skb)
550                 return -ENOMEM;
551
552         /* set up ulp submode and page size */
553         req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
554         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
555         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
556         req->reply = V_NO_REPLY(reply ? 0 : 1);
557         req->cpu_idx = 0;
558         req->word = htons(31);
559         req->mask = cpu_to_be64(0x0F000000);
560         req->val = cpu_to_be64(val << 24);
561         skb->priority = CPL_PRIORITY_CONTROL;
562
563         cxgb3_ofld_send(tdev, skb);
564         return 0;
565 }
566 EXPORT_SYMBOL_GPL(cxgb3i_setup_conn_digest);
567
568
569 /**
570  * cxgb3i_adapter_ddp_info - read the adapter's ddp information
571  * @tdev: t3cdev adapter
572  * @tformat: tag format
573  * @txsz: max tx pdu payload size, filled in by this func.
574  * @rxsz: max rx pdu payload size, filled in by this func.
575  * setup the tag format for a given iscsi entity
576  */
577 int cxgb3i_adapter_ddp_info(struct t3cdev *tdev,
578                             struct cxgb3i_tag_format *tformat,
579                             unsigned int *txsz, unsigned int *rxsz)
580 {
581         struct cxgb3i_ddp_info *ddp;
582         unsigned char idx_bits;
583
584         if (!tformat)
585                 return -EINVAL;
586
587         if (!tdev->ulp_iscsi)
588                 return -EINVAL;
589
590         ddp = (struct cxgb3i_ddp_info *)tdev->ulp_iscsi;
591
592         idx_bits = 32 - tformat->sw_bits;
593         tformat->rsvd_bits = ddp->idx_bits;
594         tformat->rsvd_shift = PPOD_IDX_SHIFT;
595         tformat->rsvd_mask = (1 << tformat->rsvd_bits) - 1;
596
597         ddp_log_info("tag format: sw %u, rsvd %u,%u, mask 0x%x.\n",
598                       tformat->sw_bits, tformat->rsvd_bits,
599                       tformat->rsvd_shift, tformat->rsvd_mask);
600
601         *txsz = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
602                         ddp->max_txsz - ISCSI_PDU_NONPAYLOAD_LEN);
603         *rxsz = min_t(unsigned int, ULP2_MAX_PDU_PAYLOAD,
604                         ddp->max_rxsz - ISCSI_PDU_NONPAYLOAD_LEN);
605         ddp_log_info("max payload size: %u/%u, %u/%u.\n",
606                      *txsz, ddp->max_txsz, *rxsz, ddp->max_rxsz);
607         return 0;
608 }
609 EXPORT_SYMBOL_GPL(cxgb3i_adapter_ddp_info);
610
611 /**
612  * ddp_release - release the cxgb3 adapter's ddp resource
613  * @tdev: t3cdev adapter
614  * release all the resource held by the ddp pagepod manager for a given
615  * adapter if needed
616  */
617 static void ddp_release(struct t3cdev *tdev)
618 {
619         int i = 0;
620         struct cxgb3i_ddp_info *ddp = (struct cxgb3i_ddp_info *)tdev->ulp_iscsi;
621
622         ddp_log_info("t3dev 0x%p, release ddp 0x%p.\n", tdev, ddp);
623
624         if (ddp) {
625                 tdev->ulp_iscsi = NULL;
626                 while (i < ddp->nppods) {
627                         struct cxgb3i_gather_list *gl = ddp->gl_map[i];
628                         if (gl) {
629                                 int npods = (gl->nelem + PPOD_PAGES_MAX - 1)
630                                                 >> PPOD_PAGES_SHIFT;
631                                 ddp_log_info("t3dev 0x%p, ddp %d + %d.\n",
632                                                 tdev, i, npods);
633                                 kfree(gl);
634                                 ddp_free_gl_skb(ddp, i, npods);
635                                 i += npods;
636                         } else
637                                 i++;
638                 }
639                 cxgb3i_free_big_mem(ddp);
640         }
641 }
642
643 /**
644  * ddp_init - initialize the cxgb3 adapter's ddp resource
645  * @tdev: t3cdev adapter
646  * initialize the ddp pagepod manager for a given adapter
647  */
648 static void ddp_init(struct t3cdev *tdev)
649 {
650         struct cxgb3i_ddp_info *ddp;
651         struct ulp_iscsi_info uinfo;
652         unsigned int ppmax, bits;
653         int i, err;
654         static int vers_printed;
655
656         if (tdev->ulp_iscsi) {
657                 ddp_log_warn("t3dev 0x%p, ddp 0x%p already set up.\n",
658                                 tdev, tdev->ulp_iscsi);
659                 return;
660         }
661
662         if (!vers_printed) {
663                 printk(KERN_INFO "%s", version);
664                 vers_printed = 1;
665         }
666
667         err = tdev->ctl(tdev, ULP_ISCSI_GET_PARAMS, &uinfo);
668         if (err < 0) {
669                 ddp_log_error("%s, failed to get iscsi param err=%d.\n",
670                                  tdev->name, err);
671                 return;
672         }
673
674         ppmax = (uinfo.ulimit - uinfo.llimit + 1) >> PPOD_SIZE_SHIFT;
675         bits = __ilog2_u32(ppmax) + 1;
676         if (bits > PPOD_IDX_MAX_SIZE)
677                 bits = PPOD_IDX_MAX_SIZE;
678         ppmax = (1 << (bits - 1)) - 1;
679
680         ddp = cxgb3i_alloc_big_mem(sizeof(struct cxgb3i_ddp_info) +
681                                    ppmax *
682                                         (sizeof(struct cxgb3i_gather_list *) +
683                                         sizeof(struct sk_buff *)),
684                                    GFP_KERNEL);
685         if (!ddp) {
686                 ddp_log_warn("%s unable to alloc ddp 0x%d, ddp disabled.\n",
687                              tdev->name, ppmax);
688                 return;
689         }
690         ddp->gl_map = (struct cxgb3i_gather_list **)(ddp + 1);
691         ddp->gl_skb = (struct sk_buff **)(((char *)ddp->gl_map) +
692                                           ppmax *
693                                           sizeof(struct cxgb3i_gather_list *));
694         spin_lock_init(&ddp->map_lock);
695
696         ddp->tdev = tdev;
697         ddp->pdev = uinfo.pdev;
698         ddp->max_txsz = min_t(unsigned int, uinfo.max_txsz, ULP2_MAX_PKT_SIZE);
699         ddp->max_rxsz = min_t(unsigned int, uinfo.max_rxsz, ULP2_MAX_PKT_SIZE);
700         ddp->llimit = uinfo.llimit;
701         ddp->ulimit = uinfo.ulimit;
702         ddp->nppods = ppmax;
703         ddp->idx_last = ppmax;
704         ddp->idx_bits = bits;
705         ddp->idx_mask = (1 << bits) - 1;
706         ddp->rsvd_tag_mask = (1 << (bits + PPOD_IDX_SHIFT)) - 1;
707
708         uinfo.tagmask = ddp->idx_mask << PPOD_IDX_SHIFT;
709         for (i = 0; i < DDP_PGIDX_MAX; i++)
710                 uinfo.pgsz_factor[i] = ddp_page_order[i];
711         uinfo.ulimit = uinfo.llimit + (ppmax << PPOD_SIZE_SHIFT);
712
713         err = tdev->ctl(tdev, ULP_ISCSI_SET_PARAMS, &uinfo);
714         if (err < 0) {
715                 ddp_log_warn("%s unable to set iscsi param err=%d, "
716                               "ddp disabled.\n", tdev->name, err);
717                 goto free_ddp_map;
718         }
719
720         tdev->ulp_iscsi = ddp;
721
722         ddp_log_info("tdev 0x%p, nppods %u, bits %u, mask 0x%x,0x%x pkt %u/%u,"
723                         " %u/%u.\n",
724                         tdev, ppmax, ddp->idx_bits, ddp->idx_mask,
725                         ddp->rsvd_tag_mask, ddp->max_txsz, uinfo.max_txsz,
726                         ddp->max_rxsz, uinfo.max_rxsz);
727         return;
728
729 free_ddp_map:
730         cxgb3i_free_big_mem(ddp);
731 }
732
733 static struct cxgb3_client t3c_ddp_client = {
734         .name = "iscsiddp_cxgb3",
735         .add = ddp_init,
736         .remove = ddp_release,
737 };
738
739 /**
740  * cxgb3i_ddp_init_module - module init entry point
741  * initialize any driver wide global data structures and register with the
742  * cxgb3 module
743  */
744 static int __init cxgb3i_ddp_init_module(void)
745 {
746         page_idx = cxgb3i_ddp_find_page_index(PAGE_SIZE);
747         ddp_log_info("system PAGE_SIZE %lu, ddp idx %u.\n",
748                      PAGE_SIZE, page_idx);
749
750         cxgb3_register_client(&t3c_ddp_client);
751         return 0;
752 }
753
754 /**
755  * cxgb3i_ddp_exit_module - module cleanup/exit entry point
756  * go through the ddp list, unregister with the cxgb3 module and release
757  * any resource held.
758  */
759 static void __exit cxgb3i_ddp_exit_module(void)
760 {
761         cxgb3_unregister_client(&t3c_ddp_client);
762 }
763
764 module_init(cxgb3i_ddp_init_module);
765 module_exit(cxgb3i_ddp_exit_module);