osd_initiator.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * osd_initiator - Main body of the osd initiator library.
  3. *
  4. * Note: The file does not contain the advanced security functionality which
  5. * is only needed by the security_manager's initiators.
  6. *
  7. * Copyright (C) 2008 Panasas Inc. All rights reserved.
  8. *
  9. * Authors:
  10. * Boaz Harrosh <bharrosh@panasas.com>
  11. * Benny Halevy <bhalevy@panasas.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. *
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. * 3. Neither the name of the Panasas company nor the names of its
  26. * contributors may be used to endorse or promote products derived
  27. * from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  36. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <scsi/osd_initiator.h>
  42. #include <scsi/osd_sec.h>
  43. #include <scsi/osd_attributes.h>
  44. #include <scsi/osd_sense.h>
  45. #include <scsi/scsi_device.h>
  46. #include "osd_debug.h"
  47. #ifndef __unused
  48. # define __unused __attribute__((unused))
  49. #endif
  50. enum { OSD_REQ_RETRIES = 1 };
  51. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  52. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  53. MODULE_LICENSE("GPL");
  54. static inline void build_test(void)
  55. {
  56. /* structures were not packed */
  57. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  58. BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
  59. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  60. }
  61. static const char *_osd_ver_desc(struct osd_request *or)
  62. {
  63. return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
  64. }
  65. #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
  66. static int _osd_get_print_system_info(struct osd_dev *od,
  67. void *caps, struct osd_dev_info *odi)
  68. {
  69. struct osd_request *or;
  70. struct osd_attr get_attrs[] = {
  71. ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
  72. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
  73. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
  74. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
  75. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
  76. ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
  77. ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
  78. ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
  79. ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
  80. ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
  81. /* IBM-OSD-SIM Has a bug with this one put it last */
  82. ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
  83. };
  84. void *iter = NULL, *pFirst;
  85. int nelem = ARRAY_SIZE(get_attrs), a = 0;
  86. int ret;
  87. or = osd_start_request(od, GFP_KERNEL);
  88. if (!or)
  89. return -ENOMEM;
  90. /* get attrs */
  91. osd_req_get_attributes(or, &osd_root_object);
  92. osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
  93. ret = osd_finalize_request(or, 0, caps, NULL);
  94. if (ret)
  95. goto out;
  96. ret = osd_execute_request(or);
  97. if (ret) {
  98. OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
  99. goto out;
  100. }
  101. osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
  102. OSD_INFO("Detected %s device\n",
  103. _osd_ver_desc(or));
  104. pFirst = get_attrs[a++].val_ptr;
  105. OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
  106. (char *)pFirst);
  107. pFirst = get_attrs[a++].val_ptr;
  108. OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
  109. (char *)pFirst);
  110. pFirst = get_attrs[a++].val_ptr;
  111. OSD_INFO("PRODUCT_MODEL [%s]\n",
  112. (char *)pFirst);
  113. pFirst = get_attrs[a++].val_ptr;
  114. OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
  115. pFirst ? get_unaligned_be32(pFirst) : ~0U);
  116. pFirst = get_attrs[a++].val_ptr;
  117. OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
  118. (char *)pFirst);
  119. odi->osdname_len = get_attrs[a].len;
  120. /* Avoid NULL for memcmp optimization 0-length is good enough */
  121. odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
  122. if (odi->osdname_len)
  123. memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
  124. OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
  125. a++;
  126. pFirst = get_attrs[a++].val_ptr;
  127. OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
  128. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  129. pFirst = get_attrs[a++].val_ptr;
  130. OSD_INFO("USED_CAPACITY [0x%llx]\n",
  131. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  132. pFirst = get_attrs[a++].val_ptr;
  133. OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
  134. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  135. if (a >= nelem)
  136. goto out;
  137. /* FIXME: Where are the time utilities */
  138. pFirst = get_attrs[a++].val_ptr;
  139. OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
  140. ((char *)pFirst)[0], ((char *)pFirst)[1],
  141. ((char *)pFirst)[2], ((char *)pFirst)[3],
  142. ((char *)pFirst)[4], ((char *)pFirst)[5]);
  143. if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
  144. unsigned len = get_attrs[a].len;
  145. char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
  146. hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
  147. sid_dump, sizeof(sid_dump), true);
  148. OSD_INFO("OSD_SYSTEM_ID(%d)\n"
  149. " [%s]\n", len, sid_dump);
  150. if (unlikely(len > sizeof(odi->systemid))) {
  151. OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
  152. "device idetification might not work\n", len);
  153. len = sizeof(odi->systemid);
  154. }
  155. odi->systemid_len = len;
  156. memcpy(odi->systemid, get_attrs[a].val_ptr, len);
  157. a++;
  158. }
  159. out:
  160. osd_end_request(or);
  161. return ret;
  162. }
  163. int osd_auto_detect_ver(struct osd_dev *od,
  164. void *caps, struct osd_dev_info *odi)
  165. {
  166. int ret;
  167. /* Auto-detect the osd version */
  168. ret = _osd_get_print_system_info(od, caps, odi);
  169. if (ret) {
  170. osd_dev_set_ver(od, OSD_VER1);
  171. OSD_DEBUG("converting to OSD1\n");
  172. ret = _osd_get_print_system_info(od, caps, odi);
  173. }
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(osd_auto_detect_ver);
  177. static unsigned _osd_req_cdb_len(struct osd_request *or)
  178. {
  179. return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
  180. }
  181. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  182. {
  183. return osd_req_is_ver1(or) ?
  184. osdv1_attr_list_elem_size(len) :
  185. osdv2_attr_list_elem_size(len);
  186. }
  187. static void _osd_req_alist_elem_encode(struct osd_request *or,
  188. void *attr_last, const struct osd_attr *oa)
  189. {
  190. if (osd_req_is_ver1(or)) {
  191. struct osdv1_attributes_list_element *attr = attr_last;
  192. attr->attr_page = cpu_to_be32(oa->attr_page);
  193. attr->attr_id = cpu_to_be32(oa->attr_id);
  194. attr->attr_bytes = cpu_to_be16(oa->len);
  195. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  196. } else {
  197. struct osdv2_attributes_list_element *attr = attr_last;
  198. attr->attr_page = cpu_to_be32(oa->attr_page);
  199. attr->attr_id = cpu_to_be32(oa->attr_id);
  200. attr->attr_bytes = cpu_to_be16(oa->len);
  201. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  202. }
  203. }
  204. static int _osd_req_alist_elem_decode(struct osd_request *or,
  205. void *cur_p, struct osd_attr *oa, unsigned max_bytes)
  206. {
  207. unsigned inc;
  208. if (osd_req_is_ver1(or)) {
  209. struct osdv1_attributes_list_element *attr = cur_p;
  210. if (max_bytes < sizeof(*attr))
  211. return -1;
  212. oa->len = be16_to_cpu(attr->attr_bytes);
  213. inc = _osd_req_alist_elem_size(or, oa->len);
  214. if (inc > max_bytes)
  215. return -1;
  216. oa->attr_page = be32_to_cpu(attr->attr_page);
  217. oa->attr_id = be32_to_cpu(attr->attr_id);
  218. /* OSD1: On empty attributes we return a pointer to 2 bytes
  219. * of zeros. This keeps similar behaviour with OSD2.
  220. * (See below)
  221. */
  222. oa->val_ptr = likely(oa->len) ? attr->attr_val :
  223. (u8 *)&attr->attr_bytes;
  224. } else {
  225. struct osdv2_attributes_list_element *attr = cur_p;
  226. if (max_bytes < sizeof(*attr))
  227. return -1;
  228. oa->len = be16_to_cpu(attr->attr_bytes);
  229. inc = _osd_req_alist_elem_size(or, oa->len);
  230. if (inc > max_bytes)
  231. return -1;
  232. oa->attr_page = be32_to_cpu(attr->attr_page);
  233. oa->attr_id = be32_to_cpu(attr->attr_id);
  234. /* OSD2: For convenience, on empty attributes, we return 8 bytes
  235. * of zeros here. This keeps the same behaviour with OSD2r04,
  236. * and is nice with null terminating ASCII fields.
  237. * oa->val_ptr == NULL marks the end-of-list, or error.
  238. */
  239. oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
  240. }
  241. return inc;
  242. }
  243. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  244. {
  245. return osd_req_is_ver1(or) ?
  246. osdv1_list_size(list_head) :
  247. osdv2_list_size(list_head);
  248. }
  249. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  250. {
  251. return osd_req_is_ver1(or) ?
  252. sizeof(struct osdv1_attributes_list_header) :
  253. sizeof(struct osdv2_attributes_list_header);
  254. }
  255. static void _osd_req_set_alist_type(struct osd_request *or,
  256. void *list, int list_type)
  257. {
  258. if (osd_req_is_ver1(or)) {
  259. struct osdv1_attributes_list_header *attr_list = list;
  260. memset(attr_list, 0, sizeof(*attr_list));
  261. attr_list->type = list_type;
  262. } else {
  263. struct osdv2_attributes_list_header *attr_list = list;
  264. memset(attr_list, 0, sizeof(*attr_list));
  265. attr_list->type = list_type;
  266. }
  267. }
  268. static bool _osd_req_is_alist_type(struct osd_request *or,
  269. void *list, int list_type)
  270. {
  271. if (!list)
  272. return false;
  273. if (osd_req_is_ver1(or)) {
  274. struct osdv1_attributes_list_header *attr_list = list;
  275. return attr_list->type == list_type;
  276. } else {
  277. struct osdv2_attributes_list_header *attr_list = list;
  278. return attr_list->type == list_type;
  279. }
  280. }
  281. /* This is for List-objects not Attributes-Lists */
  282. static void _osd_req_encode_olist(struct osd_request *or,
  283. struct osd_obj_id_list *list)
  284. {
  285. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  286. if (osd_req_is_ver1(or)) {
  287. cdbh->v1.list_identifier = list->list_identifier;
  288. cdbh->v1.start_address = list->continuation_id;
  289. } else {
  290. cdbh->v2.list_identifier = list->list_identifier;
  291. cdbh->v2.start_address = list->continuation_id;
  292. }
  293. }
  294. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  295. u64 offset, unsigned *padding)
  296. {
  297. return __osd_encode_offset(offset, padding,
  298. osd_req_is_ver1(or) ?
  299. OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
  300. OSD_OFFSET_MAX_SHIFT);
  301. }
  302. static struct osd_security_parameters *
  303. _osd_req_sec_params(struct osd_request *or)
  304. {
  305. struct osd_cdb *ocdb = &or->cdb;
  306. if (osd_req_is_ver1(or))
  307. return (struct osd_security_parameters *)&ocdb->v1.sec_params;
  308. else
  309. return (struct osd_security_parameters *)&ocdb->v2.sec_params;
  310. }
  311. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  312. {
  313. memset(osdd, 0, sizeof(*osdd));
  314. osdd->scsi_device = scsi_device;
  315. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  316. #ifdef OSD_VER1_SUPPORT
  317. osdd->version = OSD_VER2;
  318. #endif
  319. /* TODO: Allocate pools for osd_request attributes ... */
  320. }
  321. EXPORT_SYMBOL(osd_dev_init);
  322. void osd_dev_fini(struct osd_dev *osdd)
  323. {
  324. /* TODO: De-allocate pools */
  325. osdd->scsi_device = NULL;
  326. }
  327. EXPORT_SYMBOL(osd_dev_fini);
  328. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  329. {
  330. struct osd_request *or;
  331. /* TODO: Use mempool with one saved request */
  332. or = kzalloc(sizeof(*or), gfp);
  333. return or;
  334. }
  335. static void _osd_request_free(struct osd_request *or)
  336. {
  337. kfree(or);
  338. }
  339. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  340. {
  341. struct osd_request *or;
  342. or = _osd_request_alloc(gfp);
  343. if (!or)
  344. return NULL;
  345. or->osd_dev = dev;
  346. or->alloc_flags = gfp;
  347. or->timeout = dev->def_timeout;
  348. or->retries = OSD_REQ_RETRIES;
  349. return or;
  350. }
  351. EXPORT_SYMBOL(osd_start_request);
  352. static void _osd_free_seg(struct osd_request *or __unused,
  353. struct _osd_req_data_segment *seg)
  354. {
  355. if (!seg->buff || !seg->alloc_size)
  356. return;
  357. kfree(seg->buff);
  358. seg->buff = NULL;
  359. seg->alloc_size = 0;
  360. }
  361. static void _put_request(struct request *rq , bool is_async)
  362. {
  363. if (is_async) {
  364. WARN_ON(rq->bio);
  365. __blk_put_request(rq->q, rq);
  366. } else {
  367. /*
  368. * If osd_finalize_request() was called but the request was not
  369. * executed through the block layer, then we must release BIOs.
  370. * TODO: Keep error code in or->async_error. Need to audit all
  371. * code paths.
  372. */
  373. if (unlikely(rq->bio))
  374. blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
  375. else
  376. blk_put_request(rq);
  377. }
  378. }
  379. void osd_end_request(struct osd_request *or)
  380. {
  381. struct request *rq = or->request;
  382. /* IMPORTANT: make sure this agrees with osd_execute_request_async */
  383. bool is_async = (or->request->end_io_data == or);
  384. _osd_free_seg(or, &or->set_attr);
  385. _osd_free_seg(or, &or->enc_get_attr);
  386. _osd_free_seg(or, &or->get_attr);
  387. if (rq) {
  388. if (rq->next_rq) {
  389. _put_request(rq->next_rq, is_async);
  390. rq->next_rq = NULL;
  391. }
  392. _put_request(rq, is_async);
  393. }
  394. _osd_request_free(or);
  395. }
  396. EXPORT_SYMBOL(osd_end_request);
  397. int osd_execute_request(struct osd_request *or)
  398. {
  399. return or->async_error =
  400. blk_execute_rq(or->request->q, NULL, or->request, 0);
  401. }
  402. EXPORT_SYMBOL(osd_execute_request);
  403. static void osd_request_async_done(struct request *req, int error)
  404. {
  405. struct osd_request *or = req->end_io_data;
  406. or->async_error = error;
  407. if (unlikely(error)) {
  408. OSD_DEBUG("osd_request_async_done error recieved %d "
  409. "errors 0x%x\n", error, req->errors);
  410. if (!req->errors) /* don't miss out on this one */
  411. req->errors = error;
  412. }
  413. if (or->async_done)
  414. or->async_done(or, or->async_private);
  415. else
  416. osd_end_request(or);
  417. }
  418. int osd_execute_request_async(struct osd_request *or,
  419. osd_req_done_fn *done, void *private)
  420. {
  421. or->request->end_io_data = or;
  422. or->async_private = private;
  423. or->async_done = done;
  424. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  425. osd_request_async_done);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL(osd_execute_request_async);
  429. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  430. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  431. static int _osd_realloc_seg(struct osd_request *or,
  432. struct _osd_req_data_segment *seg, unsigned max_bytes)
  433. {
  434. void *buff;
  435. if (seg->alloc_size >= max_bytes)
  436. return 0;
  437. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  438. if (!buff) {
  439. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  440. seg->alloc_size);
  441. return -ENOMEM;
  442. }
  443. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  444. seg->buff = buff;
  445. seg->alloc_size = max_bytes;
  446. return 0;
  447. }
  448. static int _alloc_set_attr_list(struct osd_request *or,
  449. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  450. {
  451. unsigned total_bytes = add_bytes;
  452. for (; nelem; --nelem, ++oa)
  453. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  454. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  455. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  456. }
  457. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  458. {
  459. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  460. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  461. }
  462. static int _alloc_get_attr_list(struct osd_request *or)
  463. {
  464. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  465. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  466. }
  467. /*
  468. * Common to all OSD commands
  469. */
  470. static void _osdv1_req_encode_common(struct osd_request *or,
  471. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  472. {
  473. struct osdv1_cdb *ocdb = &or->cdb.v1;
  474. /*
  475. * For speed, the commands
  476. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  477. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  478. * are not supported here. Should pass zero and set after the call
  479. */
  480. act &= cpu_to_be16(~0x0080); /* V1 action code */
  481. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  482. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  483. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  484. ocdb->h.varlen_cdb.service_action = act;
  485. ocdb->h.partition = cpu_to_be64(obj->partition);
  486. ocdb->h.object = cpu_to_be64(obj->id);
  487. ocdb->h.v1.length = cpu_to_be64(len);
  488. ocdb->h.v1.start_address = cpu_to_be64(offset);
  489. }
  490. static void _osdv2_req_encode_common(struct osd_request *or,
  491. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  492. {
  493. struct osdv2_cdb *ocdb = &or->cdb.v2;
  494. OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
  495. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  496. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  497. ocdb->h.varlen_cdb.service_action = act;
  498. ocdb->h.partition = cpu_to_be64(obj->partition);
  499. ocdb->h.object = cpu_to_be64(obj->id);
  500. ocdb->h.v2.length = cpu_to_be64(len);
  501. ocdb->h.v2.start_address = cpu_to_be64(offset);
  502. }
  503. static void _osd_req_encode_common(struct osd_request *or,
  504. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  505. {
  506. if (osd_req_is_ver1(or))
  507. _osdv1_req_encode_common(or, act, obj, offset, len);
  508. else
  509. _osdv2_req_encode_common(or, act, obj, offset, len);
  510. }
  511. /*
  512. * Device commands
  513. */
  514. /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
  515. /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
  516. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  517. {
  518. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  519. tot_capacity);
  520. }
  521. EXPORT_SYMBOL(osd_req_format);
  522. int osd_req_list_dev_partitions(struct osd_request *or,
  523. osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
  524. {
  525. return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
  526. }
  527. EXPORT_SYMBOL(osd_req_list_dev_partitions);
  528. static void _osd_req_encode_flush(struct osd_request *or,
  529. enum osd_options_flush_scope_values op)
  530. {
  531. struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
  532. ocdb->command_specific_options = op;
  533. }
  534. void osd_req_flush_obsd(struct osd_request *or,
  535. enum osd_options_flush_scope_values op)
  536. {
  537. _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
  538. _osd_req_encode_flush(or, op);
  539. }
  540. EXPORT_SYMBOL(osd_req_flush_obsd);
  541. /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
  542. const u8 *cdb, ...); */
  543. /*TODO: void osd_req_task_management(struct osd_request *, ...); */
  544. /*
  545. * Partition commands
  546. */
  547. static void _osd_req_encode_partition(struct osd_request *or,
  548. __be16 act, osd_id partition)
  549. {
  550. struct osd_obj_id par = {
  551. .partition = partition,
  552. .id = 0,
  553. };
  554. _osd_req_encode_common(or, act, &par, 0, 0);
  555. }
  556. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  557. {
  558. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  559. }
  560. EXPORT_SYMBOL(osd_req_create_partition);
  561. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  562. {
  563. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  564. }
  565. EXPORT_SYMBOL(osd_req_remove_partition);
  566. /*TODO: void osd_req_set_partition_key(struct osd_request *,
  567. osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
  568. u8 seed[OSD_CRYPTO_SEED_SIZE]); */
  569. static int _osd_req_list_objects(struct osd_request *or,
  570. __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
  571. struct osd_obj_id_list *list, unsigned nelem)
  572. {
  573. struct request_queue *q = osd_request_queue(or->osd_dev);
  574. u64 len = nelem * sizeof(osd_id) + sizeof(*list);
  575. struct bio *bio;
  576. _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
  577. if (list->list_identifier)
  578. _osd_req_encode_olist(or, list);
  579. WARN_ON(or->in.bio);
  580. bio = bio_map_kern(q, list, len, or->alloc_flags);
  581. if (IS_ERR(bio)) {
  582. OSD_ERR("!!! Failed to allocate list_objects BIO\n");
  583. return PTR_ERR(bio);
  584. }
  585. bio->bi_rw &= ~(1 << BIO_RW);
  586. or->in.bio = bio;
  587. or->in.total_bytes = bio->bi_size;
  588. return 0;
  589. }
  590. int osd_req_list_partition_collections(struct osd_request *or,
  591. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  592. unsigned nelem)
  593. {
  594. struct osd_obj_id par = {
  595. .partition = partition,
  596. .id = 0,
  597. };
  598. return osd_req_list_collection_objects(or, &par, initial_id, list,
  599. nelem);
  600. }
  601. EXPORT_SYMBOL(osd_req_list_partition_collections);
  602. int osd_req_list_partition_objects(struct osd_request *or,
  603. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  604. unsigned nelem)
  605. {
  606. struct osd_obj_id par = {
  607. .partition = partition,
  608. .id = 0,
  609. };
  610. return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
  611. nelem);
  612. }
  613. EXPORT_SYMBOL(osd_req_list_partition_objects);
  614. void osd_req_flush_partition(struct osd_request *or,
  615. osd_id partition, enum osd_options_flush_scope_values op)
  616. {
  617. _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
  618. _osd_req_encode_flush(or, op);
  619. }
  620. EXPORT_SYMBOL(osd_req_flush_partition);
  621. /*
  622. * Collection commands
  623. */
  624. /*TODO: void osd_req_create_collection(struct osd_request *,
  625. const struct osd_obj_id *); */
  626. /*TODO: void osd_req_remove_collection(struct osd_request *,
  627. const struct osd_obj_id *); */
  628. int osd_req_list_collection_objects(struct osd_request *or,
  629. const struct osd_obj_id *obj, osd_id initial_id,
  630. struct osd_obj_id_list *list, unsigned nelem)
  631. {
  632. return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
  633. initial_id, list, nelem);
  634. }
  635. EXPORT_SYMBOL(osd_req_list_collection_objects);
  636. /*TODO: void query(struct osd_request *, ...); V2 */
  637. void osd_req_flush_collection(struct osd_request *or,
  638. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
  639. {
  640. _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
  641. _osd_req_encode_flush(or, op);
  642. }
  643. EXPORT_SYMBOL(osd_req_flush_collection);
  644. /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
  645. /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
  646. /*
  647. * Object commands
  648. */
  649. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  650. {
  651. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  652. }
  653. EXPORT_SYMBOL(osd_req_create_object);
  654. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  655. {
  656. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  657. }
  658. EXPORT_SYMBOL(osd_req_remove_object);
  659. /*TODO: void osd_req_create_multi(struct osd_request *or,
  660. struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
  661. */
  662. void osd_req_write(struct osd_request *or,
  663. const struct osd_obj_id *obj, u64 offset,
  664. struct bio *bio, u64 len)
  665. {
  666. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
  667. WARN_ON(or->out.bio || or->out.total_bytes);
  668. WARN_ON(0 == bio_rw_flagged(bio, BIO_RW));
  669. or->out.bio = bio;
  670. or->out.total_bytes = len;
  671. }
  672. EXPORT_SYMBOL(osd_req_write);
  673. int osd_req_write_kern(struct osd_request *or,
  674. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  675. {
  676. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  677. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  678. if (IS_ERR(bio))
  679. return PTR_ERR(bio);
  680. bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
  681. osd_req_write(or, obj, offset, bio, len);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(osd_req_write_kern);
  685. /*TODO: void osd_req_append(struct osd_request *,
  686. const struct osd_obj_id *, struct bio *data_out); */
  687. /*TODO: void osd_req_create_write(struct osd_request *,
  688. const struct osd_obj_id *, struct bio *data_out, u64 offset); */
  689. /*TODO: void osd_req_clear(struct osd_request *,
  690. const struct osd_obj_id *, u64 offset, u64 len); */
  691. /*TODO: void osd_req_punch(struct osd_request *,
  692. const struct osd_obj_id *, u64 offset, u64 len); V2 */
  693. void osd_req_flush_object(struct osd_request *or,
  694. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
  695. /*V2*/ u64 offset, /*V2*/ u64 len)
  696. {
  697. if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
  698. OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
  699. offset = 0;
  700. len = 0;
  701. }
  702. _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
  703. _osd_req_encode_flush(or, op);
  704. }
  705. EXPORT_SYMBOL(osd_req_flush_object);
  706. void osd_req_read(struct osd_request *or,
  707. const struct osd_obj_id *obj, u64 offset,
  708. struct bio *bio, u64 len)
  709. {
  710. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
  711. WARN_ON(or->in.bio || or->in.total_bytes);
  712. WARN_ON(1 == bio_rw_flagged(bio, BIO_RW));
  713. or->in.bio = bio;
  714. or->in.total_bytes = len;
  715. }
  716. EXPORT_SYMBOL(osd_req_read);
  717. int osd_req_read_kern(struct osd_request *or,
  718. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  719. {
  720. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  721. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  722. if (IS_ERR(bio))
  723. return PTR_ERR(bio);
  724. osd_req_read(or, obj, offset, bio, len);
  725. return 0;
  726. }
  727. EXPORT_SYMBOL(osd_req_read_kern);
  728. void osd_req_get_attributes(struct osd_request *or,
  729. const struct osd_obj_id *obj)
  730. {
  731. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  732. }
  733. EXPORT_SYMBOL(osd_req_get_attributes);
  734. void osd_req_set_attributes(struct osd_request *or,
  735. const struct osd_obj_id *obj)
  736. {
  737. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  738. }
  739. EXPORT_SYMBOL(osd_req_set_attributes);
  740. /*
  741. * Attributes List-mode
  742. */
  743. int osd_req_add_set_attr_list(struct osd_request *or,
  744. const struct osd_attr *oa, unsigned nelem)
  745. {
  746. unsigned total_bytes = or->set_attr.total_bytes;
  747. void *attr_last;
  748. int ret;
  749. if (or->attributes_mode &&
  750. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  751. WARN_ON(1);
  752. return -EINVAL;
  753. }
  754. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  755. if (!total_bytes) { /* first-time: allocate and put list header */
  756. total_bytes = _osd_req_sizeof_alist_header(or);
  757. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  758. if (ret)
  759. return ret;
  760. _osd_req_set_alist_type(or, or->set_attr.buff,
  761. OSD_ATTR_LIST_SET_RETRIEVE);
  762. }
  763. attr_last = or->set_attr.buff + total_bytes;
  764. for (; nelem; --nelem) {
  765. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  766. total_bytes += elem_size;
  767. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  768. or->set_attr.total_bytes = total_bytes - elem_size;
  769. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  770. if (ret)
  771. return ret;
  772. attr_last =
  773. or->set_attr.buff + or->set_attr.total_bytes;
  774. }
  775. _osd_req_alist_elem_encode(or, attr_last, oa);
  776. attr_last += elem_size;
  777. ++oa;
  778. }
  779. or->set_attr.total_bytes = total_bytes;
  780. return 0;
  781. }
  782. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  783. static int _req_append_segment(struct osd_request *or,
  784. unsigned padding, struct _osd_req_data_segment *seg,
  785. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  786. {
  787. void *pad_buff;
  788. int ret;
  789. if (padding) {
  790. /* check if we can just add it to last buffer */
  791. if (last_seg &&
  792. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  793. pad_buff = last_seg->buff + last_seg->total_bytes;
  794. else
  795. pad_buff = io->pad_buff;
  796. ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
  797. or->alloc_flags);
  798. if (ret)
  799. return ret;
  800. io->total_bytes += padding;
  801. }
  802. ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
  803. or->alloc_flags);
  804. if (ret)
  805. return ret;
  806. io->total_bytes += seg->total_bytes;
  807. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  808. seg->total_bytes);
  809. return 0;
  810. }
  811. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  812. {
  813. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  814. unsigned padding;
  815. int ret;
  816. if (!or->set_attr.total_bytes) {
  817. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  818. return 0;
  819. }
  820. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  821. cdbh->attrs_list.set_attr_offset =
  822. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  823. ret = _req_append_segment(or, padding, &or->set_attr,
  824. or->out.last_seg, &or->out);
  825. if (ret)
  826. return ret;
  827. or->out.last_seg = &or->set_attr;
  828. return 0;
  829. }
  830. int osd_req_add_get_attr_list(struct osd_request *or,
  831. const struct osd_attr *oa, unsigned nelem)
  832. {
  833. unsigned total_bytes = or->enc_get_attr.total_bytes;
  834. void *attr_last;
  835. int ret;
  836. if (or->attributes_mode &&
  837. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  838. WARN_ON(1);
  839. return -EINVAL;
  840. }
  841. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  842. /* first time calc data-in list header size */
  843. if (!or->get_attr.total_bytes)
  844. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  845. /* calc data-out info */
  846. if (!total_bytes) { /* first-time: allocate and put list header */
  847. unsigned max_bytes;
  848. total_bytes = _osd_req_sizeof_alist_header(or);
  849. max_bytes = total_bytes +
  850. nelem * sizeof(struct osd_attributes_list_attrid);
  851. ret = _alloc_get_attr_desc(or, max_bytes);
  852. if (ret)
  853. return ret;
  854. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  855. OSD_ATTR_LIST_GET);
  856. }
  857. attr_last = or->enc_get_attr.buff + total_bytes;
  858. for (; nelem; --nelem) {
  859. struct osd_attributes_list_attrid *attrid;
  860. const unsigned cur_size = sizeof(*attrid);
  861. total_bytes += cur_size;
  862. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  863. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  864. ret = _alloc_get_attr_desc(or,
  865. total_bytes + nelem * sizeof(*attrid));
  866. if (ret)
  867. return ret;
  868. attr_last = or->enc_get_attr.buff +
  869. or->enc_get_attr.total_bytes;
  870. }
  871. attrid = attr_last;
  872. attrid->attr_page = cpu_to_be32(oa->attr_page);
  873. attrid->attr_id = cpu_to_be32(oa->attr_id);
  874. attr_last += cur_size;
  875. /* calc data-in size */
  876. or->get_attr.total_bytes +=
  877. _osd_req_alist_elem_size(or, oa->len);
  878. ++oa;
  879. }
  880. or->enc_get_attr.total_bytes = total_bytes;
  881. OSD_DEBUG(
  882. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  883. or->get_attr.total_bytes,
  884. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  885. or->enc_get_attr.total_bytes,
  886. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  887. / sizeof(struct osd_attributes_list_attrid));
  888. return 0;
  889. }
  890. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  891. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  892. {
  893. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  894. unsigned out_padding;
  895. unsigned in_padding;
  896. int ret;
  897. if (!or->enc_get_attr.total_bytes) {
  898. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  899. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  900. return 0;
  901. }
  902. ret = _alloc_get_attr_list(or);
  903. if (ret)
  904. return ret;
  905. /* The out-going buffer info update */
  906. OSD_DEBUG("out-going\n");
  907. cdbh->attrs_list.get_attr_desc_bytes =
  908. cpu_to_be32(or->enc_get_attr.total_bytes);
  909. cdbh->attrs_list.get_attr_desc_offset =
  910. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  911. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  912. or->out.last_seg, &or->out);
  913. if (ret)
  914. return ret;
  915. or->out.last_seg = &or->enc_get_attr;
  916. /* The incoming buffer info update */
  917. OSD_DEBUG("in-coming\n");
  918. cdbh->attrs_list.get_attr_alloc_length =
  919. cpu_to_be32(or->get_attr.total_bytes);
  920. cdbh->attrs_list.get_attr_offset =
  921. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  922. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  923. &or->in);
  924. if (ret)
  925. return ret;
  926. or->in.last_seg = &or->get_attr;
  927. return 0;
  928. }
  929. int osd_req_decode_get_attr_list(struct osd_request *or,
  930. struct osd_attr *oa, int *nelem, void **iterator)
  931. {
  932. unsigned cur_bytes, returned_bytes;
  933. int n;
  934. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  935. void *cur_p;
  936. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  937. OSD_ATTR_LIST_SET_RETRIEVE)) {
  938. oa->attr_page = 0;
  939. oa->attr_id = 0;
  940. oa->val_ptr = NULL;
  941. oa->len = 0;
  942. *iterator = NULL;
  943. return 0;
  944. }
  945. if (*iterator) {
  946. BUG_ON((*iterator < or->get_attr.buff) ||
  947. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  948. cur_p = *iterator;
  949. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  950. returned_bytes = or->get_attr.total_bytes;
  951. } else { /* first time decode the list header */
  952. cur_bytes = sizeof_attr_list;
  953. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  954. sizeof_attr_list;
  955. cur_p = or->get_attr.buff + sizeof_attr_list;
  956. if (returned_bytes > or->get_attr.alloc_size) {
  957. OSD_DEBUG("target report: space was not big enough! "
  958. "Allocate=%u Needed=%u\n",
  959. or->get_attr.alloc_size,
  960. returned_bytes + sizeof_attr_list);
  961. returned_bytes =
  962. or->get_attr.alloc_size - sizeof_attr_list;
  963. }
  964. or->get_attr.total_bytes = returned_bytes;
  965. }
  966. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  967. int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
  968. returned_bytes - cur_bytes);
  969. if (inc < 0) {
  970. OSD_ERR("BAD FOOD from target. list not valid!"
  971. "c=%d r=%d n=%d\n",
  972. cur_bytes, returned_bytes, n);
  973. oa->val_ptr = NULL;
  974. cur_bytes = returned_bytes; /* break the caller loop */
  975. break;
  976. }
  977. cur_bytes += inc;
  978. cur_p += inc;
  979. ++oa;
  980. }
  981. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  982. *nelem = n;
  983. return returned_bytes - cur_bytes;
  984. }
  985. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  986. /*
  987. * Attributes Page-mode
  988. */
  989. int osd_req_add_get_attr_page(struct osd_request *or,
  990. u32 page_id, void *attar_page, unsigned max_page_len,
  991. const struct osd_attr *set_one_attr)
  992. {
  993. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  994. if (or->attributes_mode &&
  995. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  996. WARN_ON(1);
  997. return -EINVAL;
  998. }
  999. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  1000. or->get_attr.buff = attar_page;
  1001. or->get_attr.total_bytes = max_page_len;
  1002. or->set_attr.buff = set_one_attr->val_ptr;
  1003. or->set_attr.total_bytes = set_one_attr->len;
  1004. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  1005. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1006. /* ocdb->attrs_page.get_attr_offset; */
  1007. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1008. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1009. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1010. /* ocdb->attrs_page.set_attr_offset; */
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1014. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1015. {
  1016. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1017. unsigned in_padding, out_padding;
  1018. int ret;
  1019. /* returned page */
  1020. cdbh->attrs_page.get_attr_offset =
  1021. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1022. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1023. &or->in);
  1024. if (ret)
  1025. return ret;
  1026. /* set one value */
  1027. cdbh->attrs_page.set_attr_offset =
  1028. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1029. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  1030. &or->out);
  1031. return ret;
  1032. }
  1033. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1034. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1035. {
  1036. if (is_v1)
  1037. sec_parms->v1.data_out_integrity_check_offset = offset;
  1038. else
  1039. sec_parms->v2.data_out_integrity_check_offset = offset;
  1040. }
  1041. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1042. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1043. {
  1044. if (is_v1)
  1045. sec_parms->v1.data_in_integrity_check_offset = offset;
  1046. else
  1047. sec_parms->v2.data_in_integrity_check_offset = offset;
  1048. }
  1049. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1050. bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key)
  1051. {
  1052. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1053. int ret;
  1054. if (!osd_is_sec_alldata(sec_parms))
  1055. return 0;
  1056. if (has_out) {
  1057. struct _osd_req_data_segment seg = {
  1058. .buff = &or->out_data_integ,
  1059. .total_bytes = sizeof(or->out_data_integ),
  1060. };
  1061. unsigned pad;
  1062. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1063. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1064. or->set_attr.total_bytes);
  1065. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1066. or->enc_get_attr.total_bytes);
  1067. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1068. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1069. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1070. &or->out);
  1071. if (ret)
  1072. return ret;
  1073. or->out.last_seg = NULL;
  1074. /* they are now all chained to request sign them all together */
  1075. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  1076. cap_key);
  1077. }
  1078. if (has_in) {
  1079. struct _osd_req_data_segment seg = {
  1080. .buff = &or->in_data_integ,
  1081. .total_bytes = sizeof(or->in_data_integ),
  1082. };
  1083. unsigned pad;
  1084. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1085. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1086. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1087. &or->in);
  1088. if (ret)
  1089. return ret;
  1090. or->in.last_seg = NULL;
  1091. }
  1092. return 0;
  1093. }
  1094. /*
  1095. * osd_finalize_request and helpers
  1096. */
  1097. static struct request *_make_request(struct request_queue *q, bool has_write,
  1098. struct _osd_io_info *oii, gfp_t flags)
  1099. {
  1100. if (oii->bio)
  1101. return blk_make_request(q, oii->bio, flags);
  1102. else {
  1103. struct request *req;
  1104. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1105. if (unlikely(!req))
  1106. return ERR_PTR(-ENOMEM);
  1107. return req;
  1108. }
  1109. }
  1110. static int _init_blk_request(struct osd_request *or,
  1111. bool has_in, bool has_out)
  1112. {
  1113. gfp_t flags = or->alloc_flags;
  1114. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1115. struct request_queue *q = scsi_device->request_queue;
  1116. struct request *req;
  1117. int ret;
  1118. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1119. if (IS_ERR(req)) {
  1120. ret = PTR_ERR(req);
  1121. goto out;
  1122. }
  1123. or->request = req;
  1124. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1125. req->cmd_flags |= REQ_QUIET;
  1126. req->timeout = or->timeout;
  1127. req->retries = or->retries;
  1128. req->sense = or->sense;
  1129. req->sense_len = 0;
  1130. if (has_out) {
  1131. or->out.req = req;
  1132. if (has_in) {
  1133. /* allocate bidi request */
  1134. req = _make_request(q, false, &or->in, flags);
  1135. if (IS_ERR(req)) {
  1136. OSD_DEBUG("blk_get_request for bidi failed\n");
  1137. ret = PTR_ERR(req);
  1138. goto out;
  1139. }
  1140. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1141. or->in.req = or->request->next_rq = req;
  1142. }
  1143. } else if (has_in)
  1144. or->in.req = req;
  1145. ret = 0;
  1146. out:
  1147. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1148. or, has_in, has_out, ret, or->request);
  1149. return ret;
  1150. }
  1151. int osd_finalize_request(struct osd_request *or,
  1152. u8 options, const void *cap, const u8 *cap_key)
  1153. {
  1154. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1155. bool has_in, has_out;
  1156. u64 out_data_bytes = or->out.total_bytes;
  1157. int ret;
  1158. if (options & OSD_REQ_FUA)
  1159. cdbh->options |= OSD_CDB_FUA;
  1160. if (options & OSD_REQ_DPO)
  1161. cdbh->options |= OSD_CDB_DPO;
  1162. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1163. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1164. osd_set_caps(&or->cdb, cap);
  1165. has_in = or->in.bio || or->get_attr.total_bytes;
  1166. has_out = or->out.bio || or->set_attr.total_bytes ||
  1167. or->enc_get_attr.total_bytes;
  1168. ret = _init_blk_request(or, has_in, has_out);
  1169. if (ret) {
  1170. OSD_DEBUG("_init_blk_request failed\n");
  1171. return ret;
  1172. }
  1173. or->out.pad_buff = sg_out_pad_buffer;
  1174. or->in.pad_buff = sg_in_pad_buffer;
  1175. if (!or->attributes_mode)
  1176. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1177. cdbh->command_specific_options |= or->attributes_mode;
  1178. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1179. ret = _osd_req_finalize_attr_page(or);
  1180. } else {
  1181. /* TODO: I think that for the GET_ATTR command these 2 should
  1182. * be reversed to keep them in execution order (for embeded
  1183. * targets with low memory footprint)
  1184. */
  1185. ret = _osd_req_finalize_set_attr_list(or);
  1186. if (ret) {
  1187. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1188. return ret;
  1189. }
  1190. ret = _osd_req_finalize_get_attr_list(or);
  1191. if (ret) {
  1192. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1193. return ret;
  1194. }
  1195. }
  1196. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1197. out_data_bytes, cap_key);
  1198. if (ret)
  1199. return ret;
  1200. osd_sec_sign_cdb(&or->cdb, cap_key);
  1201. or->request->cmd = or->cdb.buff;
  1202. or->request->cmd_len = _osd_req_cdb_len(or);
  1203. return 0;
  1204. }
  1205. EXPORT_SYMBOL(osd_finalize_request);
  1206. static bool _is_osd_security_code(int code)
  1207. {
  1208. return (code == osd_security_audit_value_frozen) ||
  1209. (code == osd_security_working_key_frozen) ||
  1210. (code == osd_nonce_not_unique) ||
  1211. (code == osd_nonce_timestamp_out_of_range) ||
  1212. (code == osd_invalid_dataout_buffer_integrity_check_value);
  1213. }
  1214. #define OSD_SENSE_PRINT1(fmt, a...) \
  1215. do { \
  1216. if (__cur_sense_need_output) \
  1217. OSD_ERR(fmt, ##a); \
  1218. } while (0)
  1219. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1220. int osd_req_decode_sense_full(struct osd_request *or,
  1221. struct osd_sense_info *osi, bool silent,
  1222. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1223. struct osd_attr *bad_attr_list, int max_attr)
  1224. {
  1225. int sense_len, original_sense_len;
  1226. struct osd_sense_info local_osi;
  1227. struct scsi_sense_descriptor_based *ssdb;
  1228. void *cur_descriptor;
  1229. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1230. const bool __cur_sense_need_output = false;
  1231. #else
  1232. bool __cur_sense_need_output = !silent;
  1233. #endif
  1234. int ret;
  1235. if (likely(!or->request->errors)) {
  1236. osi->out_resid = 0;
  1237. osi->in_resid = 0;
  1238. return 0;
  1239. }
  1240. osi = osi ? : &local_osi;
  1241. memset(osi, 0, sizeof(*osi));
  1242. ssdb = or->request->sense;
  1243. sense_len = or->request->sense_len;
  1244. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1245. OSD_ERR("Block-layer returned error(0x%x) but "
  1246. "sense_len(%u) || key(%d) is empty\n",
  1247. or->request->errors, sense_len, ssdb->sense_key);
  1248. goto analyze;
  1249. }
  1250. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1251. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1252. ssdb->response_code, sense_len);
  1253. goto analyze;
  1254. }
  1255. osi->key = ssdb->sense_key;
  1256. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1257. original_sense_len = ssdb->additional_sense_length + 8;
  1258. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1259. if (__cur_sense_need_output)
  1260. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1261. #endif
  1262. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1263. "additional_code=0x%x async_error=%d errors=0x%x\n",
  1264. osi->key, original_sense_len, sense_len,
  1265. osi->additional_code, or->async_error,
  1266. or->request->errors);
  1267. if (original_sense_len < sense_len)
  1268. sense_len = original_sense_len;
  1269. cur_descriptor = ssdb->ssd;
  1270. sense_len -= sizeof(*ssdb);
  1271. while (sense_len > 0) {
  1272. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1273. int cur_len = ssd->additional_length + 2;
  1274. sense_len -= cur_len;
  1275. if (sense_len < 0)
  1276. break; /* sense was truncated */
  1277. switch (ssd->descriptor_type) {
  1278. case scsi_sense_information:
  1279. case scsi_sense_command_specific_information:
  1280. {
  1281. struct scsi_sense_command_specific_data_descriptor
  1282. *sscd = cur_descriptor;
  1283. osi->command_info =
  1284. get_unaligned_be64(&sscd->information) ;
  1285. OSD_SENSE_PRINT2(
  1286. "command_specific_information 0x%llx \n",
  1287. _LLU(osi->command_info));
  1288. break;
  1289. }
  1290. case scsi_sense_key_specific:
  1291. {
  1292. struct scsi_sense_key_specific_data_descriptor
  1293. *ssks = cur_descriptor;
  1294. osi->sense_info = get_unaligned_be16(&ssks->value);
  1295. OSD_SENSE_PRINT2(
  1296. "sense_key_specific_information %u"
  1297. "sksv_cd_bpv_bp (0x%x)\n",
  1298. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1299. break;
  1300. }
  1301. case osd_sense_object_identification:
  1302. { /*FIXME: Keep first not last, Store in array*/
  1303. struct osd_sense_identification_data_descriptor
  1304. *osidd = cur_descriptor;
  1305. osi->not_initiated_command_functions =
  1306. le32_to_cpu(osidd->not_initiated_functions);
  1307. osi->completed_command_functions =
  1308. le32_to_cpu(osidd->completed_functions);
  1309. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1310. osi->obj.id = be64_to_cpu(osidd->object_id);
  1311. OSD_SENSE_PRINT2(
  1312. "object_identification pid=0x%llx oid=0x%llx\n",
  1313. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1314. OSD_SENSE_PRINT2(
  1315. "not_initiated_bits(%x) "
  1316. "completed_command_bits(%x)\n",
  1317. osi->not_initiated_command_functions,
  1318. osi->completed_command_functions);
  1319. break;
  1320. }
  1321. case osd_sense_response_integrity_check:
  1322. {
  1323. struct osd_sense_response_integrity_check_descriptor
  1324. *osricd = cur_descriptor;
  1325. const unsigned len =
  1326. sizeof(osricd->integrity_check_value);
  1327. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1328. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1329. 32, 1, key_dump, sizeof(key_dump), true);
  1330. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1331. }
  1332. case osd_sense_attribute_identification:
  1333. {
  1334. struct osd_sense_attributes_data_descriptor
  1335. *osadd = cur_descriptor;
  1336. unsigned len = min(cur_len, sense_len);
  1337. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1338. while (len >= sizeof(*pattr)) {
  1339. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1340. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1341. if (!osi->attr.attr_page) {
  1342. osi->attr.attr_page = attr_page;
  1343. osi->attr.attr_id = attr_id;
  1344. }
  1345. if (bad_attr_list && max_attr) {
  1346. bad_attr_list->attr_page = attr_page;
  1347. bad_attr_list->attr_id = attr_id;
  1348. bad_attr_list++;
  1349. max_attr--;
  1350. }
  1351. len -= sizeof(*pattr);
  1352. OSD_SENSE_PRINT2(
  1353. "osd_sense_attribute_identification"
  1354. "attr_page=0x%x attr_id=0x%x\n",
  1355. attr_page, attr_id);
  1356. }
  1357. }
  1358. /*These are not legal for OSD*/
  1359. case scsi_sense_field_replaceable_unit:
  1360. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1361. break;
  1362. case scsi_sense_stream_commands:
  1363. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1364. break;
  1365. case scsi_sense_block_commands:
  1366. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1367. break;
  1368. case scsi_sense_ata_return:
  1369. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1370. break;
  1371. default:
  1372. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1373. OSD_SENSE_PRINT2(
  1374. "scsi_sense Reserved descriptor (0x%x)",
  1375. ssd->descriptor_type);
  1376. else
  1377. OSD_SENSE_PRINT2(
  1378. "scsi_sense Vendor descriptor (0x%x)",
  1379. ssd->descriptor_type);
  1380. }
  1381. cur_descriptor += cur_len;
  1382. }
  1383. analyze:
  1384. if (!osi->key) {
  1385. /* scsi sense is Empty, the request was never issued to target
  1386. * linux return code might tell us what happened.
  1387. */
  1388. if (or->async_error == -ENOMEM)
  1389. osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
  1390. else
  1391. osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
  1392. ret = or->async_error;
  1393. } else if (osi->key <= scsi_sk_recovered_error) {
  1394. osi->osd_err_pri = 0;
  1395. ret = 0;
  1396. } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
  1397. if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
  1398. osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
  1399. ret = -EFAULT; /* caller should recover from this */
  1400. } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
  1401. osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
  1402. ret = -ENOENT;
  1403. } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
  1404. osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
  1405. ret = -EACCES;
  1406. } else {
  1407. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1408. ret = -EINVAL;
  1409. }
  1410. } else if (osi->additional_code == osd_quota_error) {
  1411. osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
  1412. ret = -ENOSPC;
  1413. } else if (_is_osd_security_code(osi->additional_code)) {
  1414. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1415. ret = -EINVAL;
  1416. } else {
  1417. osi->osd_err_pri = OSD_ERR_PRI_EIO;
  1418. ret = -EIO;
  1419. }
  1420. if (or->out.req)
  1421. osi->out_resid = or->out.req->resid_len ?: or->out.total_bytes;
  1422. if (or->in.req)
  1423. osi->in_resid = or->in.req->resid_len ?: or->in.total_bytes;
  1424. return ret;
  1425. }
  1426. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1427. /*
  1428. * Implementation of osd_sec.h API
  1429. * TODO: Move to a separate osd_sec.c file at a later stage.
  1430. */
  1431. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1432. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1433. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1434. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1435. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1436. };
  1437. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1438. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1439. };
  1440. void osd_sec_init_nosec_doall_caps(void *caps,
  1441. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1442. {
  1443. struct osd_capability *cap = caps;
  1444. u8 type;
  1445. u8 descriptor_type;
  1446. if (likely(obj->id)) {
  1447. if (unlikely(is_collection)) {
  1448. type = OSD_SEC_OBJ_COLLECTION;
  1449. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1450. OSD_SEC_OBJ_DESC_COL;
  1451. } else {
  1452. type = OSD_SEC_OBJ_USER;
  1453. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1454. }
  1455. WARN_ON(!obj->partition);
  1456. } else {
  1457. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1458. OSD_SEC_OBJ_ROOT;
  1459. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1460. }
  1461. memset(cap, 0, sizeof(*cap));
  1462. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1463. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1464. cap->h.security_method = OSD_SEC_NOSEC;
  1465. /* cap->expiration_time;
  1466. cap->AUDIT[30-10];
  1467. cap->discriminator[42-30];
  1468. cap->object_created_time; */
  1469. cap->h.object_type = type;
  1470. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1471. cap->h.object_descriptor_type = descriptor_type;
  1472. cap->od.obj_desc.policy_access_tag = 0;
  1473. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1474. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1475. }
  1476. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1477. /* FIXME: Extract version from caps pointer.
  1478. * Also Pete's target only supports caps from OSDv1 for now
  1479. */
  1480. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1481. {
  1482. bool is_ver1 = true;
  1483. /* NOTE: They start at same address */
  1484. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1485. }
  1486. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1487. {
  1488. return false;
  1489. }
  1490. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1491. {
  1492. }
  1493. void osd_sec_sign_data(void *data_integ __unused,
  1494. struct bio *bio __unused, const u8 *cap_key __unused)
  1495. {
  1496. }
  1497. /*
  1498. * Declared in osd_protocol.h
  1499. * 4.12.5 Data-In and Data-Out buffer offsets
  1500. * byte offset = mantissa * (2^(exponent+8))
  1501. * Returns the smallest allowed encoded offset that contains given @offset
  1502. * The actual encoded offset returned is @offset + *@padding.
  1503. */
  1504. osd_cdb_offset __osd_encode_offset(
  1505. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1506. {
  1507. u64 try_offset = -1, mod, align;
  1508. osd_cdb_offset be32_offset;
  1509. int shift;
  1510. *padding = 0;
  1511. if (!offset)
  1512. return 0;
  1513. for (shift = min_shift; shift < max_shift; ++shift) {
  1514. try_offset = offset >> shift;
  1515. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1516. break;
  1517. }
  1518. BUG_ON(shift == max_shift);
  1519. align = 1 << shift;
  1520. mod = offset & (align - 1);
  1521. if (mod) {
  1522. *padding = align - mod;
  1523. try_offset += 1;
  1524. }
  1525. try_offset |= ((shift - 8) & 0xf) << 28;
  1526. be32_offset = cpu_to_be32((u32)try_offset);
  1527. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1528. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1529. be32_offset, *padding);
  1530. return be32_offset;
  1531. }