target_core_user.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 {
  420. pr_warn("TCMU: data direction was %d!\n", se_cmd->data_direction);
  421. }
  422. target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
  423. cmd->se_cmd = NULL;
  424. kmem_cache_free(tcmu_cmd_cache, cmd);
  425. }
  426. static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
  427. {
  428. struct tcmu_mailbox *mb;
  429. LIST_HEAD(cpl_cmds);
  430. unsigned long flags;
  431. int handled = 0;
  432. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
  433. pr_err("ring broken, not handling completions\n");
  434. return 0;
  435. }
  436. spin_lock_irqsave(&udev->cmdr_lock, flags);
  437. mb = udev->mb_addr;
  438. tcmu_flush_dcache_range(mb, sizeof(*mb));
  439. while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
  440. struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
  441. struct tcmu_cmd *cmd;
  442. tcmu_flush_dcache_range(entry, sizeof(*entry));
  443. if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
  444. UPDATE_HEAD(udev->cmdr_last_cleaned,
  445. tcmu_hdr_get_len(entry->hdr.len_op),
  446. udev->cmdr_size);
  447. continue;
  448. }
  449. WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
  450. spin_lock(&udev->commands_lock);
  451. cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
  452. if (cmd)
  453. idr_remove(&udev->commands, cmd->cmd_id);
  454. spin_unlock(&udev->commands_lock);
  455. if (!cmd) {
  456. pr_err("cmd_id not found, ring is broken\n");
  457. set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
  458. break;
  459. }
  460. tcmu_handle_completion(cmd, entry);
  461. UPDATE_HEAD(udev->cmdr_last_cleaned,
  462. tcmu_hdr_get_len(entry->hdr.len_op),
  463. udev->cmdr_size);
  464. handled++;
  465. }
  466. if (mb->cmd_tail == mb->cmd_head)
  467. del_timer(&udev->timeout); /* no more pending cmds */
  468. spin_unlock_irqrestore(&udev->cmdr_lock, flags);
  469. wake_up(&udev->wait_cmdr);
  470. return handled;
  471. }
  472. static int tcmu_check_expired_cmd(int id, void *p, void *data)
  473. {
  474. struct tcmu_cmd *cmd = p;
  475. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  476. return 0;
  477. if (!time_after(cmd->deadline, jiffies))
  478. return 0;
  479. set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
  480. target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
  481. cmd->se_cmd = NULL;
  482. kmem_cache_free(tcmu_cmd_cache, cmd);
  483. return 0;
  484. }
  485. static void tcmu_device_timedout(unsigned long data)
  486. {
  487. struct tcmu_dev *udev = (struct tcmu_dev *)data;
  488. unsigned long flags;
  489. int handled;
  490. handled = tcmu_handle_completions(udev);
  491. pr_warn("%d completions handled from timeout\n", handled);
  492. spin_lock_irqsave(&udev->commands_lock, flags);
  493. idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
  494. spin_unlock_irqrestore(&udev->commands_lock, flags);
  495. /*
  496. * We don't need to wakeup threads on wait_cmdr since they have their
  497. * own timeout.
  498. */
  499. }
  500. static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
  501. {
  502. struct tcmu_hba *tcmu_hba;
  503. tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
  504. if (!tcmu_hba)
  505. return -ENOMEM;
  506. tcmu_hba->host_id = host_id;
  507. hba->hba_ptr = tcmu_hba;
  508. return 0;
  509. }
  510. static void tcmu_detach_hba(struct se_hba *hba)
  511. {
  512. kfree(hba->hba_ptr);
  513. hba->hba_ptr = NULL;
  514. }
  515. static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
  516. {
  517. struct tcmu_dev *udev;
  518. udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
  519. if (!udev)
  520. return NULL;
  521. udev->name = kstrdup(name, GFP_KERNEL);
  522. if (!udev->name) {
  523. kfree(udev);
  524. return NULL;
  525. }
  526. udev->hba = hba;
  527. init_waitqueue_head(&udev->wait_cmdr);
  528. spin_lock_init(&udev->cmdr_lock);
  529. idr_init(&udev->commands);
  530. spin_lock_init(&udev->commands_lock);
  531. setup_timer(&udev->timeout, tcmu_device_timedout,
  532. (unsigned long)udev);
  533. return &udev->se_dev;
  534. }
  535. static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
  536. {
  537. struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
  538. tcmu_handle_completions(tcmu_dev);
  539. return 0;
  540. }
  541. /*
  542. * mmap code from uio.c. Copied here because we want to hook mmap()
  543. * and this stuff must come along.
  544. */
  545. static int tcmu_find_mem_index(struct vm_area_struct *vma)
  546. {
  547. struct tcmu_dev *udev = vma->vm_private_data;
  548. struct uio_info *info = &udev->uio_info;
  549. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  550. if (info->mem[vma->vm_pgoff].size == 0)
  551. return -1;
  552. return (int)vma->vm_pgoff;
  553. }
  554. return -1;
  555. }
  556. static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  557. {
  558. struct tcmu_dev *udev = vma->vm_private_data;
  559. struct uio_info *info = &udev->uio_info;
  560. struct page *page;
  561. unsigned long offset;
  562. void *addr;
  563. int mi = tcmu_find_mem_index(vma);
  564. if (mi < 0)
  565. return VM_FAULT_SIGBUS;
  566. /*
  567. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  568. * to use mem[N].
  569. */
  570. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  571. addr = (void *)(unsigned long)info->mem[mi].addr + offset;
  572. if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
  573. page = virt_to_page(addr);
  574. else
  575. page = vmalloc_to_page(addr);
  576. get_page(page);
  577. vmf->page = page;
  578. return 0;
  579. }
  580. static const struct vm_operations_struct tcmu_vm_ops = {
  581. .fault = tcmu_vma_fault,
  582. };
  583. static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
  584. {
  585. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  586. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  587. vma->vm_ops = &tcmu_vm_ops;
  588. vma->vm_private_data = udev;
  589. /* Ensure the mmap is exactly the right size */
  590. if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
  591. return -EINVAL;
  592. return 0;
  593. }
  594. static int tcmu_open(struct uio_info *info, struct inode *inode)
  595. {
  596. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  597. /* O_EXCL not supported for char devs, so fake it? */
  598. if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
  599. return -EBUSY;
  600. pr_debug("open\n");
  601. return 0;
  602. }
  603. static int tcmu_release(struct uio_info *info, struct inode *inode)
  604. {
  605. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  606. clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
  607. pr_debug("close\n");
  608. return 0;
  609. }
  610. static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
  611. {
  612. struct sk_buff *skb;
  613. void *msg_header;
  614. int ret = -ENOMEM;
  615. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  616. if (!skb)
  617. return ret;
  618. msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
  619. if (!msg_header)
  620. goto free_skb;
  621. ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
  622. if (ret < 0)
  623. goto free_skb;
  624. ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
  625. if (ret < 0)
  626. goto free_skb;
  627. genlmsg_end(skb, msg_header);
  628. ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
  629. TCMU_MCGRP_CONFIG, GFP_KERNEL);
  630. /* We don't care if no one is listening */
  631. if (ret == -ESRCH)
  632. ret = 0;
  633. return ret;
  634. free_skb:
  635. nlmsg_free(skb);
  636. return ret;
  637. }
  638. static int tcmu_configure_device(struct se_device *dev)
  639. {
  640. struct tcmu_dev *udev = TCMU_DEV(dev);
  641. struct tcmu_hba *hba = udev->hba->hba_ptr;
  642. struct uio_info *info;
  643. struct tcmu_mailbox *mb;
  644. size_t size;
  645. size_t used;
  646. int ret = 0;
  647. char *str;
  648. info = &udev->uio_info;
  649. size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
  650. udev->dev_config);
  651. size += 1; /* for \0 */
  652. str = kmalloc(size, GFP_KERNEL);
  653. if (!str)
  654. return -ENOMEM;
  655. used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
  656. if (udev->dev_config[0])
  657. snprintf(str + used, size - used, "/%s", udev->dev_config);
  658. info->name = str;
  659. udev->mb_addr = vzalloc(TCMU_RING_SIZE);
  660. if (!udev->mb_addr) {
  661. ret = -ENOMEM;
  662. goto err_vzalloc;
  663. }
  664. /* mailbox fits in first part of CMDR space */
  665. udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
  666. udev->data_off = CMDR_SIZE;
  667. udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
  668. mb = udev->mb_addr;
  669. mb->version = TCMU_MAILBOX_VERSION;
  670. mb->cmdr_off = CMDR_OFF;
  671. mb->cmdr_size = udev->cmdr_size;
  672. WARN_ON(!PAGE_ALIGNED(udev->data_off));
  673. WARN_ON(udev->data_size % PAGE_SIZE);
  674. info->version = xstr(TCMU_MAILBOX_VERSION);
  675. info->mem[0].name = "tcm-user command & data buffer";
  676. info->mem[0].addr = (phys_addr_t) udev->mb_addr;
  677. info->mem[0].size = TCMU_RING_SIZE;
  678. info->mem[0].memtype = UIO_MEM_VIRTUAL;
  679. info->irqcontrol = tcmu_irqcontrol;
  680. info->irq = UIO_IRQ_CUSTOM;
  681. info->mmap = tcmu_mmap;
  682. info->open = tcmu_open;
  683. info->release = tcmu_release;
  684. ret = uio_register_device(tcmu_root_device, info);
  685. if (ret)
  686. goto err_register;
  687. /* Other attributes can be configured in userspace */
  688. dev->dev_attrib.hw_block_size = 512;
  689. dev->dev_attrib.hw_max_sectors = 128;
  690. dev->dev_attrib.hw_queue_depth = 128;
  691. ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
  692. udev->uio_info.uio_dev->minor);
  693. if (ret)
  694. goto err_netlink;
  695. return 0;
  696. err_netlink:
  697. uio_unregister_device(&udev->uio_info);
  698. err_register:
  699. vfree(udev->mb_addr);
  700. err_vzalloc:
  701. kfree(info->name);
  702. return ret;
  703. }
  704. static int tcmu_check_pending_cmd(int id, void *p, void *data)
  705. {
  706. struct tcmu_cmd *cmd = p;
  707. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  708. return 0;
  709. return -EINVAL;
  710. }
  711. static void tcmu_free_device(struct se_device *dev)
  712. {
  713. struct tcmu_dev *udev = TCMU_DEV(dev);
  714. int i;
  715. del_timer_sync(&udev->timeout);
  716. vfree(udev->mb_addr);
  717. /* Upper layer should drain all requests before calling this */
  718. spin_lock_irq(&udev->commands_lock);
  719. i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
  720. idr_destroy(&udev->commands);
  721. spin_unlock_irq(&udev->commands_lock);
  722. WARN_ON(i);
  723. /* Device was configured */
  724. if (udev->uio_info.uio_dev) {
  725. tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
  726. udev->uio_info.uio_dev->minor);
  727. uio_unregister_device(&udev->uio_info);
  728. kfree(udev->uio_info.name);
  729. kfree(udev->name);
  730. }
  731. kfree(udev);
  732. }
  733. enum {
  734. Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
  735. };
  736. static match_table_t tokens = {
  737. {Opt_dev_config, "dev_config=%s"},
  738. {Opt_dev_size, "dev_size=%u"},
  739. {Opt_hw_block_size, "hw_block_size=%u"},
  740. {Opt_err, NULL}
  741. };
  742. static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
  743. const char *page, ssize_t count)
  744. {
  745. struct tcmu_dev *udev = TCMU_DEV(dev);
  746. char *orig, *ptr, *opts, *arg_p;
  747. substring_t args[MAX_OPT_ARGS];
  748. int ret = 0, token;
  749. unsigned long tmp_ul;
  750. opts = kstrdup(page, GFP_KERNEL);
  751. if (!opts)
  752. return -ENOMEM;
  753. orig = opts;
  754. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  755. if (!*ptr)
  756. continue;
  757. token = match_token(ptr, tokens, args);
  758. switch (token) {
  759. case Opt_dev_config:
  760. if (match_strlcpy(udev->dev_config, &args[0],
  761. TCMU_CONFIG_LEN) == 0) {
  762. ret = -EINVAL;
  763. break;
  764. }
  765. pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
  766. break;
  767. case Opt_dev_size:
  768. arg_p = match_strdup(&args[0]);
  769. if (!arg_p) {
  770. ret = -ENOMEM;
  771. break;
  772. }
  773. ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
  774. kfree(arg_p);
  775. if (ret < 0)
  776. pr_err("kstrtoul() failed for dev_size=\n");
  777. break;
  778. case Opt_hw_block_size:
  779. arg_p = match_strdup(&args[0]);
  780. if (!arg_p) {
  781. ret = -ENOMEM;
  782. break;
  783. }
  784. ret = kstrtoul(arg_p, 0, &tmp_ul);
  785. kfree(arg_p);
  786. if (ret < 0) {
  787. pr_err("kstrtoul() failed for hw_block_size=\n");
  788. break;
  789. }
  790. if (!tmp_ul) {
  791. pr_err("hw_block_size must be nonzero\n");
  792. break;
  793. }
  794. dev->dev_attrib.hw_block_size = tmp_ul;
  795. break;
  796. default:
  797. break;
  798. }
  799. }
  800. kfree(orig);
  801. return (!ret) ? count : ret;
  802. }
  803. static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
  804. {
  805. struct tcmu_dev *udev = TCMU_DEV(dev);
  806. ssize_t bl = 0;
  807. bl = sprintf(b + bl, "Config: %s ",
  808. udev->dev_config[0] ? udev->dev_config : "NULL");
  809. bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
  810. return bl;
  811. }
  812. static sector_t tcmu_get_blocks(struct se_device *dev)
  813. {
  814. struct tcmu_dev *udev = TCMU_DEV(dev);
  815. return div_u64(udev->dev_size - dev->dev_attrib.block_size,
  816. dev->dev_attrib.block_size);
  817. }
  818. static sense_reason_t
  819. tcmu_pass_op(struct se_cmd *se_cmd)
  820. {
  821. int ret = tcmu_queue_cmd(se_cmd);
  822. if (ret != 0)
  823. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  824. else
  825. return TCM_NO_SENSE;
  826. }
  827. static sense_reason_t
  828. tcmu_parse_cdb(struct se_cmd *cmd)
  829. {
  830. return passthrough_parse_cdb(cmd, tcmu_pass_op);
  831. }
  832. DEF_TB_DEV_ATTRIB_RO(tcmu, hw_pi_prot_type);
  833. TB_DEV_ATTR_RO(tcmu, hw_pi_prot_type);
  834. DEF_TB_DEV_ATTRIB_RO(tcmu, hw_block_size);
  835. TB_DEV_ATTR_RO(tcmu, hw_block_size);
  836. DEF_TB_DEV_ATTRIB_RO(tcmu, hw_max_sectors);
  837. TB_DEV_ATTR_RO(tcmu, hw_max_sectors);
  838. DEF_TB_DEV_ATTRIB_RO(tcmu, hw_queue_depth);
  839. TB_DEV_ATTR_RO(tcmu, hw_queue_depth);
  840. static struct configfs_attribute *tcmu_backend_dev_attrs[] = {
  841. &tcmu_dev_attrib_hw_pi_prot_type.attr,
  842. &tcmu_dev_attrib_hw_block_size.attr,
  843. &tcmu_dev_attrib_hw_max_sectors.attr,
  844. &tcmu_dev_attrib_hw_queue_depth.attr,
  845. NULL,
  846. };
  847. static struct se_subsystem_api tcmu_template = {
  848. .name = "user",
  849. .inquiry_prod = "USER",
  850. .inquiry_rev = TCMU_VERSION,
  851. .owner = THIS_MODULE,
  852. .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV,
  853. .attach_hba = tcmu_attach_hba,
  854. .detach_hba = tcmu_detach_hba,
  855. .alloc_device = tcmu_alloc_device,
  856. .configure_device = tcmu_configure_device,
  857. .free_device = tcmu_free_device,
  858. .parse_cdb = tcmu_parse_cdb,
  859. .set_configfs_dev_params = tcmu_set_configfs_dev_params,
  860. .show_configfs_dev_params = tcmu_show_configfs_dev_params,
  861. .get_device_type = sbc_get_device_type,
  862. .get_blocks = tcmu_get_blocks,
  863. };
  864. static int __init tcmu_module_init(void)
  865. {
  866. struct target_backend_cits *tbc = &tcmu_template.tb_cits;
  867. int ret;
  868. BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
  869. tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
  870. sizeof(struct tcmu_cmd),
  871. __alignof__(struct tcmu_cmd),
  872. 0, NULL);
  873. if (!tcmu_cmd_cache)
  874. return -ENOMEM;
  875. tcmu_root_device = root_device_register("tcm_user");
  876. if (IS_ERR(tcmu_root_device)) {
  877. ret = PTR_ERR(tcmu_root_device);
  878. goto out_free_cache;
  879. }
  880. ret = genl_register_family(&tcmu_genl_family);
  881. if (ret < 0) {
  882. goto out_unreg_device;
  883. }
  884. target_core_setup_sub_cits(&tcmu_template);
  885. tbc->tb_dev_attrib_cit.ct_attrs = tcmu_backend_dev_attrs;
  886. ret = transport_subsystem_register(&tcmu_template);
  887. if (ret)
  888. goto out_unreg_genl;
  889. return 0;
  890. out_unreg_genl:
  891. genl_unregister_family(&tcmu_genl_family);
  892. out_unreg_device:
  893. root_device_unregister(tcmu_root_device);
  894. out_free_cache:
  895. kmem_cache_destroy(tcmu_cmd_cache);
  896. return ret;
  897. }
  898. static void __exit tcmu_module_exit(void)
  899. {
  900. transport_subsystem_release(&tcmu_template);
  901. genl_unregister_family(&tcmu_genl_family);
  902. root_device_unregister(tcmu_root_device);
  903. kmem_cache_destroy(tcmu_cmd_cache);
  904. }
  905. MODULE_DESCRIPTION("TCM USER subsystem plugin");
  906. MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
  907. MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
  908. MODULE_LICENSE("GPL");
  909. module_init(tcmu_module_init);
  910. module_exit(tcmu_module_exit);