libsrp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * SCSI RDMA Protocol lib functions
  3. *
  4. * Copyright (C) 2006 FUJITA Tomonori <tomof@acm.org>
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. * 02110-1301 USA
  20. */
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/kfifo.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/module.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_tgt.h>
  31. #include <scsi/srp.h>
  32. #include <scsi/libsrp.h>
  33. enum srp_task_attributes {
  34. SRP_SIMPLE_TASK = 0,
  35. SRP_HEAD_TASK = 1,
  36. SRP_ORDERED_TASK = 2,
  37. SRP_ACA_TASK = 4
  38. };
  39. /* tmp - will replace with SCSI logging stuff */
  40. #define eprintk(fmt, args...) \
  41. do { \
  42. printk("%s(%d) " fmt, __func__, __LINE__, ##args); \
  43. } while (0)
  44. /* #define dprintk eprintk */
  45. #define dprintk(fmt, args...)
  46. static int srp_iu_pool_alloc(struct srp_queue *q, size_t max,
  47. struct srp_buf **ring)
  48. {
  49. int i;
  50. struct iu_entry *iue;
  51. q->pool = kcalloc(max, sizeof(struct iu_entry *), GFP_KERNEL);
  52. if (!q->pool)
  53. return -ENOMEM;
  54. q->items = kcalloc(max, sizeof(struct iu_entry), GFP_KERNEL);
  55. if (!q->items)
  56. goto free_pool;
  57. spin_lock_init(&q->lock);
  58. kfifo_init(&q->queue, (void *) q->pool, max * sizeof(void *));
  59. for (i = 0, iue = q->items; i < max; i++) {
  60. kfifo_in(&q->queue, (void *) &iue, sizeof(void *));
  61. iue->sbuf = ring[i];
  62. iue++;
  63. }
  64. return 0;
  65. kfree(q->items);
  66. free_pool:
  67. kfree(q->pool);
  68. return -ENOMEM;
  69. }
  70. static void srp_iu_pool_free(struct srp_queue *q)
  71. {
  72. kfree(q->items);
  73. kfree(q->pool);
  74. }
  75. static struct srp_buf **srp_ring_alloc(struct device *dev,
  76. size_t max, size_t size)
  77. {
  78. int i;
  79. struct srp_buf **ring;
  80. ring = kcalloc(max, sizeof(struct srp_buf *), GFP_KERNEL);
  81. if (!ring)
  82. return NULL;
  83. for (i = 0; i < max; i++) {
  84. ring[i] = kzalloc(sizeof(struct srp_buf), GFP_KERNEL);
  85. if (!ring[i])
  86. goto out;
  87. ring[i]->buf = dma_alloc_coherent(dev, size, &ring[i]->dma,
  88. GFP_KERNEL);
  89. if (!ring[i]->buf)
  90. goto out;
  91. }
  92. return ring;
  93. out:
  94. for (i = 0; i < max && ring[i]; i++) {
  95. if (ring[i]->buf)
  96. dma_free_coherent(dev, size, ring[i]->buf, ring[i]->dma);
  97. kfree(ring[i]);
  98. }
  99. kfree(ring);
  100. return NULL;
  101. }
  102. static void srp_ring_free(struct device *dev, struct srp_buf **ring, size_t max,
  103. size_t size)
  104. {
  105. int i;
  106. for (i = 0; i < max; i++) {
  107. dma_free_coherent(dev, size, ring[i]->buf, ring[i]->dma);
  108. kfree(ring[i]);
  109. }
  110. kfree(ring);
  111. }
  112. int srp_target_alloc(struct srp_target *target, struct device *dev,
  113. size_t nr, size_t iu_size)
  114. {
  115. int err;
  116. spin_lock_init(&target->lock);
  117. INIT_LIST_HEAD(&target->cmd_queue);
  118. target->dev = dev;
  119. dev_set_drvdata(target->dev, target);
  120. target->srp_iu_size = iu_size;
  121. target->rx_ring_size = nr;
  122. target->rx_ring = srp_ring_alloc(target->dev, nr, iu_size);
  123. if (!target->rx_ring)
  124. return -ENOMEM;
  125. err = srp_iu_pool_alloc(&target->iu_queue, nr, target->rx_ring);
  126. if (err)
  127. goto free_ring;
  128. return 0;
  129. free_ring:
  130. srp_ring_free(target->dev, target->rx_ring, nr, iu_size);
  131. return -ENOMEM;
  132. }
  133. EXPORT_SYMBOL_GPL(srp_target_alloc);
  134. void srp_target_free(struct srp_target *target)
  135. {
  136. srp_ring_free(target->dev, target->rx_ring, target->rx_ring_size,
  137. target->srp_iu_size);
  138. srp_iu_pool_free(&target->iu_queue);
  139. }
  140. EXPORT_SYMBOL_GPL(srp_target_free);
  141. struct iu_entry *srp_iu_get(struct srp_target *target)
  142. {
  143. struct iu_entry *iue = NULL;
  144. if (kfifo_out_locked(&target->iu_queue.queue, (void *) &iue,
  145. sizeof(void *), &target->iu_queue.lock) != sizeof(void *)) {
  146. WARN_ONCE(1, "unexpected fifo state");
  147. return NULL;
  148. }
  149. if (!iue)
  150. return iue;
  151. iue->target = target;
  152. INIT_LIST_HEAD(&iue->ilist);
  153. iue->flags = 0;
  154. return iue;
  155. }
  156. EXPORT_SYMBOL_GPL(srp_iu_get);
  157. void srp_iu_put(struct iu_entry *iue)
  158. {
  159. kfifo_in_locked(&iue->target->iu_queue.queue, (void *) &iue,
  160. sizeof(void *), &iue->target->iu_queue.lock);
  161. }
  162. EXPORT_SYMBOL_GPL(srp_iu_put);
  163. static int srp_direct_data(struct scsi_cmnd *sc, struct srp_direct_buf *md,
  164. enum dma_data_direction dir, srp_rdma_t rdma_io,
  165. int dma_map, int ext_desc)
  166. {
  167. struct iu_entry *iue = NULL;
  168. struct scatterlist *sg = NULL;
  169. int err, nsg = 0, len;
  170. if (dma_map) {
  171. iue = (struct iu_entry *) sc->SCp.ptr;
  172. sg = scsi_sglist(sc);
  173. dprintk("%p %u %u %d\n", iue, scsi_bufflen(sc),
  174. md->len, scsi_sg_count(sc));
  175. nsg = dma_map_sg(iue->target->dev, sg, scsi_sg_count(sc),
  176. DMA_BIDIRECTIONAL);
  177. if (!nsg) {
  178. printk("fail to map %p %d\n", iue, scsi_sg_count(sc));
  179. return 0;
  180. }
  181. len = min(scsi_bufflen(sc), md->len);
  182. } else
  183. len = md->len;
  184. err = rdma_io(sc, sg, nsg, md, 1, dir, len);
  185. if (dma_map)
  186. dma_unmap_sg(iue->target->dev, sg, nsg, DMA_BIDIRECTIONAL);
  187. return err;
  188. }
  189. static int srp_indirect_data(struct scsi_cmnd *sc, struct srp_cmd *cmd,
  190. struct srp_indirect_buf *id,
  191. enum dma_data_direction dir, srp_rdma_t rdma_io,
  192. int dma_map, int ext_desc)
  193. {
  194. struct iu_entry *iue = NULL;
  195. struct srp_direct_buf *md = NULL;
  196. struct scatterlist dummy, *sg = NULL;
  197. dma_addr_t token = 0;
  198. int err = 0;
  199. int nmd, nsg = 0, len;
  200. if (dma_map || ext_desc) {
  201. iue = (struct iu_entry *) sc->SCp.ptr;
  202. sg = scsi_sglist(sc);
  203. dprintk("%p %u %u %d %d\n",
  204. iue, scsi_bufflen(sc), id->len,
  205. cmd->data_in_desc_cnt, cmd->data_out_desc_cnt);
  206. }
  207. nmd = id->table_desc.len / sizeof(struct srp_direct_buf);
  208. if ((dir == DMA_FROM_DEVICE && nmd == cmd->data_in_desc_cnt) ||
  209. (dir == DMA_TO_DEVICE && nmd == cmd->data_out_desc_cnt)) {
  210. md = &id->desc_list[0];
  211. goto rdma;
  212. }
  213. if (ext_desc && dma_map) {
  214. md = dma_alloc_coherent(iue->target->dev, id->table_desc.len,
  215. &token, GFP_KERNEL);
  216. if (!md) {
  217. eprintk("Can't get dma memory %u\n", id->table_desc.len);
  218. return -ENOMEM;
  219. }
  220. sg_init_one(&dummy, md, id->table_desc.len);
  221. sg_dma_address(&dummy) = token;
  222. sg_dma_len(&dummy) = id->table_desc.len;
  223. err = rdma_io(sc, &dummy, 1, &id->table_desc, 1, DMA_TO_DEVICE,
  224. id->table_desc.len);
  225. if (err) {
  226. eprintk("Error copying indirect table %d\n", err);
  227. goto free_mem;
  228. }
  229. } else {
  230. eprintk("This command uses external indirect buffer\n");
  231. return -EINVAL;
  232. }
  233. rdma:
  234. if (dma_map) {
  235. nsg = dma_map_sg(iue->target->dev, sg, scsi_sg_count(sc),
  236. DMA_BIDIRECTIONAL);
  237. if (!nsg) {
  238. eprintk("fail to map %p %d\n", iue, scsi_sg_count(sc));
  239. err = -EIO;
  240. goto free_mem;
  241. }
  242. len = min(scsi_bufflen(sc), id->len);
  243. } else
  244. len = id->len;
  245. err = rdma_io(sc, sg, nsg, md, nmd, dir, len);
  246. if (dma_map)
  247. dma_unmap_sg(iue->target->dev, sg, nsg, DMA_BIDIRECTIONAL);
  248. free_mem:
  249. if (token && dma_map)
  250. dma_free_coherent(iue->target->dev, id->table_desc.len, md, token);
  251. return err;
  252. }
  253. static int data_out_desc_size(struct srp_cmd *cmd)
  254. {
  255. int size = 0;
  256. u8 fmt = cmd->buf_fmt >> 4;
  257. switch (fmt) {
  258. case SRP_NO_DATA_DESC:
  259. break;
  260. case SRP_DATA_DESC_DIRECT:
  261. size = sizeof(struct srp_direct_buf);
  262. break;
  263. case SRP_DATA_DESC_INDIRECT:
  264. size = sizeof(struct srp_indirect_buf) +
  265. sizeof(struct srp_direct_buf) * cmd->data_out_desc_cnt;
  266. break;
  267. default:
  268. eprintk("client error. Invalid data_out_format %x\n", fmt);
  269. break;
  270. }
  271. return size;
  272. }
  273. /*
  274. * TODO: this can be called multiple times for a single command if it
  275. * has very long data.
  276. */
  277. int srp_transfer_data(struct scsi_cmnd *sc, struct srp_cmd *cmd,
  278. srp_rdma_t rdma_io, int dma_map, int ext_desc)
  279. {
  280. struct srp_direct_buf *md;
  281. struct srp_indirect_buf *id;
  282. enum dma_data_direction dir;
  283. int offset, err = 0;
  284. u8 format;
  285. offset = cmd->add_cdb_len & ~3;
  286. dir = srp_cmd_direction(cmd);
  287. if (dir == DMA_FROM_DEVICE)
  288. offset += data_out_desc_size(cmd);
  289. if (dir == DMA_TO_DEVICE)
  290. format = cmd->buf_fmt >> 4;
  291. else
  292. format = cmd->buf_fmt & ((1U << 4) - 1);
  293. switch (format) {
  294. case SRP_NO_DATA_DESC:
  295. break;
  296. case SRP_DATA_DESC_DIRECT:
  297. md = (struct srp_direct_buf *)
  298. (cmd->add_data + offset);
  299. err = srp_direct_data(sc, md, dir, rdma_io, dma_map, ext_desc);
  300. break;
  301. case SRP_DATA_DESC_INDIRECT:
  302. id = (struct srp_indirect_buf *)
  303. (cmd->add_data + offset);
  304. err = srp_indirect_data(sc, cmd, id, dir, rdma_io, dma_map,
  305. ext_desc);
  306. break;
  307. default:
  308. eprintk("Unknown format %d %x\n", dir, format);
  309. err = -EINVAL;
  310. }
  311. return err;
  312. }
  313. EXPORT_SYMBOL_GPL(srp_transfer_data);
  314. static int vscsis_data_length(struct srp_cmd *cmd, enum dma_data_direction dir)
  315. {
  316. struct srp_direct_buf *md;
  317. struct srp_indirect_buf *id;
  318. int len = 0, offset = cmd->add_cdb_len & ~3;
  319. u8 fmt;
  320. if (dir == DMA_TO_DEVICE)
  321. fmt = cmd->buf_fmt >> 4;
  322. else {
  323. fmt = cmd->buf_fmt & ((1U << 4) - 1);
  324. offset += data_out_desc_size(cmd);
  325. }
  326. switch (fmt) {
  327. case SRP_NO_DATA_DESC:
  328. break;
  329. case SRP_DATA_DESC_DIRECT:
  330. md = (struct srp_direct_buf *) (cmd->add_data + offset);
  331. len = md->len;
  332. break;
  333. case SRP_DATA_DESC_INDIRECT:
  334. id = (struct srp_indirect_buf *) (cmd->add_data + offset);
  335. len = id->len;
  336. break;
  337. default:
  338. eprintk("invalid data format %x\n", fmt);
  339. break;
  340. }
  341. return len;
  342. }
  343. int srp_cmd_queue(struct Scsi_Host *shost, struct srp_cmd *cmd, void *info,
  344. u64 itn_id, u64 addr)
  345. {
  346. enum dma_data_direction dir;
  347. struct scsi_cmnd *sc;
  348. int tag, len, err;
  349. switch (cmd->task_attr) {
  350. case SRP_SIMPLE_TASK:
  351. tag = MSG_SIMPLE_TAG;
  352. break;
  353. case SRP_ORDERED_TASK:
  354. tag = MSG_ORDERED_TAG;
  355. break;
  356. case SRP_HEAD_TASK:
  357. tag = MSG_HEAD_TAG;
  358. break;
  359. default:
  360. eprintk("Task attribute %d not supported\n", cmd->task_attr);
  361. tag = MSG_ORDERED_TAG;
  362. }
  363. dir = srp_cmd_direction(cmd);
  364. len = vscsis_data_length(cmd, dir);
  365. dprintk("%p %x %lx %d %d %d %llx\n", info, cmd->cdb[0],
  366. cmd->lun, dir, len, tag, (unsigned long long) cmd->tag);
  367. sc = scsi_host_get_command(shost, dir, GFP_KERNEL);
  368. if (!sc)
  369. return -ENOMEM;
  370. sc->SCp.ptr = info;
  371. memcpy(sc->cmnd, cmd->cdb, MAX_COMMAND_SIZE);
  372. sc->sdb.length = len;
  373. sc->sdb.table.sgl = (void *) (unsigned long) addr;
  374. sc->tag = tag;
  375. err = scsi_tgt_queue_command(sc, itn_id, (struct scsi_lun *)&cmd->lun,
  376. cmd->tag);
  377. if (err)
  378. scsi_host_put_command(shost, sc);
  379. return err;
  380. }
  381. EXPORT_SYMBOL_GPL(srp_cmd_queue);
  382. MODULE_DESCRIPTION("SCSI RDMA Protocol lib functions");
  383. MODULE_AUTHOR("FUJITA Tomonori");
  384. MODULE_LICENSE("GPL");