scsi_tgt_lib.c 15 KB

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