scsi_tgt_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * SCSI target lib functions
  3. *
  4. * Copyright (C) 2005 Mike Christie <michaelc@cs.wisc.edu>
  5. * Copyright (C) 2005 FUJITA Tomonori <tomof@acm.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  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.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/blkdev.h>
  23. #include <linux/hash.h>
  24. #include <linux/module.h>
  25. #include <linux/pagemap.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_tgt.h>
  31. #include <../drivers/md/dm-bio-list.h>
  32. #include "scsi_tgt_priv.h"
  33. static struct workqueue_struct *scsi_tgtd;
  34. static kmem_cache_t *scsi_tgt_cmd_cache;
  35. /*
  36. * TODO: this struct will be killed when the block layer supports large bios
  37. * and James's work struct code is in
  38. */
  39. struct scsi_tgt_cmd {
  40. /* TODO replace work with James b's code */
  41. struct work_struct work;
  42. /* TODO replace the lists with a large bio */
  43. struct bio_list xfer_done_list;
  44. struct bio_list xfer_list;
  45. struct list_head hash_list;
  46. struct request *rq;
  47. u64 tag;
  48. void *buffer;
  49. unsigned bufflen;
  50. };
  51. #define TGT_HASH_ORDER 4
  52. #define cmd_hashfn(tag) hash_long((unsigned long) (tag), TGT_HASH_ORDER)
  53. struct scsi_tgt_queuedata {
  54. struct Scsi_Host *shost;
  55. struct list_head cmd_hash[1 << TGT_HASH_ORDER];
  56. spinlock_t cmd_hash_lock;
  57. };
  58. /*
  59. * Function: scsi_host_get_command()
  60. *
  61. * Purpose: Allocate and setup a scsi command block and blk request
  62. *
  63. * Arguments: shost - scsi host
  64. * data_dir - dma data dir
  65. * gfp_mask- allocator flags
  66. *
  67. * Returns: The allocated scsi command structure.
  68. *
  69. * This should be called by target LLDs to get a command.
  70. */
  71. struct scsi_cmnd *scsi_host_get_command(struct Scsi_Host *shost,
  72. enum dma_data_direction data_dir,
  73. gfp_t gfp_mask)
  74. {
  75. int write = (data_dir == DMA_TO_DEVICE);
  76. struct request *rq;
  77. struct scsi_cmnd *cmd;
  78. struct scsi_tgt_cmd *tcmd;
  79. /* Bail if we can't get a reference to the device */
  80. if (!get_device(&shost->shost_gendev))
  81. return NULL;
  82. tcmd = kmem_cache_alloc(scsi_tgt_cmd_cache, GFP_ATOMIC);
  83. if (!tcmd)
  84. goto put_dev;
  85. rq = blk_get_request(shost->uspace_req_q, write, gfp_mask);
  86. if (!rq)
  87. goto free_tcmd;
  88. cmd = __scsi_get_command(shost, gfp_mask);
  89. if (!cmd)
  90. goto release_rq;
  91. memset(cmd, 0, sizeof(*cmd));
  92. cmd->sc_data_direction = data_dir;
  93. cmd->jiffies_at_alloc = jiffies;
  94. cmd->request = rq;
  95. rq->special = cmd;
  96. rq->cmd_type = REQ_TYPE_SPECIAL;
  97. rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
  98. rq->end_io_data = tcmd;
  99. bio_list_init(&tcmd->xfer_list);
  100. bio_list_init(&tcmd->xfer_done_list);
  101. tcmd->rq = rq;
  102. return cmd;
  103. release_rq:
  104. blk_put_request(rq);
  105. free_tcmd:
  106. kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
  107. put_dev:
  108. put_device(&shost->shost_gendev);
  109. return NULL;
  110. }
  111. EXPORT_SYMBOL_GPL(scsi_host_get_command);
  112. /*
  113. * Function: scsi_host_put_command()
  114. *
  115. * Purpose: Free a scsi command block
  116. *
  117. * Arguments: shost - scsi host
  118. * cmd - command block to free
  119. *
  120. * Returns: Nothing.
  121. *
  122. * Notes: The command must not belong to any lists.
  123. */
  124. void scsi_host_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  125. {
  126. struct request_queue *q = shost->uspace_req_q;
  127. struct request *rq = cmd->request;
  128. struct scsi_tgt_cmd *tcmd = rq->end_io_data;
  129. unsigned long flags;
  130. kmem_cache_free(scsi_tgt_cmd_cache, tcmd);
  131. spin_lock_irqsave(q->queue_lock, flags);
  132. __blk_put_request(q, rq);
  133. spin_unlock_irqrestore(q->queue_lock, flags);
  134. __scsi_put_command(shost, cmd, &shost->shost_gendev);
  135. }
  136. EXPORT_SYMBOL_GPL(scsi_host_put_command);
  137. static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
  138. {
  139. struct bio *bio;
  140. /* must call bio_endio in case bio was bounced */
  141. while ((bio = bio_list_pop(&tcmd->xfer_done_list))) {
  142. bio_endio(bio, bio->bi_size, 0);
  143. bio_unmap_user(bio);
  144. }
  145. while ((bio = bio_list_pop(&tcmd->xfer_list))) {
  146. bio_endio(bio, bio->bi_size, 0);
  147. bio_unmap_user(bio);
  148. }
  149. }
  150. static void cmd_hashlist_del(struct scsi_cmnd *cmd)
  151. {
  152. struct request_queue *q = cmd->request->q;
  153. struct scsi_tgt_queuedata *qdata = q->queuedata;
  154. unsigned long flags;
  155. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  156. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  157. list_del(&tcmd->hash_list);
  158. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  159. }
  160. static void scsi_tgt_cmd_destroy(void *data)
  161. {
  162. struct scsi_cmnd *cmd = data;
  163. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  164. dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
  165. rq_data_dir(cmd->request));
  166. /*
  167. * We fix rq->cmd_flags here since when we told bio_map_user
  168. * to write vm for WRITE commands, blk_rq_bio_prep set
  169. * rq_data_dir the flags to READ.
  170. */
  171. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  172. cmd->request->cmd_flags |= REQ_RW;
  173. else
  174. cmd->request->cmd_flags &= ~REQ_RW;
  175. scsi_unmap_user_pages(tcmd);
  176. scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
  177. }
  178. static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
  179. u64 tag)
  180. {
  181. struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
  182. unsigned long flags;
  183. struct list_head *head;
  184. tcmd->tag = tag;
  185. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  186. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  187. list_add(&tcmd->hash_list, head);
  188. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  189. }
  190. /*
  191. * scsi_tgt_alloc_queue - setup queue used for message passing
  192. * shost: scsi host
  193. *
  194. * This should be called by the LLD after host allocation.
  195. * And will be released when the host is released.
  196. */
  197. int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
  198. {
  199. struct scsi_tgt_queuedata *queuedata;
  200. struct request_queue *q;
  201. int err, i;
  202. /*
  203. * Do we need to send a netlink event or should uspace
  204. * just respond to the hotplug event?
  205. */
  206. q = __scsi_alloc_queue(shost, NULL);
  207. if (!q)
  208. return -ENOMEM;
  209. queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
  210. if (!queuedata) {
  211. err = -ENOMEM;
  212. goto cleanup_queue;
  213. }
  214. queuedata->shost = shost;
  215. q->queuedata = queuedata;
  216. /*
  217. * this is a silly hack. We should probably just queue as many
  218. * command as is recvd to userspace. uspace can then make
  219. * sure we do not overload the HBA
  220. */
  221. q->nr_requests = shost->hostt->can_queue;
  222. /*
  223. * We currently only support software LLDs so this does
  224. * not matter for now. Do we need this for the cards we support?
  225. * If so we should make it a host template value.
  226. */
  227. blk_queue_dma_alignment(q, 0);
  228. shost->uspace_req_q = q;
  229. for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
  230. INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
  231. spin_lock_init(&queuedata->cmd_hash_lock);
  232. return 0;
  233. cleanup_queue:
  234. blk_cleanup_queue(q);
  235. return err;
  236. }
  237. EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
  238. void scsi_tgt_free_queue(struct Scsi_Host *shost)
  239. {
  240. int i;
  241. unsigned long flags;
  242. struct request_queue *q = shost->uspace_req_q;
  243. struct scsi_cmnd *cmd;
  244. struct scsi_tgt_queuedata *qdata = q->queuedata;
  245. struct scsi_tgt_cmd *tcmd, *n;
  246. LIST_HEAD(cmds);
  247. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  248. for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
  249. list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
  250. hash_list) {
  251. list_del(&tcmd->hash_list);
  252. list_add(&tcmd->hash_list, &cmds);
  253. }
  254. }
  255. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  256. while (!list_empty(&cmds)) {
  257. tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
  258. list_del(&tcmd->hash_list);
  259. cmd = tcmd->rq->special;
  260. shost->hostt->eh_abort_handler(cmd);
  261. scsi_tgt_cmd_destroy(cmd);
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
  265. struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
  266. {
  267. struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
  268. return queue->shost;
  269. }
  270. EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
  271. /*
  272. * scsi_tgt_queue_command - queue command for userspace processing
  273. * @cmd: scsi command
  274. * @scsilun: scsi lun
  275. * @tag: unique value to identify this command for tmf
  276. */
  277. int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
  278. u64 tag)
  279. {
  280. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  281. int err;
  282. init_scsi_tgt_cmd(cmd->request, tcmd, tag);
  283. err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
  284. if (err)
  285. cmd_hashlist_del(cmd);
  286. return err;
  287. }
  288. EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
  289. /*
  290. * This is run from a interrpt handler normally and the unmap
  291. * needs process context so we must queue
  292. */
  293. static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
  294. {
  295. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  296. dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
  297. scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  298. INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy, cmd);
  299. queue_work(scsi_tgtd, &tcmd->work);
  300. }
  301. static int __scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  302. {
  303. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  304. int err;
  305. dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
  306. err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
  307. switch (err) {
  308. case SCSI_MLQUEUE_HOST_BUSY:
  309. case SCSI_MLQUEUE_DEVICE_BUSY:
  310. return -EAGAIN;
  311. }
  312. return 0;
  313. }
  314. static void scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  315. {
  316. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  317. int err;
  318. err = __scsi_tgt_transfer_response(cmd);
  319. if (!err)
  320. return;
  321. cmd->result = DID_BUS_BUSY << 16;
  322. err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  323. if (err <= 0)
  324. /* the eh will have to pick this up */
  325. printk(KERN_ERR "Could not send cmd %p status\n", cmd);
  326. }
  327. static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  328. {
  329. struct request *rq = cmd->request;
  330. struct scsi_tgt_cmd *tcmd = rq->end_io_data;
  331. int count;
  332. cmd->use_sg = rq->nr_phys_segments;
  333. cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
  334. if (!cmd->request_buffer)
  335. return -ENOMEM;
  336. cmd->request_bufflen = rq->data_len;
  337. dprintk("cmd %p addr %p cnt %d %lu\n", cmd, tcmd->buffer, cmd->use_sg,
  338. rq_data_dir(rq));
  339. count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
  340. if (likely(count <= cmd->use_sg)) {
  341. cmd->use_sg = count;
  342. return 0;
  343. }
  344. eprintk("cmd %p addr %p cnt %d\n", cmd, tcmd->buffer, cmd->use_sg);
  345. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  346. return -EINVAL;
  347. }
  348. /* TODO: test this crap and replace bio_map_user with new interface maybe */
  349. static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
  350. int rw)
  351. {
  352. struct request_queue *q = cmd->request->q;
  353. struct request *rq = cmd->request;
  354. void *uaddr = tcmd->buffer;
  355. unsigned int len = tcmd->bufflen;
  356. struct bio *bio;
  357. int err;
  358. while (len > 0) {
  359. dprintk("%lx %u\n", (unsigned long) uaddr, len);
  360. bio = bio_map_user(q, NULL, (unsigned long) uaddr, len, rw);
  361. if (IS_ERR(bio)) {
  362. err = PTR_ERR(bio);
  363. dprintk("fail to map %lx %u %d %x\n",
  364. (unsigned long) uaddr, len, err, cmd->cmnd[0]);
  365. goto unmap_bios;
  366. }
  367. uaddr += bio->bi_size;
  368. len -= bio->bi_size;
  369. /*
  370. * The first bio is added and merged. We could probably
  371. * try to add others using scsi_merge_bio() but for now
  372. * we keep it simple. The first bio should be pretty large
  373. * (either hitting the 1 MB bio pages limit or a queue limit)
  374. * already but for really large IO we may want to try and
  375. * merge these.
  376. */
  377. if (!rq->bio) {
  378. blk_rq_bio_prep(q, rq, bio);
  379. rq->data_len = bio->bi_size;
  380. } else
  381. /* put list of bios to transfer in next go around */
  382. bio_list_add(&tcmd->xfer_list, bio);
  383. }
  384. cmd->offset = 0;
  385. err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
  386. if (err)
  387. goto unmap_bios;
  388. return 0;
  389. unmap_bios:
  390. if (rq->bio) {
  391. bio_unmap_user(rq->bio);
  392. while ((bio = bio_list_pop(&tcmd->xfer_list)))
  393. bio_unmap_user(bio);
  394. }
  395. return err;
  396. }
  397. static int scsi_tgt_transfer_data(struct scsi_cmnd *);
  398. static void scsi_tgt_data_transfer_done(struct scsi_cmnd *cmd)
  399. {
  400. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  401. struct bio *bio;
  402. int err;
  403. /* should we free resources here on error ? */
  404. if (cmd->result) {
  405. send_uspace_err:
  406. err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  407. if (err <= 0)
  408. /* the tgt uspace eh will have to pick this up */
  409. printk(KERN_ERR "Could not send cmd %p status\n", cmd);
  410. return;
  411. }
  412. dprintk("cmd %p request_bufflen %u bufflen %u\n",
  413. cmd, cmd->request_bufflen, tcmd->bufflen);
  414. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  415. bio_list_add(&tcmd->xfer_done_list, cmd->request->bio);
  416. tcmd->buffer += cmd->request_bufflen;
  417. cmd->offset += cmd->request_bufflen;
  418. if (!tcmd->xfer_list.head) {
  419. scsi_tgt_transfer_response(cmd);
  420. return;
  421. }
  422. dprintk("cmd2 %p request_bufflen %u bufflen %u\n",
  423. cmd, cmd->request_bufflen, tcmd->bufflen);
  424. bio = bio_list_pop(&tcmd->xfer_list);
  425. BUG_ON(!bio);
  426. blk_rq_bio_prep(cmd->request->q, cmd->request, bio);
  427. cmd->request->data_len = bio->bi_size;
  428. err = scsi_tgt_init_cmd(cmd, GFP_ATOMIC);
  429. if (err) {
  430. cmd->result = DID_ERROR << 16;
  431. goto send_uspace_err;
  432. }
  433. if (scsi_tgt_transfer_data(cmd)) {
  434. cmd->result = DID_NO_CONNECT << 16;
  435. goto send_uspace_err;
  436. }
  437. }
  438. static int scsi_tgt_transfer_data(struct scsi_cmnd *cmd)
  439. {
  440. int err;
  441. struct Scsi_Host *host = scsi_tgt_cmd_to_host(cmd);
  442. err = host->hostt->transfer_data(cmd, scsi_tgt_data_transfer_done);
  443. switch (err) {
  444. case SCSI_MLQUEUE_HOST_BUSY:
  445. case SCSI_MLQUEUE_DEVICE_BUSY:
  446. return -EAGAIN;
  447. default:
  448. return 0;
  449. }
  450. }
  451. static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
  452. unsigned len)
  453. {
  454. char __user *p = (char __user *) uaddr;
  455. if (copy_from_user(cmd->sense_buffer, p,
  456. min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
  457. printk(KERN_ERR "Could not copy the sense buffer\n");
  458. return -EIO;
  459. }
  460. return 0;
  461. }
  462. static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  463. {
  464. int err;
  465. err = shost->hostt->eh_abort_handler(cmd);
  466. if (err)
  467. eprintk("fail to abort %p\n", cmd);
  468. scsi_tgt_cmd_destroy(cmd);
  469. return err;
  470. }
  471. static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
  472. {
  473. struct scsi_tgt_queuedata *qdata = q->queuedata;
  474. struct request *rq = NULL;
  475. struct list_head *head;
  476. struct scsi_tgt_cmd *tcmd;
  477. unsigned long flags;
  478. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  479. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  480. list_for_each_entry(tcmd, head, hash_list) {
  481. if (tcmd->tag == tag) {
  482. rq = tcmd->rq;
  483. list_del(&tcmd->hash_list);
  484. break;
  485. }
  486. }
  487. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  488. return rq;
  489. }
  490. int scsi_tgt_kspace_exec(int host_no, u64 tag, int result, u32 len,
  491. unsigned long uaddr, u8 rw)
  492. {
  493. struct Scsi_Host *shost;
  494. struct scsi_cmnd *cmd;
  495. struct request *rq;
  496. struct scsi_tgt_cmd *tcmd;
  497. int err = 0;
  498. dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
  499. result, len, uaddr, rw);
  500. /* TODO: replace with a O(1) alg */
  501. shost = scsi_host_lookup(host_no);
  502. if (IS_ERR(shost)) {
  503. printk(KERN_ERR "Could not find host no %d\n", host_no);
  504. return -EINVAL;
  505. }
  506. if (!shost->uspace_req_q) {
  507. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  508. goto done;
  509. }
  510. rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
  511. if (!rq) {
  512. printk(KERN_ERR "Could not find tag %llu\n",
  513. (unsigned long long) tag);
  514. err = -EINVAL;
  515. goto done;
  516. }
  517. cmd = rq->special;
  518. dprintk("cmd %p result %d len %d bufflen %u %lu %x\n", cmd,
  519. result, len, cmd->request_bufflen, rq_data_dir(rq), cmd->cmnd[0]);
  520. if (result == TASK_ABORTED) {
  521. scsi_tgt_abort_cmd(shost, cmd);
  522. goto done;
  523. }
  524. /*
  525. * store the userspace values here, the working values are
  526. * in the request_* values
  527. */
  528. tcmd = cmd->request->end_io_data;
  529. tcmd->buffer = (void *)uaddr;
  530. tcmd->bufflen = len;
  531. cmd->result = result;
  532. if (!tcmd->bufflen || cmd->request_buffer) {
  533. err = __scsi_tgt_transfer_response(cmd);
  534. goto done;
  535. }
  536. /*
  537. * TODO: Do we need to handle case where request does not
  538. * align with LLD.
  539. */
  540. err = scsi_map_user_pages(rq->end_io_data, cmd, rw);
  541. if (err) {
  542. eprintk("%p %d\n", cmd, err);
  543. err = -EAGAIN;
  544. goto done;
  545. }
  546. /* userspace failure */
  547. if (cmd->result) {
  548. if (status_byte(cmd->result) == CHECK_CONDITION)
  549. scsi_tgt_copy_sense(cmd, uaddr, len);
  550. err = __scsi_tgt_transfer_response(cmd);
  551. goto done;
  552. }
  553. /* ask the target LLD to transfer the data to the buffer */
  554. err = scsi_tgt_transfer_data(cmd);
  555. done:
  556. scsi_host_put(shost);
  557. return err;
  558. }
  559. int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
  560. struct scsi_lun *scsilun, void *data)
  561. {
  562. int err;
  563. /* TODO: need to retry if this fails. */
  564. err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
  565. tag, scsilun, data);
  566. if (err < 0)
  567. eprintk("The task management request lost!\n");
  568. return err;
  569. }
  570. EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
  571. int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
  572. {
  573. struct Scsi_Host *shost;
  574. int err = -EINVAL;
  575. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  576. shost = scsi_host_lookup(host_no);
  577. if (IS_ERR(shost)) {
  578. printk(KERN_ERR "Could not find host no %d\n", host_no);
  579. return err;
  580. }
  581. if (!shost->uspace_req_q) {
  582. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  583. goto done;
  584. }
  585. err = shost->hostt->tsk_mgmt_response(mid, result);
  586. done:
  587. scsi_host_put(shost);
  588. return err;
  589. }
  590. static int __init scsi_tgt_init(void)
  591. {
  592. int err;
  593. scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
  594. sizeof(struct scsi_tgt_cmd),
  595. 0, 0, NULL, NULL);
  596. if (!scsi_tgt_cmd_cache)
  597. return -ENOMEM;
  598. scsi_tgtd = create_workqueue("scsi_tgtd");
  599. if (!scsi_tgtd) {
  600. err = -ENOMEM;
  601. goto free_kmemcache;
  602. }
  603. err = scsi_tgt_if_init();
  604. if (err)
  605. goto destroy_wq;
  606. return 0;
  607. destroy_wq:
  608. destroy_workqueue(scsi_tgtd);
  609. free_kmemcache:
  610. kmem_cache_destroy(scsi_tgt_cmd_cache);
  611. return err;
  612. }
  613. static void __exit scsi_tgt_exit(void)
  614. {
  615. destroy_workqueue(scsi_tgtd);
  616. scsi_tgt_if_exit();
  617. kmem_cache_destroy(scsi_tgt_cmd_cache);
  618. }
  619. module_init(scsi_tgt_init);
  620. module_exit(scsi_tgt_exit);
  621. MODULE_DESCRIPTION("SCSI target core");
  622. MODULE_LICENSE("GPL");