target_core_alua.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. /*******************************************************************************
  2. * Filename: target_core_alua.c
  3. *
  4. * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
  5. *
  6. * Copyright (c) 2009-2010 Rising Tide Systems
  7. * Copyright (c) 2009-2010 Linux-iSCSI.org
  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/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/configfs.h>
  29. #include <linux/export.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <asm/unaligned.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_backend.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/target_core_configfs.h>
  37. #include "target_core_internal.h"
  38. #include "target_core_alua.h"
  39. #include "target_core_ua.h"
  40. static int core_alua_check_transition(int state, int *primary);
  41. static int core_alua_set_tg_pt_secondary_state(
  42. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  43. struct se_port *port, int explict, int offline);
  44. static u16 alua_lu_gps_counter;
  45. static u32 alua_lu_gps_count;
  46. static DEFINE_SPINLOCK(lu_gps_lock);
  47. static LIST_HEAD(lu_gps_list);
  48. struct t10_alua_lu_gp *default_lu_gp;
  49. /*
  50. * REPORT_TARGET_PORT_GROUPS
  51. *
  52. * See spc4r17 section 6.27
  53. */
  54. int target_emulate_report_target_port_groups(struct se_cmd *cmd)
  55. {
  56. struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
  57. struct se_port *port;
  58. struct t10_alua_tg_pt_gp *tg_pt_gp;
  59. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  60. unsigned char *buf;
  61. u32 rd_len = 0, off;
  62. int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
  63. /*
  64. * Skip over RESERVED area to first Target port group descriptor
  65. * depending on the PARAMETER DATA FORMAT type..
  66. */
  67. if (ext_hdr != 0)
  68. off = 8;
  69. else
  70. off = 4;
  71. if (cmd->data_length < off) {
  72. pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
  73. " small for %s header\n", cmd->data_length,
  74. (ext_hdr) ? "extended" : "normal");
  75. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  76. return -EINVAL;
  77. }
  78. buf = transport_kmap_data_sg(cmd);
  79. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  80. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  81. tg_pt_gp_list) {
  82. /*
  83. * Check if the Target port group and Target port descriptor list
  84. * based on tg_pt_gp_members count will fit into the response payload.
  85. * Otherwise, bump rd_len to let the initiator know we have exceeded
  86. * the allocation length and the response is truncated.
  87. */
  88. if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
  89. cmd->data_length) {
  90. rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
  91. continue;
  92. }
  93. /*
  94. * PREF: Preferred target port bit, determine if this
  95. * bit should be set for port group.
  96. */
  97. if (tg_pt_gp->tg_pt_gp_pref)
  98. buf[off] = 0x80;
  99. /*
  100. * Set the ASYMMETRIC ACCESS State
  101. */
  102. buf[off++] |= (atomic_read(
  103. &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  104. /*
  105. * Set supported ASYMMETRIC ACCESS State bits
  106. */
  107. buf[off] = 0x80; /* T_SUP */
  108. buf[off] |= 0x40; /* O_SUP */
  109. buf[off] |= 0x8; /* U_SUP */
  110. buf[off] |= 0x4; /* S_SUP */
  111. buf[off] |= 0x2; /* AN_SUP */
  112. buf[off++] |= 0x1; /* AO_SUP */
  113. /*
  114. * TARGET PORT GROUP
  115. */
  116. buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  117. buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  118. off++; /* Skip over Reserved */
  119. /*
  120. * STATUS CODE
  121. */
  122. buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  123. /*
  124. * Vendor Specific field
  125. */
  126. buf[off++] = 0x00;
  127. /*
  128. * TARGET PORT COUNT
  129. */
  130. buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  131. rd_len += 8;
  132. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  133. list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  134. tg_pt_gp_mem_list) {
  135. port = tg_pt_gp_mem->tg_pt;
  136. /*
  137. * Start Target Port descriptor format
  138. *
  139. * See spc4r17 section 6.2.7 Table 247
  140. */
  141. off += 2; /* Skip over Obsolete */
  142. /*
  143. * Set RELATIVE TARGET PORT IDENTIFIER
  144. */
  145. buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  146. buf[off++] = (port->sep_rtpi & 0xff);
  147. rd_len += 4;
  148. }
  149. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  150. }
  151. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  152. /*
  153. * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  154. */
  155. put_unaligned_be32(rd_len, &buf[0]);
  156. /*
  157. * Fill in the Extended header parameter data format if requested
  158. */
  159. if (ext_hdr != 0) {
  160. buf[4] = 0x10;
  161. /*
  162. * Set the implict transition time (in seconds) for the application
  163. * client to use as a base for it's transition timeout value.
  164. *
  165. * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
  166. * this CDB was received upon to determine this value individually
  167. * for ALUA target port group.
  168. */
  169. port = cmd->se_lun->lun_sep;
  170. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  171. if (tg_pt_gp_mem) {
  172. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  173. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  174. if (tg_pt_gp)
  175. buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
  176. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  177. }
  178. }
  179. transport_kunmap_data_sg(cmd);
  180. target_complete_cmd(cmd, GOOD);
  181. return 0;
  182. }
  183. /*
  184. * SET_TARGET_PORT_GROUPS for explict ALUA operation.
  185. *
  186. * See spc4r17 section 6.35
  187. */
  188. int target_emulate_set_target_port_groups(struct se_cmd *cmd)
  189. {
  190. struct se_device *dev = cmd->se_dev;
  191. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  192. struct se_port *port, *l_port = cmd->se_lun->lun_sep;
  193. struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
  194. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  195. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
  196. unsigned char *buf;
  197. unsigned char *ptr;
  198. u32 len = 4; /* Skip over RESERVED area in header */
  199. int alua_access_state, primary = 0, rc;
  200. u16 tg_pt_id, rtpi;
  201. if (!l_port) {
  202. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  203. return -EINVAL;
  204. }
  205. buf = transport_kmap_data_sg(cmd);
  206. /*
  207. * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  208. * for the local tg_pt_gp.
  209. */
  210. l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
  211. if (!l_tg_pt_gp_mem) {
  212. pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
  213. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  214. rc = -EINVAL;
  215. goto out;
  216. }
  217. spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  218. l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
  219. if (!l_tg_pt_gp) {
  220. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  221. pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
  222. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  223. rc = -EINVAL;
  224. goto out;
  225. }
  226. rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  227. spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  228. if (!rc) {
  229. pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
  230. " while TPGS_EXPLICT_ALUA is disabled\n");
  231. cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  232. rc = -EINVAL;
  233. goto out;
  234. }
  235. ptr = &buf[4]; /* Skip over RESERVED area in header */
  236. while (len < cmd->data_length) {
  237. alua_access_state = (ptr[0] & 0x0f);
  238. /*
  239. * Check the received ALUA access state, and determine if
  240. * the state is a primary or secondary target port asymmetric
  241. * access state.
  242. */
  243. rc = core_alua_check_transition(alua_access_state, &primary);
  244. if (rc != 0) {
  245. /*
  246. * If the SET TARGET PORT GROUPS attempts to establish
  247. * an invalid combination of target port asymmetric
  248. * access states or attempts to establish an
  249. * unsupported target port asymmetric access state,
  250. * then the command shall be terminated with CHECK
  251. * CONDITION status, with the sense key set to ILLEGAL
  252. * REQUEST, and the additional sense code set to INVALID
  253. * FIELD IN PARAMETER LIST.
  254. */
  255. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  256. rc = -EINVAL;
  257. goto out;
  258. }
  259. rc = -1;
  260. /*
  261. * If the ASYMMETRIC ACCESS STATE field (see table 267)
  262. * specifies a primary target port asymmetric access state,
  263. * then the TARGET PORT GROUP OR TARGET PORT field specifies
  264. * a primary target port group for which the primary target
  265. * port asymmetric access state shall be changed. If the
  266. * ASYMMETRIC ACCESS STATE field specifies a secondary target
  267. * port asymmetric access state, then the TARGET PORT GROUP OR
  268. * TARGET PORT field specifies the relative target port
  269. * identifier (see 3.1.120) of the target port for which the
  270. * secondary target port asymmetric access state shall be
  271. * changed.
  272. */
  273. if (primary) {
  274. tg_pt_id = get_unaligned_be16(ptr + 2);
  275. /*
  276. * Locate the matching target port group ID from
  277. * the global tg_pt_gp list
  278. */
  279. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  280. list_for_each_entry(tg_pt_gp,
  281. &su_dev->t10_alua.tg_pt_gps_list,
  282. tg_pt_gp_list) {
  283. if (!tg_pt_gp->tg_pt_gp_valid_id)
  284. continue;
  285. if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  286. continue;
  287. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  288. smp_mb__after_atomic_inc();
  289. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  290. rc = core_alua_do_port_transition(tg_pt_gp,
  291. dev, l_port, nacl,
  292. alua_access_state, 1);
  293. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  294. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  295. smp_mb__after_atomic_dec();
  296. break;
  297. }
  298. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  299. /*
  300. * If not matching target port group ID can be located
  301. * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  302. */
  303. if (rc != 0) {
  304. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  305. rc = -EINVAL;
  306. goto out;
  307. }
  308. } else {
  309. /*
  310. * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  311. * the Target Port in question for the the incoming
  312. * SET_TARGET_PORT_GROUPS op.
  313. */
  314. rtpi = get_unaligned_be16(ptr + 2);
  315. /*
  316. * Locate the matching relative target port identifer
  317. * for the struct se_device storage object.
  318. */
  319. spin_lock(&dev->se_port_lock);
  320. list_for_each_entry(port, &dev->dev_sep_list,
  321. sep_list) {
  322. if (port->sep_rtpi != rtpi)
  323. continue;
  324. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  325. spin_unlock(&dev->se_port_lock);
  326. rc = core_alua_set_tg_pt_secondary_state(
  327. tg_pt_gp_mem, port, 1, 1);
  328. spin_lock(&dev->se_port_lock);
  329. break;
  330. }
  331. spin_unlock(&dev->se_port_lock);
  332. /*
  333. * If not matching relative target port identifier can
  334. * be located, throw an exception with ASCQ:
  335. * INVALID_PARAMETER_LIST
  336. */
  337. if (rc != 0) {
  338. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  339. rc = -EINVAL;
  340. goto out;
  341. }
  342. }
  343. ptr += 4;
  344. len += 4;
  345. }
  346. out:
  347. transport_kunmap_data_sg(cmd);
  348. target_complete_cmd(cmd, GOOD);
  349. return 0;
  350. }
  351. static inline int core_alua_state_nonoptimized(
  352. struct se_cmd *cmd,
  353. unsigned char *cdb,
  354. int nonop_delay_msecs,
  355. u8 *alua_ascq)
  356. {
  357. /*
  358. * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  359. * later to determine if processing of this cmd needs to be
  360. * temporarily delayed for the Active/NonOptimized primary access state.
  361. */
  362. cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  363. cmd->alua_nonop_delay = nonop_delay_msecs;
  364. return 0;
  365. }
  366. static inline int core_alua_state_standby(
  367. struct se_cmd *cmd,
  368. unsigned char *cdb,
  369. u8 *alua_ascq)
  370. {
  371. /*
  372. * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  373. * spc4r17 section 5.9.2.4.4
  374. */
  375. switch (cdb[0]) {
  376. case INQUIRY:
  377. case LOG_SELECT:
  378. case LOG_SENSE:
  379. case MODE_SELECT:
  380. case MODE_SENSE:
  381. case REPORT_LUNS:
  382. case RECEIVE_DIAGNOSTIC:
  383. case SEND_DIAGNOSTIC:
  384. case MAINTENANCE_IN:
  385. switch (cdb[1] & 0x1f) {
  386. case MI_REPORT_TARGET_PGS:
  387. return 0;
  388. default:
  389. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  390. return 1;
  391. }
  392. case MAINTENANCE_OUT:
  393. switch (cdb[1]) {
  394. case MO_SET_TARGET_PGS:
  395. return 0;
  396. default:
  397. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  398. return 1;
  399. }
  400. case REQUEST_SENSE:
  401. case PERSISTENT_RESERVE_IN:
  402. case PERSISTENT_RESERVE_OUT:
  403. case READ_BUFFER:
  404. case WRITE_BUFFER:
  405. return 0;
  406. default:
  407. *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. static inline int core_alua_state_unavailable(
  413. struct se_cmd *cmd,
  414. unsigned char *cdb,
  415. u8 *alua_ascq)
  416. {
  417. /*
  418. * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  419. * spc4r17 section 5.9.2.4.5
  420. */
  421. switch (cdb[0]) {
  422. case INQUIRY:
  423. case REPORT_LUNS:
  424. case MAINTENANCE_IN:
  425. switch (cdb[1] & 0x1f) {
  426. case MI_REPORT_TARGET_PGS:
  427. return 0;
  428. default:
  429. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  430. return 1;
  431. }
  432. case MAINTENANCE_OUT:
  433. switch (cdb[1]) {
  434. case MO_SET_TARGET_PGS:
  435. return 0;
  436. default:
  437. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  438. return 1;
  439. }
  440. case REQUEST_SENSE:
  441. case READ_BUFFER:
  442. case WRITE_BUFFER:
  443. return 0;
  444. default:
  445. *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  446. return 1;
  447. }
  448. return 0;
  449. }
  450. static inline int core_alua_state_transition(
  451. struct se_cmd *cmd,
  452. unsigned char *cdb,
  453. u8 *alua_ascq)
  454. {
  455. /*
  456. * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  457. * spc4r17 section 5.9.2.5
  458. */
  459. switch (cdb[0]) {
  460. case INQUIRY:
  461. case REPORT_LUNS:
  462. case MAINTENANCE_IN:
  463. switch (cdb[1] & 0x1f) {
  464. case MI_REPORT_TARGET_PGS:
  465. return 0;
  466. default:
  467. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  468. return 1;
  469. }
  470. case REQUEST_SENSE:
  471. case READ_BUFFER:
  472. case WRITE_BUFFER:
  473. return 0;
  474. default:
  475. *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. /*
  481. * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
  482. * in transport_cmd_sequencer(). This function is assigned to
  483. * struct t10_alua *->state_check() in core_setup_alua()
  484. */
  485. static int core_alua_state_check_nop(
  486. struct se_cmd *cmd,
  487. unsigned char *cdb,
  488. u8 *alua_ascq)
  489. {
  490. return 0;
  491. }
  492. /*
  493. * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
  494. * This function is assigned to struct t10_alua *->state_check() in
  495. * core_setup_alua()
  496. *
  497. * Also, this function can return three different return codes to
  498. * signal transport_generic_cmd_sequencer()
  499. *
  500. * return 1: Is used to signal LUN not accecsable, and check condition/not ready
  501. * return 0: Used to signal success
  502. * reutrn -1: Used to signal failure, and invalid cdb field
  503. */
  504. static int core_alua_state_check(
  505. struct se_cmd *cmd,
  506. unsigned char *cdb,
  507. u8 *alua_ascq)
  508. {
  509. struct se_lun *lun = cmd->se_lun;
  510. struct se_port *port = lun->lun_sep;
  511. struct t10_alua_tg_pt_gp *tg_pt_gp;
  512. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  513. int out_alua_state, nonop_delay_msecs;
  514. if (!port)
  515. return 0;
  516. /*
  517. * First, check for a struct se_port specific secondary ALUA target port
  518. * access state: OFFLINE
  519. */
  520. if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  521. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  522. pr_debug("ALUA: Got secondary offline status for local"
  523. " target port\n");
  524. *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  525. return 1;
  526. }
  527. /*
  528. * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  529. * ALUA target port group, to obtain current ALUA access state.
  530. * Otherwise look for the underlying struct se_device association with
  531. * a ALUA logical unit group.
  532. */
  533. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  534. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  535. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  536. out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  537. nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  538. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  539. /*
  540. * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
  541. * statement so the compiler knows explicitly to check this case first.
  542. * For the Optimized ALUA access state case, we want to process the
  543. * incoming fabric cmd ASAP..
  544. */
  545. if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  546. return 0;
  547. switch (out_alua_state) {
  548. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  549. return core_alua_state_nonoptimized(cmd, cdb,
  550. nonop_delay_msecs, alua_ascq);
  551. case ALUA_ACCESS_STATE_STANDBY:
  552. return core_alua_state_standby(cmd, cdb, alua_ascq);
  553. case ALUA_ACCESS_STATE_UNAVAILABLE:
  554. return core_alua_state_unavailable(cmd, cdb, alua_ascq);
  555. case ALUA_ACCESS_STATE_TRANSITION:
  556. return core_alua_state_transition(cmd, cdb, alua_ascq);
  557. /*
  558. * OFFLINE is a secondary ALUA target port group access state, that is
  559. * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  560. */
  561. case ALUA_ACCESS_STATE_OFFLINE:
  562. default:
  563. pr_err("Unknown ALUA access state: 0x%02x\n",
  564. out_alua_state);
  565. return -EINVAL;
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Check implict and explict ALUA state change request.
  571. */
  572. static int core_alua_check_transition(int state, int *primary)
  573. {
  574. switch (state) {
  575. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  576. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  577. case ALUA_ACCESS_STATE_STANDBY:
  578. case ALUA_ACCESS_STATE_UNAVAILABLE:
  579. /*
  580. * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  581. * defined as primary target port asymmetric access states.
  582. */
  583. *primary = 1;
  584. break;
  585. case ALUA_ACCESS_STATE_OFFLINE:
  586. /*
  587. * OFFLINE state is defined as a secondary target port
  588. * asymmetric access state.
  589. */
  590. *primary = 0;
  591. break;
  592. default:
  593. pr_err("Unknown ALUA access state: 0x%02x\n", state);
  594. return -EINVAL;
  595. }
  596. return 0;
  597. }
  598. static char *core_alua_dump_state(int state)
  599. {
  600. switch (state) {
  601. case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  602. return "Active/Optimized";
  603. case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  604. return "Active/NonOptimized";
  605. case ALUA_ACCESS_STATE_STANDBY:
  606. return "Standby";
  607. case ALUA_ACCESS_STATE_UNAVAILABLE:
  608. return "Unavailable";
  609. case ALUA_ACCESS_STATE_OFFLINE:
  610. return "Offline";
  611. default:
  612. return "Unknown";
  613. }
  614. return NULL;
  615. }
  616. char *core_alua_dump_status(int status)
  617. {
  618. switch (status) {
  619. case ALUA_STATUS_NONE:
  620. return "None";
  621. case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  622. return "Altered by Explict STPG";
  623. case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  624. return "Altered by Implict ALUA";
  625. default:
  626. return "Unknown";
  627. }
  628. return NULL;
  629. }
  630. /*
  631. * Used by fabric modules to determine when we need to delay processing
  632. * for the Active/NonOptimized paths..
  633. */
  634. int core_alua_check_nonop_delay(
  635. struct se_cmd *cmd)
  636. {
  637. if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  638. return 0;
  639. if (in_interrupt())
  640. return 0;
  641. /*
  642. * The ALUA Active/NonOptimized access state delay can be disabled
  643. * in via configfs with a value of zero
  644. */
  645. if (!cmd->alua_nonop_delay)
  646. return 0;
  647. /*
  648. * struct se_cmd->alua_nonop_delay gets set by a target port group
  649. * defined interval in core_alua_state_nonoptimized()
  650. */
  651. msleep_interruptible(cmd->alua_nonop_delay);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(core_alua_check_nonop_delay);
  655. /*
  656. * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
  657. *
  658. */
  659. static int core_alua_write_tpg_metadata(
  660. const char *path,
  661. unsigned char *md_buf,
  662. u32 md_buf_len)
  663. {
  664. mm_segment_t old_fs;
  665. struct file *file;
  666. struct iovec iov[1];
  667. int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  668. memset(iov, 0, sizeof(struct iovec));
  669. file = filp_open(path, flags, 0600);
  670. if (IS_ERR(file) || !file || !file->f_dentry) {
  671. pr_err("filp_open(%s) for ALUA metadata failed\n",
  672. path);
  673. return -ENODEV;
  674. }
  675. iov[0].iov_base = &md_buf[0];
  676. iov[0].iov_len = md_buf_len;
  677. old_fs = get_fs();
  678. set_fs(get_ds());
  679. ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  680. set_fs(old_fs);
  681. if (ret < 0) {
  682. pr_err("Error writing ALUA metadata file: %s\n", path);
  683. filp_close(file, NULL);
  684. return -EIO;
  685. }
  686. filp_close(file, NULL);
  687. return 0;
  688. }
  689. /*
  690. * Called with tg_pt_gp->tg_pt_gp_md_mutex held
  691. */
  692. static int core_alua_update_tpg_primary_metadata(
  693. struct t10_alua_tg_pt_gp *tg_pt_gp,
  694. int primary_state,
  695. unsigned char *md_buf)
  696. {
  697. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  698. struct t10_wwn *wwn = &su_dev->t10_wwn;
  699. char path[ALUA_METADATA_PATH_LEN];
  700. int len;
  701. memset(path, 0, ALUA_METADATA_PATH_LEN);
  702. len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  703. "tg_pt_gp_id=%hu\n"
  704. "alua_access_state=0x%02x\n"
  705. "alua_access_status=0x%02x\n",
  706. tg_pt_gp->tg_pt_gp_id, primary_state,
  707. tg_pt_gp->tg_pt_gp_alua_access_status);
  708. snprintf(path, ALUA_METADATA_PATH_LEN,
  709. "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  710. config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  711. return core_alua_write_tpg_metadata(path, md_buf, len);
  712. }
  713. static int core_alua_do_transition_tg_pt(
  714. struct t10_alua_tg_pt_gp *tg_pt_gp,
  715. struct se_port *l_port,
  716. struct se_node_acl *nacl,
  717. unsigned char *md_buf,
  718. int new_state,
  719. int explict)
  720. {
  721. struct se_dev_entry *se_deve;
  722. struct se_lun_acl *lacl;
  723. struct se_port *port;
  724. struct t10_alua_tg_pt_gp_member *mem;
  725. int old_state = 0;
  726. /*
  727. * Save the old primary ALUA access state, and set the current state
  728. * to ALUA_ACCESS_STATE_TRANSITION.
  729. */
  730. old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  731. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  732. ALUA_ACCESS_STATE_TRANSITION);
  733. tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  734. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  735. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  736. /*
  737. * Check for the optional ALUA primary state transition delay
  738. */
  739. if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  740. msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  741. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  742. list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  743. tg_pt_gp_mem_list) {
  744. port = mem->tg_pt;
  745. /*
  746. * After an implicit target port asymmetric access state
  747. * change, a device server shall establish a unit attention
  748. * condition for the initiator port associated with every I_T
  749. * nexus with the additional sense code set to ASYMMETRIC
  750. * ACCESS STATE CHAGED.
  751. *
  752. * After an explicit target port asymmetric access state
  753. * change, a device server shall establish a unit attention
  754. * condition with the additional sense code set to ASYMMETRIC
  755. * ACCESS STATE CHANGED for the initiator port associated with
  756. * every I_T nexus other than the I_T nexus on which the SET
  757. * TARGET PORT GROUPS command
  758. */
  759. atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  760. smp_mb__after_atomic_inc();
  761. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  762. spin_lock_bh(&port->sep_alua_lock);
  763. list_for_each_entry(se_deve, &port->sep_alua_list,
  764. alua_port_list) {
  765. lacl = se_deve->se_lun_acl;
  766. /*
  767. * se_deve->se_lun_acl pointer may be NULL for a
  768. * entry created without explict Node+MappedLUN ACLs
  769. */
  770. if (!lacl)
  771. continue;
  772. if (explict &&
  773. (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  774. (l_port != NULL) && (l_port == port))
  775. continue;
  776. core_scsi3_ua_allocate(lacl->se_lun_nacl,
  777. se_deve->mapped_lun, 0x2A,
  778. ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  779. }
  780. spin_unlock_bh(&port->sep_alua_lock);
  781. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  782. atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  783. smp_mb__after_atomic_dec();
  784. }
  785. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  786. /*
  787. * Update the ALUA metadata buf that has been allocated in
  788. * core_alua_do_port_transition(), this metadata will be written
  789. * to struct file.
  790. *
  791. * Note that there is the case where we do not want to update the
  792. * metadata when the saved metadata is being parsed in userspace
  793. * when setting the existing port access state and access status.
  794. *
  795. * Also note that the failure to write out the ALUA metadata to
  796. * struct file does NOT affect the actual ALUA transition.
  797. */
  798. if (tg_pt_gp->tg_pt_gp_write_metadata) {
  799. mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  800. core_alua_update_tpg_primary_metadata(tg_pt_gp,
  801. new_state, md_buf);
  802. mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  803. }
  804. /*
  805. * Set the current primary ALUA access state to the requested new state
  806. */
  807. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
  808. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  809. " from primary access state %s to %s\n", (explict) ? "explict" :
  810. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  811. tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  812. core_alua_dump_state(new_state));
  813. return 0;
  814. }
  815. int core_alua_do_port_transition(
  816. struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  817. struct se_device *l_dev,
  818. struct se_port *l_port,
  819. struct se_node_acl *l_nacl,
  820. int new_state,
  821. int explict)
  822. {
  823. struct se_device *dev;
  824. struct se_port *port;
  825. struct se_subsystem_dev *su_dev;
  826. struct se_node_acl *nacl;
  827. struct t10_alua_lu_gp *lu_gp;
  828. struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  829. struct t10_alua_tg_pt_gp *tg_pt_gp;
  830. unsigned char *md_buf;
  831. int primary;
  832. if (core_alua_check_transition(new_state, &primary) != 0)
  833. return -EINVAL;
  834. md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
  835. if (!md_buf) {
  836. pr_err("Unable to allocate buf for ALUA metadata\n");
  837. return -ENOMEM;
  838. }
  839. local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  840. spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  841. lu_gp = local_lu_gp_mem->lu_gp;
  842. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  843. smp_mb__after_atomic_inc();
  844. spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  845. /*
  846. * For storage objects that are members of the 'default_lu_gp',
  847. * we only do transition on the passed *l_tp_pt_gp, and not
  848. * on all of the matching target port groups IDs in default_lu_gp.
  849. */
  850. if (!lu_gp->lu_gp_id) {
  851. /*
  852. * core_alua_do_transition_tg_pt() will always return
  853. * success.
  854. */
  855. core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  856. md_buf, new_state, explict);
  857. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  858. smp_mb__after_atomic_dec();
  859. kfree(md_buf);
  860. return 0;
  861. }
  862. /*
  863. * For all other LU groups aside from 'default_lu_gp', walk all of
  864. * the associated storage objects looking for a matching target port
  865. * group ID from the local target port group.
  866. */
  867. spin_lock(&lu_gp->lu_gp_lock);
  868. list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  869. lu_gp_mem_list) {
  870. dev = lu_gp_mem->lu_gp_mem_dev;
  871. su_dev = dev->se_sub_dev;
  872. atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  873. smp_mb__after_atomic_inc();
  874. spin_unlock(&lu_gp->lu_gp_lock);
  875. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  876. list_for_each_entry(tg_pt_gp,
  877. &su_dev->t10_alua.tg_pt_gps_list,
  878. tg_pt_gp_list) {
  879. if (!tg_pt_gp->tg_pt_gp_valid_id)
  880. continue;
  881. /*
  882. * If the target behavior port asymmetric access state
  883. * is changed for any target port group accessiable via
  884. * a logical unit within a LU group, the target port
  885. * behavior group asymmetric access states for the same
  886. * target port group accessible via other logical units
  887. * in that LU group will also change.
  888. */
  889. if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  890. continue;
  891. if (l_tg_pt_gp == tg_pt_gp) {
  892. port = l_port;
  893. nacl = l_nacl;
  894. } else {
  895. port = NULL;
  896. nacl = NULL;
  897. }
  898. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  899. smp_mb__after_atomic_inc();
  900. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  901. /*
  902. * core_alua_do_transition_tg_pt() will always return
  903. * success.
  904. */
  905. core_alua_do_transition_tg_pt(tg_pt_gp, port,
  906. nacl, md_buf, new_state, explict);
  907. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  908. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  909. smp_mb__after_atomic_dec();
  910. }
  911. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  912. spin_lock(&lu_gp->lu_gp_lock);
  913. atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  914. smp_mb__after_atomic_dec();
  915. }
  916. spin_unlock(&lu_gp->lu_gp_lock);
  917. pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
  918. " Group IDs: %hu %s transition to primary state: %s\n",
  919. config_item_name(&lu_gp->lu_gp_group.cg_item),
  920. l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  921. core_alua_dump_state(new_state));
  922. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  923. smp_mb__after_atomic_dec();
  924. kfree(md_buf);
  925. return 0;
  926. }
  927. /*
  928. * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
  929. */
  930. static int core_alua_update_tpg_secondary_metadata(
  931. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  932. struct se_port *port,
  933. unsigned char *md_buf,
  934. u32 md_buf_len)
  935. {
  936. struct se_portal_group *se_tpg = port->sep_tpg;
  937. char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  938. int len;
  939. memset(path, 0, ALUA_METADATA_PATH_LEN);
  940. memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  941. len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
  942. se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
  943. if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
  944. snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
  945. se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
  946. len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
  947. "alua_tg_pt_status=0x%02x\n",
  948. atomic_read(&port->sep_tg_pt_secondary_offline),
  949. port->sep_tg_pt_secondary_stat);
  950. snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
  951. se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
  952. port->sep_lun->unpacked_lun);
  953. return core_alua_write_tpg_metadata(path, md_buf, len);
  954. }
  955. static int core_alua_set_tg_pt_secondary_state(
  956. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  957. struct se_port *port,
  958. int explict,
  959. int offline)
  960. {
  961. struct t10_alua_tg_pt_gp *tg_pt_gp;
  962. unsigned char *md_buf;
  963. u32 md_buf_len;
  964. int trans_delay_msecs;
  965. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  966. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  967. if (!tg_pt_gp) {
  968. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  969. pr_err("Unable to complete secondary state"
  970. " transition\n");
  971. return -EINVAL;
  972. }
  973. trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  974. /*
  975. * Set the secondary ALUA target port access state to OFFLINE
  976. * or release the previously secondary state for struct se_port
  977. */
  978. if (offline)
  979. atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  980. else
  981. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  982. md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  983. port->sep_tg_pt_secondary_stat = (explict) ?
  984. ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  985. ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  986. pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
  987. " to secondary access state: %s\n", (explict) ? "explict" :
  988. "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  989. tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  990. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  991. /*
  992. * Do the optional transition delay after we set the secondary
  993. * ALUA access state.
  994. */
  995. if (trans_delay_msecs != 0)
  996. msleep_interruptible(trans_delay_msecs);
  997. /*
  998. * See if we need to update the ALUA fabric port metadata for
  999. * secondary state and status
  1000. */
  1001. if (port->sep_tg_pt_secondary_write_md) {
  1002. md_buf = kzalloc(md_buf_len, GFP_KERNEL);
  1003. if (!md_buf) {
  1004. pr_err("Unable to allocate md_buf for"
  1005. " secondary ALUA access metadata\n");
  1006. return -ENOMEM;
  1007. }
  1008. mutex_lock(&port->sep_tg_pt_md_mutex);
  1009. core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  1010. md_buf, md_buf_len);
  1011. mutex_unlock(&port->sep_tg_pt_md_mutex);
  1012. kfree(md_buf);
  1013. }
  1014. return 0;
  1015. }
  1016. struct t10_alua_lu_gp *
  1017. core_alua_allocate_lu_gp(const char *name, int def_group)
  1018. {
  1019. struct t10_alua_lu_gp *lu_gp;
  1020. lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
  1021. if (!lu_gp) {
  1022. pr_err("Unable to allocate struct t10_alua_lu_gp\n");
  1023. return ERR_PTR(-ENOMEM);
  1024. }
  1025. INIT_LIST_HEAD(&lu_gp->lu_gp_node);
  1026. INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  1027. spin_lock_init(&lu_gp->lu_gp_lock);
  1028. atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  1029. if (def_group) {
  1030. lu_gp->lu_gp_id = alua_lu_gps_counter++;
  1031. lu_gp->lu_gp_valid_id = 1;
  1032. alua_lu_gps_count++;
  1033. }
  1034. return lu_gp;
  1035. }
  1036. int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  1037. {
  1038. struct t10_alua_lu_gp *lu_gp_tmp;
  1039. u16 lu_gp_id_tmp;
  1040. /*
  1041. * The lu_gp->lu_gp_id may only be set once..
  1042. */
  1043. if (lu_gp->lu_gp_valid_id) {
  1044. pr_warn("ALUA LU Group already has a valid ID,"
  1045. " ignoring request\n");
  1046. return -EINVAL;
  1047. }
  1048. spin_lock(&lu_gps_lock);
  1049. if (alua_lu_gps_count == 0x0000ffff) {
  1050. pr_err("Maximum ALUA alua_lu_gps_count:"
  1051. " 0x0000ffff reached\n");
  1052. spin_unlock(&lu_gps_lock);
  1053. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1054. return -ENOSPC;
  1055. }
  1056. again:
  1057. lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
  1058. alua_lu_gps_counter++;
  1059. list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
  1060. if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
  1061. if (!lu_gp_id)
  1062. goto again;
  1063. pr_warn("ALUA Logical Unit Group ID: %hu"
  1064. " already exists, ignoring request\n",
  1065. lu_gp_id);
  1066. spin_unlock(&lu_gps_lock);
  1067. return -EINVAL;
  1068. }
  1069. }
  1070. lu_gp->lu_gp_id = lu_gp_id_tmp;
  1071. lu_gp->lu_gp_valid_id = 1;
  1072. list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  1073. alua_lu_gps_count++;
  1074. spin_unlock(&lu_gps_lock);
  1075. return 0;
  1076. }
  1077. static struct t10_alua_lu_gp_member *
  1078. core_alua_allocate_lu_gp_mem(struct se_device *dev)
  1079. {
  1080. struct t10_alua_lu_gp_member *lu_gp_mem;
  1081. lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
  1082. if (!lu_gp_mem) {
  1083. pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
  1084. return ERR_PTR(-ENOMEM);
  1085. }
  1086. INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  1087. spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  1088. atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  1089. lu_gp_mem->lu_gp_mem_dev = dev;
  1090. dev->dev_alua_lu_gp_mem = lu_gp_mem;
  1091. return lu_gp_mem;
  1092. }
  1093. void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  1094. {
  1095. struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  1096. /*
  1097. * Once we have reached this point, config_item_put() has
  1098. * already been called from target_core_alua_drop_lu_gp().
  1099. *
  1100. * Here, we remove the *lu_gp from the global list so that
  1101. * no associations can be made while we are releasing
  1102. * struct t10_alua_lu_gp.
  1103. */
  1104. spin_lock(&lu_gps_lock);
  1105. list_del(&lu_gp->lu_gp_node);
  1106. alua_lu_gps_count--;
  1107. spin_unlock(&lu_gps_lock);
  1108. /*
  1109. * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  1110. * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  1111. * released with core_alua_put_lu_gp_from_name()
  1112. */
  1113. while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  1114. cpu_relax();
  1115. /*
  1116. * Release reference to struct t10_alua_lu_gp * from all associated
  1117. * struct se_device.
  1118. */
  1119. spin_lock(&lu_gp->lu_gp_lock);
  1120. list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  1121. &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  1122. if (lu_gp_mem->lu_gp_assoc) {
  1123. list_del(&lu_gp_mem->lu_gp_mem_list);
  1124. lu_gp->lu_gp_members--;
  1125. lu_gp_mem->lu_gp_assoc = 0;
  1126. }
  1127. spin_unlock(&lu_gp->lu_gp_lock);
  1128. /*
  1129. *
  1130. * lu_gp_mem is associated with a single
  1131. * struct se_device->dev_alua_lu_gp_mem, and is released when
  1132. * struct se_device is released via core_alua_free_lu_gp_mem().
  1133. *
  1134. * If the passed lu_gp does NOT match the default_lu_gp, assume
  1135. * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  1136. */
  1137. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1138. if (lu_gp != default_lu_gp)
  1139. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1140. default_lu_gp);
  1141. else
  1142. lu_gp_mem->lu_gp = NULL;
  1143. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1144. spin_lock(&lu_gp->lu_gp_lock);
  1145. }
  1146. spin_unlock(&lu_gp->lu_gp_lock);
  1147. kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  1148. }
  1149. void core_alua_free_lu_gp_mem(struct se_device *dev)
  1150. {
  1151. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1152. struct t10_alua *alua = &su_dev->t10_alua;
  1153. struct t10_alua_lu_gp *lu_gp;
  1154. struct t10_alua_lu_gp_member *lu_gp_mem;
  1155. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1156. return;
  1157. lu_gp_mem = dev->dev_alua_lu_gp_mem;
  1158. if (!lu_gp_mem)
  1159. return;
  1160. while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  1161. cpu_relax();
  1162. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1163. lu_gp = lu_gp_mem->lu_gp;
  1164. if (lu_gp) {
  1165. spin_lock(&lu_gp->lu_gp_lock);
  1166. if (lu_gp_mem->lu_gp_assoc) {
  1167. list_del(&lu_gp_mem->lu_gp_mem_list);
  1168. lu_gp->lu_gp_members--;
  1169. lu_gp_mem->lu_gp_assoc = 0;
  1170. }
  1171. spin_unlock(&lu_gp->lu_gp_lock);
  1172. lu_gp_mem->lu_gp = NULL;
  1173. }
  1174. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1175. kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  1176. }
  1177. struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  1178. {
  1179. struct t10_alua_lu_gp *lu_gp;
  1180. struct config_item *ci;
  1181. spin_lock(&lu_gps_lock);
  1182. list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
  1183. if (!lu_gp->lu_gp_valid_id)
  1184. continue;
  1185. ci = &lu_gp->lu_gp_group.cg_item;
  1186. if (!strcmp(config_item_name(ci), name)) {
  1187. atomic_inc(&lu_gp->lu_gp_ref_cnt);
  1188. spin_unlock(&lu_gps_lock);
  1189. return lu_gp;
  1190. }
  1191. }
  1192. spin_unlock(&lu_gps_lock);
  1193. return NULL;
  1194. }
  1195. void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  1196. {
  1197. spin_lock(&lu_gps_lock);
  1198. atomic_dec(&lu_gp->lu_gp_ref_cnt);
  1199. spin_unlock(&lu_gps_lock);
  1200. }
  1201. /*
  1202. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1203. */
  1204. void __core_alua_attach_lu_gp_mem(
  1205. struct t10_alua_lu_gp_member *lu_gp_mem,
  1206. struct t10_alua_lu_gp *lu_gp)
  1207. {
  1208. spin_lock(&lu_gp->lu_gp_lock);
  1209. lu_gp_mem->lu_gp = lu_gp;
  1210. lu_gp_mem->lu_gp_assoc = 1;
  1211. list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  1212. lu_gp->lu_gp_members++;
  1213. spin_unlock(&lu_gp->lu_gp_lock);
  1214. }
  1215. /*
  1216. * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
  1217. */
  1218. void __core_alua_drop_lu_gp_mem(
  1219. struct t10_alua_lu_gp_member *lu_gp_mem,
  1220. struct t10_alua_lu_gp *lu_gp)
  1221. {
  1222. spin_lock(&lu_gp->lu_gp_lock);
  1223. list_del(&lu_gp_mem->lu_gp_mem_list);
  1224. lu_gp_mem->lu_gp = NULL;
  1225. lu_gp_mem->lu_gp_assoc = 0;
  1226. lu_gp->lu_gp_members--;
  1227. spin_unlock(&lu_gp->lu_gp_lock);
  1228. }
  1229. struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
  1230. struct se_subsystem_dev *su_dev,
  1231. const char *name,
  1232. int def_group)
  1233. {
  1234. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1235. tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
  1236. if (!tg_pt_gp) {
  1237. pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
  1238. return NULL;
  1239. }
  1240. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  1241. INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  1242. mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  1243. spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  1244. atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  1245. tg_pt_gp->tg_pt_gp_su_dev = su_dev;
  1246. tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  1247. atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  1248. ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  1249. /*
  1250. * Enable both explict and implict ALUA support by default
  1251. */
  1252. tg_pt_gp->tg_pt_gp_alua_access_type =
  1253. TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  1254. /*
  1255. * Set the default Active/NonOptimized Delay in milliseconds
  1256. */
  1257. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  1258. tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  1259. tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
  1260. if (def_group) {
  1261. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1262. tg_pt_gp->tg_pt_gp_id =
  1263. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1264. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1265. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1266. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1267. &su_dev->t10_alua.tg_pt_gps_list);
  1268. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1269. }
  1270. return tg_pt_gp;
  1271. }
  1272. int core_alua_set_tg_pt_gp_id(
  1273. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1274. u16 tg_pt_gp_id)
  1275. {
  1276. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1277. struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  1278. u16 tg_pt_gp_id_tmp;
  1279. /*
  1280. * The tg_pt_gp->tg_pt_gp_id may only be set once..
  1281. */
  1282. if (tg_pt_gp->tg_pt_gp_valid_id) {
  1283. pr_warn("ALUA TG PT Group already has a valid ID,"
  1284. " ignoring request\n");
  1285. return -EINVAL;
  1286. }
  1287. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1288. if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
  1289. pr_err("Maximum ALUA alua_tg_pt_gps_count:"
  1290. " 0x0000ffff reached\n");
  1291. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1292. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1293. return -ENOSPC;
  1294. }
  1295. again:
  1296. tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
  1297. su_dev->t10_alua.alua_tg_pt_gps_counter++;
  1298. list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
  1299. tg_pt_gp_list) {
  1300. if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
  1301. if (!tg_pt_gp_id)
  1302. goto again;
  1303. pr_err("ALUA Target Port Group ID: %hu already"
  1304. " exists, ignoring request\n", tg_pt_gp_id);
  1305. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1306. return -EINVAL;
  1307. }
  1308. }
  1309. tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  1310. tg_pt_gp->tg_pt_gp_valid_id = 1;
  1311. list_add_tail(&tg_pt_gp->tg_pt_gp_list,
  1312. &su_dev->t10_alua.tg_pt_gps_list);
  1313. su_dev->t10_alua.alua_tg_pt_gps_count++;
  1314. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1315. return 0;
  1316. }
  1317. struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  1318. struct se_port *port)
  1319. {
  1320. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1321. tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  1322. GFP_KERNEL);
  1323. if (!tg_pt_gp_mem) {
  1324. pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
  1325. return ERR_PTR(-ENOMEM);
  1326. }
  1327. INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1328. spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1329. atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  1330. tg_pt_gp_mem->tg_pt = port;
  1331. port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
  1332. return tg_pt_gp_mem;
  1333. }
  1334. void core_alua_free_tg_pt_gp(
  1335. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1336. {
  1337. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1338. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  1339. /*
  1340. * Once we have reached this point, config_item_put() has already
  1341. * been called from target_core_alua_drop_tg_pt_gp().
  1342. *
  1343. * Here we remove *tg_pt_gp from the global list so that
  1344. * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  1345. * can be made while we are releasing struct t10_alua_tg_pt_gp.
  1346. */
  1347. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1348. list_del(&tg_pt_gp->tg_pt_gp_list);
  1349. su_dev->t10_alua.alua_tg_pt_gps_counter--;
  1350. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1351. /*
  1352. * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  1353. * core_alua_get_tg_pt_gp_by_name() in
  1354. * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  1355. * to be released with core_alua_put_tg_pt_gp_from_name().
  1356. */
  1357. while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  1358. cpu_relax();
  1359. /*
  1360. * Release reference to struct t10_alua_tg_pt_gp from all associated
  1361. * struct se_port.
  1362. */
  1363. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1364. list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  1365. &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  1366. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1367. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1368. tg_pt_gp->tg_pt_gp_members--;
  1369. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1370. }
  1371. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1372. /*
  1373. * tg_pt_gp_mem is associated with a single
  1374. * se_port->sep_alua_tg_pt_gp_mem, and is released via
  1375. * core_alua_free_tg_pt_gp_mem().
  1376. *
  1377. * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  1378. * assume we want to re-assocate a given tg_pt_gp_mem with
  1379. * default_tg_pt_gp.
  1380. */
  1381. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1382. if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
  1383. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1384. su_dev->t10_alua.default_tg_pt_gp);
  1385. } else
  1386. tg_pt_gp_mem->tg_pt_gp = NULL;
  1387. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1388. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1389. }
  1390. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1391. kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  1392. }
  1393. void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  1394. {
  1395. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1396. struct t10_alua *alua = &su_dev->t10_alua;
  1397. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1398. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1399. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1400. return;
  1401. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1402. if (!tg_pt_gp_mem)
  1403. return;
  1404. while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  1405. cpu_relax();
  1406. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1407. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1408. if (tg_pt_gp) {
  1409. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1410. if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  1411. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1412. tg_pt_gp->tg_pt_gp_members--;
  1413. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1414. }
  1415. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1416. tg_pt_gp_mem->tg_pt_gp = NULL;
  1417. }
  1418. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1419. kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  1420. }
  1421. static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  1422. struct se_subsystem_dev *su_dev,
  1423. const char *name)
  1424. {
  1425. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1426. struct config_item *ci;
  1427. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1428. list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
  1429. tg_pt_gp_list) {
  1430. if (!tg_pt_gp->tg_pt_gp_valid_id)
  1431. continue;
  1432. ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1433. if (!strcmp(config_item_name(ci), name)) {
  1434. atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1435. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1436. return tg_pt_gp;
  1437. }
  1438. }
  1439. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1440. return NULL;
  1441. }
  1442. static void core_alua_put_tg_pt_gp_from_name(
  1443. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1444. {
  1445. struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  1446. spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  1447. atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  1448. spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  1449. }
  1450. /*
  1451. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1452. */
  1453. void __core_alua_attach_tg_pt_gp_mem(
  1454. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1455. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1456. {
  1457. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1458. tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  1459. tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  1460. list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  1461. &tg_pt_gp->tg_pt_gp_mem_list);
  1462. tg_pt_gp->tg_pt_gp_members++;
  1463. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1464. }
  1465. /*
  1466. * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
  1467. */
  1468. static void __core_alua_drop_tg_pt_gp_mem(
  1469. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  1470. struct t10_alua_tg_pt_gp *tg_pt_gp)
  1471. {
  1472. spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  1473. list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  1474. tg_pt_gp_mem->tg_pt_gp = NULL;
  1475. tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  1476. tg_pt_gp->tg_pt_gp_members--;
  1477. spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  1478. }
  1479. ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  1480. {
  1481. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1482. struct config_item *tg_pt_ci;
  1483. struct t10_alua *alua = &su_dev->t10_alua;
  1484. struct t10_alua_tg_pt_gp *tg_pt_gp;
  1485. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1486. ssize_t len = 0;
  1487. if (alua->alua_type != SPC3_ALUA_EMULATED)
  1488. return len;
  1489. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1490. if (!tg_pt_gp_mem)
  1491. return len;
  1492. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1493. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1494. if (tg_pt_gp) {
  1495. tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  1496. len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
  1497. " %hu\nTG Port Primary Access State: %s\nTG Port "
  1498. "Primary Access Status: %s\nTG Port Secondary Access"
  1499. " State: %s\nTG Port Secondary Access Status: %s\n",
  1500. config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  1501. core_alua_dump_state(atomic_read(
  1502. &tg_pt_gp->tg_pt_gp_alua_access_state)),
  1503. core_alua_dump_status(
  1504. tg_pt_gp->tg_pt_gp_alua_access_status),
  1505. (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  1506. "Offline" : "None",
  1507. core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  1508. }
  1509. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1510. return len;
  1511. }
  1512. ssize_t core_alua_store_tg_pt_gp_info(
  1513. struct se_port *port,
  1514. const char *page,
  1515. size_t count)
  1516. {
  1517. struct se_portal_group *tpg;
  1518. struct se_lun *lun;
  1519. struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  1520. struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  1521. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1522. unsigned char buf[TG_PT_GROUP_NAME_BUF];
  1523. int move = 0;
  1524. tpg = port->sep_tpg;
  1525. lun = port->sep_lun;
  1526. if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
  1527. pr_warn("SPC3_ALUA_EMULATED not enabled for"
  1528. " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1529. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1530. config_item_name(&lun->lun_group.cg_item));
  1531. return -EINVAL;
  1532. }
  1533. if (count > TG_PT_GROUP_NAME_BUF) {
  1534. pr_err("ALUA Target Port Group alias too large!\n");
  1535. return -EINVAL;
  1536. }
  1537. memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  1538. memcpy(buf, page, count);
  1539. /*
  1540. * Any ALUA target port group alias besides "NULL" means we will be
  1541. * making a new group association.
  1542. */
  1543. if (strcmp(strstrip(buf), "NULL")) {
  1544. /*
  1545. * core_alua_get_tg_pt_gp_by_name() will increment reference to
  1546. * struct t10_alua_tg_pt_gp. This reference is released with
  1547. * core_alua_put_tg_pt_gp_from_name() below.
  1548. */
  1549. tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
  1550. strstrip(buf));
  1551. if (!tg_pt_gp_new)
  1552. return -ENODEV;
  1553. }
  1554. tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  1555. if (!tg_pt_gp_mem) {
  1556. if (tg_pt_gp_new)
  1557. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1558. pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
  1559. return -EINVAL;
  1560. }
  1561. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1562. tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  1563. if (tg_pt_gp) {
  1564. /*
  1565. * Clearing an existing tg_pt_gp association, and replacing
  1566. * with the default_tg_pt_gp.
  1567. */
  1568. if (!tg_pt_gp_new) {
  1569. pr_debug("Target_Core_ConfigFS: Moving"
  1570. " %s/tpgt_%hu/%s from ALUA Target Port Group:"
  1571. " alua/%s, ID: %hu back to"
  1572. " default_tg_pt_gp\n",
  1573. tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1574. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1575. config_item_name(&lun->lun_group.cg_item),
  1576. config_item_name(
  1577. &tg_pt_gp->tg_pt_gp_group.cg_item),
  1578. tg_pt_gp->tg_pt_gp_id);
  1579. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1580. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  1581. su_dev->t10_alua.default_tg_pt_gp);
  1582. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1583. return count;
  1584. }
  1585. /*
  1586. * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  1587. */
  1588. __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  1589. move = 1;
  1590. }
  1591. /*
  1592. * Associate tg_pt_gp_mem with tg_pt_gp_new.
  1593. */
  1594. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  1595. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  1596. pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
  1597. " Target Port Group: alua/%s, ID: %hu\n", (move) ?
  1598. "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  1599. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1600. config_item_name(&lun->lun_group.cg_item),
  1601. config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  1602. tg_pt_gp_new->tg_pt_gp_id);
  1603. core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  1604. return count;
  1605. }
  1606. ssize_t core_alua_show_access_type(
  1607. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1608. char *page)
  1609. {
  1610. if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  1611. (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  1612. return sprintf(page, "Implict and Explict\n");
  1613. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  1614. return sprintf(page, "Implict\n");
  1615. else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  1616. return sprintf(page, "Explict\n");
  1617. else
  1618. return sprintf(page, "None\n");
  1619. }
  1620. ssize_t core_alua_store_access_type(
  1621. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1622. const char *page,
  1623. size_t count)
  1624. {
  1625. unsigned long tmp;
  1626. int ret;
  1627. ret = strict_strtoul(page, 0, &tmp);
  1628. if (ret < 0) {
  1629. pr_err("Unable to extract alua_access_type\n");
  1630. return -EINVAL;
  1631. }
  1632. if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
  1633. pr_err("Illegal value for alua_access_type:"
  1634. " %lu\n", tmp);
  1635. return -EINVAL;
  1636. }
  1637. if (tmp == 3)
  1638. tg_pt_gp->tg_pt_gp_alua_access_type =
  1639. TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  1640. else if (tmp == 2)
  1641. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  1642. else if (tmp == 1)
  1643. tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  1644. else
  1645. tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  1646. return count;
  1647. }
  1648. ssize_t core_alua_show_nonop_delay_msecs(
  1649. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1650. char *page)
  1651. {
  1652. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  1653. }
  1654. ssize_t core_alua_store_nonop_delay_msecs(
  1655. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1656. const char *page,
  1657. size_t count)
  1658. {
  1659. unsigned long tmp;
  1660. int ret;
  1661. ret = strict_strtoul(page, 0, &tmp);
  1662. if (ret < 0) {
  1663. pr_err("Unable to extract nonop_delay_msecs\n");
  1664. return -EINVAL;
  1665. }
  1666. if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
  1667. pr_err("Passed nonop_delay_msecs: %lu, exceeds"
  1668. " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
  1669. ALUA_MAX_NONOP_DELAY_MSECS);
  1670. return -EINVAL;
  1671. }
  1672. tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  1673. return count;
  1674. }
  1675. ssize_t core_alua_show_trans_delay_msecs(
  1676. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1677. char *page)
  1678. {
  1679. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  1680. }
  1681. ssize_t core_alua_store_trans_delay_msecs(
  1682. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1683. const char *page,
  1684. size_t count)
  1685. {
  1686. unsigned long tmp;
  1687. int ret;
  1688. ret = strict_strtoul(page, 0, &tmp);
  1689. if (ret < 0) {
  1690. pr_err("Unable to extract trans_delay_msecs\n");
  1691. return -EINVAL;
  1692. }
  1693. if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
  1694. pr_err("Passed trans_delay_msecs: %lu, exceeds"
  1695. " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
  1696. ALUA_MAX_TRANS_DELAY_MSECS);
  1697. return -EINVAL;
  1698. }
  1699. tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  1700. return count;
  1701. }
  1702. ssize_t core_alua_show_implict_trans_secs(
  1703. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1704. char *page)
  1705. {
  1706. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
  1707. }
  1708. ssize_t core_alua_store_implict_trans_secs(
  1709. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1710. const char *page,
  1711. size_t count)
  1712. {
  1713. unsigned long tmp;
  1714. int ret;
  1715. ret = strict_strtoul(page, 0, &tmp);
  1716. if (ret < 0) {
  1717. pr_err("Unable to extract implict_trans_secs\n");
  1718. return -EINVAL;
  1719. }
  1720. if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
  1721. pr_err("Passed implict_trans_secs: %lu, exceeds"
  1722. " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
  1723. ALUA_MAX_IMPLICT_TRANS_SECS);
  1724. return -EINVAL;
  1725. }
  1726. tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
  1727. return count;
  1728. }
  1729. ssize_t core_alua_show_preferred_bit(
  1730. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1731. char *page)
  1732. {
  1733. return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
  1734. }
  1735. ssize_t core_alua_store_preferred_bit(
  1736. struct t10_alua_tg_pt_gp *tg_pt_gp,
  1737. const char *page,
  1738. size_t count)
  1739. {
  1740. unsigned long tmp;
  1741. int ret;
  1742. ret = strict_strtoul(page, 0, &tmp);
  1743. if (ret < 0) {
  1744. pr_err("Unable to extract preferred ALUA value\n");
  1745. return -EINVAL;
  1746. }
  1747. if ((tmp != 0) && (tmp != 1)) {
  1748. pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
  1749. return -EINVAL;
  1750. }
  1751. tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  1752. return count;
  1753. }
  1754. ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  1755. {
  1756. if (!lun->lun_sep)
  1757. return -ENODEV;
  1758. return sprintf(page, "%d\n",
  1759. atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  1760. }
  1761. ssize_t core_alua_store_offline_bit(
  1762. struct se_lun *lun,
  1763. const char *page,
  1764. size_t count)
  1765. {
  1766. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  1767. unsigned long tmp;
  1768. int ret;
  1769. if (!lun->lun_sep)
  1770. return -ENODEV;
  1771. ret = strict_strtoul(page, 0, &tmp);
  1772. if (ret < 0) {
  1773. pr_err("Unable to extract alua_tg_pt_offline value\n");
  1774. return -EINVAL;
  1775. }
  1776. if ((tmp != 0) && (tmp != 1)) {
  1777. pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
  1778. tmp);
  1779. return -EINVAL;
  1780. }
  1781. tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
  1782. if (!tg_pt_gp_mem) {
  1783. pr_err("Unable to locate *tg_pt_gp_mem\n");
  1784. return -EINVAL;
  1785. }
  1786. ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  1787. lun->lun_sep, 0, (int)tmp);
  1788. if (ret < 0)
  1789. return -EINVAL;
  1790. return count;
  1791. }
  1792. ssize_t core_alua_show_secondary_status(
  1793. struct se_lun *lun,
  1794. char *page)
  1795. {
  1796. return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
  1797. }
  1798. ssize_t core_alua_store_secondary_status(
  1799. struct se_lun *lun,
  1800. const char *page,
  1801. size_t count)
  1802. {
  1803. unsigned long tmp;
  1804. int ret;
  1805. ret = strict_strtoul(page, 0, &tmp);
  1806. if (ret < 0) {
  1807. pr_err("Unable to extract alua_tg_pt_status\n");
  1808. return -EINVAL;
  1809. }
  1810. if ((tmp != ALUA_STATUS_NONE) &&
  1811. (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  1812. (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
  1813. pr_err("Illegal value for alua_tg_pt_status: %lu\n",
  1814. tmp);
  1815. return -EINVAL;
  1816. }
  1817. lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  1818. return count;
  1819. }
  1820. ssize_t core_alua_show_secondary_write_metadata(
  1821. struct se_lun *lun,
  1822. char *page)
  1823. {
  1824. return sprintf(page, "%d\n",
  1825. lun->lun_sep->sep_tg_pt_secondary_write_md);
  1826. }
  1827. ssize_t core_alua_store_secondary_write_metadata(
  1828. struct se_lun *lun,
  1829. const char *page,
  1830. size_t count)
  1831. {
  1832. unsigned long tmp;
  1833. int ret;
  1834. ret = strict_strtoul(page, 0, &tmp);
  1835. if (ret < 0) {
  1836. pr_err("Unable to extract alua_tg_pt_write_md\n");
  1837. return -EINVAL;
  1838. }
  1839. if ((tmp != 0) && (tmp != 1)) {
  1840. pr_err("Illegal value for alua_tg_pt_write_md:"
  1841. " %lu\n", tmp);
  1842. return -EINVAL;
  1843. }
  1844. lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  1845. return count;
  1846. }
  1847. int core_setup_alua(struct se_device *dev, int force_pt)
  1848. {
  1849. struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  1850. struct t10_alua *alua = &su_dev->t10_alua;
  1851. struct t10_alua_lu_gp_member *lu_gp_mem;
  1852. /*
  1853. * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
  1854. * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
  1855. * cause a problem because libata and some SATA RAID HBAs appear
  1856. * under Linux/SCSI, but emulate SCSI logic themselves.
  1857. */
  1858. if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  1859. !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
  1860. alua->alua_type = SPC_ALUA_PASSTHROUGH;
  1861. alua->alua_state_check = &core_alua_state_check_nop;
  1862. pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
  1863. " emulation\n", dev->transport->name);
  1864. return 0;
  1865. }
  1866. /*
  1867. * If SPC-3 or above is reported by real or emulated struct se_device,
  1868. * use emulated ALUA.
  1869. */
  1870. if (dev->transport->get_device_rev(dev) >= SCSI_3) {
  1871. pr_debug("%s: Enabling ALUA Emulation for SPC-3"
  1872. " device\n", dev->transport->name);
  1873. /*
  1874. * Associate this struct se_device with the default ALUA
  1875. * LUN Group.
  1876. */
  1877. lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
  1878. if (IS_ERR(lu_gp_mem))
  1879. return PTR_ERR(lu_gp_mem);
  1880. alua->alua_type = SPC3_ALUA_EMULATED;
  1881. alua->alua_state_check = &core_alua_state_check;
  1882. spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  1883. __core_alua_attach_lu_gp_mem(lu_gp_mem,
  1884. default_lu_gp);
  1885. spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  1886. pr_debug("%s: Adding to default ALUA LU Group:"
  1887. " core/alua/lu_gps/default_lu_gp\n",
  1888. dev->transport->name);
  1889. } else {
  1890. alua->alua_type = SPC2_ALUA_DISABLED;
  1891. alua->alua_state_check = &core_alua_state_check_nop;
  1892. pr_debug("%s: Disabling ALUA Emulation for SPC-2"
  1893. " device\n", dev->transport->name);
  1894. }
  1895. return 0;
  1896. }