scsi_tgt_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. rq->special = cmd;
  98. rq->cmd_type = REQ_TYPE_SPECIAL;
  99. rq->cmd_flags |= REQ_TYPE_BLOCK_PC;
  100. rq->end_io_data = tcmd;
  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 cmd_hashlist_del(struct scsi_cmnd *cmd)
  138. {
  139. struct request_queue *q = cmd->request->q;
  140. struct scsi_tgt_queuedata *qdata = q->queuedata;
  141. unsigned long flags;
  142. struct scsi_tgt_cmd *tcmd = cmd->request->end_io_data;
  143. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  144. list_del(&tcmd->hash_list);
  145. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  146. }
  147. static void scsi_unmap_user_pages(struct scsi_tgt_cmd *tcmd)
  148. {
  149. blk_rq_unmap_user(tcmd->bio);
  150. }
  151. static void scsi_tgt_cmd_destroy(struct work_struct *work)
  152. {
  153. struct scsi_tgt_cmd *tcmd =
  154. container_of(work, struct scsi_tgt_cmd, work);
  155. struct scsi_cmnd *cmd = tcmd->rq->special;
  156. dprintk("cmd %p %d %u\n", cmd, cmd->sc_data_direction,
  157. rq_data_dir(cmd->request));
  158. scsi_unmap_user_pages(tcmd);
  159. scsi_host_put_command(scsi_tgt_cmd_to_host(cmd), cmd);
  160. }
  161. static void init_scsi_tgt_cmd(struct request *rq, struct scsi_tgt_cmd *tcmd,
  162. u64 itn_id, u64 tag)
  163. {
  164. struct scsi_tgt_queuedata *qdata = rq->q->queuedata;
  165. unsigned long flags;
  166. struct list_head *head;
  167. tcmd->itn_id = itn_id;
  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->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, u64 itn_id,
  264. struct scsi_lun *scsilun, 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, itn_id, tag);
  269. err = scsi_tgt_uspace_send_cmd(cmd, itn_id, 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 interrupt 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 %u\n", cmd, rq_data_dir(cmd->request));
  283. scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag);
  284. scsi_release_buffers(cmd);
  285. queue_work(scsi_tgtd, &tcmd->work);
  286. }
  287. static int scsi_tgt_transfer_response(struct scsi_cmnd *cmd)
  288. {
  289. struct Scsi_Host *shost = scsi_tgt_cmd_to_host(cmd);
  290. int err;
  291. dprintk("cmd %p %u\n", cmd, rq_data_dir(cmd->request));
  292. err = shost->hostt->transfer_response(cmd, scsi_tgt_cmd_done);
  293. switch (err) {
  294. case SCSI_MLQUEUE_HOST_BUSY:
  295. case SCSI_MLQUEUE_DEVICE_BUSY:
  296. return -EAGAIN;
  297. }
  298. return 0;
  299. }
  300. /* TODO: test this crap and replace bio_map_user with new interface maybe */
  301. static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
  302. unsigned long uaddr, unsigned int len, int rw)
  303. {
  304. struct request_queue *q = cmd->request->q;
  305. struct request *rq = cmd->request;
  306. int err;
  307. dprintk("%lx %u\n", uaddr, len);
  308. err = blk_rq_map_user(q, rq, (void *)uaddr, len);
  309. if (err) {
  310. /*
  311. * TODO: need to fixup sg_tablesize, max_segment_size,
  312. * max_sectors, etc for modern HW and software drivers
  313. * where this value is bogus.
  314. *
  315. * TODO2: we can alloc a reserve buffer of max size
  316. * we can handle and do the slow copy path for really large
  317. * IO.
  318. */
  319. eprintk("Could not handle request of size %u.\n", len);
  320. return err;
  321. }
  322. tcmd->bio = rq->bio;
  323. err = scsi_init_io(cmd, GFP_KERNEL);
  324. if (err) {
  325. scsi_release_buffers(cmd);
  326. goto unmap_rq;
  327. }
  328. /*
  329. * we use REQ_TYPE_BLOCK_PC so scsi_init_io doesn't set the
  330. * length for us.
  331. */
  332. cmd->sdb.length = rq->data_len;
  333. return 0;
  334. unmap_rq:
  335. scsi_unmap_user_pages(tcmd);
  336. return err;
  337. }
  338. static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
  339. unsigned len)
  340. {
  341. char __user *p = (char __user *) uaddr;
  342. if (copy_from_user(cmd->sense_buffer, p,
  343. min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
  344. printk(KERN_ERR "Could not copy the sense buffer\n");
  345. return -EIO;
  346. }
  347. return 0;
  348. }
  349. static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  350. {
  351. struct scsi_tgt_cmd *tcmd;
  352. int err;
  353. err = shost->hostt->eh_abort_handler(cmd);
  354. if (err)
  355. eprintk("fail to abort %p\n", cmd);
  356. tcmd = cmd->request->end_io_data;
  357. scsi_tgt_cmd_destroy(&tcmd->work);
  358. return err;
  359. }
  360. static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
  361. {
  362. struct scsi_tgt_queuedata *qdata = q->queuedata;
  363. struct request *rq = NULL;
  364. struct list_head *head;
  365. struct scsi_tgt_cmd *tcmd;
  366. unsigned long flags;
  367. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  368. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  369. list_for_each_entry(tcmd, head, hash_list) {
  370. if (tcmd->tag == tag) {
  371. rq = tcmd->rq;
  372. list_del(&tcmd->hash_list);
  373. break;
  374. }
  375. }
  376. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  377. return rq;
  378. }
  379. int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag,
  380. unsigned long uaddr, u32 len, unsigned long sense_uaddr,
  381. u32 sense_len, u8 rw)
  382. {
  383. struct Scsi_Host *shost;
  384. struct scsi_cmnd *cmd;
  385. struct request *rq;
  386. struct scsi_tgt_cmd *tcmd;
  387. int err = 0;
  388. dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
  389. result, len, uaddr, rw);
  390. /* TODO: replace with a O(1) alg */
  391. shost = scsi_host_lookup(host_no);
  392. if (IS_ERR(shost)) {
  393. printk(KERN_ERR "Could not find host no %d\n", host_no);
  394. return -EINVAL;
  395. }
  396. if (!shost->uspace_req_q) {
  397. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  398. goto done;
  399. }
  400. rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
  401. if (!rq) {
  402. printk(KERN_ERR "Could not find tag %llu\n",
  403. (unsigned long long) tag);
  404. err = -EINVAL;
  405. goto done;
  406. }
  407. cmd = rq->special;
  408. dprintk("cmd %p scb %x result %d len %d bufflen %u %u %x\n",
  409. cmd, cmd->cmnd[0], result, len, scsi_bufflen(cmd),
  410. rq_data_dir(rq), cmd->cmnd[0]);
  411. if (result == TASK_ABORTED) {
  412. scsi_tgt_abort_cmd(shost, cmd);
  413. goto done;
  414. }
  415. /*
  416. * store the userspace values here, the working values are
  417. * in the request_* values
  418. */
  419. tcmd = cmd->request->end_io_data;
  420. cmd->result = result;
  421. if (cmd->result == SAM_STAT_CHECK_CONDITION)
  422. scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
  423. if (len) {
  424. err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
  425. if (err) {
  426. /*
  427. * user-space daemon bugs or OOM
  428. * TODO: we can do better for OOM.
  429. */
  430. struct scsi_tgt_queuedata *qdata;
  431. struct list_head *head;
  432. unsigned long flags;
  433. eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
  434. cmd, err, uaddr, len, rw);
  435. qdata = shost->uspace_req_q->queuedata;
  436. head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)];
  437. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  438. list_add(&tcmd->hash_list, head);
  439. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  440. goto done;
  441. }
  442. }
  443. err = scsi_tgt_transfer_response(cmd);
  444. done:
  445. scsi_host_put(shost);
  446. return err;
  447. }
  448. int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id,
  449. int function, u64 tag, struct scsi_lun *scsilun,
  450. void *data)
  451. {
  452. int err;
  453. /* TODO: need to retry if this fails. */
  454. err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id,
  455. function, tag, scsilun, data);
  456. if (err < 0)
  457. eprintk("The task management request lost!\n");
  458. return err;
  459. }
  460. EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
  461. int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result)
  462. {
  463. struct Scsi_Host *shost;
  464. int err = -EINVAL;
  465. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  466. shost = scsi_host_lookup(host_no);
  467. if (IS_ERR(shost)) {
  468. printk(KERN_ERR "Could not find host no %d\n", host_no);
  469. return err;
  470. }
  471. if (!shost->uspace_req_q) {
  472. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  473. goto done;
  474. }
  475. err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result);
  476. done:
  477. scsi_host_put(shost);
  478. return err;
  479. }
  480. int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id,
  481. char *initiator)
  482. {
  483. int err;
  484. /* TODO: need to retry if this fails. */
  485. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0,
  486. initiator);
  487. if (err < 0)
  488. eprintk("The i_t_neuxs request lost, %d %llx!\n",
  489. shost->host_no, (unsigned long long)itn_id);
  490. return err;
  491. }
  492. EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_create);
  493. int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id)
  494. {
  495. int err;
  496. /* TODO: need to retry if this fails. */
  497. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no,
  498. itn_id, 1, NULL);
  499. if (err < 0)
  500. eprintk("The i_t_neuxs request lost, %d %llx!\n",
  501. shost->host_no, (unsigned long long)itn_id);
  502. return err;
  503. }
  504. EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy);
  505. int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result)
  506. {
  507. struct Scsi_Host *shost;
  508. int err = -EINVAL;
  509. dprintk("%d %d%llx\n", host_no, result, (unsigned long long)itn_id);
  510. shost = scsi_host_lookup(host_no);
  511. if (IS_ERR(shost)) {
  512. printk(KERN_ERR "Could not find host no %d\n", host_no);
  513. return err;
  514. }
  515. if (!shost->uspace_req_q) {
  516. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  517. goto done;
  518. }
  519. err = shost->transportt->it_nexus_response(shost, itn_id, result);
  520. done:
  521. scsi_host_put(shost);
  522. return err;
  523. }
  524. static int __init scsi_tgt_init(void)
  525. {
  526. int err;
  527. scsi_tgt_cmd_cache = KMEM_CACHE(scsi_tgt_cmd, 0);
  528. if (!scsi_tgt_cmd_cache)
  529. return -ENOMEM;
  530. scsi_tgtd = create_workqueue("scsi_tgtd");
  531. if (!scsi_tgtd) {
  532. err = -ENOMEM;
  533. goto free_kmemcache;
  534. }
  535. err = scsi_tgt_if_init();
  536. if (err)
  537. goto destroy_wq;
  538. return 0;
  539. destroy_wq:
  540. destroy_workqueue(scsi_tgtd);
  541. free_kmemcache:
  542. kmem_cache_destroy(scsi_tgt_cmd_cache);
  543. return err;
  544. }
  545. static void __exit scsi_tgt_exit(void)
  546. {
  547. destroy_workqueue(scsi_tgtd);
  548. scsi_tgt_if_exit();
  549. kmem_cache_destroy(scsi_tgt_cmd_cache);
  550. }
  551. module_init(scsi_tgt_init);
  552. module_exit(scsi_tgt_exit);
  553. MODULE_DESCRIPTION("SCSI target core");
  554. MODULE_LICENSE("GPL");