scsi_tgt_lib.c 17 KB

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