target_core_user.c 29 KB

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