target_core_device.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2012 RisingTide Systems LLC.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <net/sock.h>
  36. #include <net/tcp.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_device.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_backend.h>
  41. #include <target/target_core_fabric.h>
  42. #include "target_core_internal.h"
  43. #include "target_core_alua.h"
  44. #include "target_core_pr.h"
  45. #include "target_core_ua.h"
  46. static struct se_hba *lun0_hba;
  47. /* not static, needed by tpg.c */
  48. struct se_device *g_lun0_dev;
  49. sense_reason_t
  50. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  51. {
  52. struct se_lun *se_lun = NULL;
  53. struct se_session *se_sess = se_cmd->se_sess;
  54. struct se_device *dev;
  55. unsigned long flags;
  56. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  57. return TCM_NON_EXISTENT_LUN;
  58. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  59. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  60. if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  61. struct se_dev_entry *deve = se_cmd->se_deve;
  62. deve->total_cmds++;
  63. deve->total_bytes += se_cmd->data_length;
  64. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  65. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
  66. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  67. " Access for 0x%08x\n",
  68. se_cmd->se_tfo->get_fabric_name(),
  69. unpacked_lun);
  70. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  71. return TCM_WRITE_PROTECTED;
  72. }
  73. if (se_cmd->data_direction == DMA_TO_DEVICE)
  74. deve->write_bytes += se_cmd->data_length;
  75. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  76. deve->read_bytes += se_cmd->data_length;
  77. deve->deve_cmds++;
  78. se_lun = deve->se_lun;
  79. se_cmd->se_lun = deve->se_lun;
  80. se_cmd->pr_res_key = deve->pr_res_key;
  81. se_cmd->orig_fe_lun = unpacked_lun;
  82. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  83. }
  84. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  85. if (!se_lun) {
  86. /*
  87. * Use the se_portal_group->tpg_virt_lun0 to allow for
  88. * REPORT_LUNS, et al to be returned when no active
  89. * MappedLUN=0 exists for this Initiator Port.
  90. */
  91. if (unpacked_lun != 0) {
  92. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  93. " Access for 0x%08x\n",
  94. se_cmd->se_tfo->get_fabric_name(),
  95. unpacked_lun);
  96. return TCM_NON_EXISTENT_LUN;
  97. }
  98. /*
  99. * Force WRITE PROTECT for virtual LUN 0
  100. */
  101. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  102. (se_cmd->data_direction != DMA_NONE))
  103. return TCM_WRITE_PROTECTED;
  104. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  105. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  106. se_cmd->orig_fe_lun = 0;
  107. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  108. }
  109. /* Directly associate cmd with se_dev */
  110. se_cmd->se_dev = se_lun->lun_se_dev;
  111. /* TODO: get rid of this and use atomics for stats */
  112. dev = se_lun->lun_se_dev;
  113. spin_lock_irqsave(&dev->stats_lock, flags);
  114. dev->num_cmds++;
  115. if (se_cmd->data_direction == DMA_TO_DEVICE)
  116. dev->write_bytes += se_cmd->data_length;
  117. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  118. dev->read_bytes += se_cmd->data_length;
  119. spin_unlock_irqrestore(&dev->stats_lock, flags);
  120. spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
  121. list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
  122. spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  126. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  127. {
  128. struct se_dev_entry *deve;
  129. struct se_lun *se_lun = NULL;
  130. struct se_session *se_sess = se_cmd->se_sess;
  131. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  132. unsigned long flags;
  133. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  134. return -ENODEV;
  135. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  136. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  137. deve = se_cmd->se_deve;
  138. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  139. se_tmr->tmr_lun = deve->se_lun;
  140. se_cmd->se_lun = deve->se_lun;
  141. se_lun = deve->se_lun;
  142. se_cmd->pr_res_key = deve->pr_res_key;
  143. se_cmd->orig_fe_lun = unpacked_lun;
  144. }
  145. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  146. if (!se_lun) {
  147. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  148. " Access for 0x%08x\n",
  149. se_cmd->se_tfo->get_fabric_name(),
  150. unpacked_lun);
  151. return -ENODEV;
  152. }
  153. /* Directly associate cmd with se_dev */
  154. se_cmd->se_dev = se_lun->lun_se_dev;
  155. se_tmr->tmr_dev = se_lun->lun_se_dev;
  156. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  157. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  158. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  162. /*
  163. * This function is called from core_scsi3_emulate_pro_register_and_move()
  164. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
  165. * when a matching rtpi is found.
  166. */
  167. struct se_dev_entry *core_get_se_deve_from_rtpi(
  168. struct se_node_acl *nacl,
  169. u16 rtpi)
  170. {
  171. struct se_dev_entry *deve;
  172. struct se_lun *lun;
  173. struct se_port *port;
  174. struct se_portal_group *tpg = nacl->se_tpg;
  175. u32 i;
  176. spin_lock_irq(&nacl->device_list_lock);
  177. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  178. deve = nacl->device_list[i];
  179. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  180. continue;
  181. lun = deve->se_lun;
  182. if (!lun) {
  183. pr_err("%s device entries device pointer is"
  184. " NULL, but Initiator has access.\n",
  185. tpg->se_tpg_tfo->get_fabric_name());
  186. continue;
  187. }
  188. port = lun->lun_sep;
  189. if (!port) {
  190. pr_err("%s device entries device pointer is"
  191. " NULL, but Initiator has access.\n",
  192. tpg->se_tpg_tfo->get_fabric_name());
  193. continue;
  194. }
  195. if (port->sep_rtpi != rtpi)
  196. continue;
  197. atomic_inc(&deve->pr_ref_count);
  198. smp_mb__after_atomic_inc();
  199. spin_unlock_irq(&nacl->device_list_lock);
  200. return deve;
  201. }
  202. spin_unlock_irq(&nacl->device_list_lock);
  203. return NULL;
  204. }
  205. int core_free_device_list_for_node(
  206. struct se_node_acl *nacl,
  207. struct se_portal_group *tpg)
  208. {
  209. struct se_dev_entry *deve;
  210. struct se_lun *lun;
  211. u32 i;
  212. if (!nacl->device_list)
  213. return 0;
  214. spin_lock_irq(&nacl->device_list_lock);
  215. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  216. deve = nacl->device_list[i];
  217. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  218. continue;
  219. if (!deve->se_lun) {
  220. pr_err("%s device entries device pointer is"
  221. " NULL, but Initiator has access.\n",
  222. tpg->se_tpg_tfo->get_fabric_name());
  223. continue;
  224. }
  225. lun = deve->se_lun;
  226. spin_unlock_irq(&nacl->device_list_lock);
  227. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  228. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  229. spin_lock_irq(&nacl->device_list_lock);
  230. }
  231. spin_unlock_irq(&nacl->device_list_lock);
  232. array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
  233. nacl->device_list = NULL;
  234. return 0;
  235. }
  236. void core_dec_lacl_count(struct se_node_acl *se_nacl, struct se_cmd *se_cmd)
  237. {
  238. struct se_dev_entry *deve;
  239. unsigned long flags;
  240. spin_lock_irqsave(&se_nacl->device_list_lock, flags);
  241. deve = se_nacl->device_list[se_cmd->orig_fe_lun];
  242. deve->deve_cmds--;
  243. spin_unlock_irqrestore(&se_nacl->device_list_lock, flags);
  244. }
  245. void core_update_device_list_access(
  246. u32 mapped_lun,
  247. u32 lun_access,
  248. struct se_node_acl *nacl)
  249. {
  250. struct se_dev_entry *deve;
  251. spin_lock_irq(&nacl->device_list_lock);
  252. deve = nacl->device_list[mapped_lun];
  253. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  254. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  255. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  256. } else {
  257. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  258. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  259. }
  260. spin_unlock_irq(&nacl->device_list_lock);
  261. }
  262. /* core_enable_device_list_for_node():
  263. *
  264. *
  265. */
  266. int core_enable_device_list_for_node(
  267. struct se_lun *lun,
  268. struct se_lun_acl *lun_acl,
  269. u32 mapped_lun,
  270. u32 lun_access,
  271. struct se_node_acl *nacl,
  272. struct se_portal_group *tpg)
  273. {
  274. struct se_port *port = lun->lun_sep;
  275. struct se_dev_entry *deve;
  276. spin_lock_irq(&nacl->device_list_lock);
  277. deve = nacl->device_list[mapped_lun];
  278. /*
  279. * Check if the call is handling demo mode -> explict LUN ACL
  280. * transition. This transition must be for the same struct se_lun
  281. * + mapped_lun that was setup in demo mode..
  282. */
  283. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  284. if (deve->se_lun_acl != NULL) {
  285. pr_err("struct se_dev_entry->se_lun_acl"
  286. " already set for demo mode -> explict"
  287. " LUN ACL transition\n");
  288. spin_unlock_irq(&nacl->device_list_lock);
  289. return -EINVAL;
  290. }
  291. if (deve->se_lun != lun) {
  292. pr_err("struct se_dev_entry->se_lun does"
  293. " match passed struct se_lun for demo mode"
  294. " -> explict LUN ACL transition\n");
  295. spin_unlock_irq(&nacl->device_list_lock);
  296. return -EINVAL;
  297. }
  298. deve->se_lun_acl = lun_acl;
  299. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  300. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  301. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  302. } else {
  303. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  304. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  305. }
  306. spin_unlock_irq(&nacl->device_list_lock);
  307. return 0;
  308. }
  309. deve->se_lun = lun;
  310. deve->se_lun_acl = lun_acl;
  311. deve->mapped_lun = mapped_lun;
  312. deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
  313. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  314. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  315. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  316. } else {
  317. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  318. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  319. }
  320. deve->creation_time = get_jiffies_64();
  321. deve->attach_count++;
  322. spin_unlock_irq(&nacl->device_list_lock);
  323. spin_lock_bh(&port->sep_alua_lock);
  324. list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
  325. spin_unlock_bh(&port->sep_alua_lock);
  326. return 0;
  327. }
  328. /* core_disable_device_list_for_node():
  329. *
  330. *
  331. */
  332. int core_disable_device_list_for_node(
  333. struct se_lun *lun,
  334. struct se_lun_acl *lun_acl,
  335. u32 mapped_lun,
  336. u32 lun_access,
  337. struct se_node_acl *nacl,
  338. struct se_portal_group *tpg)
  339. {
  340. struct se_port *port = lun->lun_sep;
  341. struct se_dev_entry *deve = nacl->device_list[mapped_lun];
  342. /*
  343. * If the MappedLUN entry is being disabled, the entry in
  344. * port->sep_alua_list must be removed now before clearing the
  345. * struct se_dev_entry pointers below as logic in
  346. * core_alua_do_transition_tg_pt() depends on these being present.
  347. *
  348. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  349. * that have not been explicitly converted to MappedLUNs ->
  350. * struct se_lun_acl, but we remove deve->alua_port_list from
  351. * port->sep_alua_list. This also means that active UAs and
  352. * NodeACL context specific PR metadata for demo-mode
  353. * MappedLUN *deve will be released below..
  354. */
  355. spin_lock_bh(&port->sep_alua_lock);
  356. list_del(&deve->alua_port_list);
  357. spin_unlock_bh(&port->sep_alua_lock);
  358. /*
  359. * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
  360. * PR operation to complete.
  361. */
  362. while (atomic_read(&deve->pr_ref_count) != 0)
  363. cpu_relax();
  364. spin_lock_irq(&nacl->device_list_lock);
  365. /*
  366. * Disable struct se_dev_entry LUN ACL mapping
  367. */
  368. core_scsi3_ua_release_all(deve);
  369. deve->se_lun = NULL;
  370. deve->se_lun_acl = NULL;
  371. deve->lun_flags = 0;
  372. deve->creation_time = 0;
  373. deve->attach_count--;
  374. spin_unlock_irq(&nacl->device_list_lock);
  375. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  376. return 0;
  377. }
  378. /* core_clear_lun_from_tpg():
  379. *
  380. *
  381. */
  382. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  383. {
  384. struct se_node_acl *nacl;
  385. struct se_dev_entry *deve;
  386. u32 i;
  387. spin_lock_irq(&tpg->acl_node_lock);
  388. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  389. spin_unlock_irq(&tpg->acl_node_lock);
  390. spin_lock_irq(&nacl->device_list_lock);
  391. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  392. deve = nacl->device_list[i];
  393. if (lun != deve->se_lun)
  394. continue;
  395. spin_unlock_irq(&nacl->device_list_lock);
  396. core_disable_device_list_for_node(lun, NULL,
  397. deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
  398. nacl, tpg);
  399. spin_lock_irq(&nacl->device_list_lock);
  400. }
  401. spin_unlock_irq(&nacl->device_list_lock);
  402. spin_lock_irq(&tpg->acl_node_lock);
  403. }
  404. spin_unlock_irq(&tpg->acl_node_lock);
  405. }
  406. static struct se_port *core_alloc_port(struct se_device *dev)
  407. {
  408. struct se_port *port, *port_tmp;
  409. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  410. if (!port) {
  411. pr_err("Unable to allocate struct se_port\n");
  412. return ERR_PTR(-ENOMEM);
  413. }
  414. INIT_LIST_HEAD(&port->sep_alua_list);
  415. INIT_LIST_HEAD(&port->sep_list);
  416. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  417. spin_lock_init(&port->sep_alua_lock);
  418. mutex_init(&port->sep_tg_pt_md_mutex);
  419. spin_lock(&dev->se_port_lock);
  420. if (dev->dev_port_count == 0x0000ffff) {
  421. pr_warn("Reached dev->dev_port_count =="
  422. " 0x0000ffff\n");
  423. spin_unlock(&dev->se_port_lock);
  424. return ERR_PTR(-ENOSPC);
  425. }
  426. again:
  427. /*
  428. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  429. * Here is the table from spc4r17 section 7.7.3.8.
  430. *
  431. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  432. *
  433. * Code Description
  434. * 0h Reserved
  435. * 1h Relative port 1, historically known as port A
  436. * 2h Relative port 2, historically known as port B
  437. * 3h to FFFFh Relative port 3 through 65 535
  438. */
  439. port->sep_rtpi = dev->dev_rpti_counter++;
  440. if (!port->sep_rtpi)
  441. goto again;
  442. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  443. /*
  444. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  445. * for 16-bit wrap..
  446. */
  447. if (port->sep_rtpi == port_tmp->sep_rtpi)
  448. goto again;
  449. }
  450. spin_unlock(&dev->se_port_lock);
  451. return port;
  452. }
  453. static void core_export_port(
  454. struct se_device *dev,
  455. struct se_portal_group *tpg,
  456. struct se_port *port,
  457. struct se_lun *lun)
  458. {
  459. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  460. spin_lock(&dev->se_port_lock);
  461. spin_lock(&lun->lun_sep_lock);
  462. port->sep_tpg = tpg;
  463. port->sep_lun = lun;
  464. lun->lun_sep = port;
  465. spin_unlock(&lun->lun_sep_lock);
  466. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  467. spin_unlock(&dev->se_port_lock);
  468. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  469. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  470. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  471. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  472. pr_err("Unable to allocate t10_alua_tg_pt"
  473. "_gp_member_t\n");
  474. return;
  475. }
  476. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  477. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  478. dev->t10_alua.default_tg_pt_gp);
  479. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  480. pr_debug("%s/%s: Adding to default ALUA Target Port"
  481. " Group: alua/default_tg_pt_gp\n",
  482. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  483. }
  484. dev->dev_port_count++;
  485. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  486. }
  487. /*
  488. * Called with struct se_device->se_port_lock spinlock held.
  489. */
  490. static void core_release_port(struct se_device *dev, struct se_port *port)
  491. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  492. {
  493. /*
  494. * Wait for any port reference for PR ALL_TG_PT=1 operation
  495. * to complete in __core_scsi3_alloc_registration()
  496. */
  497. spin_unlock(&dev->se_port_lock);
  498. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  499. cpu_relax();
  500. spin_lock(&dev->se_port_lock);
  501. core_alua_free_tg_pt_gp_mem(port);
  502. list_del(&port->sep_list);
  503. dev->dev_port_count--;
  504. kfree(port);
  505. }
  506. int core_dev_export(
  507. struct se_device *dev,
  508. struct se_portal_group *tpg,
  509. struct se_lun *lun)
  510. {
  511. struct se_hba *hba = dev->se_hba;
  512. struct se_port *port;
  513. port = core_alloc_port(dev);
  514. if (IS_ERR(port))
  515. return PTR_ERR(port);
  516. lun->lun_se_dev = dev;
  517. spin_lock(&hba->device_lock);
  518. dev->export_count++;
  519. spin_unlock(&hba->device_lock);
  520. core_export_port(dev, tpg, port, lun);
  521. return 0;
  522. }
  523. void core_dev_unexport(
  524. struct se_device *dev,
  525. struct se_portal_group *tpg,
  526. struct se_lun *lun)
  527. {
  528. struct se_hba *hba = dev->se_hba;
  529. struct se_port *port = lun->lun_sep;
  530. spin_lock(&lun->lun_sep_lock);
  531. if (lun->lun_se_dev == NULL) {
  532. spin_unlock(&lun->lun_sep_lock);
  533. return;
  534. }
  535. spin_unlock(&lun->lun_sep_lock);
  536. spin_lock(&dev->se_port_lock);
  537. core_release_port(dev, port);
  538. spin_unlock(&dev->se_port_lock);
  539. spin_lock(&hba->device_lock);
  540. dev->export_count--;
  541. spin_unlock(&hba->device_lock);
  542. lun->lun_se_dev = NULL;
  543. }
  544. static void se_release_vpd_for_dev(struct se_device *dev)
  545. {
  546. struct t10_vpd *vpd, *vpd_tmp;
  547. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  548. list_for_each_entry_safe(vpd, vpd_tmp,
  549. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  550. list_del(&vpd->vpd_list);
  551. kfree(vpd);
  552. }
  553. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  554. }
  555. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  556. {
  557. u32 aligned_max_sectors;
  558. u32 alignment;
  559. /*
  560. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  561. * transport_allocate_data_tasks() operation.
  562. */
  563. alignment = max(1ul, PAGE_SIZE / block_size);
  564. aligned_max_sectors = rounddown(max_sectors, alignment);
  565. if (max_sectors != aligned_max_sectors)
  566. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  567. max_sectors, aligned_max_sectors);
  568. return aligned_max_sectors;
  569. }
  570. int se_dev_set_max_unmap_lba_count(
  571. struct se_device *dev,
  572. u32 max_unmap_lba_count)
  573. {
  574. dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  575. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  576. dev, dev->dev_attrib.max_unmap_lba_count);
  577. return 0;
  578. }
  579. int se_dev_set_max_unmap_block_desc_count(
  580. struct se_device *dev,
  581. u32 max_unmap_block_desc_count)
  582. {
  583. dev->dev_attrib.max_unmap_block_desc_count =
  584. max_unmap_block_desc_count;
  585. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  586. dev, dev->dev_attrib.max_unmap_block_desc_count);
  587. return 0;
  588. }
  589. int se_dev_set_unmap_granularity(
  590. struct se_device *dev,
  591. u32 unmap_granularity)
  592. {
  593. dev->dev_attrib.unmap_granularity = unmap_granularity;
  594. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  595. dev, dev->dev_attrib.unmap_granularity);
  596. return 0;
  597. }
  598. int se_dev_set_unmap_granularity_alignment(
  599. struct se_device *dev,
  600. u32 unmap_granularity_alignment)
  601. {
  602. dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  603. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  604. dev, dev->dev_attrib.unmap_granularity_alignment);
  605. return 0;
  606. }
  607. int se_dev_set_max_write_same_len(
  608. struct se_device *dev,
  609. u32 max_write_same_len)
  610. {
  611. dev->dev_attrib.max_write_same_len = max_write_same_len;
  612. pr_debug("dev[%p]: Set max_write_same_len: %u\n",
  613. dev, dev->dev_attrib.max_write_same_len);
  614. return 0;
  615. }
  616. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  617. {
  618. if (flag != 0 && flag != 1) {
  619. pr_err("Illegal value %d\n", flag);
  620. return -EINVAL;
  621. }
  622. if (flag) {
  623. pr_err("dpo_emulated not supported\n");
  624. return -EINVAL;
  625. }
  626. return 0;
  627. }
  628. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  629. {
  630. if (flag != 0 && flag != 1) {
  631. pr_err("Illegal value %d\n", flag);
  632. return -EINVAL;
  633. }
  634. if (flag &&
  635. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  636. pr_err("emulate_fua_write not supported for pSCSI\n");
  637. return -EINVAL;
  638. }
  639. dev->dev_attrib.emulate_fua_write = flag;
  640. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  641. dev, dev->dev_attrib.emulate_fua_write);
  642. return 0;
  643. }
  644. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  645. {
  646. if (flag != 0 && flag != 1) {
  647. pr_err("Illegal value %d\n", flag);
  648. return -EINVAL;
  649. }
  650. if (flag) {
  651. pr_err("ua read emulated not supported\n");
  652. return -EINVAL;
  653. }
  654. return 0;
  655. }
  656. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  657. {
  658. if (flag != 0 && flag != 1) {
  659. pr_err("Illegal value %d\n", flag);
  660. return -EINVAL;
  661. }
  662. if (flag &&
  663. dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  664. pr_err("emulate_write_cache not supported for pSCSI\n");
  665. return -EINVAL;
  666. }
  667. if (dev->transport->get_write_cache) {
  668. pr_warn("emulate_write_cache cannot be changed when underlying"
  669. " HW reports WriteCacheEnabled, ignoring request\n");
  670. return 0;
  671. }
  672. dev->dev_attrib.emulate_write_cache = flag;
  673. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  674. dev, dev->dev_attrib.emulate_write_cache);
  675. return 0;
  676. }
  677. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  678. {
  679. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  680. pr_err("Illegal value %d\n", flag);
  681. return -EINVAL;
  682. }
  683. if (dev->export_count) {
  684. pr_err("dev[%p]: Unable to change SE Device"
  685. " UA_INTRLCK_CTRL while export_count is %d\n",
  686. dev, dev->export_count);
  687. return -EINVAL;
  688. }
  689. dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
  690. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  691. dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
  692. return 0;
  693. }
  694. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  695. {
  696. if ((flag != 0) && (flag != 1)) {
  697. pr_err("Illegal value %d\n", flag);
  698. return -EINVAL;
  699. }
  700. if (dev->export_count) {
  701. pr_err("dev[%p]: Unable to change SE Device TAS while"
  702. " export_count is %d\n",
  703. dev, dev->export_count);
  704. return -EINVAL;
  705. }
  706. dev->dev_attrib.emulate_tas = flag;
  707. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  708. dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  709. return 0;
  710. }
  711. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  712. {
  713. if ((flag != 0) && (flag != 1)) {
  714. pr_err("Illegal value %d\n", flag);
  715. return -EINVAL;
  716. }
  717. /*
  718. * We expect this value to be non-zero when generic Block Layer
  719. * Discard supported is detected iblock_create_virtdevice().
  720. */
  721. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  722. pr_err("Generic Block Discard not supported\n");
  723. return -ENOSYS;
  724. }
  725. dev->dev_attrib.emulate_tpu = flag;
  726. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  727. dev, flag);
  728. return 0;
  729. }
  730. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  731. {
  732. if ((flag != 0) && (flag != 1)) {
  733. pr_err("Illegal value %d\n", flag);
  734. return -EINVAL;
  735. }
  736. /*
  737. * We expect this value to be non-zero when generic Block Layer
  738. * Discard supported is detected iblock_create_virtdevice().
  739. */
  740. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  741. pr_err("Generic Block Discard not supported\n");
  742. return -ENOSYS;
  743. }
  744. dev->dev_attrib.emulate_tpws = flag;
  745. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  746. dev, flag);
  747. return 0;
  748. }
  749. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  750. {
  751. if ((flag != 0) && (flag != 1)) {
  752. pr_err("Illegal value %d\n", flag);
  753. return -EINVAL;
  754. }
  755. dev->dev_attrib.enforce_pr_isids = flag;
  756. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  757. (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  758. return 0;
  759. }
  760. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  761. {
  762. if ((flag != 0) && (flag != 1)) {
  763. printk(KERN_ERR "Illegal value %d\n", flag);
  764. return -EINVAL;
  765. }
  766. dev->dev_attrib.is_nonrot = flag;
  767. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  768. dev, flag);
  769. return 0;
  770. }
  771. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  772. {
  773. if (flag != 0) {
  774. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  775. " reordering not implemented\n", dev);
  776. return -ENOSYS;
  777. }
  778. dev->dev_attrib.emulate_rest_reord = flag;
  779. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  780. return 0;
  781. }
  782. /*
  783. * Note, this can only be called on unexported SE Device Object.
  784. */
  785. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  786. {
  787. if (dev->export_count) {
  788. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  789. " export_count is %d\n",
  790. dev, dev->export_count);
  791. return -EINVAL;
  792. }
  793. if (!queue_depth) {
  794. pr_err("dev[%p]: Illegal ZERO value for queue"
  795. "_depth\n", dev);
  796. return -EINVAL;
  797. }
  798. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  799. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  800. pr_err("dev[%p]: Passed queue_depth: %u"
  801. " exceeds TCM/SE_Device TCQ: %u\n",
  802. dev, queue_depth,
  803. dev->dev_attrib.hw_queue_depth);
  804. return -EINVAL;
  805. }
  806. } else {
  807. if (queue_depth > dev->dev_attrib.queue_depth) {
  808. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  809. pr_err("dev[%p]: Passed queue_depth:"
  810. " %u exceeds TCM/SE_Device MAX"
  811. " TCQ: %u\n", dev, queue_depth,
  812. dev->dev_attrib.hw_queue_depth);
  813. return -EINVAL;
  814. }
  815. }
  816. }
  817. dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  818. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  819. dev, queue_depth);
  820. return 0;
  821. }
  822. int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
  823. {
  824. int block_size = dev->dev_attrib.block_size;
  825. if (dev->export_count) {
  826. pr_err("dev[%p]: Unable to change SE Device"
  827. " fabric_max_sectors while export_count is %d\n",
  828. dev, dev->export_count);
  829. return -EINVAL;
  830. }
  831. if (!fabric_max_sectors) {
  832. pr_err("dev[%p]: Illegal ZERO value for"
  833. " fabric_max_sectors\n", dev);
  834. return -EINVAL;
  835. }
  836. if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  837. pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
  838. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
  839. DA_STATUS_MAX_SECTORS_MIN);
  840. return -EINVAL;
  841. }
  842. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  843. if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
  844. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  845. " greater than TCM/SE_Device max_sectors:"
  846. " %u\n", dev, fabric_max_sectors,
  847. dev->dev_attrib.hw_max_sectors);
  848. return -EINVAL;
  849. }
  850. } else {
  851. if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  852. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  853. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  854. " %u\n", dev, fabric_max_sectors,
  855. DA_STATUS_MAX_SECTORS_MAX);
  856. return -EINVAL;
  857. }
  858. }
  859. /*
  860. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  861. */
  862. if (!block_size) {
  863. block_size = 512;
  864. pr_warn("Defaulting to 512 for zero block_size\n");
  865. }
  866. fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
  867. block_size);
  868. dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
  869. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  870. dev, fabric_max_sectors);
  871. return 0;
  872. }
  873. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  874. {
  875. if (dev->export_count) {
  876. pr_err("dev[%p]: Unable to change SE Device"
  877. " optimal_sectors while export_count is %d\n",
  878. dev, dev->export_count);
  879. return -EINVAL;
  880. }
  881. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  882. pr_err("dev[%p]: Passed optimal_sectors cannot be"
  883. " changed for TCM/pSCSI\n", dev);
  884. return -EINVAL;
  885. }
  886. if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
  887. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  888. " greater than fabric_max_sectors: %u\n", dev,
  889. optimal_sectors, dev->dev_attrib.fabric_max_sectors);
  890. return -EINVAL;
  891. }
  892. dev->dev_attrib.optimal_sectors = optimal_sectors;
  893. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  894. dev, optimal_sectors);
  895. return 0;
  896. }
  897. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  898. {
  899. if (dev->export_count) {
  900. pr_err("dev[%p]: Unable to change SE Device block_size"
  901. " while export_count is %d\n",
  902. dev, dev->export_count);
  903. return -EINVAL;
  904. }
  905. if ((block_size != 512) &&
  906. (block_size != 1024) &&
  907. (block_size != 2048) &&
  908. (block_size != 4096)) {
  909. pr_err("dev[%p]: Illegal value for block_device: %u"
  910. " for SE device, must be 512, 1024, 2048 or 4096\n",
  911. dev, block_size);
  912. return -EINVAL;
  913. }
  914. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  915. pr_err("dev[%p]: Not allowed to change block_size for"
  916. " Physical Device, use for Linux/SCSI to change"
  917. " block_size for underlying hardware\n", dev);
  918. return -EINVAL;
  919. }
  920. dev->dev_attrib.block_size = block_size;
  921. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  922. dev, block_size);
  923. return 0;
  924. }
  925. struct se_lun *core_dev_add_lun(
  926. struct se_portal_group *tpg,
  927. struct se_device *dev,
  928. u32 lun)
  929. {
  930. struct se_lun *lun_p;
  931. int rc;
  932. lun_p = core_tpg_pre_addlun(tpg, lun);
  933. if (IS_ERR(lun_p))
  934. return lun_p;
  935. rc = core_tpg_post_addlun(tpg, lun_p,
  936. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  937. if (rc < 0)
  938. return ERR_PTR(rc);
  939. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  940. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  941. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
  942. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  943. /*
  944. * Update LUN maps for dynamically added initiators when
  945. * generate_node_acl is enabled.
  946. */
  947. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  948. struct se_node_acl *acl;
  949. spin_lock_irq(&tpg->acl_node_lock);
  950. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  951. if (acl->dynamic_node_acl &&
  952. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  953. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  954. spin_unlock_irq(&tpg->acl_node_lock);
  955. core_tpg_add_node_to_devs(acl, tpg);
  956. spin_lock_irq(&tpg->acl_node_lock);
  957. }
  958. }
  959. spin_unlock_irq(&tpg->acl_node_lock);
  960. }
  961. return lun_p;
  962. }
  963. /* core_dev_del_lun():
  964. *
  965. *
  966. */
  967. int core_dev_del_lun(
  968. struct se_portal_group *tpg,
  969. u32 unpacked_lun)
  970. {
  971. struct se_lun *lun;
  972. lun = core_tpg_pre_dellun(tpg, unpacked_lun);
  973. if (IS_ERR(lun))
  974. return PTR_ERR(lun);
  975. core_tpg_post_dellun(tpg, lun);
  976. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
  977. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  978. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
  979. tpg->se_tpg_tfo->get_fabric_name());
  980. return 0;
  981. }
  982. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  983. {
  984. struct se_lun *lun;
  985. spin_lock(&tpg->tpg_lun_lock);
  986. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  987. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  988. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  989. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  990. TRANSPORT_MAX_LUNS_PER_TPG-1,
  991. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  992. spin_unlock(&tpg->tpg_lun_lock);
  993. return NULL;
  994. }
  995. lun = tpg->tpg_lun_list[unpacked_lun];
  996. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  997. pr_err("%s Logical Unit Number: %u is not free on"
  998. " Target Portal Group: %hu, ignoring request.\n",
  999. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1000. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1001. spin_unlock(&tpg->tpg_lun_lock);
  1002. return NULL;
  1003. }
  1004. spin_unlock(&tpg->tpg_lun_lock);
  1005. return lun;
  1006. }
  1007. /* core_dev_get_lun():
  1008. *
  1009. *
  1010. */
  1011. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1012. {
  1013. struct se_lun *lun;
  1014. spin_lock(&tpg->tpg_lun_lock);
  1015. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1016. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1017. "_TPG-1: %u for Target Portal Group: %hu\n",
  1018. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1019. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1020. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1021. spin_unlock(&tpg->tpg_lun_lock);
  1022. return NULL;
  1023. }
  1024. lun = tpg->tpg_lun_list[unpacked_lun];
  1025. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1026. pr_err("%s Logical Unit Number: %u is not active on"
  1027. " Target Portal Group: %hu, ignoring request.\n",
  1028. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1029. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1030. spin_unlock(&tpg->tpg_lun_lock);
  1031. return NULL;
  1032. }
  1033. spin_unlock(&tpg->tpg_lun_lock);
  1034. return lun;
  1035. }
  1036. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1037. struct se_portal_group *tpg,
  1038. u32 mapped_lun,
  1039. char *initiatorname,
  1040. int *ret)
  1041. {
  1042. struct se_lun_acl *lacl;
  1043. struct se_node_acl *nacl;
  1044. if (strlen(initiatorname) >= TRANSPORT_IQN_LEN) {
  1045. pr_err("%s InitiatorName exceeds maximum size.\n",
  1046. tpg->se_tpg_tfo->get_fabric_name());
  1047. *ret = -EOVERFLOW;
  1048. return NULL;
  1049. }
  1050. nacl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
  1051. if (!nacl) {
  1052. *ret = -EINVAL;
  1053. return NULL;
  1054. }
  1055. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1056. if (!lacl) {
  1057. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1058. *ret = -ENOMEM;
  1059. return NULL;
  1060. }
  1061. INIT_LIST_HEAD(&lacl->lacl_list);
  1062. lacl->mapped_lun = mapped_lun;
  1063. lacl->se_lun_nacl = nacl;
  1064. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
  1065. return lacl;
  1066. }
  1067. int core_dev_add_initiator_node_lun_acl(
  1068. struct se_portal_group *tpg,
  1069. struct se_lun_acl *lacl,
  1070. u32 unpacked_lun,
  1071. u32 lun_access)
  1072. {
  1073. struct se_lun *lun;
  1074. struct se_node_acl *nacl;
  1075. lun = core_dev_get_lun(tpg, unpacked_lun);
  1076. if (!lun) {
  1077. pr_err("%s Logical Unit Number: %u is not active on"
  1078. " Target Portal Group: %hu, ignoring request.\n",
  1079. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1080. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1081. return -EINVAL;
  1082. }
  1083. nacl = lacl->se_lun_nacl;
  1084. if (!nacl)
  1085. return -EINVAL;
  1086. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1087. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1088. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1089. lacl->se_lun = lun;
  1090. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1091. lun_access, nacl, tpg) < 0)
  1092. return -EINVAL;
  1093. spin_lock(&lun->lun_acl_lock);
  1094. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1095. atomic_inc(&lun->lun_acl_count);
  1096. smp_mb__after_atomic_inc();
  1097. spin_unlock(&lun->lun_acl_lock);
  1098. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1099. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1100. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1101. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1102. lacl->initiatorname);
  1103. /*
  1104. * Check to see if there are any existing persistent reservation APTPL
  1105. * pre-registrations that need to be enabled for this LUN ACL..
  1106. */
  1107. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
  1108. return 0;
  1109. }
  1110. /* core_dev_del_initiator_node_lun_acl():
  1111. *
  1112. *
  1113. */
  1114. int core_dev_del_initiator_node_lun_acl(
  1115. struct se_portal_group *tpg,
  1116. struct se_lun *lun,
  1117. struct se_lun_acl *lacl)
  1118. {
  1119. struct se_node_acl *nacl;
  1120. nacl = lacl->se_lun_nacl;
  1121. if (!nacl)
  1122. return -EINVAL;
  1123. spin_lock(&lun->lun_acl_lock);
  1124. list_del(&lacl->lacl_list);
  1125. atomic_dec(&lun->lun_acl_count);
  1126. smp_mb__after_atomic_dec();
  1127. spin_unlock(&lun->lun_acl_lock);
  1128. core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1129. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  1130. lacl->se_lun = NULL;
  1131. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1132. " InitiatorNode: %s Mapped LUN: %u\n",
  1133. tpg->se_tpg_tfo->get_fabric_name(),
  1134. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1135. lacl->initiatorname, lacl->mapped_lun);
  1136. return 0;
  1137. }
  1138. void core_dev_free_initiator_node_lun_acl(
  1139. struct se_portal_group *tpg,
  1140. struct se_lun_acl *lacl)
  1141. {
  1142. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1143. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1144. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1145. tpg->se_tpg_tfo->get_fabric_name(),
  1146. lacl->initiatorname, lacl->mapped_lun);
  1147. kfree(lacl);
  1148. }
  1149. static void scsi_dump_inquiry(struct se_device *dev)
  1150. {
  1151. struct t10_wwn *wwn = &dev->t10_wwn;
  1152. char buf[17];
  1153. int i, device_type;
  1154. /*
  1155. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1156. */
  1157. for (i = 0; i < 8; i++)
  1158. if (wwn->vendor[i] >= 0x20)
  1159. buf[i] = wwn->vendor[i];
  1160. else
  1161. buf[i] = ' ';
  1162. buf[i] = '\0';
  1163. pr_debug(" Vendor: %s\n", buf);
  1164. for (i = 0; i < 16; i++)
  1165. if (wwn->model[i] >= 0x20)
  1166. buf[i] = wwn->model[i];
  1167. else
  1168. buf[i] = ' ';
  1169. buf[i] = '\0';
  1170. pr_debug(" Model: %s\n", buf);
  1171. for (i = 0; i < 4; i++)
  1172. if (wwn->revision[i] >= 0x20)
  1173. buf[i] = wwn->revision[i];
  1174. else
  1175. buf[i] = ' ';
  1176. buf[i] = '\0';
  1177. pr_debug(" Revision: %s\n", buf);
  1178. device_type = dev->transport->get_device_type(dev);
  1179. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1180. }
  1181. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  1182. {
  1183. struct se_device *dev;
  1184. dev = hba->transport->alloc_device(hba, name);
  1185. if (!dev)
  1186. return NULL;
  1187. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1188. dev->se_hba = hba;
  1189. dev->transport = hba->transport;
  1190. INIT_LIST_HEAD(&dev->dev_list);
  1191. INIT_LIST_HEAD(&dev->dev_sep_list);
  1192. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1193. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1194. INIT_LIST_HEAD(&dev->state_list);
  1195. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1196. spin_lock_init(&dev->stats_lock);
  1197. spin_lock_init(&dev->execute_task_lock);
  1198. spin_lock_init(&dev->delayed_cmd_lock);
  1199. spin_lock_init(&dev->dev_reservation_lock);
  1200. spin_lock_init(&dev->se_port_lock);
  1201. spin_lock_init(&dev->se_tmr_lock);
  1202. spin_lock_init(&dev->qf_cmd_lock);
  1203. atomic_set(&dev->dev_ordered_id, 0);
  1204. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  1205. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  1206. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  1207. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  1208. spin_lock_init(&dev->t10_pr.registration_lock);
  1209. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  1210. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  1211. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  1212. dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
  1213. dev->t10_wwn.t10_dev = dev;
  1214. dev->t10_alua.t10_dev = dev;
  1215. dev->dev_attrib.da_dev = dev;
  1216. dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  1217. dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  1218. dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  1219. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  1220. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  1221. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  1222. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  1223. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  1224. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  1225. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  1226. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  1227. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  1228. dev->dev_attrib.max_unmap_block_desc_count =
  1229. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  1230. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  1231. dev->dev_attrib.unmap_granularity_alignment =
  1232. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  1233. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  1234. dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
  1235. dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
  1236. return dev;
  1237. }
  1238. int target_configure_device(struct se_device *dev)
  1239. {
  1240. struct se_hba *hba = dev->se_hba;
  1241. int ret;
  1242. if (dev->dev_flags & DF_CONFIGURED) {
  1243. pr_err("se_dev->se_dev_ptr already set for storage"
  1244. " object\n");
  1245. return -EEXIST;
  1246. }
  1247. ret = dev->transport->configure_device(dev);
  1248. if (ret)
  1249. goto out;
  1250. dev->dev_flags |= DF_CONFIGURED;
  1251. /*
  1252. * XXX: there is not much point to have two different values here..
  1253. */
  1254. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  1255. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  1256. /*
  1257. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  1258. */
  1259. dev->dev_attrib.hw_max_sectors =
  1260. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  1261. dev->dev_attrib.hw_block_size);
  1262. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1263. dev->creation_time = get_jiffies_64();
  1264. ret = core_setup_alua(dev);
  1265. if (ret)
  1266. goto out;
  1267. /*
  1268. * Startup the struct se_device processing thread
  1269. */
  1270. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  1271. dev->transport->name);
  1272. if (!dev->tmr_wq) {
  1273. pr_err("Unable to create tmr workqueue for %s\n",
  1274. dev->transport->name);
  1275. ret = -ENOMEM;
  1276. goto out_free_alua;
  1277. }
  1278. /*
  1279. * Setup work_queue for QUEUE_FULL
  1280. */
  1281. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1282. /*
  1283. * Preload the initial INQUIRY const values if we are doing
  1284. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1285. * passthrough because this is being provided by the backend LLD.
  1286. */
  1287. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1288. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1289. strncpy(&dev->t10_wwn.model[0],
  1290. dev->transport->inquiry_prod, 16);
  1291. strncpy(&dev->t10_wwn.revision[0],
  1292. dev->transport->inquiry_rev, 4);
  1293. }
  1294. scsi_dump_inquiry(dev);
  1295. spin_lock(&hba->device_lock);
  1296. hba->dev_count++;
  1297. spin_unlock(&hba->device_lock);
  1298. return 0;
  1299. out_free_alua:
  1300. core_alua_free_lu_gp_mem(dev);
  1301. out:
  1302. se_release_vpd_for_dev(dev);
  1303. return ret;
  1304. }
  1305. void target_free_device(struct se_device *dev)
  1306. {
  1307. struct se_hba *hba = dev->se_hba;
  1308. WARN_ON(!list_empty(&dev->dev_sep_list));
  1309. if (dev->dev_flags & DF_CONFIGURED) {
  1310. destroy_workqueue(dev->tmr_wq);
  1311. spin_lock(&hba->device_lock);
  1312. hba->dev_count--;
  1313. spin_unlock(&hba->device_lock);
  1314. }
  1315. core_alua_free_lu_gp_mem(dev);
  1316. core_scsi3_free_all_registrations(dev);
  1317. se_release_vpd_for_dev(dev);
  1318. dev->transport->free_device(dev);
  1319. }
  1320. int core_dev_setup_virtual_lun0(void)
  1321. {
  1322. struct se_hba *hba;
  1323. struct se_device *dev;
  1324. char buf[16];
  1325. int ret;
  1326. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1327. if (IS_ERR(hba))
  1328. return PTR_ERR(hba);
  1329. dev = target_alloc_device(hba, "virt_lun0");
  1330. if (!dev) {
  1331. ret = -ENOMEM;
  1332. goto out_free_hba;
  1333. }
  1334. memset(buf, 0, 16);
  1335. sprintf(buf, "rd_pages=8");
  1336. hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
  1337. ret = target_configure_device(dev);
  1338. if (ret)
  1339. goto out_free_se_dev;
  1340. lun0_hba = hba;
  1341. g_lun0_dev = dev;
  1342. return 0;
  1343. out_free_se_dev:
  1344. target_free_device(dev);
  1345. out_free_hba:
  1346. core_delete_hba(hba);
  1347. return ret;
  1348. }
  1349. void core_dev_release_virtual_lun0(void)
  1350. {
  1351. struct se_hba *hba = lun0_hba;
  1352. if (!hba)
  1353. return;
  1354. if (g_lun0_dev)
  1355. target_free_device(g_lun0_dev);
  1356. core_delete_hba(hba);
  1357. }