scsi_tgt_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. 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 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 interrpt 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 %lu\n", cmd, rq_data_dir(cmd->request));
  284. scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag);
  285. if (cmd->request_buffer)
  286. scsi_free_sgtable(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 %lu\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. static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  303. {
  304. struct request *rq = cmd->request;
  305. int count;
  306. cmd->use_sg = rq->nr_phys_segments;
  307. cmd->request_buffer = scsi_alloc_sgtable(cmd, gfp_mask);
  308. if (!cmd->request_buffer)
  309. return -ENOMEM;
  310. cmd->request_bufflen = rq->data_len;
  311. dprintk("cmd %p cnt %d %lu\n", cmd, cmd->use_sg, rq_data_dir(rq));
  312. count = blk_rq_map_sg(rq->q, rq, cmd->request_buffer);
  313. if (likely(count <= cmd->use_sg)) {
  314. cmd->use_sg = count;
  315. return 0;
  316. }
  317. eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg);
  318. scsi_free_sgtable(cmd);
  319. return -EINVAL;
  320. }
  321. /* TODO: test this crap and replace bio_map_user with new interface maybe */
  322. static int scsi_map_user_pages(struct scsi_tgt_cmd *tcmd, struct scsi_cmnd *cmd,
  323. unsigned long uaddr, unsigned int len, int rw)
  324. {
  325. struct request_queue *q = cmd->request->q;
  326. struct request *rq = cmd->request;
  327. int err;
  328. dprintk("%lx %u\n", uaddr, len);
  329. err = blk_rq_map_user(q, rq, (void *)uaddr, len);
  330. if (err) {
  331. /*
  332. * TODO: need to fixup sg_tablesize, max_segment_size,
  333. * max_sectors, etc for modern HW and software drivers
  334. * where this value is bogus.
  335. *
  336. * TODO2: we can alloc a reserve buffer of max size
  337. * we can handle and do the slow copy path for really large
  338. * IO.
  339. */
  340. eprintk("Could not handle request of size %u.\n", len);
  341. return err;
  342. }
  343. tcmd->bio = rq->bio;
  344. err = scsi_tgt_init_cmd(cmd, GFP_KERNEL);
  345. if (err)
  346. goto unmap_rq;
  347. return 0;
  348. unmap_rq:
  349. scsi_unmap_user_pages(tcmd);
  350. return err;
  351. }
  352. static int scsi_tgt_copy_sense(struct scsi_cmnd *cmd, unsigned long uaddr,
  353. unsigned len)
  354. {
  355. char __user *p = (char __user *) uaddr;
  356. if (copy_from_user(cmd->sense_buffer, p,
  357. min_t(unsigned, SCSI_SENSE_BUFFERSIZE, len))) {
  358. printk(KERN_ERR "Could not copy the sense buffer\n");
  359. return -EIO;
  360. }
  361. return 0;
  362. }
  363. static int scsi_tgt_abort_cmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  364. {
  365. struct scsi_tgt_cmd *tcmd;
  366. int err;
  367. err = shost->hostt->eh_abort_handler(cmd);
  368. if (err)
  369. eprintk("fail to abort %p\n", cmd);
  370. tcmd = cmd->request->end_io_data;
  371. scsi_tgt_cmd_destroy(&tcmd->work);
  372. return err;
  373. }
  374. static struct request *tgt_cmd_hash_lookup(struct request_queue *q, u64 tag)
  375. {
  376. struct scsi_tgt_queuedata *qdata = q->queuedata;
  377. struct request *rq = NULL;
  378. struct list_head *head;
  379. struct scsi_tgt_cmd *tcmd;
  380. unsigned long flags;
  381. head = &qdata->cmd_hash[cmd_hashfn(tag)];
  382. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  383. list_for_each_entry(tcmd, head, hash_list) {
  384. if (tcmd->tag == tag) {
  385. rq = tcmd->rq;
  386. list_del(&tcmd->hash_list);
  387. break;
  388. }
  389. }
  390. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  391. return rq;
  392. }
  393. int scsi_tgt_kspace_exec(int host_no, u64 itn_id, int result, u64 tag,
  394. unsigned long uaddr, u32 len, unsigned long sense_uaddr,
  395. u32 sense_len, u8 rw)
  396. {
  397. struct Scsi_Host *shost;
  398. struct scsi_cmnd *cmd;
  399. struct request *rq;
  400. struct scsi_tgt_cmd *tcmd;
  401. int err = 0;
  402. dprintk("%d %llu %d %u %lx %u\n", host_no, (unsigned long long) tag,
  403. result, len, uaddr, rw);
  404. /* TODO: replace with a O(1) alg */
  405. shost = scsi_host_lookup(host_no);
  406. if (IS_ERR(shost)) {
  407. printk(KERN_ERR "Could not find host no %d\n", host_no);
  408. return -EINVAL;
  409. }
  410. if (!shost->uspace_req_q) {
  411. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  412. goto done;
  413. }
  414. rq = tgt_cmd_hash_lookup(shost->uspace_req_q, tag);
  415. if (!rq) {
  416. printk(KERN_ERR "Could not find tag %llu\n",
  417. (unsigned long long) tag);
  418. err = -EINVAL;
  419. goto done;
  420. }
  421. cmd = rq->special;
  422. dprintk("cmd %p scb %x result %d len %d bufflen %u %lu %x\n",
  423. cmd, cmd->cmnd[0], result, len, cmd->request_bufflen,
  424. rq_data_dir(rq), cmd->cmnd[0]);
  425. if (result == TASK_ABORTED) {
  426. scsi_tgt_abort_cmd(shost, cmd);
  427. goto done;
  428. }
  429. /*
  430. * store the userspace values here, the working values are
  431. * in the request_* values
  432. */
  433. tcmd = cmd->request->end_io_data;
  434. cmd->result = result;
  435. if (cmd->result == SAM_STAT_CHECK_CONDITION)
  436. scsi_tgt_copy_sense(cmd, sense_uaddr, sense_len);
  437. if (len) {
  438. err = scsi_map_user_pages(rq->end_io_data, cmd, uaddr, len, rw);
  439. if (err) {
  440. /*
  441. * user-space daemon bugs or OOM
  442. * TODO: we can do better for OOM.
  443. */
  444. struct scsi_tgt_queuedata *qdata;
  445. struct list_head *head;
  446. unsigned long flags;
  447. eprintk("cmd %p ret %d uaddr %lx len %d rw %d\n",
  448. cmd, err, uaddr, len, rw);
  449. qdata = shost->uspace_req_q->queuedata;
  450. head = &qdata->cmd_hash[cmd_hashfn(tcmd->tag)];
  451. spin_lock_irqsave(&qdata->cmd_hash_lock, flags);
  452. list_add(&tcmd->hash_list, head);
  453. spin_unlock_irqrestore(&qdata->cmd_hash_lock, flags);
  454. goto done;
  455. }
  456. }
  457. err = scsi_tgt_transfer_response(cmd);
  458. done:
  459. scsi_host_put(shost);
  460. return err;
  461. }
  462. int scsi_tgt_tsk_mgmt_request(struct Scsi_Host *shost, u64 itn_id,
  463. int function, u64 tag, struct scsi_lun *scsilun,
  464. void *data)
  465. {
  466. int err;
  467. /* TODO: need to retry if this fails. */
  468. err = scsi_tgt_uspace_send_tsk_mgmt(shost->host_no, itn_id,
  469. function, tag, scsilun, data);
  470. if (err < 0)
  471. eprintk("The task management request lost!\n");
  472. return err;
  473. }
  474. EXPORT_SYMBOL_GPL(scsi_tgt_tsk_mgmt_request);
  475. int scsi_tgt_kspace_tsk_mgmt(int host_no, u64 itn_id, u64 mid, int result)
  476. {
  477. struct Scsi_Host *shost;
  478. int err = -EINVAL;
  479. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  480. shost = scsi_host_lookup(host_no);
  481. if (IS_ERR(shost)) {
  482. printk(KERN_ERR "Could not find host no %d\n", host_no);
  483. return err;
  484. }
  485. if (!shost->uspace_req_q) {
  486. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  487. goto done;
  488. }
  489. err = shost->transportt->tsk_mgmt_response(shost, itn_id, mid, result);
  490. done:
  491. scsi_host_put(shost);
  492. return err;
  493. }
  494. int scsi_tgt_it_nexus_create(struct Scsi_Host *shost, u64 itn_id,
  495. char *initiator)
  496. {
  497. int err;
  498. /* TODO: need to retry if this fails. */
  499. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no, itn_id, 0,
  500. initiator);
  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_create);
  507. int scsi_tgt_it_nexus_destroy(struct Scsi_Host *shost, u64 itn_id)
  508. {
  509. int err;
  510. /* TODO: need to retry if this fails. */
  511. err = scsi_tgt_uspace_send_it_nexus_request(shost->host_no,
  512. itn_id, 1, NULL);
  513. if (err < 0)
  514. eprintk("The i_t_neuxs request lost, %d %llx!\n",
  515. shost->host_no, (unsigned long long)itn_id);
  516. return err;
  517. }
  518. EXPORT_SYMBOL_GPL(scsi_tgt_it_nexus_destroy);
  519. int scsi_tgt_kspace_it_nexus_rsp(int host_no, u64 itn_id, int result)
  520. {
  521. struct Scsi_Host *shost;
  522. int err = -EINVAL;
  523. dprintk("%d %d %llx\n", host_no, result, (unsigned long long) mid);
  524. shost = scsi_host_lookup(host_no);
  525. if (IS_ERR(shost)) {
  526. printk(KERN_ERR "Could not find host no %d\n", host_no);
  527. return err;
  528. }
  529. if (!shost->uspace_req_q) {
  530. printk(KERN_ERR "Not target scsi host %d\n", host_no);
  531. goto done;
  532. }
  533. err = shost->transportt->it_nexus_response(shost, itn_id, result);
  534. done:
  535. scsi_host_put(shost);
  536. return err;
  537. }
  538. static int __init scsi_tgt_init(void)
  539. {
  540. int err;
  541. scsi_tgt_cmd_cache = kmem_cache_create("scsi_tgt_cmd",
  542. sizeof(struct scsi_tgt_cmd),
  543. 0, 0, NULL);
  544. if (!scsi_tgt_cmd_cache)
  545. return -ENOMEM;
  546. scsi_tgtd = create_workqueue("scsi_tgtd");
  547. if (!scsi_tgtd) {
  548. err = -ENOMEM;
  549. goto free_kmemcache;
  550. }
  551. err = scsi_tgt_if_init();
  552. if (err)
  553. goto destroy_wq;
  554. return 0;
  555. destroy_wq:
  556. destroy_workqueue(scsi_tgtd);
  557. free_kmemcache:
  558. kmem_cache_destroy(scsi_tgt_cmd_cache);
  559. return err;
  560. }
  561. static void __exit scsi_tgt_exit(void)
  562. {
  563. destroy_workqueue(scsi_tgtd);
  564. scsi_tgt_if_exit();
  565. kmem_cache_destroy(scsi_tgt_cmd_cache);
  566. }
  567. module_init(scsi_tgt_init);
  568. module_exit(scsi_tgt_exit);
  569. MODULE_DESCRIPTION("SCSI target core");
  570. MODULE_LICENSE("GPL");