2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
20 BFA_TRC_FILE(HAL, SGPG);
24 * bfa_sgpg_mod BFA SGPG Mode module
28 * Compute and return memory needed by FCP(im) module.
31 bfa_sgpg_meminfo(struct bfa_iocfc_cfg_s *cfg, u32 *km_len,
34 if (cfg->drvcfg.num_sgpgs < BFA_SGPG_MIN)
35 cfg->drvcfg.num_sgpgs = BFA_SGPG_MIN;
37 *km_len += (cfg->drvcfg.num_sgpgs + 1) * sizeof(struct bfa_sgpg_s);
38 *dm_len += (cfg->drvcfg.num_sgpgs + 1) * sizeof(struct bfi_sgpg_s);
43 bfa_sgpg_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
44 struct bfa_meminfo_s *minfo, struct bfa_pcidev_s *pcidev)
46 struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
48 struct bfa_sgpg_s *hsgpg;
49 struct bfi_sgpg_s *sgpg;
54 union bfi_addr_u addr;
57 INIT_LIST_HEAD(&mod->sgpg_q);
58 INIT_LIST_HEAD(&mod->sgpg_wait_q);
60 bfa_trc(bfa, cfg->drvcfg.num_sgpgs);
62 mod->num_sgpgs = cfg->drvcfg.num_sgpgs;
63 mod->sgpg_arr_pa = bfa_meminfo_dma_phys(minfo);
64 align_len = (BFA_SGPG_ROUNDUP(mod->sgpg_arr_pa) - mod->sgpg_arr_pa);
65 mod->sgpg_arr_pa += align_len;
66 mod->hsgpg_arr = (struct bfa_sgpg_s *) (bfa_meminfo_kva(minfo) +
68 mod->sgpg_arr = (struct bfi_sgpg_s *) (bfa_meminfo_dma_virt(minfo) +
71 hsgpg = mod->hsgpg_arr;
73 sgpg_pa.pa = mod->sgpg_arr_pa;
74 mod->free_sgpgs = mod->num_sgpgs;
76 bfa_assert(!(sgpg_pa.pa & (sizeof(struct bfi_sgpg_s) - 1)));
78 for (i = 0; i < mod->num_sgpgs; i++) {
79 bfa_os_memset(hsgpg, 0, sizeof(*hsgpg));
80 bfa_os_memset(sgpg, 0, sizeof(*sgpg));
83 hsgpg->sgpg_pa = sgpg_pa.addr;
84 list_add_tail(&hsgpg->qe, &mod->sgpg_q);
88 sgpg_pa.pa += sizeof(struct bfi_sgpg_s);
91 bfa_meminfo_kva(minfo) = (u8 *) hsgpg;
92 bfa_meminfo_dma_virt(minfo) = (u8 *) sgpg;
93 bfa_meminfo_dma_phys(minfo) = sgpg_pa.pa;
97 bfa_sgpg_detach(struct bfa_s *bfa)
102 bfa_sgpg_start(struct bfa_s *bfa)
107 bfa_sgpg_stop(struct bfa_s *bfa)
112 bfa_sgpg_iocdisable(struct bfa_s *bfa)
119 * bfa_sgpg_public BFA SGPG public functions
123 bfa_sgpg_malloc(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpgs)
125 struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
126 struct bfa_sgpg_s *hsgpg;
129 bfa_trc_fp(bfa, nsgpgs);
131 if (mod->free_sgpgs < nsgpgs)
132 return BFA_STATUS_ENOMEM;
134 for (i = 0; i < nsgpgs; i++) {
135 bfa_q_deq(&mod->sgpg_q, &hsgpg);
137 list_add_tail(&hsgpg->qe, sgpg_q);
140 mod->free_sgpgs -= nsgpgs;
141 return BFA_STATUS_OK;
145 bfa_sgpg_mfree(struct bfa_s *bfa, struct list_head *sgpg_q, int nsgpg)
147 struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
148 struct bfa_sgpg_wqe_s *wqe;
150 bfa_trc_fp(bfa, nsgpg);
152 mod->free_sgpgs += nsgpg;
153 bfa_assert(mod->free_sgpgs <= mod->num_sgpgs);
155 list_splice_tail_init(sgpg_q, &mod->sgpg_q);
157 if (list_empty(&mod->sgpg_wait_q))
161 * satisfy as many waiting requests as possible
164 wqe = bfa_q_first(&mod->sgpg_wait_q);
165 if (mod->free_sgpgs < wqe->nsgpg)
166 nsgpg = mod->free_sgpgs;
169 bfa_sgpg_malloc(bfa, &wqe->sgpg_q, nsgpg);
171 if (wqe->nsgpg == 0) {
173 wqe->cbfn(wqe->cbarg);
175 } while (mod->free_sgpgs && !list_empty(&mod->sgpg_wait_q));
179 bfa_sgpg_wait(struct bfa_s *bfa, struct bfa_sgpg_wqe_s *wqe, int nsgpg)
181 struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
183 bfa_assert(nsgpg > 0);
184 bfa_assert(nsgpg > mod->free_sgpgs);
186 wqe->nsgpg_total = wqe->nsgpg = nsgpg;
189 * allocate any left to this one first
191 if (mod->free_sgpgs) {
193 * no one else is waiting for SGPG
195 bfa_assert(list_empty(&mod->sgpg_wait_q));
196 list_splice_tail_init(&mod->sgpg_q, &wqe->sgpg_q);
197 wqe->nsgpg -= mod->free_sgpgs;
201 list_add_tail(&wqe->qe, &mod->sgpg_wait_q);
205 bfa_sgpg_wcancel(struct bfa_s *bfa, struct bfa_sgpg_wqe_s *wqe)
207 struct bfa_sgpg_mod_s *mod = BFA_SGPG_MOD(bfa);
209 bfa_assert(bfa_q_is_on_q(&mod->sgpg_wait_q, wqe));
212 if (wqe->nsgpg_total != wqe->nsgpg)
213 bfa_sgpg_mfree(bfa, &wqe->sgpg_q,
214 wqe->nsgpg_total - wqe->nsgpg);
218 bfa_sgpg_winit(struct bfa_sgpg_wqe_s *wqe, void (*cbfn) (void *cbarg),
221 INIT_LIST_HEAD(&wqe->sgpg_q);