target_core_user.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
  3. * Copyright (C) 2014 Red Hat, Inc.
  4. * Copyright (C) 2015 Arrikto, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/spinlock.h>
  20. #include <linux/module.h>
  21. #include <linux/idr.h>
  22. #include <linux/kernel.h>
  23. #include <linux/timer.h>
  24. #include <linux/parser.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/uio_driver.h>
  27. #include <linux/stringify.h>
  28. #include <net/genetlink.h>
  29. #include <scsi/scsi_common.h>
  30. #include <scsi/scsi_proto.h>
  31. #include <target/target_core_base.h>
  32. #include <target/target_core_fabric.h>
  33. #include <target/target_core_backend.h>
  34. #include <linux/target_core_user.h>
  35. /*
  36. * Define a shared-memory interface for LIO to pass SCSI commands and
  37. * data to userspace for processing. This is to allow backends that
  38. * are too complex for in-kernel support to be possible.
  39. *
  40. * It uses the UIO framework to do a lot of the device-creation and
  41. * introspection work for us.
  42. *
  43. * See the .h file for how the ring is laid out. Note that while the
  44. * command ring is defined, the particulars of the data area are
  45. * not. Offset values in the command entry point to other locations
  46. * internal to the mmap()ed area. There is separate space outside the
  47. * command ring for data buffers. This leaves maximum flexibility for
  48. * moving buffer allocations, or even page flipping or other
  49. * allocation techniques, without altering the command ring layout.
  50. *
  51. * SECURITY:
  52. * The user process must be assumed to be malicious. There's no way to
  53. * prevent it breaking the command ring protocol if it wants, but in
  54. * order to prevent other issues we must only ever read *data* from
  55. * the shared memory area, not offsets or sizes. This applies to
  56. * command ring entries as well as the mailbox. Extra code needed for
  57. * this may have a 'UAM' comment.
  58. */
  59. #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
  60. #define CMDR_SIZE (16 * 4096)
  61. #define DATA_SIZE (257 * 4096)
  62. #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
  63. static struct device *tcmu_root_device;
  64. struct tcmu_hba {
  65. u32 host_id;
  66. };
  67. #define TCMU_CONFIG_LEN 256
  68. struct tcmu_dev {
  69. struct se_device se_dev;
  70. char *name;
  71. struct se_hba *hba;
  72. #define TCMU_DEV_BIT_OPEN 0
  73. #define TCMU_DEV_BIT_BROKEN 1
  74. unsigned long flags;
  75. struct uio_info uio_info;
  76. struct tcmu_mailbox *mb_addr;
  77. size_t dev_size;
  78. u32 cmdr_size;
  79. u32 cmdr_last_cleaned;
  80. /* Offset of data ring from start of mb */
  81. size_t data_off;
  82. size_t data_size;
  83. /* Ring head + tail values. */
  84. /* Must add data_off and mb_addr to get the address */
  85. size_t data_head;
  86. size_t data_tail;
  87. wait_queue_head_t wait_cmdr;
  88. /* TODO should this be a mutex? */
  89. spinlock_t cmdr_lock;
  90. struct idr commands;
  91. spinlock_t commands_lock;
  92. struct timer_list timeout;
  93. char dev_config[TCMU_CONFIG_LEN];
  94. };
  95. #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
  96. #define CMDR_OFF sizeof(struct tcmu_mailbox)
  97. struct tcmu_cmd {
  98. struct se_cmd *se_cmd;
  99. struct tcmu_dev *tcmu_dev;
  100. uint16_t cmd_id;
  101. /* Can't use se_cmd->data_length when cleaning up expired cmds, because if
  102. cmd has been completed then accessing se_cmd is off limits */
  103. size_t data_length;
  104. unsigned long deadline;
  105. #define TCMU_CMD_BIT_EXPIRED 0
  106. unsigned long flags;
  107. };
  108. static struct kmem_cache *tcmu_cmd_cache;
  109. /* multicast group */
  110. enum tcmu_multicast_groups {
  111. TCMU_MCGRP_CONFIG,
  112. };
  113. static const struct genl_multicast_group tcmu_mcgrps[] = {
  114. [TCMU_MCGRP_CONFIG] = { .name = "config", },
  115. };
  116. /* Our generic netlink family */
  117. static struct genl_family tcmu_genl_family = {
  118. .id = GENL_ID_GENERATE,
  119. .hdrsize = 0,
  120. .name = "TCM-USER",
  121. .version = 1,
  122. .maxattr = TCMU_ATTR_MAX,
  123. .mcgrps = tcmu_mcgrps,
  124. .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
  125. };
  126. static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
  127. {
  128. struct se_device *se_dev = se_cmd->se_dev;
  129. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  130. struct tcmu_cmd *tcmu_cmd;
  131. int cmd_id;
  132. tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
  133. if (!tcmu_cmd)
  134. return NULL;
  135. tcmu_cmd->se_cmd = se_cmd;
  136. tcmu_cmd->tcmu_dev = udev;
  137. tcmu_cmd->data_length = se_cmd->data_length;
  138. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  139. BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
  140. tcmu_cmd->data_length += se_cmd->t_bidi_data_sg->length;
  141. }
  142. tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT);
  143. idr_preload(GFP_KERNEL);
  144. spin_lock_irq(&udev->commands_lock);
  145. cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
  146. USHRT_MAX, GFP_NOWAIT);
  147. spin_unlock_irq(&udev->commands_lock);
  148. idr_preload_end();
  149. if (cmd_id < 0) {
  150. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  151. return NULL;
  152. }
  153. tcmu_cmd->cmd_id = cmd_id;
  154. return tcmu_cmd;
  155. }
  156. static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
  157. {
  158. unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK;
  159. size = round_up(size+offset, PAGE_SIZE);
  160. vaddr -= offset;
  161. while (size) {
  162. flush_dcache_page(virt_to_page(vaddr));
  163. size -= PAGE_SIZE;
  164. }
  165. }
  166. /*
  167. * Some ring helper functions. We don't assume size is a power of 2 so
  168. * we can't use circ_buf.h.
  169. */
  170. static inline size_t spc_used(size_t head, size_t tail, size_t size)
  171. {
  172. int diff = head - tail;
  173. if (diff >= 0)
  174. return diff;
  175. else
  176. return size + diff;
  177. }
  178. static inline size_t spc_free(size_t head, size_t tail, size_t size)
  179. {
  180. /* Keep 1 byte unused or we can't tell full from empty */
  181. return (size - spc_used(head, tail, size) - 1);
  182. }
  183. static inline size_t head_to_end(size_t head, size_t size)
  184. {
  185. return size - head;
  186. }
  187. #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
  188. static void alloc_and_scatter_data_area(struct tcmu_dev *udev,
  189. struct scatterlist *data_sg, unsigned int data_nents,
  190. struct iovec **iov, int *iov_cnt, bool copy_data)
  191. {
  192. int i;
  193. void *from, *to;
  194. size_t copy_bytes;
  195. struct scatterlist *sg;
  196. for_each_sg(data_sg, sg, data_nents, i) {
  197. copy_bytes = min_t(size_t, sg->length,
  198. head_to_end(udev->data_head, udev->data_size));
  199. from = kmap_atomic(sg_page(sg)) + sg->offset;
  200. to = (void *) udev->mb_addr + udev->data_off + udev->data_head;
  201. if (copy_data) {
  202. memcpy(to, from, copy_bytes);
  203. tcmu_flush_dcache_range(to, copy_bytes);
  204. }
  205. /* Even iov_base is relative to mb_addr */
  206. (*iov)->iov_len = copy_bytes;
  207. (*iov)->iov_base = (void __user *) udev->data_off +
  208. udev->data_head;
  209. (*iov_cnt)++;
  210. (*iov)++;
  211. UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size);
  212. /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
  213. if (sg->length != copy_bytes) {
  214. void *from_skip = from + copy_bytes;
  215. copy_bytes = sg->length - copy_bytes;
  216. (*iov)->iov_len = copy_bytes;
  217. (*iov)->iov_base = (void __user *) udev->data_off +
  218. udev->data_head;
  219. if (copy_data) {
  220. to = (void *) udev->mb_addr +
  221. udev->data_off + udev->data_head;
  222. memcpy(to, from_skip, copy_bytes);
  223. tcmu_flush_dcache_range(to, copy_bytes);
  224. }
  225. (*iov_cnt)++;
  226. (*iov)++;
  227. UPDATE_HEAD(udev->data_head,
  228. copy_bytes, udev->data_size);
  229. }
  230. kunmap_atomic(from - sg->offset);
  231. }
  232. }
  233. static void gather_and_free_data_area(struct tcmu_dev *udev,
  234. struct scatterlist *data_sg, unsigned int data_nents)
  235. {
  236. int i;
  237. void *from, *to;
  238. size_t copy_bytes;
  239. struct scatterlist *sg;
  240. /* It'd be easier to look at entry's iovec again, but UAM */
  241. for_each_sg(data_sg, sg, data_nents, i) {
  242. copy_bytes = min_t(size_t, sg->length,
  243. head_to_end(udev->data_tail, udev->data_size));
  244. to = kmap_atomic(sg_page(sg)) + sg->offset;
  245. WARN_ON(sg->length + sg->offset > PAGE_SIZE);
  246. from = (void *) udev->mb_addr +
  247. udev->data_off + udev->data_tail;
  248. tcmu_flush_dcache_range(from, copy_bytes);
  249. memcpy(to, from, copy_bytes);
  250. UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
  251. /* Uh oh, wrapped the data buffer for this sg's data */
  252. if (sg->length != copy_bytes) {
  253. void *to_skip = to + copy_bytes;
  254. from = (void *) udev->mb_addr +
  255. udev->data_off + udev->data_tail;
  256. WARN_ON(udev->data_tail);
  257. copy_bytes = sg->length - copy_bytes;
  258. tcmu_flush_dcache_range(from, copy_bytes);
  259. memcpy(to_skip, from, copy_bytes);
  260. UPDATE_HEAD(udev->data_tail,
  261. copy_bytes, udev->data_size);
  262. }
  263. kunmap_atomic(to - sg->offset);
  264. }
  265. }
  266. /*
  267. * We can't queue a command until we have space available on the cmd ring *and*
  268. * space available on the data ring.
  269. *
  270. * Called with ring lock held.
  271. */
  272. static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
  273. {
  274. struct tcmu_mailbox *mb = udev->mb_addr;
  275. size_t space;
  276. u32 cmd_head;
  277. size_t cmd_needed;
  278. tcmu_flush_dcache_range(mb, sizeof(*mb));
  279. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  280. /*
  281. * If cmd end-of-ring space is too small then we need space for a NOP plus
  282. * original cmd - cmds are internally contiguous.
  283. */
  284. if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
  285. cmd_needed = cmd_size;
  286. else
  287. cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
  288. space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
  289. if (space < cmd_needed) {
  290. pr_debug("no cmd space: %u %u %u\n", cmd_head,
  291. udev->cmdr_last_cleaned, udev->cmdr_size);
  292. return false;
  293. }
  294. space = spc_free(udev->data_head, udev->data_tail, udev->data_size);
  295. if (space < data_needed) {
  296. pr_debug("no data space: %zu %zu %zu\n", udev->data_head,
  297. udev->data_tail, udev->data_size);
  298. return false;
  299. }
  300. return true;
  301. }
  302. static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
  303. {
  304. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  305. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  306. size_t base_command_size, command_size;
  307. struct tcmu_mailbox *mb;
  308. struct tcmu_cmd_entry *entry;
  309. struct iovec *iov;
  310. int iov_cnt;
  311. uint32_t cmd_head;
  312. uint64_t cdb_off;
  313. bool copy_to_data_area;
  314. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
  315. return -EINVAL;
  316. /*
  317. * Must be a certain minimum size for response sense info, but
  318. * also may be larger if the iov array is large.
  319. *
  320. * iovs = sgl_nents+1, for end-of-ring case, plus another 1
  321. * b/c size == offsetof one-past-element.
  322. */
  323. base_command_size = max(offsetof(struct tcmu_cmd_entry,
  324. req.iov[se_cmd->t_bidi_data_nents +
  325. se_cmd->t_data_nents + 2]),
  326. sizeof(struct tcmu_cmd_entry));
  327. command_size = base_command_size
  328. + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
  329. WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
  330. spin_lock_irq(&udev->cmdr_lock);
  331. mb = udev->mb_addr;
  332. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  333. if ((command_size > (udev->cmdr_size / 2))
  334. || tcmu_cmd->data_length > (udev->data_size - 1))
  335. pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
  336. "cmd/data ring buffers\n", command_size, tcmu_cmd->data_length,
  337. udev->cmdr_size, udev->data_size);
  338. while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) {
  339. int ret;
  340. DEFINE_WAIT(__wait);
  341. prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
  342. pr_debug("sleeping for ring space\n");
  343. spin_unlock_irq(&udev->cmdr_lock);
  344. ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
  345. finish_wait(&udev->wait_cmdr, &__wait);
  346. if (!ret) {
  347. pr_warn("tcmu: command timed out\n");
  348. return -ETIMEDOUT;
  349. }
  350. spin_lock_irq(&udev->cmdr_lock);
  351. /* We dropped cmdr_lock, cmd_head is stale */
  352. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  353. }
  354. /* Insert a PAD if end-of-ring space is too small */
  355. if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
  356. size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
  357. entry = (void *) mb + CMDR_OFF + cmd_head;
  358. tcmu_flush_dcache_range(entry, sizeof(*entry));
  359. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
  360. tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
  361. entry->hdr.cmd_id = 0; /* not used for PAD */
  362. entry->hdr.kflags = 0;
  363. entry->hdr.uflags = 0;
  364. UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
  365. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  366. WARN_ON(cmd_head != 0);
  367. }
  368. entry = (void *) mb + CMDR_OFF + cmd_head;
  369. tcmu_flush_dcache_range(entry, sizeof(*entry));
  370. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
  371. tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
  372. entry->hdr.cmd_id = tcmu_cmd->cmd_id;
  373. entry->hdr.kflags = 0;
  374. entry->hdr.uflags = 0;
  375. /*
  376. * Fix up iovecs, and handle if allocation in data ring wrapped.
  377. */
  378. iov = &entry->req.iov[0];
  379. iov_cnt = 0;
  380. copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
  381. || se_cmd->se_cmd_flags & SCF_BIDI);
  382. alloc_and_scatter_data_area(udev, se_cmd->t_data_sg,
  383. se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area);
  384. entry->req.iov_cnt = iov_cnt;
  385. entry->req.iov_dif_cnt = 0;
  386. /* Handle BIDI commands */
  387. iov_cnt = 0;
  388. alloc_and_scatter_data_area(udev, se_cmd->t_bidi_data_sg,
  389. se_cmd->t_bidi_data_nents, &iov, &iov_cnt, false);
  390. entry->req.iov_bidi_cnt = iov_cnt;
  391. /* All offsets relative to mb_addr, not start of entry! */
  392. cdb_off = CMDR_OFF + cmd_head + base_command_size;
  393. memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
  394. entry->req.cdb_off = cdb_off;
  395. tcmu_flush_dcache_range(entry, sizeof(*entry));
  396. UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
  397. tcmu_flush_dcache_range(mb, sizeof(*mb));
  398. spin_unlock_irq(&udev->cmdr_lock);
  399. /* TODO: only if FLUSH and FUA? */
  400. uio_event_notify(&udev->uio_info);
  401. mod_timer(&udev->timeout,
  402. round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
  403. return 0;
  404. }
  405. static int tcmu_queue_cmd(struct se_cmd *se_cmd)
  406. {
  407. struct se_device *se_dev = se_cmd->se_dev;
  408. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  409. struct tcmu_cmd *tcmu_cmd;
  410. int ret;
  411. tcmu_cmd = tcmu_alloc_cmd(se_cmd);
  412. if (!tcmu_cmd)
  413. return -ENOMEM;
  414. ret = tcmu_queue_cmd_ring(tcmu_cmd);
  415. if (ret < 0) {
  416. pr_err("TCMU: Could not queue command\n");
  417. spin_lock_irq(&udev->commands_lock);
  418. idr_remove(&udev->commands, tcmu_cmd->cmd_id);
  419. spin_unlock_irq(&udev->commands_lock);
  420. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  421. }
  422. return ret;
  423. }
  424. static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
  425. {
  426. struct se_cmd *se_cmd = cmd->se_cmd;
  427. struct tcmu_dev *udev = cmd->tcmu_dev;
  428. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
  429. /* cmd has been completed already from timeout, just reclaim data
  430. ring space */
  431. UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
  432. return;
  433. }
  434. if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
  435. UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
  436. pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
  437. cmd->se_cmd);
  438. entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
  439. } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
  440. memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
  441. se_cmd->scsi_sense_length);
  442. UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
  443. } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
  444. /* Discard data_out buffer */
  445. UPDATE_HEAD(udev->data_tail,
  446. (size_t)se_cmd->t_data_sg->length, udev->data_size);
  447. /* Get Data-In buffer */
  448. gather_and_free_data_area(udev,
  449. se_cmd->t_bidi_data_sg, se_cmd->t_bidi_data_nents);
  450. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  451. gather_and_free_data_area(udev,
  452. se_cmd->t_data_sg, se_cmd->t_data_nents);
  453. } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
  454. UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
  455. } else if (se_cmd->data_direction != DMA_NONE) {
  456. pr_warn("TCMU: data direction was %d!\n",
  457. se_cmd->data_direction);
  458. }
  459. target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
  460. cmd->se_cmd = NULL;
  461. kmem_cache_free(tcmu_cmd_cache, cmd);
  462. }
  463. static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
  464. {
  465. struct tcmu_mailbox *mb;
  466. unsigned long flags;
  467. int handled = 0;
  468. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
  469. pr_err("ring broken, not handling completions\n");
  470. return 0;
  471. }
  472. spin_lock_irqsave(&udev->cmdr_lock, flags);
  473. mb = udev->mb_addr;
  474. tcmu_flush_dcache_range(mb, sizeof(*mb));
  475. while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
  476. struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
  477. struct tcmu_cmd *cmd;
  478. tcmu_flush_dcache_range(entry, sizeof(*entry));
  479. if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
  480. UPDATE_HEAD(udev->cmdr_last_cleaned,
  481. tcmu_hdr_get_len(entry->hdr.len_op),
  482. udev->cmdr_size);
  483. continue;
  484. }
  485. WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
  486. spin_lock(&udev->commands_lock);
  487. cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
  488. if (cmd)
  489. idr_remove(&udev->commands, cmd->cmd_id);
  490. spin_unlock(&udev->commands_lock);
  491. if (!cmd) {
  492. pr_err("cmd_id not found, ring is broken\n");
  493. set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
  494. break;
  495. }
  496. tcmu_handle_completion(cmd, entry);
  497. UPDATE_HEAD(udev->cmdr_last_cleaned,
  498. tcmu_hdr_get_len(entry->hdr.len_op),
  499. udev->cmdr_size);
  500. handled++;
  501. }
  502. if (mb->cmd_tail == mb->cmd_head)
  503. del_timer(&udev->timeout); /* no more pending cmds */
  504. spin_unlock_irqrestore(&udev->cmdr_lock, flags);
  505. wake_up(&udev->wait_cmdr);
  506. return handled;
  507. }
  508. static int tcmu_check_expired_cmd(int id, void *p, void *data)
  509. {
  510. struct tcmu_cmd *cmd = p;
  511. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  512. return 0;
  513. if (!time_after(jiffies, cmd->deadline))
  514. return 0;
  515. set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
  516. target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
  517. cmd->se_cmd = NULL;
  518. return 0;
  519. }
  520. static void tcmu_device_timedout(unsigned long data)
  521. {
  522. struct tcmu_dev *udev = (struct tcmu_dev *)data;
  523. unsigned long flags;
  524. int handled;
  525. handled = tcmu_handle_completions(udev);
  526. pr_warn("%d completions handled from timeout\n", handled);
  527. spin_lock_irqsave(&udev->commands_lock, flags);
  528. idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
  529. spin_unlock_irqrestore(&udev->commands_lock, flags);
  530. /*
  531. * We don't need to wakeup threads on wait_cmdr since they have their
  532. * own timeout.
  533. */
  534. }
  535. static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
  536. {
  537. struct tcmu_hba *tcmu_hba;
  538. tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
  539. if (!tcmu_hba)
  540. return -ENOMEM;
  541. tcmu_hba->host_id = host_id;
  542. hba->hba_ptr = tcmu_hba;
  543. return 0;
  544. }
  545. static void tcmu_detach_hba(struct se_hba *hba)
  546. {
  547. kfree(hba->hba_ptr);
  548. hba->hba_ptr = NULL;
  549. }
  550. static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
  551. {
  552. struct tcmu_dev *udev;
  553. udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
  554. if (!udev)
  555. return NULL;
  556. udev->name = kstrdup(name, GFP_KERNEL);
  557. if (!udev->name) {
  558. kfree(udev);
  559. return NULL;
  560. }
  561. udev->hba = hba;
  562. init_waitqueue_head(&udev->wait_cmdr);
  563. spin_lock_init(&udev->cmdr_lock);
  564. idr_init(&udev->commands);
  565. spin_lock_init(&udev->commands_lock);
  566. setup_timer(&udev->timeout, tcmu_device_timedout,
  567. (unsigned long)udev);
  568. return &udev->se_dev;
  569. }
  570. static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
  571. {
  572. struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
  573. tcmu_handle_completions(tcmu_dev);
  574. return 0;
  575. }
  576. /*
  577. * mmap code from uio.c. Copied here because we want to hook mmap()
  578. * and this stuff must come along.
  579. */
  580. static int tcmu_find_mem_index(struct vm_area_struct *vma)
  581. {
  582. struct tcmu_dev *udev = vma->vm_private_data;
  583. struct uio_info *info = &udev->uio_info;
  584. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  585. if (info->mem[vma->vm_pgoff].size == 0)
  586. return -1;
  587. return (int)vma->vm_pgoff;
  588. }
  589. return -1;
  590. }
  591. static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  592. {
  593. struct tcmu_dev *udev = vma->vm_private_data;
  594. struct uio_info *info = &udev->uio_info;
  595. struct page *page;
  596. unsigned long offset;
  597. void *addr;
  598. int mi = tcmu_find_mem_index(vma);
  599. if (mi < 0)
  600. return VM_FAULT_SIGBUS;
  601. /*
  602. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  603. * to use mem[N].
  604. */
  605. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  606. addr = (void *)(unsigned long)info->mem[mi].addr + offset;
  607. if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
  608. page = virt_to_page(addr);
  609. else
  610. page = vmalloc_to_page(addr);
  611. get_page(page);
  612. vmf->page = page;
  613. return 0;
  614. }
  615. static const struct vm_operations_struct tcmu_vm_ops = {
  616. .fault = tcmu_vma_fault,
  617. };
  618. static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
  619. {
  620. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  621. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  622. vma->vm_ops = &tcmu_vm_ops;
  623. vma->vm_private_data = udev;
  624. /* Ensure the mmap is exactly the right size */
  625. if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
  626. return -EINVAL;
  627. return 0;
  628. }
  629. static int tcmu_open(struct uio_info *info, struct inode *inode)
  630. {
  631. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  632. /* O_EXCL not supported for char devs, so fake it? */
  633. if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
  634. return -EBUSY;
  635. pr_debug("open\n");
  636. return 0;
  637. }
  638. static int tcmu_release(struct uio_info *info, struct inode *inode)
  639. {
  640. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  641. clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
  642. pr_debug("close\n");
  643. return 0;
  644. }
  645. static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
  646. {
  647. struct sk_buff *skb;
  648. void *msg_header;
  649. int ret = -ENOMEM;
  650. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  651. if (!skb)
  652. return ret;
  653. msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
  654. if (!msg_header)
  655. goto free_skb;
  656. ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
  657. if (ret < 0)
  658. goto free_skb;
  659. ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
  660. if (ret < 0)
  661. goto free_skb;
  662. genlmsg_end(skb, msg_header);
  663. ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
  664. TCMU_MCGRP_CONFIG, GFP_KERNEL);
  665. /* We don't care if no one is listening */
  666. if (ret == -ESRCH)
  667. ret = 0;
  668. return ret;
  669. free_skb:
  670. nlmsg_free(skb);
  671. return ret;
  672. }
  673. static int tcmu_configure_device(struct se_device *dev)
  674. {
  675. struct tcmu_dev *udev = TCMU_DEV(dev);
  676. struct tcmu_hba *hba = udev->hba->hba_ptr;
  677. struct uio_info *info;
  678. struct tcmu_mailbox *mb;
  679. size_t size;
  680. size_t used;
  681. int ret = 0;
  682. char *str;
  683. info = &udev->uio_info;
  684. size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
  685. udev->dev_config);
  686. size += 1; /* for \0 */
  687. str = kmalloc(size, GFP_KERNEL);
  688. if (!str)
  689. return -ENOMEM;
  690. used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
  691. if (udev->dev_config[0])
  692. snprintf(str + used, size - used, "/%s", udev->dev_config);
  693. info->name = str;
  694. udev->mb_addr = vzalloc(TCMU_RING_SIZE);
  695. if (!udev->mb_addr) {
  696. ret = -ENOMEM;
  697. goto err_vzalloc;
  698. }
  699. /* mailbox fits in first part of CMDR space */
  700. udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
  701. udev->data_off = CMDR_SIZE;
  702. udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
  703. mb = udev->mb_addr;
  704. mb->version = TCMU_MAILBOX_VERSION;
  705. mb->cmdr_off = CMDR_OFF;
  706. mb->cmdr_size = udev->cmdr_size;
  707. WARN_ON(!PAGE_ALIGNED(udev->data_off));
  708. WARN_ON(udev->data_size % PAGE_SIZE);
  709. info->version = __stringify(TCMU_MAILBOX_VERSION);
  710. info->mem[0].name = "tcm-user command & data buffer";
  711. info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
  712. info->mem[0].size = TCMU_RING_SIZE;
  713. info->mem[0].memtype = UIO_MEM_VIRTUAL;
  714. info->irqcontrol = tcmu_irqcontrol;
  715. info->irq = UIO_IRQ_CUSTOM;
  716. info->mmap = tcmu_mmap;
  717. info->open = tcmu_open;
  718. info->release = tcmu_release;
  719. ret = uio_register_device(tcmu_root_device, info);
  720. if (ret)
  721. goto err_register;
  722. /* Other attributes can be configured in userspace */
  723. dev->dev_attrib.hw_block_size = 512;
  724. dev->dev_attrib.hw_max_sectors = 128;
  725. dev->dev_attrib.hw_queue_depth = 128;
  726. ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
  727. udev->uio_info.uio_dev->minor);
  728. if (ret)
  729. goto err_netlink;
  730. return 0;
  731. err_netlink:
  732. uio_unregister_device(&udev->uio_info);
  733. err_register:
  734. vfree(udev->mb_addr);
  735. err_vzalloc:
  736. kfree(info->name);
  737. return ret;
  738. }
  739. static int tcmu_check_pending_cmd(int id, void *p, void *data)
  740. {
  741. struct tcmu_cmd *cmd = p;
  742. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  743. return 0;
  744. return -EINVAL;
  745. }
  746. static void tcmu_dev_call_rcu(struct rcu_head *p)
  747. {
  748. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  749. struct tcmu_dev *udev = TCMU_DEV(dev);
  750. kfree(udev);
  751. }
  752. static void tcmu_free_device(struct se_device *dev)
  753. {
  754. struct tcmu_dev *udev = TCMU_DEV(dev);
  755. int i;
  756. del_timer_sync(&udev->timeout);
  757. vfree(udev->mb_addr);
  758. /* Upper layer should drain all requests before calling this */
  759. spin_lock_irq(&udev->commands_lock);
  760. i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
  761. idr_destroy(&udev->commands);
  762. spin_unlock_irq(&udev->commands_lock);
  763. WARN_ON(i);
  764. /* Device was configured */
  765. if (udev->uio_info.uio_dev) {
  766. tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
  767. udev->uio_info.uio_dev->minor);
  768. uio_unregister_device(&udev->uio_info);
  769. kfree(udev->uio_info.name);
  770. kfree(udev->name);
  771. }
  772. call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
  773. }
  774. enum {
  775. Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
  776. };
  777. static match_table_t tokens = {
  778. {Opt_dev_config, "dev_config=%s"},
  779. {Opt_dev_size, "dev_size=%u"},
  780. {Opt_hw_block_size, "hw_block_size=%u"},
  781. {Opt_err, NULL}
  782. };
  783. static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
  784. const char *page, ssize_t count)
  785. {
  786. struct tcmu_dev *udev = TCMU_DEV(dev);
  787. char *orig, *ptr, *opts, *arg_p;
  788. substring_t args[MAX_OPT_ARGS];
  789. int ret = 0, token;
  790. unsigned long tmp_ul;
  791. opts = kstrdup(page, GFP_KERNEL);
  792. if (!opts)
  793. return -ENOMEM;
  794. orig = opts;
  795. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  796. if (!*ptr)
  797. continue;
  798. token = match_token(ptr, tokens, args);
  799. switch (token) {
  800. case Opt_dev_config:
  801. if (match_strlcpy(udev->dev_config, &args[0],
  802. TCMU_CONFIG_LEN) == 0) {
  803. ret = -EINVAL;
  804. break;
  805. }
  806. pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
  807. break;
  808. case Opt_dev_size:
  809. arg_p = match_strdup(&args[0]);
  810. if (!arg_p) {
  811. ret = -ENOMEM;
  812. break;
  813. }
  814. ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
  815. kfree(arg_p);
  816. if (ret < 0)
  817. pr_err("kstrtoul() failed for dev_size=\n");
  818. break;
  819. case Opt_hw_block_size:
  820. arg_p = match_strdup(&args[0]);
  821. if (!arg_p) {
  822. ret = -ENOMEM;
  823. break;
  824. }
  825. ret = kstrtoul(arg_p, 0, &tmp_ul);
  826. kfree(arg_p);
  827. if (ret < 0) {
  828. pr_err("kstrtoul() failed for hw_block_size=\n");
  829. break;
  830. }
  831. if (!tmp_ul) {
  832. pr_err("hw_block_size must be nonzero\n");
  833. break;
  834. }
  835. dev->dev_attrib.hw_block_size = tmp_ul;
  836. break;
  837. default:
  838. break;
  839. }
  840. }
  841. kfree(orig);
  842. return (!ret) ? count : ret;
  843. }
  844. static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
  845. {
  846. struct tcmu_dev *udev = TCMU_DEV(dev);
  847. ssize_t bl = 0;
  848. bl = sprintf(b + bl, "Config: %s ",
  849. udev->dev_config[0] ? udev->dev_config : "NULL");
  850. bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
  851. return bl;
  852. }
  853. static sector_t tcmu_get_blocks(struct se_device *dev)
  854. {
  855. struct tcmu_dev *udev = TCMU_DEV(dev);
  856. return div_u64(udev->dev_size - dev->dev_attrib.block_size,
  857. dev->dev_attrib.block_size);
  858. }
  859. static sense_reason_t
  860. tcmu_pass_op(struct se_cmd *se_cmd)
  861. {
  862. int ret = tcmu_queue_cmd(se_cmd);
  863. if (ret != 0)
  864. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  865. else
  866. return TCM_NO_SENSE;
  867. }
  868. static sense_reason_t
  869. tcmu_parse_cdb(struct se_cmd *cmd)
  870. {
  871. return passthrough_parse_cdb(cmd, tcmu_pass_op);
  872. }
  873. static const struct target_backend_ops tcmu_ops = {
  874. .name = "user",
  875. .owner = THIS_MODULE,
  876. .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
  877. .attach_hba = tcmu_attach_hba,
  878. .detach_hba = tcmu_detach_hba,
  879. .alloc_device = tcmu_alloc_device,
  880. .configure_device = tcmu_configure_device,
  881. .free_device = tcmu_free_device,
  882. .parse_cdb = tcmu_parse_cdb,
  883. .set_configfs_dev_params = tcmu_set_configfs_dev_params,
  884. .show_configfs_dev_params = tcmu_show_configfs_dev_params,
  885. .get_device_type = sbc_get_device_type,
  886. .get_blocks = tcmu_get_blocks,
  887. .tb_dev_attrib_attrs = passthrough_attrib_attrs,
  888. };
  889. static int __init tcmu_module_init(void)
  890. {
  891. int ret;
  892. BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
  893. tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
  894. sizeof(struct tcmu_cmd),
  895. __alignof__(struct tcmu_cmd),
  896. 0, NULL);
  897. if (!tcmu_cmd_cache)
  898. return -ENOMEM;
  899. tcmu_root_device = root_device_register("tcm_user");
  900. if (IS_ERR(tcmu_root_device)) {
  901. ret = PTR_ERR(tcmu_root_device);
  902. goto out_free_cache;
  903. }
  904. ret = genl_register_family(&tcmu_genl_family);
  905. if (ret < 0) {
  906. goto out_unreg_device;
  907. }
  908. ret = transport_backend_register(&tcmu_ops);
  909. if (ret)
  910. goto out_unreg_genl;
  911. return 0;
  912. out_unreg_genl:
  913. genl_unregister_family(&tcmu_genl_family);
  914. out_unreg_device:
  915. root_device_unregister(tcmu_root_device);
  916. out_free_cache:
  917. kmem_cache_destroy(tcmu_cmd_cache);
  918. return ret;
  919. }
  920. static void __exit tcmu_module_exit(void)
  921. {
  922. target_backend_unregister(&tcmu_ops);
  923. genl_unregister_family(&tcmu_genl_family);
  924. root_device_unregister(tcmu_root_device);
  925. kmem_cache_destroy(tcmu_cmd_cache);
  926. }
  927. MODULE_DESCRIPTION("TCM USER subsystem plugin");
  928. MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
  929. MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
  930. MODULE_LICENSE("GPL");
  931. module_init(tcmu_module_init);
  932. module_exit(tcmu_module_exit);