2 * SCSI RDAM Protocol lib functions
4 * Copyright (C) 2006 FUJITA Tomonori <tomof@acm.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 #include <linux/err.h>
22 #include <linux/kfifo.h>
23 #include <linux/scatterlist.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/pci.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_cmnd.h>
28 #include <scsi/scsi_tcq.h>
29 #include <scsi/scsi_tgt.h>
31 #include <scsi/libsrp.h>
33 enum srp_task_attributes {
40 /* tmp - will replace with SCSI logging stuff */
41 #define eprintk(fmt, args...) \
43 printk("%s(%d) " fmt, __FUNCTION__, __LINE__, ##args); \
45 /* #define dprintk eprintk */
46 #define dprintk(fmt, args...)
48 static int srp_iu_pool_alloc(struct srp_queue *q, size_t max,
49 struct srp_buf **ring)
54 q->pool = kcalloc(max, sizeof(struct iu_entry *), GFP_KERNEL);
57 q->items = kcalloc(max, sizeof(struct iu_entry), GFP_KERNEL);
61 spin_lock_init(&q->lock);
62 q->queue = kfifo_init((void *) q->pool, max * sizeof(void *),
63 GFP_KERNEL, &q->lock);
67 for (i = 0, iue = q->items; i < max; i++) {
68 __kfifo_put(q->queue, (void *) &iue, sizeof(void *));
81 static void srp_iu_pool_free(struct srp_queue *q)
87 static struct srp_buf **srp_ring_alloc(struct device *dev,
88 size_t max, size_t size)
91 struct srp_buf **ring;
93 ring = kcalloc(max, sizeof(struct srp_buf *), GFP_KERNEL);
97 for (i = 0; i < max; i++) {
98 ring[i] = kzalloc(sizeof(struct srp_buf), GFP_KERNEL);
101 ring[i]->buf = dma_alloc_coherent(dev, size, &ring[i]->dma,
109 for (i = 0; i < max && ring[i]; i++) {
111 dma_free_coherent(dev, size, ring[i]->buf, ring[i]->dma);
119 static void srp_ring_free(struct device *dev, struct srp_buf **ring, size_t max,
124 for (i = 0; i < max; i++) {
125 dma_free_coherent(dev, size, ring[i]->buf, ring[i]->dma);
130 int srp_target_alloc(struct srp_target *target, struct device *dev,
131 size_t nr, size_t iu_size)
135 spin_lock_init(&target->lock);
136 INIT_LIST_HEAD(&target->cmd_queue);
139 target->dev->driver_data = target;
141 target->srp_iu_size = iu_size;
142 target->rx_ring_size = nr;
143 target->rx_ring = srp_ring_alloc(target->dev, nr, iu_size);
144 if (!target->rx_ring)
146 err = srp_iu_pool_alloc(&target->iu_queue, nr, target->rx_ring);
153 srp_ring_free(target->dev, target->rx_ring, nr, iu_size);
156 EXPORT_SYMBOL_GPL(srp_target_alloc);
158 void srp_target_free(struct srp_target *target)
160 srp_ring_free(target->dev, target->rx_ring, target->rx_ring_size,
161 target->srp_iu_size);
162 srp_iu_pool_free(&target->iu_queue);
164 EXPORT_SYMBOL_GPL(srp_target_free);
166 struct iu_entry *srp_iu_get(struct srp_target *target)
168 struct iu_entry *iue = NULL;
170 kfifo_get(target->iu_queue.queue, (void *) &iue, sizeof(void *));
173 iue->target = target;
174 INIT_LIST_HEAD(&iue->ilist);
178 EXPORT_SYMBOL_GPL(srp_iu_get);
180 void srp_iu_put(struct iu_entry *iue)
182 kfifo_put(iue->target->iu_queue.queue, (void *) &iue, sizeof(void *));
184 EXPORT_SYMBOL_GPL(srp_iu_put);
186 static int srp_direct_data(struct scsi_cmnd *sc, struct srp_direct_buf *md,
187 enum dma_data_direction dir, srp_rdma_t rdma_io,
188 int dma_map, int ext_desc)
190 struct iu_entry *iue = NULL;
191 struct scatterlist *sg = NULL;
192 int err, nsg = 0, len;
195 iue = (struct iu_entry *) sc->SCp.ptr;
196 sg = sc->request_buffer;
198 dprintk("%p %u %u %d\n", iue, sc->request_bufflen,
199 md->len, sc->use_sg);
201 nsg = dma_map_sg(iue->target->dev, sg, sc->use_sg,
204 printk("fail to map %p %d\n", iue, sc->use_sg);
207 len = min(sc->request_bufflen, md->len);
211 err = rdma_io(sc, sg, nsg, md, 1, dir, len);
214 dma_unmap_sg(iue->target->dev, sg, nsg, DMA_BIDIRECTIONAL);
219 static int srp_indirect_data(struct scsi_cmnd *sc, struct srp_cmd *cmd,
220 struct srp_indirect_buf *id,
221 enum dma_data_direction dir, srp_rdma_t rdma_io,
222 int dma_map, int ext_desc)
224 struct iu_entry *iue = NULL;
225 struct srp_direct_buf *md = NULL;
226 struct scatterlist dummy, *sg = NULL;
227 dma_addr_t token = 0;
229 unsigned int done = 0;
230 int nmd, nsg = 0, len;
232 if (dma_map || ext_desc) {
233 iue = (struct iu_entry *) sc->SCp.ptr;
234 sg = sc->request_buffer;
236 dprintk("%p %u %u %d %d\n",
237 iue, sc->request_bufflen, id->len,
238 cmd->data_in_desc_cnt, cmd->data_out_desc_cnt);
241 nmd = id->table_desc.len / sizeof(struct srp_direct_buf);
243 if ((dir == DMA_FROM_DEVICE && nmd == cmd->data_in_desc_cnt) ||
244 (dir == DMA_TO_DEVICE && nmd == cmd->data_out_desc_cnt)) {
245 md = &id->desc_list[0];
249 if (ext_desc && dma_map) {
250 md = dma_alloc_coherent(iue->target->dev, id->table_desc.len,
253 eprintk("Can't get dma memory %u\n", id->table_desc.len);
257 sg_init_one(&dummy, md, id->table_desc.len);
258 sg_dma_address(&dummy) = token;
259 err = rdma_io(sc, &dummy, 1, &id->table_desc, 1, DMA_TO_DEVICE,
262 eprintk("Error copying indirect table %ld\n", err);
266 eprintk("This command uses external indirect buffer\n");
272 nsg = dma_map_sg(iue->target->dev, sg, sc->use_sg, DMA_BIDIRECTIONAL);
274 eprintk("fail to map %p %d\n", iue, sc->use_sg);
277 len = min(sc->request_bufflen, id->len);
281 err = rdma_io(sc, sg, nsg, md, nmd, dir, len);
284 dma_unmap_sg(iue->target->dev, sg, nsg, DMA_BIDIRECTIONAL);
287 if (token && dma_map)
288 dma_free_coherent(iue->target->dev, id->table_desc.len, md, token);
293 static int data_out_desc_size(struct srp_cmd *cmd)
296 u8 fmt = cmd->buf_fmt >> 4;
299 case SRP_NO_DATA_DESC:
301 case SRP_DATA_DESC_DIRECT:
302 size = sizeof(struct srp_direct_buf);
304 case SRP_DATA_DESC_INDIRECT:
305 size = sizeof(struct srp_indirect_buf) +
306 sizeof(struct srp_direct_buf) * cmd->data_out_desc_cnt;
309 eprintk("client error. Invalid data_out_format %x\n", fmt);
316 * TODO: this can be called multiple times for a single command if it
317 * has very long data.
319 int srp_transfer_data(struct scsi_cmnd *sc, struct srp_cmd *cmd,
320 srp_rdma_t rdma_io, int dma_map, int ext_desc)
322 struct srp_direct_buf *md;
323 struct srp_indirect_buf *id;
324 enum dma_data_direction dir;
328 offset = cmd->add_cdb_len * 4;
330 dir = srp_cmd_direction(cmd);
331 if (dir == DMA_FROM_DEVICE)
332 offset += data_out_desc_size(cmd);
334 if (dir == DMA_TO_DEVICE)
335 format = cmd->buf_fmt >> 4;
337 format = cmd->buf_fmt & ((1U << 4) - 1);
340 case SRP_NO_DATA_DESC:
342 case SRP_DATA_DESC_DIRECT:
343 md = (struct srp_direct_buf *)
344 (cmd->add_data + offset);
345 err = srp_direct_data(sc, md, dir, rdma_io, dma_map, ext_desc);
347 case SRP_DATA_DESC_INDIRECT:
348 id = (struct srp_indirect_buf *)
349 (cmd->add_data + offset);
350 err = srp_indirect_data(sc, cmd, id, dir, rdma_io, dma_map,
354 eprintk("Unknown format %d %x\n", dir, format);
360 EXPORT_SYMBOL_GPL(srp_transfer_data);
362 static int vscsis_data_length(struct srp_cmd *cmd, enum dma_data_direction dir)
364 struct srp_direct_buf *md;
365 struct srp_indirect_buf *id;
366 int len = 0, offset = cmd->add_cdb_len * 4;
369 if (dir == DMA_TO_DEVICE)
370 fmt = cmd->buf_fmt >> 4;
372 fmt = cmd->buf_fmt & ((1U << 4) - 1);
373 offset += data_out_desc_size(cmd);
377 case SRP_NO_DATA_DESC:
379 case SRP_DATA_DESC_DIRECT:
380 md = (struct srp_direct_buf *) (cmd->add_data + offset);
383 case SRP_DATA_DESC_INDIRECT:
384 id = (struct srp_indirect_buf *) (cmd->add_data + offset);
388 eprintk("invalid data format %x\n", fmt);
394 int srp_cmd_queue(struct Scsi_Host *shost, struct srp_cmd *cmd, void *info,
397 enum dma_data_direction dir;
398 struct scsi_cmnd *sc;
401 switch (cmd->task_attr) {
402 case SRP_SIMPLE_TASK:
403 tag = MSG_SIMPLE_TAG;
405 case SRP_ORDERED_TASK:
406 tag = MSG_ORDERED_TAG;
412 eprintk("Task attribute %d not supported\n", cmd->task_attr);
413 tag = MSG_ORDERED_TAG;
416 dir = srp_cmd_direction(cmd);
417 len = vscsis_data_length(cmd, dir);
419 dprintk("%p %x %lx %d %d %d %llx\n", info, cmd->cdb[0],
420 cmd->lun, dir, len, tag, (unsigned long long) cmd->tag);
422 sc = scsi_host_get_command(shost, dir, GFP_KERNEL);
427 memcpy(sc->cmnd, cmd->cdb, MAX_COMMAND_SIZE);
428 sc->request_bufflen = len;
429 sc->request_buffer = (void *) (unsigned long) addr;
431 err = scsi_tgt_queue_command(sc, (struct scsi_lun *) &cmd->lun, cmd->tag);
433 scsi_host_put_command(shost, sc);
437 EXPORT_SYMBOL_GPL(srp_cmd_queue);
439 MODULE_DESCRIPTION("SCSI RDAM Protocol lib functions");
440 MODULE_AUTHOR("FUJITA Tomonori");
441 MODULE_LICENSE("GPL");