scsi_tgt_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 struct kmem_cache *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(struct work_struct *work)
  161. {
  162. struct scsi_tgt_cmd *tcmd =
  163. container_of(work, struct scsi_tgt_cmd, work);
  164. struct scsi_cmnd *cmd = tcmd->rq->special;
  165. dprintk("cmd %p %d %lu\n", cmd, cmd->sc_data_direction,
  166. rq_data_dir(cmd->request));
  167. /*
  168. * We fix rq->cmd_flags here since when we told bio_map_user
  169. * to write vm for WRITE commands, blk_rq_bio_prep set
  170. * rq_data_dir the flags to READ.
  171. */
  172. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  173. cmd->request->cmd_flags |= REQ_RW;
  174. else
  175. cmd->request->cmd_flags &= ~REQ_RW;
  176. scsi_unmap_user_pages(tcmd);
  177. scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
  178. }
  179. static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
  180. u64 tag)
  181. {
  182. struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
  183. unsigned long flags;
  184. struct list_head *head;
  185. tcmd->tag = tag;
  186. INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
  187. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  188. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  189. list_add(&tcmd->hash_list, head);
  190. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  191. }
  192. /*
  193. * scsi_tgt_alloc_queue - setup queue used for message passing
  194. * shost: scsi host
  195. *
  196. * This should be called by the LLD after host allocation.
  197. * And will be released when the host is released.
  198. */
  199. int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
  200. {
  201. struct scsi_tgt_queuedata *queuedata;
  202. struct request_queue *q;
  203. int err, i;
  204. /*
  205. * Do we need to send a netlink event or should uspace
  206. * just respond to the hotplug event?
  207. */
  208. q = __scsi_alloc_queue(shost, NULL);
  209. if (!q)
  210. return -ENOMEM;
  211. queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
  212. if (!queuedata) {
  213. err = -ENOMEM;
  214. goto cleanup_queue;
  215. }
  216. queuedata->shost = shost;
  217. q->queuedata = queuedata;
  218. /*
  219. * this is a silly hack. We should probably just queue as many
  220. * command as is recvd to userspace. uspace can then make
  221. * sure we do not overload the HBA
  222. */
  223. q->nr_requests = shost->hostt->can_queue;
  224. /*
  225. * We currently only support software LLDs so this does
  226. * not matter for now. Do we need this for the cards we support?
  227. * If so we should make it a host template value.
  228. */
  229. blk_queue_dma_alignment(q, 0);
  230. shost->uspace_req_q = q;
  231. for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
  232. INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
  233. spin_lock_init(&queuedata->cmd_hash_lock);
  234. return 0;
  235. cleanup_queue:
  236. blk_cleanup_queue(q);
  237. return err;
  238. }
  239. EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
  240. void scsi_tgt_free_queue(struct Scsi_Host *shost)
  241. {
  242. int i;
  243. unsigned long flags;
  244. struct request_queue *q = shost->uspace_req_q;
  245. struct scsi_cmnd *cmd;
  246. struct scsi_tgt_queuedata *qdata = q->queuedata;
  247. struct scsi_tgt_cmd *tcmd, *n;
  248. LIST_HEAD(cmds);
  249. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  250. for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
  251. list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
  252. hash_list) {
  253. list_del(&tcmd->hash_list);
  254. list_add(&tcmd->hash_list, &cmds);
  255. }
  256. }
  257. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  258. while (!list_empty(&cmds)) {
  259. tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
  260. list_del(&tcmd->hash_list);
  261. cmd = tcmd->rq->special;
  262. shost->hostt->eh_abort_handler(cmd);
  263. scsi_tgt_cmd_destroy(&tcmd->work);
  264. }
  265. }
  266. EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
  267. struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
  268. {
  269. struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
  270. return queue->shost;
  271. }
  272. EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
  273. /*
  274. * scsi_tgt_queue_command - queue command for userspace processing
  275. * @cmd: scsi command
  276. * @scsilun: scsi lun
  277. * @tag: unique value to identify this command for tmf
  278. */
  279. int scsi_tgt_queue_command(struct scsi_cmnd *cmd, struct scsi_lun *scsilun,
  280. u64 tag)
  281. {
  282. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  283. int err;
  284. init_scsi_tgt_cmd(cmd->request, tcmd, tag);
  285. err = scsi_tgt_uspace_send_cmd(cmd, scsilun, tag);
  286. if (err)
  287. cmd_hashlist_del(cmd);
  288. return err;
  289. }
  290. EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
  291. /*
  292. * This is run from a interrpt handler normally and the unmap
  293. * needs process context so we must queue
  294. */
  295. static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
  296. {
  297. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  298. dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
  299. scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  300. queue_work(scsi_tgtd, &tcmd->work);
  301. }
  302. static int __scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  303. {
  304. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  305. int err;
  306. dprintk("cmd %p %lu\n", cmd, rq_data_dir(cmd->request));
  307. err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
  308. switch (err) {
  309. case SCSI_MLQUEUE_HOST_BUSY:
  310. case SCSI_MLQUEUE_DEVICE_BUSY:
  311. return -EAGAIN;
  312. }
  313. return 0;
  314. }
  315. static void scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  316. {
  317. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  318. int err;
  319. err = __scsi_tgt_transfer_response(cmd);
  320. if (!err)
  321. return;
  322. cmd->result = DID_BUS_BUSY << 16;
  323. err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  324. if (err <= 0)
  325. /* the eh will have to pick this up */
  326. printk(KERN_ERR "Could not send cmd %p status\n", cmd);
  327. }
  328. static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  329. {
  330. struct request *rq = cmd->request;
  331. struct scsi_tgt_cmd *tcmd = rq->end_io_data;
  332. int count;
  333. cmd->use_sg = rq->nr_phys_segments;
  334. cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
  335. if (!cmd->request_buffer)
  336. return -ENOMEM;
  337. cmd->request_bufflen = rq->data_len;
  338. dprintk("cmd %p addr %p cnt %d %lu\n", cmd, tcmd->buffer, cmd->use_sg,
  339. rq_data_dir(rq));
  340. count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
  341. if (likely(count <= cmd->use_sg)) {
  342. cmd->use_sg = count;
  343. return 0;
  344. }
  345. eprintk("cmd %p addr %p cnt %d\n", cmd, tcmd->buffer, cmd->use_sg);
  346. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  347. return -EINVAL;
  348. }
  349. /* TODO: test this crap and replace bio_map_user with new interface maybe */
  350. static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
  351. int rw)
  352. {
  353. struct request_queue *q = cmd->request->q;
  354. struct request *rq = cmd->request;
  355. void *uaddr = tcmd->buffer;
  356. unsigned int len = tcmd->bufflen;
  357. struct bio *bio;
  358. int err;
  359. while (len > 0) {
  360. dprintk("%lx %u\n", (unsigned long) uaddr, len);
  361. bio = bio_map_user(q, NULL, (unsigned long) uaddr, len, rw);
  362. if (IS_ERR(bio)) {
  363. err = PTR_ERR(bio);
  364. dprintk("fail to map %lx %u %d %x\n",
  365. (unsigned long) uaddr, len, err, cmd->cmnd[0]);
  366. goto unmap_bios;
  367. }
  368. uaddr += bio->bi_size;
  369. len -= bio->bi_size;
  370. /*
  371. * The first bio is added and merged. We could probably
  372. * try to add others using scsi_merge_bio() but for now
  373. * we keep it simple. The first bio should be pretty large
  374. * (either hitting the 1 MB bio pages limit or a queue limit)
  375. * already but for really large IO we may want to try and
  376. * merge these.
  377. */
  378. if (!rq->bio) {
  379. blk_rq_bio_prep(q, rq, bio);
  380. rq->data_len = bio->bi_size;
  381. } else
  382. /* put list of bios to transfer in next go around */
  383. bio_list_add(&tcmd->xfer_list, bio);
  384. }
  385. cmd->offset = 0;
  386. err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
  387. if (err)
  388. goto unmap_bios;
  389. return 0;
  390. unmap_bios:
  391. if (rq->bio) {
  392. bio_unmap_user(rq->bio);
  393. while ((bio = bio_list_pop(&tcmd->xfer_list)))
  394. bio_unmap_user(bio);
  395. }
  396. return err;
  397. }
  398. static int scsi_tgt_transfer_data(struct scsi_cmnd *);
  399. static void scsi_tgt_data_transfer_done(struct scsi_cmnd *cmd)
  400. {
  401. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  402. struct bio *bio;
  403. int err;
  404. /* should we free resources here on error ? */
  405. if (cmd->result) {
  406. send_uspace_err:
  407. err = scsi_tgt_uspace_send_status(cmd, tcmd->tag);
  408. if (err <= 0)
  409. /* the tgt uspace eh will have to pick this up */
  410. printk(KERN_ERR "Could not send cmd %p status\n", cmd);
  411. return;
  412. }
  413. dprintk("cmd %p request_bufflen %u bufflen %u\n",
  414. cmd, cmd->request_bufflen, tcmd->bufflen);
  415. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  416. bio_list_add(&tcmd->xfer_done_list, cmd->request->bio);
  417. tcmd->buffer += cmd->request_bufflen;
  418. cmd->offset += cmd->request_bufflen;
  419. if (!tcmd->xfer_list.head) {
  420. scsi_tgt_transfer_response(cmd);
  421. return;
  422. }
  423. dprintk("cmd2 %p request_bufflen %u bufflen %u\n",
  424. cmd, cmd->request_bufflen, tcmd->bufflen);
  425. bio = bio_list_pop(&tcmd->xfer_list);
  426. BUG_ON(!bio);
  427. blk_rq_bio_prep(cmd->request->q, cmd->request, bio);
  428. cmd->request->data_len = bio->bi_size;
  429. err = scsi_tgt_init_cmd(cmd, GFP_ATOMIC);
  430. if (err) {
  431. cmd->result = DID_ERROR << 16;
  432. goto send_uspace_err;
  433. }
  434. if (scsi_tgt_transfer_data(cmd)) {
  435. cmd->result = DID_NO_CONNECT << 16;
  436. goto send_uspace_err;
  437. }
  438. }
  439. static int scsi_tgt_transfer_data(struct scsi_cmnd *cmd)
  440. {
  441. int err;
  442. struct Scsi_Host *host = scsi_tgt_cmd_to_host(cmd);
  443. err = host->hostt->transfer_data(cmd, scsi_tgt_data_transfer_done);
  444. switch (err) {
  445. case SCSI_MLQUEUE_HOST_BUSY:
  446. case SCSI_MLQUEUE_DEVICE_BUSY:
  447. return -EAGAIN;
  448. default:
  449. return 0;
  450. }
  451. }
  452. static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
  453. unsigned len)
  454. {
  455. char __user *p = (char __user *) uaddr;
  456. if (copy_from_user(cmd->sense_buffer, p,
  457. min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
  458. printk(KERN_ERR "Could not copy the sense buffer\n");
  459. return -EIO;
  460. }
  461. return 0;
  462. }
  463. static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  464. {
  465. struct scsi_tgt_cmd *tcmd;
  466. int err;
  467. err = shost->hostt->eh_abort_handler(cmd);
  468. if (err)
  469. eprintk("fail to abort %p\n", cmd);
  470. tcmd = cmd->request->end_io_data;
  471. scsi_tgt_cmd_destroy(&tcmd->work);
  472. return err;
  473. }
  474. static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
  475. {
  476. struct scsi_tgt_queuedata *qdata = q->queuedata;
  477. struct request *rq = NULL;
  478. struct list_head *head;
  479. struct scsi_tgt_cmd *tcmd;
  480. unsigned long flags;
  481. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  482. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  483. list_for_each_entry(tcmd, head, hash_list) {
  484. if (tcmd->tag == tag) {
  485. rq = tcmd->rq;
  486. list_del(&tcmd->hash_list);
  487. break;
  488. }
  489. }
  490. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  491. return rq;
  492. }
  493. int scsi_tgt_kspace_exec(int host_no, u64 tag, int result, u32 len,
  494. unsigned long uaddr, u8 rw)
  495. {
  496. struct Scsi_Host *shost;
  497. struct scsi_cmnd *cmd;
  498. struct request *rq;
  499. struct scsi_tgt_cmd *tcmd;
  500. int err = 0;
  501. dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
  502. result, len, uaddr, rw);
  503. /* TODO: replace with a O(1) alg */
  504. shost = scsi_host_lookup(host_no);
  505. if (IS_ERR(shost)) {
  506. printk(KERN_ERR "Could not find host no %d\n", host_no);
  507. return -EINVAL;
  508. }
  509. if (!shost->uspace_req_q) {
  510. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  511. goto done;
  512. }
  513. rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
  514. if (!rq) {
  515. printk(KERN_ERR "Could not find tag %llu\n",
  516. (unsigned long long) tag);
  517. err = -EINVAL;
  518. goto done;
  519. }
  520. cmd = rq->special;
  521. dprintk("cmd %p result %d len %d bufflen %u %lu %x\n", cmd,
  522. result, len, cmd->request_bufflen, rq_data_dir(rq), cmd->cmnd[0]);
  523. if (result == TASK_ABORTED) {
  524. scsi_tgt_abort_cmd(shost, cmd);
  525. goto done;
  526. }
  527. /*
  528. * store the userspace values here, the working values are
  529. * in the request_* values
  530. */
  531. tcmd = cmd->request->end_io_data;
  532. tcmd->buffer = (void *)uaddr;
  533. tcmd->bufflen = len;
  534. cmd->result = result;
  535. if (!tcmd->bufflen || cmd->request_buffer) {
  536. err = __scsi_tgt_transfer_response(cmd);
  537. goto done;
  538. }
  539. /*
  540. * TODO: Do we need to handle case where request does not
  541. * align with LLD.
  542. */
  543. err = scsi_map_user_pages(rq->end_io_data, cmd, rw);
  544. if (err) {
  545. eprintk("%p %d\n", cmd, err);
  546. err = -EAGAIN;
  547. goto done;
  548. }
  549. /* userspace failure */
  550. if (cmd->result) {
  551. if (status_byte(cmd->result) == CHECK_CONDITION)
  552. scsi_tgt_copy_sense(cmd, uaddr, len);
  553. err = __scsi_tgt_transfer_response(cmd);
  554. goto done;
  555. }
  556. /* ask the target LLD to transfer the data to the buffer */
  557. err = scsi_tgt_transfer_data(cmd);
  558. done:
  559. scsi_host_put(shost);
  560. return err;
  561. }
  562. int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, int function, u64 tag,
  563. struct scsi_lun *scsilun, void *data)
  564. {
  565. int err;
  566. /* TODO: need to retry if this fails. */
  567. err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, function,
  568. tag, scsilun, data);
  569. if (err < 0)
  570. eprintk("The task management request lost!\n");
  571. return err;
  572. }
  573. EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
  574. int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 mid, int result)
  575. {
  576. struct Scsi_Host *shost;
  577. int err = -EINVAL;
  578. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  579. shost = scsi_host_lookup(host_no);
  580. if (IS_ERR(shost)) {
  581. printk(KERN_ERR "Could not find host no %d\n", host_no);
  582. return err;
  583. }
  584. if (!shost->uspace_req_q) {
  585. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  586. goto done;
  587. }
  588. err = shost->hostt->tsk_mgmt_response(mid, result);
  589. done:
  590. scsi_host_put(shost);
  591. return err;
  592. }
  593. static int __init scsi_tgt_init(void)
  594. {
  595. int err;
  596. scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
  597. sizeof(struct scsi_tgt_cmd),
  598. 0, 0, NULL, NULL);
  599. if (!scsi_tgt_cmd_cache)
  600. return -ENOMEM;
  601. scsi_tgtd = create_workqueue("scsi_tgtd");
  602. if (!scsi_tgtd) {
  603. err = -ENOMEM;
  604. goto free_kmemcache;
  605. }
  606. err = scsi_tgt_if_init();
  607. if (err)
  608. goto destroy_wq;
  609. return 0;
  610. destroy_wq:
  611. destroy_workqueue(scsi_tgtd);
  612. free_kmemcache:
  613. kmem_cache_destroy(scsi_tgt_cmd_cache);
  614. return err;
  615. }
  616. static void __exit scsi_tgt_exit(void)
  617. {
  618. destroy_workqueue(scsi_tgtd);
  619. scsi_tgt_if_exit();
  620. kmem_cache_destroy(scsi_tgt_cmd_cache);
  621. }
  622. module_init(scsi_tgt_init);
  623. module_exit(scsi_tgt_exit);
  624. MODULE_DESCRIPTION("SCSI target core");
  625. MODULE_LICENSE("GPL");