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. * transferring 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. tcmd->rq->bio = NULL;
  162. scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
  163. }
  164. static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
  165. u64 itn_id, u64 tag)
  166. {
  167. struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
  168. unsigned long flags;
  169. struct list_head *head;
  170. tcmd->itn_id = itn_id;
  171. tcmd->tag = tag;
  172. tcmd->bio = NULL;
  173. INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
  174. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  175. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  176. list_add(&tcmd->hash_list, head);
  177. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  178. }
  179. /*
  180. * scsi_tgt_alloc_queue - setup queue used for message passing
  181. * shost: scsi host
  182. *
  183. * This should be called by the LLD after host allocation.
  184. * And will be released when the host is released.
  185. */
  186. int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
  187. {
  188. struct scsi_tgt_queuedata *queuedata;
  189. struct request_queue *q;
  190. int err, i;
  191. /*
  192. * Do we need to send a netlink event or should uspace
  193. * just respond to the hotplug event?
  194. */
  195. q = __scsi_alloc_queue(shost, NULL);
  196. if (!q)
  197. return -ENOMEM;
  198. queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
  199. if (!queuedata) {
  200. err = -ENOMEM;
  201. goto cleanup_queue;
  202. }
  203. queuedata->shost = shost;
  204. q->queuedata = queuedata;
  205. /*
  206. * this is a silly hack. We should probably just queue as many
  207. * command as is recvd to userspace. uspace can then make
  208. * sure we do not overload the HBA
  209. */
  210. q->nr_requests = shost->can_queue;
  211. /*
  212. * We currently only support software LLDs so this does
  213. * not matter for now. Do we need this for the cards we support?
  214. * If so we should make it a host template value.
  215. */
  216. blk_queue_dma_alignment(q, 0);
  217. shost->uspace_req_q = q;
  218. for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
  219. INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
  220. spin_lock_init(&queuedata->cmd_hash_lock);
  221. return 0;
  222. cleanup_queue:
  223. blk_cleanup_queue(q);
  224. return err;
  225. }
  226. EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
  227. void scsi_tgt_free_queue(struct Scsi_Host *shost)
  228. {
  229. int i;
  230. unsigned long flags;
  231. struct request_queue *q = shost->uspace_req_q;
  232. struct scsi_cmnd *cmd;
  233. struct scsi_tgt_queuedata *qdata = q->queuedata;
  234. struct scsi_tgt_cmd *tcmd, *n;
  235. LIST_HEAD(cmds);
  236. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  237. for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
  238. list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
  239. hash_list)
  240. list_move(&tcmd->hash_list, &cmds);
  241. }
  242. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  243. while (!list_empty(&cmds)) {
  244. tcmd = list_entry(cmds.next, struct scsi_tgt_cmd, hash_list);
  245. list_del(&tcmd->hash_list);
  246. cmd = tcmd->rq->special;
  247. shost->hostt->eh_abort_handler(cmd);
  248. scsi_tgt_cmd_destroy(&tcmd->work);
  249. }
  250. }
  251. EXPORT_SYMBOL_GPL(scsi_tgt_free_queue);
  252. struct Scsi_Host *scsi_tgt_cmd_to_host(struct scsi_cmnd *cmd)
  253. {
  254. struct scsi_tgt_queuedata *queue = cmd->request->q->queuedata;
  255. return queue->shost;
  256. }
  257. EXPORT_SYMBOL_GPL(scsi_tgt_cmd_to_host);
  258. /*
  259. * scsi_tgt_queue_command - queue command for userspace processing
  260. * @cmd: scsi command
  261. * @scsilun: scsi lun
  262. * @tag: unique value to identify this command for tmf
  263. */
  264. int scsi_tgt_queue_command(struct scsi_cmnd *cmd, u64 itn_id,
  265. struct scsi_lun *scsilun, u64 tag)
  266. {
  267. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  268. int err;
  269. init_scsi_tgt_cmd(cmd->request, tcmd, itn_id, tag);
  270. err = scsi_tgt_uspace_send_cmd(cmd, itn_id, scsilun, tag);
  271. if (err)
  272. cmd_hashlist_del(cmd);
  273. return err;
  274. }
  275. EXPORT_SYMBOL_GPL(scsi_tgt_queue_command);
  276. /*
  277. * This is run from a interrupt handler normally and the unmap
  278. * needs process context so we must queue
  279. */
  280. static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
  281. {
  282. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  283. dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request));
  284. scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag);
  285. scsi_release_buffers(cmd);
  286. queue_work(scsi_tgtd, &tcmd->work);
  287. }
  288. static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  289. {
  290. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  291. int err;
  292. dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request));
  293. err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
  294. switch (err) {
  295. case SCSI_MLQUEUE_HOST_BUSY:
  296. case SCSI_MLQUEUE_DEVICE_BUSY:
  297. return -EAGAIN;
  298. }
  299. return 0;
  300. }
  301. /* TODO: test this crap and replace bio_map_user with new interface maybe */
  302. static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
  303. unsigned long uaddr, unsigned int len, int rw)
  304. {
  305. struct request_queue *q = cmd->request->q;
  306. struct request *rq = cmd->request;
  307. int err;
  308. dprintk("%lx %u\n", uaddr, len);
  309. err = blk_rq_map_user(q, rq, NULL, (void *)uaddr, len, GFP_KERNEL);
  310. if (err) {
  311. /*
  312. * TODO: need to fixup sg_tablesize, max_segment_size,
  313. * max_sectors, etc for modern HW and software drivers
  314. * where this value is bogus.
  315. *
  316. * TODO2: we can alloc a reserve buffer of max size
  317. * we can handle and do the slow copy path for really large
  318. * IO.
  319. */
  320. eprintk("Could not handle request of size %u.\n", len);
  321. return err;
  322. }
  323. tcmd->bio = rq->bio;
  324. err = scsi_init_io(cmd, GFP_KERNEL);
  325. if (err) {
  326. scsi_release_buffers(cmd);
  327. goto unmap_rq;
  328. }
  329. /*
  330. * we use REQ_TYPE_BLOCK_PC so scsi_init_io doesn't set the
  331. * length for us.
  332. */
  333. cmd->sdb.length = blk_rq_bytes(rq);
  334. return 0;
  335. unmap_rq:
  336. scsi_unmap_user_pages(tcmd);
  337. return err;
  338. }
  339. static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
  340. unsigned len)
  341. {
  342. char __user *p = (char __user *) uaddr;
  343. if (copy_from_user(cmd->sense_buffer, p,
  344. min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
  345. printk(KERN_ERR "Could not copy the sense buffer\n");
  346. return -EIO;
  347. }
  348. return 0;
  349. }
  350. static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  351. {
  352. struct scsi_tgt_cmd *tcmd;
  353. int err;
  354. err = shost->hostt->eh_abort_handler(cmd);
  355. if (err)
  356. eprintk("fail to abort %p\n", cmd);
  357. tcmd = cmd->request->end_io_data;
  358. scsi_tgt_cmd_destroy(&tcmd->work);
  359. return err;
  360. }
  361. static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
  362. {
  363. struct scsi_tgt_queuedata *qdata = q->queuedata;
  364. struct request *rq = NULL;
  365. struct list_head *head;
  366. struct scsi_tgt_cmd *tcmd;
  367. unsigned long flags;
  368. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  369. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  370. list_for_each_entry(tcmd, head, hash_list) {
  371. if (tcmd->tag == tag) {
  372. rq = tcmd->rq;
  373. list_del(&tcmd->hash_list);
  374. break;
  375. }
  376. }
  377. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  378. return rq;
  379. }
  380. int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag,
  381. unsigned long uaddr, u32 len, unsigned long sense_uaddr,
  382. u32 sense_len, u8 rw)
  383. {
  384. struct Scsi_Host *shost;
  385. struct scsi_cmnd *cmd;
  386. struct request *rq;
  387. struct scsi_tgt_cmd *tcmd;
  388. int err = 0;
  389. dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
  390. result, len, uaddr, rw);
  391. /* TODO: replace with a O(1) alg */
  392. shost = scsi_host_lookup(host_no);
  393. if (!shost) {
  394. printk(KERN_ERR "Could not find host no %d\n", host_no);
  395. return -EINVAL;
  396. }
  397. if (!shost->uspace_req_q) {
  398. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  399. goto done;
  400. }
  401. rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
  402. if (!rq) {
  403. printk(KERN_ERR "Could not find tag %llu\n",
  404. (unsigned long long) tag);
  405. err = -EINVAL;
  406. goto done;
  407. }
  408. cmd = rq->special;
  409. dprintk("cmd %p scb %x result %d len %d bufflen %u %u %x\n",
  410. cmd, cmd->cmnd[0], result, len, scsi_bufflen(cmd),
  411. rq_data_dir(rq), cmd->cmnd[0]);
  412. if (result == TASK_ABORTED) {
  413. scsi_tgt_abort_cmd(shost, cmd);
  414. goto done;
  415. }
  416. /*
  417. * store the userspace values here, the working values are
  418. * in the request_* values
  419. */
  420. tcmd = cmd->request->end_io_data;
  421. cmd->result = result;
  422. if (cmd->result == SAM_STAT_CHECK_CONDITION)
  423. scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
  424. if (len) {
  425. err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
  426. if (err) {
  427. /*
  428. * user-space daemon bugs or OOM
  429. * TODO: we can do better for OOM.
  430. */
  431. struct scsi_tgt_queuedata *qdata;
  432. struct list_head *head;
  433. unsigned long flags;
  434. eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
  435. cmd, err, uaddr, len, rw);
  436. qdata = shost->uspace_req_q->queuedata;
  437. head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)];
  438. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  439. list_add(&tcmd->hash_list, head);
  440. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  441. goto done;
  442. }
  443. }
  444. err = scsi_tgt_transfer_response(cmd);
  445. done:
  446. scsi_host_put(shost);
  447. return err;
  448. }
  449. int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id,
  450. int function, u64 tag, struct scsi_lun *scsilun,
  451. void *data)
  452. {
  453. int err;
  454. /* TODO: need to retry if this fails. */
  455. err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id,
  456. function, tag, scsilun, data);
  457. if (err < 0)
  458. eprintk("The task management request lost!\n");
  459. return err;
  460. }
  461. EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
  462. int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result)
  463. {
  464. struct Scsi_Host *shost;
  465. int err = -EINVAL;
  466. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  467. shost = scsi_host_lookup(host_no);
  468. if (!shost) {
  469. printk(KERN_ERR "Could not find host no %d\n", host_no);
  470. return err;
  471. }
  472. if (!shost->uspace_req_q) {
  473. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  474. goto done;
  475. }
  476. err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result);
  477. done:
  478. scsi_host_put(shost);
  479. return err;
  480. }
  481. int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id,
  482. char *initiator)
  483. {
  484. int err;
  485. /* TODO: need to retry if this fails. */
  486. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0,
  487. initiator);
  488. if (err < 0)
  489. eprintk("The i_t_neuxs request lost, %d %llx!\n",
  490. shost->host_no, (unsigned long long)itn_id);
  491. return err;
  492. }
  493. EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create);
  494. int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id)
  495. {
  496. int err;
  497. /* TODO: need to retry if this fails. */
  498. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no,
  499. itn_id, 1, NULL);
  500. if (err < 0)
  501. eprintk("The i_t_neuxs request lost, %d %llx!\n",
  502. shost->host_no, (unsigned long long)itn_id);
  503. return err;
  504. }
  505. EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy);
  506. int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result)
  507. {
  508. struct Scsi_Host *shost;
  509. int err = -EINVAL;
  510. dprintk("%d %d%llx\n", host_no, result, (unsigned long long)itn_id);
  511. shost = scsi_host_lookup(host_no);
  512. if (!shost) {
  513. printk(KERN_ERR "Could not find host no %d\n", host_no);
  514. return err;
  515. }
  516. if (!shost->uspace_req_q) {
  517. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  518. goto done;
  519. }
  520. err = shost->transportt->it_nexus_response(shost, itn_id, result);
  521. done:
  522. scsi_host_put(shost);
  523. return err;
  524. }
  525. static int __init scsi_tgt_init(void)
  526. {
  527. int err;
  528. scsi_tgt_cmd_cache = KMEM_CACHE(scsi_tgt_cmd, 0);
  529. if (!scsi_tgt_cmd_cache)
  530. return -ENOMEM;
  531. scsi_tgtd = alloc_workqueue("scsi_tgtd", 0, 1);
  532. if (!scsi_tgtd) {
  533. err = -ENOMEM;
  534. goto free_kmemcache;
  535. }
  536. err = scsi_tgt_if_init();
  537. if (err)
  538. goto destroy_wq;
  539. return 0;
  540. destroy_wq:
  541. destroy_workqueue(scsi_tgtd);
  542. free_kmemcache:
  543. kmem_cache_destroy(scsi_tgt_cmd_cache);
  544. return err;
  545. }
  546. static void __exit scsi_tgt_exit(void)
  547. {
  548. destroy_workqueue(scsi_tgtd);
  549. scsi_tgt_if_exit();
  550. kmem_cache_destroy(scsi_tgt_cmd_cache);
  551. }
  552. module_init(scsi_tgt_init);
  553. module_exit(scsi_tgt_exit);
  554. MODULE_DESCRIPTION("SCSI target core");
  555. MODULE_LICENSE("GPL");