scsi_tgt_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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_transport.h>
  31. #include <scsi/scsi_tgt.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 fix limits of some drivers */
  43. struct bio *bio;
  44. struct list_head hash_list;
  45. struct request *rq;
  46. u64 itn_id;
  47. u64 tag;
  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. cmd->sc_data_direction = data_dir;
  95. cmd->jiffies_at_alloc = jiffies;
  96. cmd->request = rq;
  97. cmd->cmnd = rq->cmd;
  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 %u\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 itn_id, u64 tag)
  164. {
  165. struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
  166. unsigned long flags;
  167. struct list_head *head;
  168. tcmd->itn_id = itn_id;
  169. tcmd->tag = tag;
  170. tcmd->bio = NULL;
  171. INIT_WORK(&tcmd->work, scsi_tgt_cmd_destroy);
  172. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  173. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  174. list_add(&tcmd->hash_list, head);
  175. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  176. }
  177. /*
  178. * scsi_tgt_alloc_queue - setup queue used for message passing
  179. * shost: scsi host
  180. *
  181. * This should be called by the LLD after host allocation.
  182. * And will be released when the host is released.
  183. */
  184. int scsi_tgt_alloc_queue(struct Scsi_Host *shost)
  185. {
  186. struct scsi_tgt_queuedata *queuedata;
  187. struct request_queue *q;
  188. int err, i;
  189. /*
  190. * Do we need to send a netlink event or should uspace
  191. * just respond to the hotplug event?
  192. */
  193. q = __scsi_alloc_queue(shost, NULL);
  194. if (!q)
  195. return -ENOMEM;
  196. queuedata = kzalloc(sizeof(*queuedata), GFP_KERNEL);
  197. if (!queuedata) {
  198. err = -ENOMEM;
  199. goto cleanup_queue;
  200. }
  201. queuedata->shost = shost;
  202. q->queuedata = queuedata;
  203. /*
  204. * this is a silly hack. We should probably just queue as many
  205. * command as is recvd to userspace. uspace can then make
  206. * sure we do not overload the HBA
  207. */
  208. q->nr_requests = shost->can_queue;
  209. /*
  210. * We currently only support software LLDs so this does
  211. * not matter for now. Do we need this for the cards we support?
  212. * If so we should make it a host template value.
  213. */
  214. blk_queue_dma_alignment(q, 0);
  215. shost->uspace_req_q = q;
  216. for (i = 0; i < ARRAY_SIZE(queuedata->cmd_hash); i++)
  217. INIT_LIST_HEAD(&queuedata->cmd_hash[i]);
  218. spin_lock_init(&queuedata->cmd_hash_lock);
  219. return 0;
  220. cleanup_queue:
  221. blk_cleanup_queue(q);
  222. return err;
  223. }
  224. EXPORT_SYMBOL_GPL(scsi_tgt_alloc_queue);
  225. void scsi_tgt_free_queue(struct Scsi_Host *shost)
  226. {
  227. int i;
  228. unsigned long flags;
  229. struct request_queue *q = shost->uspace_req_q;
  230. struct scsi_cmnd *cmd;
  231. struct scsi_tgt_queuedata *qdata = q->queuedata;
  232. struct scsi_tgt_cmd *tcmd, *n;
  233. LIST_HEAD(cmds);
  234. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  235. for (i = 0; i < ARRAY_SIZE(qdata->cmd_hash); i++) {
  236. list_for_each_entry_safe(tcmd, n, &qdata->cmd_hash[i],
  237. hash_list) {
  238. list_del(&tcmd->hash_list);
  239. list_add(&tcmd->hash_list, &cmds);
  240. }
  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 = create_workqueue("scsi_tgtd");
  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");