target_core_user.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /*
  2. * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
  3. * Copyright (C) 2014 Red Hat, Inc.
  4. * Copyright (C) 2015 Arrikto, Inc.
  5. * Copyright (C) 2017 Chinamobile, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/spinlock.h>
  21. #include <linux/module.h>
  22. #include <linux/idr.h>
  23. #include <linux/kernel.h>
  24. #include <linux/timer.h>
  25. #include <linux/parser.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/uio_driver.h>
  28. #include <linux/radix-tree.h>
  29. #include <linux/stringify.h>
  30. #include <linux/bitops.h>
  31. #include <linux/highmem.h>
  32. #include <linux/configfs.h>
  33. #include <linux/mutex.h>
  34. #include <linux/kthread.h>
  35. #include <net/genetlink.h>
  36. #include <scsi/scsi_common.h>
  37. #include <scsi/scsi_proto.h>
  38. #include <target/target_core_base.h>
  39. #include <target/target_core_fabric.h>
  40. #include <target/target_core_backend.h>
  41. #include <linux/target_core_user.h>
  42. /*
  43. * Define a shared-memory interface for LIO to pass SCSI commands and
  44. * data to userspace for processing. This is to allow backends that
  45. * are too complex for in-kernel support to be possible.
  46. *
  47. * It uses the UIO framework to do a lot of the device-creation and
  48. * introspection work for us.
  49. *
  50. * See the .h file for how the ring is laid out. Note that while the
  51. * command ring is defined, the particulars of the data area are
  52. * not. Offset values in the command entry point to other locations
  53. * internal to the mmap()ed area. There is separate space outside the
  54. * command ring for data buffers. This leaves maximum flexibility for
  55. * moving buffer allocations, or even page flipping or other
  56. * allocation techniques, without altering the command ring layout.
  57. *
  58. * SECURITY:
  59. * The user process must be assumed to be malicious. There's no way to
  60. * prevent it breaking the command ring protocol if it wants, but in
  61. * order to prevent other issues we must only ever read *data* from
  62. * the shared memory area, not offsets or sizes. This applies to
  63. * command ring entries as well as the mailbox. Extra code needed for
  64. * this may have a 'UAM' comment.
  65. */
  66. #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
  67. /* For cmd area, the size is fixed 8MB */
  68. #define CMDR_SIZE (8 * 1024 * 1024)
  69. /*
  70. * For data area, the block size is PAGE_SIZE and
  71. * the total size is 256K * PAGE_SIZE.
  72. */
  73. #define DATA_BLOCK_SIZE PAGE_SIZE
  74. #define DATA_BLOCK_BITS (256 * 1024)
  75. #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
  76. #define DATA_BLOCK_INIT_BITS 128
  77. /* The total size of the ring is 8M + 256K * PAGE_SIZE */
  78. #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
  79. /* Default maximum of the global data blocks(512K * PAGE_SIZE) */
  80. #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
  81. static u8 tcmu_kern_cmd_reply_supported;
  82. static struct device *tcmu_root_device;
  83. struct tcmu_hba {
  84. u32 host_id;
  85. };
  86. #define TCMU_CONFIG_LEN 256
  87. struct tcmu_nl_cmd {
  88. /* wake up thread waiting for reply */
  89. struct completion complete;
  90. int cmd;
  91. int status;
  92. };
  93. struct tcmu_dev {
  94. struct list_head node;
  95. struct kref kref;
  96. struct se_device se_dev;
  97. char *name;
  98. struct se_hba *hba;
  99. #define TCMU_DEV_BIT_OPEN 0
  100. #define TCMU_DEV_BIT_BROKEN 1
  101. unsigned long flags;
  102. struct uio_info uio_info;
  103. struct inode *inode;
  104. struct tcmu_mailbox *mb_addr;
  105. size_t dev_size;
  106. u32 cmdr_size;
  107. u32 cmdr_last_cleaned;
  108. /* Offset of data area from start of mb */
  109. /* Must add data_off and mb_addr to get the address */
  110. size_t data_off;
  111. size_t data_size;
  112. wait_queue_head_t wait_cmdr;
  113. struct mutex cmdr_lock;
  114. bool waiting_global;
  115. uint32_t dbi_max;
  116. uint32_t dbi_thresh;
  117. DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
  118. struct radix_tree_root data_blocks;
  119. struct idr commands;
  120. spinlock_t commands_lock;
  121. struct timer_list timeout;
  122. unsigned int cmd_time_out;
  123. spinlock_t nl_cmd_lock;
  124. struct tcmu_nl_cmd curr_nl_cmd;
  125. /* wake up threads waiting on curr_nl_cmd */
  126. wait_queue_head_t nl_cmd_wq;
  127. char dev_config[TCMU_CONFIG_LEN];
  128. };
  129. #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
  130. #define CMDR_OFF sizeof(struct tcmu_mailbox)
  131. struct tcmu_cmd {
  132. struct se_cmd *se_cmd;
  133. struct tcmu_dev *tcmu_dev;
  134. uint16_t cmd_id;
  135. /* Can't use se_cmd when cleaning up expired cmds, because if
  136. cmd has been completed then accessing se_cmd is off limits */
  137. uint32_t dbi_cnt;
  138. uint32_t dbi_cur;
  139. uint32_t *dbi;
  140. unsigned long deadline;
  141. #define TCMU_CMD_BIT_EXPIRED 0
  142. unsigned long flags;
  143. };
  144. static struct task_struct *unmap_thread;
  145. static wait_queue_head_t unmap_wait;
  146. static DEFINE_MUTEX(root_udev_mutex);
  147. static LIST_HEAD(root_udev);
  148. static atomic_t global_db_count = ATOMIC_INIT(0);
  149. static struct kmem_cache *tcmu_cmd_cache;
  150. /* multicast group */
  151. enum tcmu_multicast_groups {
  152. TCMU_MCGRP_CONFIG,
  153. };
  154. static const struct genl_multicast_group tcmu_mcgrps[] = {
  155. [TCMU_MCGRP_CONFIG] = { .name = "config", },
  156. };
  157. static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
  158. [TCMU_ATTR_DEVICE] = { .type = NLA_STRING },
  159. [TCMU_ATTR_MINOR] = { .type = NLA_U32 },
  160. [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 },
  161. [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 },
  162. [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
  163. };
  164. static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
  165. {
  166. struct se_device *dev;
  167. struct tcmu_dev *udev;
  168. struct tcmu_nl_cmd *nl_cmd;
  169. int dev_id, rc, ret = 0;
  170. bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
  171. if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
  172. !info->attrs[TCMU_ATTR_DEVICE_ID]) {
  173. printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
  174. return -EINVAL;
  175. }
  176. dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
  177. rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
  178. dev = target_find_device(dev_id, !is_removed);
  179. if (!dev) {
  180. printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
  181. completed_cmd, rc, dev_id);
  182. return -ENODEV;
  183. }
  184. udev = TCMU_DEV(dev);
  185. spin_lock(&udev->nl_cmd_lock);
  186. nl_cmd = &udev->curr_nl_cmd;
  187. pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
  188. nl_cmd->cmd, completed_cmd, rc);
  189. if (nl_cmd->cmd != completed_cmd) {
  190. printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
  191. completed_cmd, nl_cmd->cmd);
  192. ret = -EINVAL;
  193. } else {
  194. nl_cmd->status = rc;
  195. }
  196. spin_unlock(&udev->nl_cmd_lock);
  197. if (!is_removed)
  198. target_undepend_item(&dev->dev_group.cg_item);
  199. if (!ret)
  200. complete(&nl_cmd->complete);
  201. return ret;
  202. }
  203. static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
  204. {
  205. return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
  206. }
  207. static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
  208. {
  209. return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
  210. }
  211. static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
  212. struct genl_info *info)
  213. {
  214. return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
  215. }
  216. static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
  217. {
  218. if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
  219. tcmu_kern_cmd_reply_supported =
  220. nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
  221. printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
  222. tcmu_kern_cmd_reply_supported);
  223. }
  224. return 0;
  225. }
  226. static const struct genl_ops tcmu_genl_ops[] = {
  227. {
  228. .cmd = TCMU_CMD_SET_FEATURES,
  229. .flags = GENL_ADMIN_PERM,
  230. .policy = tcmu_attr_policy,
  231. .doit = tcmu_genl_set_features,
  232. },
  233. {
  234. .cmd = TCMU_CMD_ADDED_DEVICE_DONE,
  235. .flags = GENL_ADMIN_PERM,
  236. .policy = tcmu_attr_policy,
  237. .doit = tcmu_genl_add_dev_done,
  238. },
  239. {
  240. .cmd = TCMU_CMD_REMOVED_DEVICE_DONE,
  241. .flags = GENL_ADMIN_PERM,
  242. .policy = tcmu_attr_policy,
  243. .doit = tcmu_genl_rm_dev_done,
  244. },
  245. {
  246. .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE,
  247. .flags = GENL_ADMIN_PERM,
  248. .policy = tcmu_attr_policy,
  249. .doit = tcmu_genl_reconfig_dev_done,
  250. },
  251. };
  252. /* Our generic netlink family */
  253. static struct genl_family tcmu_genl_family __ro_after_init = {
  254. .module = THIS_MODULE,
  255. .hdrsize = 0,
  256. .name = "TCM-USER",
  257. .version = 2,
  258. .maxattr = TCMU_ATTR_MAX,
  259. .mcgrps = tcmu_mcgrps,
  260. .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
  261. .netnsok = true,
  262. .ops = tcmu_genl_ops,
  263. .n_ops = ARRAY_SIZE(tcmu_genl_ops),
  264. };
  265. #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
  266. #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
  267. #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
  268. #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
  269. static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
  270. {
  271. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  272. uint32_t i;
  273. for (i = 0; i < len; i++)
  274. clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
  275. }
  276. static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
  277. struct tcmu_cmd *tcmu_cmd)
  278. {
  279. struct page *page;
  280. int ret, dbi;
  281. dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
  282. if (dbi == udev->dbi_thresh)
  283. return false;
  284. page = radix_tree_lookup(&udev->data_blocks, dbi);
  285. if (!page) {
  286. if (atomic_add_return(1, &global_db_count) >
  287. TCMU_GLOBAL_MAX_BLOCKS) {
  288. atomic_dec(&global_db_count);
  289. return false;
  290. }
  291. /* try to get new page from the mm */
  292. page = alloc_page(GFP_KERNEL);
  293. if (!page)
  294. goto err_alloc;
  295. ret = radix_tree_insert(&udev->data_blocks, dbi, page);
  296. if (ret)
  297. goto err_insert;
  298. }
  299. if (dbi > udev->dbi_max)
  300. udev->dbi_max = dbi;
  301. set_bit(dbi, udev->data_bitmap);
  302. tcmu_cmd_set_dbi(tcmu_cmd, dbi);
  303. return true;
  304. err_insert:
  305. __free_page(page);
  306. err_alloc:
  307. atomic_dec(&global_db_count);
  308. return false;
  309. }
  310. static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
  311. struct tcmu_cmd *tcmu_cmd)
  312. {
  313. int i;
  314. udev->waiting_global = false;
  315. for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
  316. if (!tcmu_get_empty_block(udev, tcmu_cmd))
  317. goto err;
  318. }
  319. return true;
  320. err:
  321. udev->waiting_global = true;
  322. /* Try to wake up the unmap thread */
  323. wake_up(&unmap_wait);
  324. return false;
  325. }
  326. static inline struct page *
  327. tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
  328. {
  329. return radix_tree_lookup(&udev->data_blocks, dbi);
  330. }
  331. static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
  332. {
  333. kfree(tcmu_cmd->dbi);
  334. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  335. }
  336. static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
  337. {
  338. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  339. size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
  340. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  341. BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
  342. data_length += round_up(se_cmd->t_bidi_data_sg->length,
  343. DATA_BLOCK_SIZE);
  344. }
  345. return data_length;
  346. }
  347. static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
  348. {
  349. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  350. return data_length / DATA_BLOCK_SIZE;
  351. }
  352. static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
  353. {
  354. struct se_device *se_dev = se_cmd->se_dev;
  355. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  356. struct tcmu_cmd *tcmu_cmd;
  357. int cmd_id;
  358. tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
  359. if (!tcmu_cmd)
  360. return NULL;
  361. tcmu_cmd->se_cmd = se_cmd;
  362. tcmu_cmd->tcmu_dev = udev;
  363. if (udev->cmd_time_out)
  364. tcmu_cmd->deadline = jiffies +
  365. msecs_to_jiffies(udev->cmd_time_out);
  366. tcmu_cmd_reset_dbi_cur(tcmu_cmd);
  367. tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
  368. tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
  369. GFP_KERNEL);
  370. if (!tcmu_cmd->dbi) {
  371. kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
  372. return NULL;
  373. }
  374. idr_preload(GFP_KERNEL);
  375. spin_lock_irq(&udev->commands_lock);
  376. cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
  377. USHRT_MAX, GFP_NOWAIT);
  378. spin_unlock_irq(&udev->commands_lock);
  379. idr_preload_end();
  380. if (cmd_id < 0) {
  381. tcmu_free_cmd(tcmu_cmd);
  382. return NULL;
  383. }
  384. tcmu_cmd->cmd_id = cmd_id;
  385. return tcmu_cmd;
  386. }
  387. static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
  388. {
  389. unsigned long offset = offset_in_page(vaddr);
  390. size = round_up(size+offset, PAGE_SIZE);
  391. vaddr -= offset;
  392. while (size) {
  393. flush_dcache_page(virt_to_page(vaddr));
  394. size -= PAGE_SIZE;
  395. }
  396. }
  397. /*
  398. * Some ring helper functions. We don't assume size is a power of 2 so
  399. * we can't use circ_buf.h.
  400. */
  401. static inline size_t spc_used(size_t head, size_t tail, size_t size)
  402. {
  403. int diff = head - tail;
  404. if (diff >= 0)
  405. return diff;
  406. else
  407. return size + diff;
  408. }
  409. static inline size_t spc_free(size_t head, size_t tail, size_t size)
  410. {
  411. /* Keep 1 byte unused or we can't tell full from empty */
  412. return (size - spc_used(head, tail, size) - 1);
  413. }
  414. static inline size_t head_to_end(size_t head, size_t size)
  415. {
  416. return size - head;
  417. }
  418. static inline void new_iov(struct iovec **iov, int *iov_cnt,
  419. struct tcmu_dev *udev)
  420. {
  421. struct iovec *iovec;
  422. if (*iov_cnt != 0)
  423. (*iov)++;
  424. (*iov_cnt)++;
  425. iovec = *iov;
  426. memset(iovec, 0, sizeof(struct iovec));
  427. }
  428. #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
  429. /* offset is relative to mb_addr */
  430. static inline size_t get_block_offset_user(struct tcmu_dev *dev,
  431. int dbi, int remaining)
  432. {
  433. return dev->data_off + dbi * DATA_BLOCK_SIZE +
  434. DATA_BLOCK_SIZE - remaining;
  435. }
  436. static inline size_t iov_tail(struct iovec *iov)
  437. {
  438. return (size_t)iov->iov_base + iov->iov_len;
  439. }
  440. static int scatter_data_area(struct tcmu_dev *udev,
  441. struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
  442. unsigned int data_nents, struct iovec **iov,
  443. int *iov_cnt, bool copy_data)
  444. {
  445. int i, dbi;
  446. int block_remaining = 0;
  447. void *from, *to = NULL;
  448. size_t copy_bytes, to_offset, offset;
  449. struct scatterlist *sg;
  450. struct page *page;
  451. for_each_sg(data_sg, sg, data_nents, i) {
  452. int sg_remaining = sg->length;
  453. from = kmap_atomic(sg_page(sg)) + sg->offset;
  454. while (sg_remaining > 0) {
  455. if (block_remaining == 0) {
  456. if (to)
  457. kunmap_atomic(to);
  458. block_remaining = DATA_BLOCK_SIZE;
  459. dbi = tcmu_cmd_get_dbi(tcmu_cmd);
  460. page = tcmu_get_block_page(udev, dbi);
  461. to = kmap_atomic(page);
  462. }
  463. copy_bytes = min_t(size_t, sg_remaining,
  464. block_remaining);
  465. to_offset = get_block_offset_user(udev, dbi,
  466. block_remaining);
  467. if (*iov_cnt != 0 &&
  468. to_offset == iov_tail(*iov)) {
  469. (*iov)->iov_len += copy_bytes;
  470. } else {
  471. new_iov(iov, iov_cnt, udev);
  472. (*iov)->iov_base = (void __user *)to_offset;
  473. (*iov)->iov_len = copy_bytes;
  474. }
  475. if (copy_data) {
  476. offset = DATA_BLOCK_SIZE - block_remaining;
  477. memcpy(to + offset,
  478. from + sg->length - sg_remaining,
  479. copy_bytes);
  480. tcmu_flush_dcache_range(to, copy_bytes);
  481. }
  482. sg_remaining -= copy_bytes;
  483. block_remaining -= copy_bytes;
  484. }
  485. kunmap_atomic(from - sg->offset);
  486. }
  487. if (to)
  488. kunmap_atomic(to);
  489. return 0;
  490. }
  491. static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
  492. bool bidi)
  493. {
  494. struct se_cmd *se_cmd = cmd->se_cmd;
  495. int i, dbi;
  496. int block_remaining = 0;
  497. void *from = NULL, *to;
  498. size_t copy_bytes, offset;
  499. struct scatterlist *sg, *data_sg;
  500. struct page *page;
  501. unsigned int data_nents;
  502. uint32_t count = 0;
  503. if (!bidi) {
  504. data_sg = se_cmd->t_data_sg;
  505. data_nents = se_cmd->t_data_nents;
  506. } else {
  507. /*
  508. * For bidi case, the first count blocks are for Data-Out
  509. * buffer blocks, and before gathering the Data-In buffer
  510. * the Data-Out buffer blocks should be discarded.
  511. */
  512. count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
  513. data_sg = se_cmd->t_bidi_data_sg;
  514. data_nents = se_cmd->t_bidi_data_nents;
  515. }
  516. tcmu_cmd_set_dbi_cur(cmd, count);
  517. for_each_sg(data_sg, sg, data_nents, i) {
  518. int sg_remaining = sg->length;
  519. to = kmap_atomic(sg_page(sg)) + sg->offset;
  520. while (sg_remaining > 0) {
  521. if (block_remaining == 0) {
  522. if (from)
  523. kunmap_atomic(from);
  524. block_remaining = DATA_BLOCK_SIZE;
  525. dbi = tcmu_cmd_get_dbi(cmd);
  526. page = tcmu_get_block_page(udev, dbi);
  527. from = kmap_atomic(page);
  528. }
  529. copy_bytes = min_t(size_t, sg_remaining,
  530. block_remaining);
  531. offset = DATA_BLOCK_SIZE - block_remaining;
  532. tcmu_flush_dcache_range(from, copy_bytes);
  533. memcpy(to + sg->length - sg_remaining, from + offset,
  534. copy_bytes);
  535. sg_remaining -= copy_bytes;
  536. block_remaining -= copy_bytes;
  537. }
  538. kunmap_atomic(to - sg->offset);
  539. }
  540. if (from)
  541. kunmap_atomic(from);
  542. }
  543. static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
  544. {
  545. return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh));
  546. }
  547. /*
  548. * We can't queue a command until we have space available on the cmd ring *and*
  549. * space available on the data area.
  550. *
  551. * Called with ring lock held.
  552. */
  553. static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
  554. size_t cmd_size, size_t data_needed)
  555. {
  556. struct tcmu_mailbox *mb = udev->mb_addr;
  557. uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
  558. / DATA_BLOCK_SIZE;
  559. size_t space, cmd_needed;
  560. u32 cmd_head;
  561. tcmu_flush_dcache_range(mb, sizeof(*mb));
  562. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  563. /*
  564. * If cmd end-of-ring space is too small then we need space for a NOP plus
  565. * original cmd - cmds are internally contiguous.
  566. */
  567. if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
  568. cmd_needed = cmd_size;
  569. else
  570. cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
  571. space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
  572. if (space < cmd_needed) {
  573. pr_debug("no cmd space: %u %u %u\n", cmd_head,
  574. udev->cmdr_last_cleaned, udev->cmdr_size);
  575. return false;
  576. }
  577. /* try to check and get the data blocks as needed */
  578. space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
  579. if (space < data_needed) {
  580. unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh;
  581. unsigned long grow;
  582. if (blocks_left < blocks_needed) {
  583. pr_debug("no data space: only %lu available, but ask for %zu\n",
  584. blocks_left * DATA_BLOCK_SIZE,
  585. data_needed);
  586. return false;
  587. }
  588. /* Try to expand the thresh */
  589. if (!udev->dbi_thresh) {
  590. /* From idle state */
  591. uint32_t init_thresh = DATA_BLOCK_INIT_BITS;
  592. udev->dbi_thresh = max(blocks_needed, init_thresh);
  593. } else {
  594. /*
  595. * Grow the data area by max(blocks needed,
  596. * dbi_thresh / 2), but limited to the max
  597. * DATA_BLOCK_BITS size.
  598. */
  599. grow = max(blocks_needed, udev->dbi_thresh / 2);
  600. udev->dbi_thresh += grow;
  601. if (udev->dbi_thresh > DATA_BLOCK_BITS)
  602. udev->dbi_thresh = DATA_BLOCK_BITS;
  603. }
  604. }
  605. return tcmu_get_empty_blocks(udev, cmd);
  606. }
  607. static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
  608. {
  609. return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
  610. sizeof(struct tcmu_cmd_entry));
  611. }
  612. static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
  613. size_t base_command_size)
  614. {
  615. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  616. size_t command_size;
  617. command_size = base_command_size +
  618. round_up(scsi_command_size(se_cmd->t_task_cdb),
  619. TCMU_OP_ALIGN_SIZE);
  620. WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
  621. return command_size;
  622. }
  623. static sense_reason_t
  624. tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
  625. {
  626. struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
  627. struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
  628. size_t base_command_size, command_size;
  629. struct tcmu_mailbox *mb;
  630. struct tcmu_cmd_entry *entry;
  631. struct iovec *iov;
  632. int iov_cnt, ret;
  633. uint32_t cmd_head;
  634. uint64_t cdb_off;
  635. bool copy_to_data_area;
  636. size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
  637. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
  638. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  639. /*
  640. * Must be a certain minimum size for response sense info, but
  641. * also may be larger if the iov array is large.
  642. *
  643. * We prepare as many iovs as possbile for potential uses here,
  644. * because it's expensive to tell how many regions are freed in
  645. * the bitmap & global data pool, as the size calculated here
  646. * will only be used to do the checks.
  647. *
  648. * The size will be recalculated later as actually needed to save
  649. * cmd area memories.
  650. */
  651. base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
  652. command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
  653. mutex_lock(&udev->cmdr_lock);
  654. mb = udev->mb_addr;
  655. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  656. if ((command_size > (udev->cmdr_size / 2)) ||
  657. data_length > udev->data_size) {
  658. pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
  659. "cmd ring/data area\n", command_size, data_length,
  660. udev->cmdr_size, udev->data_size);
  661. mutex_unlock(&udev->cmdr_lock);
  662. return TCM_INVALID_CDB_FIELD;
  663. }
  664. while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
  665. int ret;
  666. DEFINE_WAIT(__wait);
  667. prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
  668. pr_debug("sleeping for ring space\n");
  669. mutex_unlock(&udev->cmdr_lock);
  670. if (udev->cmd_time_out)
  671. ret = schedule_timeout(
  672. msecs_to_jiffies(udev->cmd_time_out));
  673. else
  674. ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
  675. finish_wait(&udev->wait_cmdr, &__wait);
  676. if (!ret) {
  677. pr_warn("tcmu: command timed out\n");
  678. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  679. }
  680. mutex_lock(&udev->cmdr_lock);
  681. /* We dropped cmdr_lock, cmd_head is stale */
  682. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  683. }
  684. /* Insert a PAD if end-of-ring space is too small */
  685. if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
  686. size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
  687. entry = (void *) mb + CMDR_OFF + cmd_head;
  688. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
  689. tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
  690. entry->hdr.cmd_id = 0; /* not used for PAD */
  691. entry->hdr.kflags = 0;
  692. entry->hdr.uflags = 0;
  693. tcmu_flush_dcache_range(entry, sizeof(*entry));
  694. UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
  695. tcmu_flush_dcache_range(mb, sizeof(*mb));
  696. cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
  697. WARN_ON(cmd_head != 0);
  698. }
  699. entry = (void *) mb + CMDR_OFF + cmd_head;
  700. memset(entry, 0, command_size);
  701. tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
  702. entry->hdr.cmd_id = tcmu_cmd->cmd_id;
  703. /* Handle allocating space from the data area */
  704. tcmu_cmd_reset_dbi_cur(tcmu_cmd);
  705. iov = &entry->req.iov[0];
  706. iov_cnt = 0;
  707. copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
  708. || se_cmd->se_cmd_flags & SCF_BIDI);
  709. ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
  710. se_cmd->t_data_nents, &iov, &iov_cnt,
  711. copy_to_data_area);
  712. if (ret) {
  713. tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
  714. mutex_unlock(&udev->cmdr_lock);
  715. pr_err("tcmu: alloc and scatter data failed\n");
  716. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  717. }
  718. entry->req.iov_cnt = iov_cnt;
  719. /* Handle BIDI commands */
  720. iov_cnt = 0;
  721. if (se_cmd->se_cmd_flags & SCF_BIDI) {
  722. iov++;
  723. ret = scatter_data_area(udev, tcmu_cmd,
  724. se_cmd->t_bidi_data_sg,
  725. se_cmd->t_bidi_data_nents,
  726. &iov, &iov_cnt, false);
  727. if (ret) {
  728. tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
  729. mutex_unlock(&udev->cmdr_lock);
  730. pr_err("tcmu: alloc and scatter bidi data failed\n");
  731. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  732. }
  733. }
  734. entry->req.iov_bidi_cnt = iov_cnt;
  735. /*
  736. * Recalaulate the command's base size and size according
  737. * to the actual needs
  738. */
  739. base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
  740. entry->req.iov_bidi_cnt);
  741. command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
  742. tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
  743. /* All offsets relative to mb_addr, not start of entry! */
  744. cdb_off = CMDR_OFF + cmd_head + base_command_size;
  745. memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
  746. entry->req.cdb_off = cdb_off;
  747. tcmu_flush_dcache_range(entry, sizeof(*entry));
  748. UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
  749. tcmu_flush_dcache_range(mb, sizeof(*mb));
  750. mutex_unlock(&udev->cmdr_lock);
  751. /* TODO: only if FLUSH and FUA? */
  752. uio_event_notify(&udev->uio_info);
  753. if (udev->cmd_time_out)
  754. mod_timer(&udev->timeout, round_jiffies_up(jiffies +
  755. msecs_to_jiffies(udev->cmd_time_out)));
  756. return TCM_NO_SENSE;
  757. }
  758. static sense_reason_t
  759. tcmu_queue_cmd(struct se_cmd *se_cmd)
  760. {
  761. struct se_device *se_dev = se_cmd->se_dev;
  762. struct tcmu_dev *udev = TCMU_DEV(se_dev);
  763. struct tcmu_cmd *tcmu_cmd;
  764. sense_reason_t ret;
  765. tcmu_cmd = tcmu_alloc_cmd(se_cmd);
  766. if (!tcmu_cmd)
  767. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  768. ret = tcmu_queue_cmd_ring(tcmu_cmd);
  769. if (ret != TCM_NO_SENSE) {
  770. pr_err("TCMU: Could not queue command\n");
  771. spin_lock_irq(&udev->commands_lock);
  772. idr_remove(&udev->commands, tcmu_cmd->cmd_id);
  773. spin_unlock_irq(&udev->commands_lock);
  774. tcmu_free_cmd(tcmu_cmd);
  775. }
  776. return ret;
  777. }
  778. static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
  779. {
  780. struct se_cmd *se_cmd = cmd->se_cmd;
  781. struct tcmu_dev *udev = cmd->tcmu_dev;
  782. /*
  783. * cmd has been completed already from timeout, just reclaim
  784. * data area space and free cmd
  785. */
  786. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  787. goto out;
  788. tcmu_cmd_reset_dbi_cur(cmd);
  789. if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
  790. pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
  791. cmd->se_cmd);
  792. entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
  793. } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
  794. transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
  795. } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
  796. /* Get Data-In buffer before clean up */
  797. gather_data_area(udev, cmd, true);
  798. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  799. gather_data_area(udev, cmd, false);
  800. } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
  801. /* TODO: */
  802. } else if (se_cmd->data_direction != DMA_NONE) {
  803. pr_warn("TCMU: data direction was %d!\n",
  804. se_cmd->data_direction);
  805. }
  806. target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
  807. out:
  808. cmd->se_cmd = NULL;
  809. tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
  810. tcmu_free_cmd(cmd);
  811. }
  812. static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
  813. {
  814. struct tcmu_mailbox *mb;
  815. int handled = 0;
  816. if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
  817. pr_err("ring broken, not handling completions\n");
  818. return 0;
  819. }
  820. mb = udev->mb_addr;
  821. tcmu_flush_dcache_range(mb, sizeof(*mb));
  822. while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
  823. struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
  824. struct tcmu_cmd *cmd;
  825. tcmu_flush_dcache_range(entry, sizeof(*entry));
  826. if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
  827. UPDATE_HEAD(udev->cmdr_last_cleaned,
  828. tcmu_hdr_get_len(entry->hdr.len_op),
  829. udev->cmdr_size);
  830. continue;
  831. }
  832. WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
  833. spin_lock(&udev->commands_lock);
  834. cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
  835. spin_unlock(&udev->commands_lock);
  836. if (!cmd) {
  837. pr_err("cmd_id not found, ring is broken\n");
  838. set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
  839. break;
  840. }
  841. tcmu_handle_completion(cmd, entry);
  842. UPDATE_HEAD(udev->cmdr_last_cleaned,
  843. tcmu_hdr_get_len(entry->hdr.len_op),
  844. udev->cmdr_size);
  845. handled++;
  846. }
  847. if (mb->cmd_tail == mb->cmd_head)
  848. del_timer(&udev->timeout); /* no more pending cmds */
  849. wake_up(&udev->wait_cmdr);
  850. return handled;
  851. }
  852. static int tcmu_check_expired_cmd(int id, void *p, void *data)
  853. {
  854. struct tcmu_cmd *cmd = p;
  855. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
  856. return 0;
  857. if (!time_after(jiffies, cmd->deadline))
  858. return 0;
  859. set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
  860. target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
  861. cmd->se_cmd = NULL;
  862. return 0;
  863. }
  864. static void tcmu_device_timedout(unsigned long data)
  865. {
  866. struct tcmu_dev *udev = (struct tcmu_dev *)data;
  867. unsigned long flags;
  868. spin_lock_irqsave(&udev->commands_lock, flags);
  869. idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
  870. spin_unlock_irqrestore(&udev->commands_lock, flags);
  871. /* Try to wake up the ummap thread */
  872. wake_up(&unmap_wait);
  873. /*
  874. * We don't need to wakeup threads on wait_cmdr since they have their
  875. * own timeout.
  876. */
  877. }
  878. static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
  879. {
  880. struct tcmu_hba *tcmu_hba;
  881. tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
  882. if (!tcmu_hba)
  883. return -ENOMEM;
  884. tcmu_hba->host_id = host_id;
  885. hba->hba_ptr = tcmu_hba;
  886. return 0;
  887. }
  888. static void tcmu_detach_hba(struct se_hba *hba)
  889. {
  890. kfree(hba->hba_ptr);
  891. hba->hba_ptr = NULL;
  892. }
  893. static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
  894. {
  895. struct tcmu_dev *udev;
  896. udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
  897. if (!udev)
  898. return NULL;
  899. kref_init(&udev->kref);
  900. udev->name = kstrdup(name, GFP_KERNEL);
  901. if (!udev->name) {
  902. kfree(udev);
  903. return NULL;
  904. }
  905. udev->hba = hba;
  906. udev->cmd_time_out = TCMU_TIME_OUT;
  907. init_waitqueue_head(&udev->wait_cmdr);
  908. mutex_init(&udev->cmdr_lock);
  909. idr_init(&udev->commands);
  910. spin_lock_init(&udev->commands_lock);
  911. setup_timer(&udev->timeout, tcmu_device_timedout,
  912. (unsigned long)udev);
  913. init_waitqueue_head(&udev->nl_cmd_wq);
  914. spin_lock_init(&udev->nl_cmd_lock);
  915. return &udev->se_dev;
  916. }
  917. static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
  918. {
  919. struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
  920. mutex_lock(&tcmu_dev->cmdr_lock);
  921. tcmu_handle_completions(tcmu_dev);
  922. mutex_unlock(&tcmu_dev->cmdr_lock);
  923. return 0;
  924. }
  925. /*
  926. * mmap code from uio.c. Copied here because we want to hook mmap()
  927. * and this stuff must come along.
  928. */
  929. static int tcmu_find_mem_index(struct vm_area_struct *vma)
  930. {
  931. struct tcmu_dev *udev = vma->vm_private_data;
  932. struct uio_info *info = &udev->uio_info;
  933. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  934. if (info->mem[vma->vm_pgoff].size == 0)
  935. return -1;
  936. return (int)vma->vm_pgoff;
  937. }
  938. return -1;
  939. }
  940. static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
  941. {
  942. struct page *page;
  943. int ret;
  944. mutex_lock(&udev->cmdr_lock);
  945. page = tcmu_get_block_page(udev, dbi);
  946. if (likely(page)) {
  947. mutex_unlock(&udev->cmdr_lock);
  948. return page;
  949. }
  950. /*
  951. * Normally it shouldn't be here:
  952. * Only when the userspace has touched the blocks which
  953. * are out of the tcmu_cmd's data iov[], and will return
  954. * one zeroed page.
  955. */
  956. pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
  957. pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
  958. if (dbi >= udev->dbi_thresh) {
  959. /* Extern the udev->dbi_thresh to dbi + 1 */
  960. udev->dbi_thresh = dbi + 1;
  961. udev->dbi_max = dbi;
  962. }
  963. page = radix_tree_lookup(&udev->data_blocks, dbi);
  964. if (!page) {
  965. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  966. if (!page) {
  967. mutex_unlock(&udev->cmdr_lock);
  968. return NULL;
  969. }
  970. ret = radix_tree_insert(&udev->data_blocks, dbi, page);
  971. if (ret) {
  972. mutex_unlock(&udev->cmdr_lock);
  973. __free_page(page);
  974. return NULL;
  975. }
  976. /*
  977. * Since this case is rare in page fault routine, here we
  978. * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
  979. * to reduce possible page fault call trace.
  980. */
  981. atomic_inc(&global_db_count);
  982. }
  983. mutex_unlock(&udev->cmdr_lock);
  984. return page;
  985. }
  986. static int tcmu_vma_fault(struct vm_fault *vmf)
  987. {
  988. struct tcmu_dev *udev = vmf->vma->vm_private_data;
  989. struct uio_info *info = &udev->uio_info;
  990. struct page *page;
  991. unsigned long offset;
  992. void *addr;
  993. int mi = tcmu_find_mem_index(vmf->vma);
  994. if (mi < 0)
  995. return VM_FAULT_SIGBUS;
  996. /*
  997. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  998. * to use mem[N].
  999. */
  1000. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  1001. if (offset < udev->data_off) {
  1002. /* For the vmalloc()ed cmd area pages */
  1003. addr = (void *)(unsigned long)info->mem[mi].addr + offset;
  1004. page = vmalloc_to_page(addr);
  1005. } else {
  1006. uint32_t dbi;
  1007. /* For the dynamically growing data area pages */
  1008. dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
  1009. page = tcmu_try_get_block_page(udev, dbi);
  1010. if (!page)
  1011. return VM_FAULT_NOPAGE;
  1012. }
  1013. get_page(page);
  1014. vmf->page = page;
  1015. return 0;
  1016. }
  1017. static const struct vm_operations_struct tcmu_vm_ops = {
  1018. .fault = tcmu_vma_fault,
  1019. };
  1020. static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
  1021. {
  1022. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1023. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  1024. vma->vm_ops = &tcmu_vm_ops;
  1025. vma->vm_private_data = udev;
  1026. /* Ensure the mmap is exactly the right size */
  1027. if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
  1028. return -EINVAL;
  1029. return 0;
  1030. }
  1031. static int tcmu_open(struct uio_info *info, struct inode *inode)
  1032. {
  1033. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1034. /* O_EXCL not supported for char devs, so fake it? */
  1035. if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
  1036. return -EBUSY;
  1037. udev->inode = inode;
  1038. kref_get(&udev->kref);
  1039. pr_debug("open\n");
  1040. return 0;
  1041. }
  1042. static void tcmu_dev_call_rcu(struct rcu_head *p)
  1043. {
  1044. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  1045. struct tcmu_dev *udev = TCMU_DEV(dev);
  1046. kfree(udev->uio_info.name);
  1047. kfree(udev->name);
  1048. kfree(udev);
  1049. }
  1050. static void tcmu_dev_kref_release(struct kref *kref)
  1051. {
  1052. struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
  1053. struct se_device *dev = &udev->se_dev;
  1054. call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
  1055. }
  1056. static int tcmu_release(struct uio_info *info, struct inode *inode)
  1057. {
  1058. struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
  1059. clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
  1060. pr_debug("close\n");
  1061. /* release ref from open */
  1062. kref_put(&udev->kref, tcmu_dev_kref_release);
  1063. return 0;
  1064. }
  1065. static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
  1066. {
  1067. struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
  1068. if (!tcmu_kern_cmd_reply_supported)
  1069. return;
  1070. relock:
  1071. spin_lock(&udev->nl_cmd_lock);
  1072. if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
  1073. spin_unlock(&udev->nl_cmd_lock);
  1074. pr_debug("sleeping for open nl cmd\n");
  1075. wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
  1076. goto relock;
  1077. }
  1078. memset(nl_cmd, 0, sizeof(*nl_cmd));
  1079. nl_cmd->cmd = cmd;
  1080. init_completion(&nl_cmd->complete);
  1081. spin_unlock(&udev->nl_cmd_lock);
  1082. }
  1083. static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
  1084. {
  1085. struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
  1086. int ret;
  1087. DEFINE_WAIT(__wait);
  1088. if (!tcmu_kern_cmd_reply_supported)
  1089. return 0;
  1090. pr_debug("sleeping for nl reply\n");
  1091. wait_for_completion(&nl_cmd->complete);
  1092. spin_lock(&udev->nl_cmd_lock);
  1093. nl_cmd->cmd = TCMU_CMD_UNSPEC;
  1094. ret = nl_cmd->status;
  1095. nl_cmd->status = 0;
  1096. spin_unlock(&udev->nl_cmd_lock);
  1097. wake_up_all(&udev->nl_cmd_wq);
  1098. return ret;;
  1099. }
  1100. static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
  1101. int reconfig_attr, const void *reconfig_data)
  1102. {
  1103. struct sk_buff *skb;
  1104. void *msg_header;
  1105. int ret = -ENOMEM;
  1106. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1107. if (!skb)
  1108. return ret;
  1109. msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
  1110. if (!msg_header)
  1111. goto free_skb;
  1112. ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
  1113. if (ret < 0)
  1114. goto free_skb;
  1115. ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
  1116. if (ret < 0)
  1117. goto free_skb;
  1118. ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
  1119. if (ret < 0)
  1120. goto free_skb;
  1121. if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
  1122. switch (reconfig_attr) {
  1123. case TCMU_ATTR_DEV_CFG:
  1124. ret = nla_put_string(skb, reconfig_attr, reconfig_data);
  1125. break;
  1126. case TCMU_ATTR_DEV_SIZE:
  1127. ret = nla_put_u64_64bit(skb, reconfig_attr,
  1128. *((u64 *)reconfig_data),
  1129. TCMU_ATTR_PAD);
  1130. break;
  1131. case TCMU_ATTR_WRITECACHE:
  1132. ret = nla_put_u8(skb, reconfig_attr,
  1133. *((u8 *)reconfig_data));
  1134. break;
  1135. default:
  1136. BUG();
  1137. }
  1138. if (ret < 0)
  1139. goto free_skb;
  1140. }
  1141. genlmsg_end(skb, msg_header);
  1142. tcmu_init_genl_cmd_reply(udev, cmd);
  1143. ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
  1144. TCMU_MCGRP_CONFIG, GFP_KERNEL);
  1145. /* We don't care if no one is listening */
  1146. if (ret == -ESRCH)
  1147. ret = 0;
  1148. if (!ret)
  1149. ret = tcmu_wait_genl_cmd_reply(udev);
  1150. return ret;
  1151. free_skb:
  1152. nlmsg_free(skb);
  1153. return ret;
  1154. }
  1155. static int tcmu_update_uio_info(struct tcmu_dev *udev)
  1156. {
  1157. struct tcmu_hba *hba = udev->hba->hba_ptr;
  1158. struct uio_info *info;
  1159. size_t size, used;
  1160. char *str;
  1161. info = &udev->uio_info;
  1162. size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
  1163. udev->dev_config);
  1164. size += 1; /* for \0 */
  1165. str = kmalloc(size, GFP_KERNEL);
  1166. if (!str)
  1167. return -ENOMEM;
  1168. used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
  1169. if (udev->dev_config[0])
  1170. snprintf(str + used, size - used, "/%s", udev->dev_config);
  1171. /* If the old string exists, free it */
  1172. kfree(info->name);
  1173. info->name = str;
  1174. return 0;
  1175. }
  1176. static int tcmu_configure_device(struct se_device *dev)
  1177. {
  1178. struct tcmu_dev *udev = TCMU_DEV(dev);
  1179. struct uio_info *info;
  1180. struct tcmu_mailbox *mb;
  1181. int ret = 0;
  1182. ret = tcmu_update_uio_info(udev);
  1183. if (ret)
  1184. return ret;
  1185. info = &udev->uio_info;
  1186. udev->mb_addr = vzalloc(CMDR_SIZE);
  1187. if (!udev->mb_addr) {
  1188. ret = -ENOMEM;
  1189. goto err_vzalloc;
  1190. }
  1191. /* mailbox fits in first part of CMDR space */
  1192. udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
  1193. udev->data_off = CMDR_SIZE;
  1194. udev->data_size = DATA_SIZE;
  1195. udev->dbi_thresh = 0; /* Default in Idle state */
  1196. udev->waiting_global = false;
  1197. /* Initialise the mailbox of the ring buffer */
  1198. mb = udev->mb_addr;
  1199. mb->version = TCMU_MAILBOX_VERSION;
  1200. mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
  1201. mb->cmdr_off = CMDR_OFF;
  1202. mb->cmdr_size = udev->cmdr_size;
  1203. WARN_ON(!PAGE_ALIGNED(udev->data_off));
  1204. WARN_ON(udev->data_size % PAGE_SIZE);
  1205. WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
  1206. INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
  1207. info->version = __stringify(TCMU_MAILBOX_VERSION);
  1208. info->mem[0].name = "tcm-user command & data buffer";
  1209. info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
  1210. info->mem[0].size = TCMU_RING_SIZE;
  1211. info->mem[0].memtype = UIO_MEM_NONE;
  1212. info->irqcontrol = tcmu_irqcontrol;
  1213. info->irq = UIO_IRQ_CUSTOM;
  1214. info->mmap = tcmu_mmap;
  1215. info->open = tcmu_open;
  1216. info->release = tcmu_release;
  1217. ret = uio_register_device(tcmu_root_device, info);
  1218. if (ret)
  1219. goto err_register;
  1220. /* User can set hw_block_size before enable the device */
  1221. if (dev->dev_attrib.hw_block_size == 0)
  1222. dev->dev_attrib.hw_block_size = 512;
  1223. /* Other attributes can be configured in userspace */
  1224. if (!dev->dev_attrib.hw_max_sectors)
  1225. dev->dev_attrib.hw_max_sectors = 128;
  1226. if (!dev->dev_attrib.emulate_write_cache)
  1227. dev->dev_attrib.emulate_write_cache = 0;
  1228. dev->dev_attrib.hw_queue_depth = 128;
  1229. /*
  1230. * Get a ref incase userspace does a close on the uio device before
  1231. * LIO has initiated tcmu_free_device.
  1232. */
  1233. kref_get(&udev->kref);
  1234. ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
  1235. if (ret)
  1236. goto err_netlink;
  1237. mutex_lock(&root_udev_mutex);
  1238. list_add(&udev->node, &root_udev);
  1239. mutex_unlock(&root_udev_mutex);
  1240. return 0;
  1241. err_netlink:
  1242. kref_put(&udev->kref, tcmu_dev_kref_release);
  1243. uio_unregister_device(&udev->uio_info);
  1244. err_register:
  1245. vfree(udev->mb_addr);
  1246. err_vzalloc:
  1247. kfree(info->name);
  1248. info->name = NULL;
  1249. return ret;
  1250. }
  1251. static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
  1252. {
  1253. if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
  1254. kmem_cache_free(tcmu_cmd_cache, cmd);
  1255. return 0;
  1256. }
  1257. return -EINVAL;
  1258. }
  1259. static bool tcmu_dev_configured(struct tcmu_dev *udev)
  1260. {
  1261. return udev->uio_info.uio_dev ? true : false;
  1262. }
  1263. static void tcmu_blocks_release(struct tcmu_dev *udev)
  1264. {
  1265. int i;
  1266. struct page *page;
  1267. /* Try to release all block pages */
  1268. mutex_lock(&udev->cmdr_lock);
  1269. for (i = 0; i <= udev->dbi_max; i++) {
  1270. page = radix_tree_delete(&udev->data_blocks, i);
  1271. if (page) {
  1272. __free_page(page);
  1273. atomic_dec(&global_db_count);
  1274. }
  1275. }
  1276. mutex_unlock(&udev->cmdr_lock);
  1277. }
  1278. static void tcmu_free_device(struct se_device *dev)
  1279. {
  1280. struct tcmu_dev *udev = TCMU_DEV(dev);
  1281. /* release ref from init */
  1282. kref_put(&udev->kref, tcmu_dev_kref_release);
  1283. }
  1284. static void tcmu_destroy_device(struct se_device *dev)
  1285. {
  1286. struct tcmu_dev *udev = TCMU_DEV(dev);
  1287. struct tcmu_cmd *cmd;
  1288. bool all_expired = true;
  1289. int i;
  1290. del_timer_sync(&udev->timeout);
  1291. mutex_lock(&root_udev_mutex);
  1292. list_del(&udev->node);
  1293. mutex_unlock(&root_udev_mutex);
  1294. vfree(udev->mb_addr);
  1295. /* Upper layer should drain all requests before calling this */
  1296. spin_lock_irq(&udev->commands_lock);
  1297. idr_for_each_entry(&udev->commands, cmd, i) {
  1298. if (tcmu_check_and_free_pending_cmd(cmd) != 0)
  1299. all_expired = false;
  1300. }
  1301. idr_destroy(&udev->commands);
  1302. spin_unlock_irq(&udev->commands_lock);
  1303. WARN_ON(!all_expired);
  1304. tcmu_blocks_release(udev);
  1305. tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
  1306. uio_unregister_device(&udev->uio_info);
  1307. /* release ref from configure */
  1308. kref_put(&udev->kref, tcmu_dev_kref_release);
  1309. }
  1310. enum {
  1311. Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
  1312. Opt_err,
  1313. };
  1314. static match_table_t tokens = {
  1315. {Opt_dev_config, "dev_config=%s"},
  1316. {Opt_dev_size, "dev_size=%u"},
  1317. {Opt_hw_block_size, "hw_block_size=%u"},
  1318. {Opt_hw_max_sectors, "hw_max_sectors=%u"},
  1319. {Opt_err, NULL}
  1320. };
  1321. static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
  1322. {
  1323. unsigned long tmp_ul;
  1324. char *arg_p;
  1325. int ret;
  1326. arg_p = match_strdup(arg);
  1327. if (!arg_p)
  1328. return -ENOMEM;
  1329. ret = kstrtoul(arg_p, 0, &tmp_ul);
  1330. kfree(arg_p);
  1331. if (ret < 0) {
  1332. pr_err("kstrtoul() failed for dev attrib\n");
  1333. return ret;
  1334. }
  1335. if (!tmp_ul) {
  1336. pr_err("dev attrib must be nonzero\n");
  1337. return -EINVAL;
  1338. }
  1339. *dev_attrib = tmp_ul;
  1340. return 0;
  1341. }
  1342. static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
  1343. const char *page, ssize_t count)
  1344. {
  1345. struct tcmu_dev *udev = TCMU_DEV(dev);
  1346. char *orig, *ptr, *opts, *arg_p;
  1347. substring_t args[MAX_OPT_ARGS];
  1348. int ret = 0, token;
  1349. opts = kstrdup(page, GFP_KERNEL);
  1350. if (!opts)
  1351. return -ENOMEM;
  1352. orig = opts;
  1353. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  1354. if (!*ptr)
  1355. continue;
  1356. token = match_token(ptr, tokens, args);
  1357. switch (token) {
  1358. case Opt_dev_config:
  1359. if (match_strlcpy(udev->dev_config, &args[0],
  1360. TCMU_CONFIG_LEN) == 0) {
  1361. ret = -EINVAL;
  1362. break;
  1363. }
  1364. pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
  1365. break;
  1366. case Opt_dev_size:
  1367. arg_p = match_strdup(&args[0]);
  1368. if (!arg_p) {
  1369. ret = -ENOMEM;
  1370. break;
  1371. }
  1372. ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
  1373. kfree(arg_p);
  1374. if (ret < 0)
  1375. pr_err("kstrtoul() failed for dev_size=\n");
  1376. break;
  1377. case Opt_hw_block_size:
  1378. ret = tcmu_set_dev_attrib(&args[0],
  1379. &(dev->dev_attrib.hw_block_size));
  1380. break;
  1381. case Opt_hw_max_sectors:
  1382. ret = tcmu_set_dev_attrib(&args[0],
  1383. &(dev->dev_attrib.hw_max_sectors));
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. if (ret)
  1389. break;
  1390. }
  1391. kfree(orig);
  1392. return (!ret) ? count : ret;
  1393. }
  1394. static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
  1395. {
  1396. struct tcmu_dev *udev = TCMU_DEV(dev);
  1397. ssize_t bl = 0;
  1398. bl = sprintf(b + bl, "Config: %s ",
  1399. udev->dev_config[0] ? udev->dev_config : "NULL");
  1400. bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
  1401. return bl;
  1402. }
  1403. static sector_t tcmu_get_blocks(struct se_device *dev)
  1404. {
  1405. struct tcmu_dev *udev = TCMU_DEV(dev);
  1406. return div_u64(udev->dev_size - dev->dev_attrib.block_size,
  1407. dev->dev_attrib.block_size);
  1408. }
  1409. static sense_reason_t
  1410. tcmu_parse_cdb(struct se_cmd *cmd)
  1411. {
  1412. return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
  1413. }
  1414. static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
  1415. {
  1416. struct se_dev_attrib *da = container_of(to_config_group(item),
  1417. struct se_dev_attrib, da_group);
  1418. struct tcmu_dev *udev = container_of(da->da_dev,
  1419. struct tcmu_dev, se_dev);
  1420. return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
  1421. }
  1422. static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
  1423. size_t count)
  1424. {
  1425. struct se_dev_attrib *da = container_of(to_config_group(item),
  1426. struct se_dev_attrib, da_group);
  1427. struct tcmu_dev *udev = container_of(da->da_dev,
  1428. struct tcmu_dev, se_dev);
  1429. u32 val;
  1430. int ret;
  1431. if (da->da_dev->export_count) {
  1432. pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
  1433. return -EINVAL;
  1434. }
  1435. ret = kstrtou32(page, 0, &val);
  1436. if (ret < 0)
  1437. return ret;
  1438. udev->cmd_time_out = val * MSEC_PER_SEC;
  1439. return count;
  1440. }
  1441. CONFIGFS_ATTR(tcmu_, cmd_time_out);
  1442. static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
  1443. {
  1444. struct se_dev_attrib *da = container_of(to_config_group(item),
  1445. struct se_dev_attrib, da_group);
  1446. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1447. return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
  1448. }
  1449. static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
  1450. size_t count)
  1451. {
  1452. struct se_dev_attrib *da = container_of(to_config_group(item),
  1453. struct se_dev_attrib, da_group);
  1454. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1455. int ret, len;
  1456. len = strlen(page);
  1457. if (!len || len > TCMU_CONFIG_LEN - 1)
  1458. return -EINVAL;
  1459. /* Check if device has been configured before */
  1460. if (tcmu_dev_configured(udev)) {
  1461. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1462. TCMU_ATTR_DEV_CFG, page);
  1463. if (ret) {
  1464. pr_err("Unable to reconfigure device\n");
  1465. return ret;
  1466. }
  1467. strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
  1468. ret = tcmu_update_uio_info(udev);
  1469. if (ret)
  1470. return ret;
  1471. return count;
  1472. }
  1473. strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
  1474. return count;
  1475. }
  1476. CONFIGFS_ATTR(tcmu_, dev_config);
  1477. static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
  1478. {
  1479. struct se_dev_attrib *da = container_of(to_config_group(item),
  1480. struct se_dev_attrib, da_group);
  1481. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1482. return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
  1483. }
  1484. static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
  1485. size_t count)
  1486. {
  1487. struct se_dev_attrib *da = container_of(to_config_group(item),
  1488. struct se_dev_attrib, da_group);
  1489. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1490. u64 val;
  1491. int ret;
  1492. ret = kstrtou64(page, 0, &val);
  1493. if (ret < 0)
  1494. return ret;
  1495. /* Check if device has been configured before */
  1496. if (tcmu_dev_configured(udev)) {
  1497. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1498. TCMU_ATTR_DEV_SIZE, &val);
  1499. if (ret) {
  1500. pr_err("Unable to reconfigure device\n");
  1501. return ret;
  1502. }
  1503. }
  1504. udev->dev_size = val;
  1505. return count;
  1506. }
  1507. CONFIGFS_ATTR(tcmu_, dev_size);
  1508. static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
  1509. char *page)
  1510. {
  1511. struct se_dev_attrib *da = container_of(to_config_group(item),
  1512. struct se_dev_attrib, da_group);
  1513. return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
  1514. }
  1515. static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
  1516. const char *page, size_t count)
  1517. {
  1518. struct se_dev_attrib *da = container_of(to_config_group(item),
  1519. struct se_dev_attrib, da_group);
  1520. struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
  1521. u8 val;
  1522. int ret;
  1523. ret = kstrtou8(page, 0, &val);
  1524. if (ret < 0)
  1525. return ret;
  1526. /* Check if device has been configured before */
  1527. if (tcmu_dev_configured(udev)) {
  1528. ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
  1529. TCMU_ATTR_WRITECACHE, &val);
  1530. if (ret) {
  1531. pr_err("Unable to reconfigure device\n");
  1532. return ret;
  1533. }
  1534. }
  1535. da->emulate_write_cache = val;
  1536. return count;
  1537. }
  1538. CONFIGFS_ATTR(tcmu_, emulate_write_cache);
  1539. static struct configfs_attribute *tcmu_attrib_attrs[] = {
  1540. &tcmu_attr_cmd_time_out,
  1541. &tcmu_attr_dev_config,
  1542. &tcmu_attr_dev_size,
  1543. &tcmu_attr_emulate_write_cache,
  1544. NULL,
  1545. };
  1546. static struct configfs_attribute **tcmu_attrs;
  1547. static struct target_backend_ops tcmu_ops = {
  1548. .name = "user",
  1549. .owner = THIS_MODULE,
  1550. .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
  1551. .attach_hba = tcmu_attach_hba,
  1552. .detach_hba = tcmu_detach_hba,
  1553. .alloc_device = tcmu_alloc_device,
  1554. .configure_device = tcmu_configure_device,
  1555. .destroy_device = tcmu_destroy_device,
  1556. .free_device = tcmu_free_device,
  1557. .parse_cdb = tcmu_parse_cdb,
  1558. .set_configfs_dev_params = tcmu_set_configfs_dev_params,
  1559. .show_configfs_dev_params = tcmu_show_configfs_dev_params,
  1560. .get_device_type = sbc_get_device_type,
  1561. .get_blocks = tcmu_get_blocks,
  1562. .tb_dev_attrib_attrs = NULL,
  1563. };
  1564. static int unmap_thread_fn(void *data)
  1565. {
  1566. struct tcmu_dev *udev;
  1567. loff_t off;
  1568. uint32_t start, end, block;
  1569. struct page *page;
  1570. int i;
  1571. while (!kthread_should_stop()) {
  1572. DEFINE_WAIT(__wait);
  1573. prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE);
  1574. schedule();
  1575. finish_wait(&unmap_wait, &__wait);
  1576. if (kthread_should_stop())
  1577. break;
  1578. mutex_lock(&root_udev_mutex);
  1579. list_for_each_entry(udev, &root_udev, node) {
  1580. mutex_lock(&udev->cmdr_lock);
  1581. /* Try to complete the finished commands first */
  1582. tcmu_handle_completions(udev);
  1583. /* Skip the udevs waiting the global pool or in idle */
  1584. if (udev->waiting_global || !udev->dbi_thresh) {
  1585. mutex_unlock(&udev->cmdr_lock);
  1586. continue;
  1587. }
  1588. end = udev->dbi_max + 1;
  1589. block = find_last_bit(udev->data_bitmap, end);
  1590. if (block == udev->dbi_max) {
  1591. /*
  1592. * The last bit is dbi_max, so there is
  1593. * no need to shrink any blocks.
  1594. */
  1595. mutex_unlock(&udev->cmdr_lock);
  1596. continue;
  1597. } else if (block == end) {
  1598. /* The current udev will goto idle state */
  1599. udev->dbi_thresh = start = 0;
  1600. udev->dbi_max = 0;
  1601. } else {
  1602. udev->dbi_thresh = start = block + 1;
  1603. udev->dbi_max = block;
  1604. }
  1605. /* Here will truncate the data area from off */
  1606. off = udev->data_off + start * DATA_BLOCK_SIZE;
  1607. unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
  1608. /* Release the block pages */
  1609. for (i = start; i < end; i++) {
  1610. page = radix_tree_delete(&udev->data_blocks, i);
  1611. if (page) {
  1612. __free_page(page);
  1613. atomic_dec(&global_db_count);
  1614. }
  1615. }
  1616. mutex_unlock(&udev->cmdr_lock);
  1617. }
  1618. /*
  1619. * Try to wake up the udevs who are waiting
  1620. * for the global data pool.
  1621. */
  1622. list_for_each_entry(udev, &root_udev, node) {
  1623. if (udev->waiting_global)
  1624. wake_up(&udev->wait_cmdr);
  1625. }
  1626. mutex_unlock(&root_udev_mutex);
  1627. }
  1628. return 0;
  1629. }
  1630. static int __init tcmu_module_init(void)
  1631. {
  1632. int ret, i, k, len = 0;
  1633. BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
  1634. tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
  1635. sizeof(struct tcmu_cmd),
  1636. __alignof__(struct tcmu_cmd),
  1637. 0, NULL);
  1638. if (!tcmu_cmd_cache)
  1639. return -ENOMEM;
  1640. tcmu_root_device = root_device_register("tcm_user");
  1641. if (IS_ERR(tcmu_root_device)) {
  1642. ret = PTR_ERR(tcmu_root_device);
  1643. goto out_free_cache;
  1644. }
  1645. ret = genl_register_family(&tcmu_genl_family);
  1646. if (ret < 0) {
  1647. goto out_unreg_device;
  1648. }
  1649. for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
  1650. len += sizeof(struct configfs_attribute *);
  1651. }
  1652. for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
  1653. len += sizeof(struct configfs_attribute *);
  1654. }
  1655. len += sizeof(struct configfs_attribute *);
  1656. tcmu_attrs = kzalloc(len, GFP_KERNEL);
  1657. if (!tcmu_attrs) {
  1658. ret = -ENOMEM;
  1659. goto out_unreg_genl;
  1660. }
  1661. for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
  1662. tcmu_attrs[i] = passthrough_attrib_attrs[i];
  1663. }
  1664. for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
  1665. tcmu_attrs[i] = tcmu_attrib_attrs[k];
  1666. i++;
  1667. }
  1668. tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
  1669. ret = transport_backend_register(&tcmu_ops);
  1670. if (ret)
  1671. goto out_attrs;
  1672. init_waitqueue_head(&unmap_wait);
  1673. unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap");
  1674. if (IS_ERR(unmap_thread)) {
  1675. ret = PTR_ERR(unmap_thread);
  1676. goto out_unreg_transport;
  1677. }
  1678. return 0;
  1679. out_unreg_transport:
  1680. target_backend_unregister(&tcmu_ops);
  1681. out_attrs:
  1682. kfree(tcmu_attrs);
  1683. out_unreg_genl:
  1684. genl_unregister_family(&tcmu_genl_family);
  1685. out_unreg_device:
  1686. root_device_unregister(tcmu_root_device);
  1687. out_free_cache:
  1688. kmem_cache_destroy(tcmu_cmd_cache);
  1689. return ret;
  1690. }
  1691. static void __exit tcmu_module_exit(void)
  1692. {
  1693. kthread_stop(unmap_thread);
  1694. target_backend_unregister(&tcmu_ops);
  1695. kfree(tcmu_attrs);
  1696. genl_unregister_family(&tcmu_genl_family);
  1697. root_device_unregister(tcmu_root_device);
  1698. kmem_cache_destroy(tcmu_cmd_cache);
  1699. }
  1700. MODULE_DESCRIPTION("TCM USER subsystem plugin");
  1701. MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
  1702. MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
  1703. MODULE_LICENSE("GPL");
  1704. module_init(tcmu_module_init);
  1705. module_exit(tcmu_module_exit);