scsi_tgt_lib.c 16 KB

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