target_core_alua.c 56 KB

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