osd_initiator.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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)
  362. {
  363. /*
  364. * If osd_finalize_request() was called but the request was not
  365. * executed through the block layer, then we must release BIOs.
  366. * TODO: Keep error code in or->async_error. Need to audit all
  367. * code paths.
  368. */
  369. if (unlikely(rq->bio))
  370. blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
  371. else
  372. blk_put_request(rq);
  373. }
  374. void osd_end_request(struct osd_request *or)
  375. {
  376. struct request *rq = or->request;
  377. _osd_free_seg(or, &or->set_attr);
  378. _osd_free_seg(or, &or->enc_get_attr);
  379. _osd_free_seg(or, &or->get_attr);
  380. if (rq) {
  381. if (rq->next_rq) {
  382. _put_request(rq->next_rq);
  383. rq->next_rq = NULL;
  384. }
  385. _put_request(rq);
  386. }
  387. _osd_request_free(or);
  388. }
  389. EXPORT_SYMBOL(osd_end_request);
  390. static void _set_error_resid(struct osd_request *or, struct request *req,
  391. int error)
  392. {
  393. or->async_error = error;
  394. or->req_errors = req->errors ? : error;
  395. or->sense_len = req->sense_len;
  396. if (or->out.req)
  397. or->out.residual = or->out.req->resid_len;
  398. if (or->in.req)
  399. or->in.residual = or->in.req->resid_len;
  400. }
  401. int osd_execute_request(struct osd_request *or)
  402. {
  403. int error = blk_execute_rq(or->request->q, NULL, or->request, 0);
  404. _set_error_resid(or, or->request, error);
  405. return error;
  406. }
  407. EXPORT_SYMBOL(osd_execute_request);
  408. static void osd_request_async_done(struct request *req, int error)
  409. {
  410. struct osd_request *or = req->end_io_data;
  411. _set_error_resid(or, req, error);
  412. if (req->next_rq) {
  413. __blk_put_request(req->q, req->next_rq);
  414. req->next_rq = NULL;
  415. }
  416. __blk_put_request(req->q, req);
  417. or->request = NULL;
  418. or->in.req = NULL;
  419. or->out.req = NULL;
  420. if (or->async_done)
  421. or->async_done(or, or->async_private);
  422. else
  423. osd_end_request(or);
  424. }
  425. int osd_execute_request_async(struct osd_request *or,
  426. osd_req_done_fn *done, void *private)
  427. {
  428. or->request->end_io_data = or;
  429. or->async_private = private;
  430. or->async_done = done;
  431. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  432. osd_request_async_done);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL(osd_execute_request_async);
  436. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  437. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  438. static int _osd_realloc_seg(struct osd_request *or,
  439. struct _osd_req_data_segment *seg, unsigned max_bytes)
  440. {
  441. void *buff;
  442. if (seg->alloc_size >= max_bytes)
  443. return 0;
  444. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  445. if (!buff) {
  446. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  447. seg->alloc_size);
  448. return -ENOMEM;
  449. }
  450. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  451. seg->buff = buff;
  452. seg->alloc_size = max_bytes;
  453. return 0;
  454. }
  455. static int _alloc_set_attr_list(struct osd_request *or,
  456. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  457. {
  458. unsigned total_bytes = add_bytes;
  459. for (; nelem; --nelem, ++oa)
  460. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  461. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  462. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  463. }
  464. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  465. {
  466. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  467. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  468. }
  469. static int _alloc_get_attr_list(struct osd_request *or)
  470. {
  471. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  472. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  473. }
  474. /*
  475. * Common to all OSD commands
  476. */
  477. static void _osdv1_req_encode_common(struct osd_request *or,
  478. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  479. {
  480. struct osdv1_cdb *ocdb = &or->cdb.v1;
  481. /*
  482. * For speed, the commands
  483. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  484. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  485. * are not supported here. Should pass zero and set after the call
  486. */
  487. act &= cpu_to_be16(~0x0080); /* V1 action code */
  488. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  489. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  490. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  491. ocdb->h.varlen_cdb.service_action = act;
  492. ocdb->h.partition = cpu_to_be64(obj->partition);
  493. ocdb->h.object = cpu_to_be64(obj->id);
  494. ocdb->h.v1.length = cpu_to_be64(len);
  495. ocdb->h.v1.start_address = cpu_to_be64(offset);
  496. }
  497. static void _osdv2_req_encode_common(struct osd_request *or,
  498. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  499. {
  500. struct osdv2_cdb *ocdb = &or->cdb.v2;
  501. OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
  502. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  503. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  504. ocdb->h.varlen_cdb.service_action = act;
  505. ocdb->h.partition = cpu_to_be64(obj->partition);
  506. ocdb->h.object = cpu_to_be64(obj->id);
  507. ocdb->h.v2.length = cpu_to_be64(len);
  508. ocdb->h.v2.start_address = cpu_to_be64(offset);
  509. }
  510. static void _osd_req_encode_common(struct osd_request *or,
  511. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  512. {
  513. if (osd_req_is_ver1(or))
  514. _osdv1_req_encode_common(or, act, obj, offset, len);
  515. else
  516. _osdv2_req_encode_common(or, act, obj, offset, len);
  517. }
  518. /*
  519. * Device commands
  520. */
  521. /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
  522. /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
  523. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  524. {
  525. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  526. tot_capacity);
  527. }
  528. EXPORT_SYMBOL(osd_req_format);
  529. int osd_req_list_dev_partitions(struct osd_request *or,
  530. osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
  531. {
  532. return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
  533. }
  534. EXPORT_SYMBOL(osd_req_list_dev_partitions);
  535. static void _osd_req_encode_flush(struct osd_request *or,
  536. enum osd_options_flush_scope_values op)
  537. {
  538. struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
  539. ocdb->command_specific_options = op;
  540. }
  541. void osd_req_flush_obsd(struct osd_request *or,
  542. enum osd_options_flush_scope_values op)
  543. {
  544. _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
  545. _osd_req_encode_flush(or, op);
  546. }
  547. EXPORT_SYMBOL(osd_req_flush_obsd);
  548. /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
  549. const u8 *cdb, ...); */
  550. /*TODO: void osd_req_task_management(struct osd_request *, ...); */
  551. /*
  552. * Partition commands
  553. */
  554. static void _osd_req_encode_partition(struct osd_request *or,
  555. __be16 act, osd_id partition)
  556. {
  557. struct osd_obj_id par = {
  558. .partition = partition,
  559. .id = 0,
  560. };
  561. _osd_req_encode_common(or, act, &par, 0, 0);
  562. }
  563. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  564. {
  565. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  566. }
  567. EXPORT_SYMBOL(osd_req_create_partition);
  568. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  569. {
  570. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  571. }
  572. EXPORT_SYMBOL(osd_req_remove_partition);
  573. /*TODO: void osd_req_set_partition_key(struct osd_request *,
  574. osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
  575. u8 seed[OSD_CRYPTO_SEED_SIZE]); */
  576. static int _osd_req_list_objects(struct osd_request *or,
  577. __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
  578. struct osd_obj_id_list *list, unsigned nelem)
  579. {
  580. struct request_queue *q = osd_request_queue(or->osd_dev);
  581. u64 len = nelem * sizeof(osd_id) + sizeof(*list);
  582. struct bio *bio;
  583. _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
  584. if (list->list_identifier)
  585. _osd_req_encode_olist(or, list);
  586. WARN_ON(or->in.bio);
  587. bio = bio_map_kern(q, list, len, or->alloc_flags);
  588. if (IS_ERR(bio)) {
  589. OSD_ERR("!!! Failed to allocate list_objects BIO\n");
  590. return PTR_ERR(bio);
  591. }
  592. bio->bi_rw &= ~(1 << BIO_RW);
  593. or->in.bio = bio;
  594. or->in.total_bytes = bio->bi_size;
  595. return 0;
  596. }
  597. int osd_req_list_partition_collections(struct osd_request *or,
  598. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  599. unsigned nelem)
  600. {
  601. struct osd_obj_id par = {
  602. .partition = partition,
  603. .id = 0,
  604. };
  605. return osd_req_list_collection_objects(or, &par, initial_id, list,
  606. nelem);
  607. }
  608. EXPORT_SYMBOL(osd_req_list_partition_collections);
  609. int osd_req_list_partition_objects(struct osd_request *or,
  610. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  611. unsigned nelem)
  612. {
  613. struct osd_obj_id par = {
  614. .partition = partition,
  615. .id = 0,
  616. };
  617. return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
  618. nelem);
  619. }
  620. EXPORT_SYMBOL(osd_req_list_partition_objects);
  621. void osd_req_flush_partition(struct osd_request *or,
  622. osd_id partition, enum osd_options_flush_scope_values op)
  623. {
  624. _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
  625. _osd_req_encode_flush(or, op);
  626. }
  627. EXPORT_SYMBOL(osd_req_flush_partition);
  628. /*
  629. * Collection commands
  630. */
  631. /*TODO: void osd_req_create_collection(struct osd_request *,
  632. const struct osd_obj_id *); */
  633. /*TODO: void osd_req_remove_collection(struct osd_request *,
  634. const struct osd_obj_id *); */
  635. int osd_req_list_collection_objects(struct osd_request *or,
  636. const struct osd_obj_id *obj, osd_id initial_id,
  637. struct osd_obj_id_list *list, unsigned nelem)
  638. {
  639. return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
  640. initial_id, list, nelem);
  641. }
  642. EXPORT_SYMBOL(osd_req_list_collection_objects);
  643. /*TODO: void query(struct osd_request *, ...); V2 */
  644. void osd_req_flush_collection(struct osd_request *or,
  645. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
  646. {
  647. _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
  648. _osd_req_encode_flush(or, op);
  649. }
  650. EXPORT_SYMBOL(osd_req_flush_collection);
  651. /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
  652. /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
  653. /*
  654. * Object commands
  655. */
  656. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  657. {
  658. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  659. }
  660. EXPORT_SYMBOL(osd_req_create_object);
  661. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  662. {
  663. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  664. }
  665. EXPORT_SYMBOL(osd_req_remove_object);
  666. /*TODO: void osd_req_create_multi(struct osd_request *or,
  667. struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
  668. */
  669. void osd_req_write(struct osd_request *or,
  670. const struct osd_obj_id *obj, u64 offset,
  671. struct bio *bio, u64 len)
  672. {
  673. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
  674. WARN_ON(or->out.bio || or->out.total_bytes);
  675. WARN_ON(0 == bio_rw_flagged(bio, BIO_RW));
  676. or->out.bio = bio;
  677. or->out.total_bytes = len;
  678. }
  679. EXPORT_SYMBOL(osd_req_write);
  680. int osd_req_write_kern(struct osd_request *or,
  681. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  682. {
  683. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  684. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  685. if (IS_ERR(bio))
  686. return PTR_ERR(bio);
  687. bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
  688. osd_req_write(or, obj, offset, bio, len);
  689. return 0;
  690. }
  691. EXPORT_SYMBOL(osd_req_write_kern);
  692. /*TODO: void osd_req_append(struct osd_request *,
  693. const struct osd_obj_id *, struct bio *data_out); */
  694. /*TODO: void osd_req_create_write(struct osd_request *,
  695. const struct osd_obj_id *, struct bio *data_out, u64 offset); */
  696. /*TODO: void osd_req_clear(struct osd_request *,
  697. const struct osd_obj_id *, u64 offset, u64 len); */
  698. /*TODO: void osd_req_punch(struct osd_request *,
  699. const struct osd_obj_id *, u64 offset, u64 len); V2 */
  700. void osd_req_flush_object(struct osd_request *or,
  701. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
  702. /*V2*/ u64 offset, /*V2*/ u64 len)
  703. {
  704. if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
  705. OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
  706. offset = 0;
  707. len = 0;
  708. }
  709. _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
  710. _osd_req_encode_flush(or, op);
  711. }
  712. EXPORT_SYMBOL(osd_req_flush_object);
  713. void osd_req_read(struct osd_request *or,
  714. const struct osd_obj_id *obj, u64 offset,
  715. struct bio *bio, u64 len)
  716. {
  717. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
  718. WARN_ON(or->in.bio || or->in.total_bytes);
  719. WARN_ON(1 == bio_rw_flagged(bio, BIO_RW));
  720. or->in.bio = bio;
  721. or->in.total_bytes = len;
  722. }
  723. EXPORT_SYMBOL(osd_req_read);
  724. int osd_req_read_kern(struct osd_request *or,
  725. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  726. {
  727. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  728. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  729. if (IS_ERR(bio))
  730. return PTR_ERR(bio);
  731. osd_req_read(or, obj, offset, bio, len);
  732. return 0;
  733. }
  734. EXPORT_SYMBOL(osd_req_read_kern);
  735. void osd_req_get_attributes(struct osd_request *or,
  736. const struct osd_obj_id *obj)
  737. {
  738. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  739. }
  740. EXPORT_SYMBOL(osd_req_get_attributes);
  741. void osd_req_set_attributes(struct osd_request *or,
  742. const struct osd_obj_id *obj)
  743. {
  744. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  745. }
  746. EXPORT_SYMBOL(osd_req_set_attributes);
  747. /*
  748. * Attributes List-mode
  749. */
  750. int osd_req_add_set_attr_list(struct osd_request *or,
  751. const struct osd_attr *oa, unsigned nelem)
  752. {
  753. unsigned total_bytes = or->set_attr.total_bytes;
  754. void *attr_last;
  755. int ret;
  756. if (or->attributes_mode &&
  757. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  758. WARN_ON(1);
  759. return -EINVAL;
  760. }
  761. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  762. if (!total_bytes) { /* first-time: allocate and put list header */
  763. total_bytes = _osd_req_sizeof_alist_header(or);
  764. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  765. if (ret)
  766. return ret;
  767. _osd_req_set_alist_type(or, or->set_attr.buff,
  768. OSD_ATTR_LIST_SET_RETRIEVE);
  769. }
  770. attr_last = or->set_attr.buff + total_bytes;
  771. for (; nelem; --nelem) {
  772. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  773. total_bytes += elem_size;
  774. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  775. or->set_attr.total_bytes = total_bytes - elem_size;
  776. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  777. if (ret)
  778. return ret;
  779. attr_last =
  780. or->set_attr.buff + or->set_attr.total_bytes;
  781. }
  782. _osd_req_alist_elem_encode(or, attr_last, oa);
  783. attr_last += elem_size;
  784. ++oa;
  785. }
  786. or->set_attr.total_bytes = total_bytes;
  787. return 0;
  788. }
  789. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  790. static int _req_append_segment(struct osd_request *or,
  791. unsigned padding, struct _osd_req_data_segment *seg,
  792. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  793. {
  794. void *pad_buff;
  795. int ret;
  796. if (padding) {
  797. /* check if we can just add it to last buffer */
  798. if (last_seg &&
  799. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  800. pad_buff = last_seg->buff + last_seg->total_bytes;
  801. else
  802. pad_buff = io->pad_buff;
  803. ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
  804. or->alloc_flags);
  805. if (ret)
  806. return ret;
  807. io->total_bytes += padding;
  808. }
  809. ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
  810. or->alloc_flags);
  811. if (ret)
  812. return ret;
  813. io->total_bytes += seg->total_bytes;
  814. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  815. seg->total_bytes);
  816. return 0;
  817. }
  818. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  819. {
  820. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  821. unsigned padding;
  822. int ret;
  823. if (!or->set_attr.total_bytes) {
  824. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  825. return 0;
  826. }
  827. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  828. cdbh->attrs_list.set_attr_offset =
  829. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  830. ret = _req_append_segment(or, padding, &or->set_attr,
  831. or->out.last_seg, &or->out);
  832. if (ret)
  833. return ret;
  834. or->out.last_seg = &or->set_attr;
  835. return 0;
  836. }
  837. int osd_req_add_get_attr_list(struct osd_request *or,
  838. const struct osd_attr *oa, unsigned nelem)
  839. {
  840. unsigned total_bytes = or->enc_get_attr.total_bytes;
  841. void *attr_last;
  842. int ret;
  843. if (or->attributes_mode &&
  844. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  845. WARN_ON(1);
  846. return -EINVAL;
  847. }
  848. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  849. /* first time calc data-in list header size */
  850. if (!or->get_attr.total_bytes)
  851. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  852. /* calc data-out info */
  853. if (!total_bytes) { /* first-time: allocate and put list header */
  854. unsigned max_bytes;
  855. total_bytes = _osd_req_sizeof_alist_header(or);
  856. max_bytes = total_bytes +
  857. nelem * sizeof(struct osd_attributes_list_attrid);
  858. ret = _alloc_get_attr_desc(or, max_bytes);
  859. if (ret)
  860. return ret;
  861. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  862. OSD_ATTR_LIST_GET);
  863. }
  864. attr_last = or->enc_get_attr.buff + total_bytes;
  865. for (; nelem; --nelem) {
  866. struct osd_attributes_list_attrid *attrid;
  867. const unsigned cur_size = sizeof(*attrid);
  868. total_bytes += cur_size;
  869. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  870. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  871. ret = _alloc_get_attr_desc(or,
  872. total_bytes + nelem * sizeof(*attrid));
  873. if (ret)
  874. return ret;
  875. attr_last = or->enc_get_attr.buff +
  876. or->enc_get_attr.total_bytes;
  877. }
  878. attrid = attr_last;
  879. attrid->attr_page = cpu_to_be32(oa->attr_page);
  880. attrid->attr_id = cpu_to_be32(oa->attr_id);
  881. attr_last += cur_size;
  882. /* calc data-in size */
  883. or->get_attr.total_bytes +=
  884. _osd_req_alist_elem_size(or, oa->len);
  885. ++oa;
  886. }
  887. or->enc_get_attr.total_bytes = total_bytes;
  888. OSD_DEBUG(
  889. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  890. or->get_attr.total_bytes,
  891. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  892. or->enc_get_attr.total_bytes,
  893. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  894. / sizeof(struct osd_attributes_list_attrid));
  895. return 0;
  896. }
  897. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  898. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  899. {
  900. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  901. unsigned out_padding;
  902. unsigned in_padding;
  903. int ret;
  904. if (!or->enc_get_attr.total_bytes) {
  905. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  906. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  907. return 0;
  908. }
  909. ret = _alloc_get_attr_list(or);
  910. if (ret)
  911. return ret;
  912. /* The out-going buffer info update */
  913. OSD_DEBUG("out-going\n");
  914. cdbh->attrs_list.get_attr_desc_bytes =
  915. cpu_to_be32(or->enc_get_attr.total_bytes);
  916. cdbh->attrs_list.get_attr_desc_offset =
  917. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  918. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  919. or->out.last_seg, &or->out);
  920. if (ret)
  921. return ret;
  922. or->out.last_seg = &or->enc_get_attr;
  923. /* The incoming buffer info update */
  924. OSD_DEBUG("in-coming\n");
  925. cdbh->attrs_list.get_attr_alloc_length =
  926. cpu_to_be32(or->get_attr.total_bytes);
  927. cdbh->attrs_list.get_attr_offset =
  928. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  929. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  930. &or->in);
  931. if (ret)
  932. return ret;
  933. or->in.last_seg = &or->get_attr;
  934. return 0;
  935. }
  936. int osd_req_decode_get_attr_list(struct osd_request *or,
  937. struct osd_attr *oa, int *nelem, void **iterator)
  938. {
  939. unsigned cur_bytes, returned_bytes;
  940. int n;
  941. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  942. void *cur_p;
  943. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  944. OSD_ATTR_LIST_SET_RETRIEVE)) {
  945. oa->attr_page = 0;
  946. oa->attr_id = 0;
  947. oa->val_ptr = NULL;
  948. oa->len = 0;
  949. *iterator = NULL;
  950. return 0;
  951. }
  952. if (*iterator) {
  953. BUG_ON((*iterator < or->get_attr.buff) ||
  954. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  955. cur_p = *iterator;
  956. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  957. returned_bytes = or->get_attr.total_bytes;
  958. } else { /* first time decode the list header */
  959. cur_bytes = sizeof_attr_list;
  960. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  961. sizeof_attr_list;
  962. cur_p = or->get_attr.buff + sizeof_attr_list;
  963. if (returned_bytes > or->get_attr.alloc_size) {
  964. OSD_DEBUG("target report: space was not big enough! "
  965. "Allocate=%u Needed=%u\n",
  966. or->get_attr.alloc_size,
  967. returned_bytes + sizeof_attr_list);
  968. returned_bytes =
  969. or->get_attr.alloc_size - sizeof_attr_list;
  970. }
  971. or->get_attr.total_bytes = returned_bytes;
  972. }
  973. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  974. int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
  975. returned_bytes - cur_bytes);
  976. if (inc < 0) {
  977. OSD_ERR("BAD FOOD from target. list not valid!"
  978. "c=%d r=%d n=%d\n",
  979. cur_bytes, returned_bytes, n);
  980. oa->val_ptr = NULL;
  981. cur_bytes = returned_bytes; /* break the caller loop */
  982. break;
  983. }
  984. cur_bytes += inc;
  985. cur_p += inc;
  986. ++oa;
  987. }
  988. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  989. *nelem = n;
  990. return returned_bytes - cur_bytes;
  991. }
  992. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  993. /*
  994. * Attributes Page-mode
  995. */
  996. int osd_req_add_get_attr_page(struct osd_request *or,
  997. u32 page_id, void *attar_page, unsigned max_page_len,
  998. const struct osd_attr *set_one_attr)
  999. {
  1000. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1001. if (or->attributes_mode &&
  1002. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1003. WARN_ON(1);
  1004. return -EINVAL;
  1005. }
  1006. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  1007. or->get_attr.buff = attar_page;
  1008. or->get_attr.total_bytes = max_page_len;
  1009. or->set_attr.buff = set_one_attr->val_ptr;
  1010. or->set_attr.total_bytes = set_one_attr->len;
  1011. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  1012. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1013. /* ocdb->attrs_page.get_attr_offset; */
  1014. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1015. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1016. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1017. /* ocdb->attrs_page.set_attr_offset; */
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1021. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1022. {
  1023. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1024. unsigned in_padding, out_padding;
  1025. int ret;
  1026. /* returned page */
  1027. cdbh->attrs_page.get_attr_offset =
  1028. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1029. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1030. &or->in);
  1031. if (ret)
  1032. return ret;
  1033. /* set one value */
  1034. cdbh->attrs_page.set_attr_offset =
  1035. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1036. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  1037. &or->out);
  1038. return ret;
  1039. }
  1040. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1041. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1042. {
  1043. if (is_v1)
  1044. sec_parms->v1.data_out_integrity_check_offset = offset;
  1045. else
  1046. sec_parms->v2.data_out_integrity_check_offset = offset;
  1047. }
  1048. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1049. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1050. {
  1051. if (is_v1)
  1052. sec_parms->v1.data_in_integrity_check_offset = offset;
  1053. else
  1054. sec_parms->v2.data_in_integrity_check_offset = offset;
  1055. }
  1056. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1057. bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key)
  1058. {
  1059. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1060. int ret;
  1061. if (!osd_is_sec_alldata(sec_parms))
  1062. return 0;
  1063. if (has_out) {
  1064. struct _osd_req_data_segment seg = {
  1065. .buff = &or->out_data_integ,
  1066. .total_bytes = sizeof(or->out_data_integ),
  1067. };
  1068. unsigned pad;
  1069. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1070. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1071. or->set_attr.total_bytes);
  1072. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1073. or->enc_get_attr.total_bytes);
  1074. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1075. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1076. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1077. &or->out);
  1078. if (ret)
  1079. return ret;
  1080. or->out.last_seg = NULL;
  1081. /* they are now all chained to request sign them all together */
  1082. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  1083. cap_key);
  1084. }
  1085. if (has_in) {
  1086. struct _osd_req_data_segment seg = {
  1087. .buff = &or->in_data_integ,
  1088. .total_bytes = sizeof(or->in_data_integ),
  1089. };
  1090. unsigned pad;
  1091. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1092. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1093. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1094. &or->in);
  1095. if (ret)
  1096. return ret;
  1097. or->in.last_seg = NULL;
  1098. }
  1099. return 0;
  1100. }
  1101. /*
  1102. * osd_finalize_request and helpers
  1103. */
  1104. static struct request *_make_request(struct request_queue *q, bool has_write,
  1105. struct _osd_io_info *oii, gfp_t flags)
  1106. {
  1107. if (oii->bio)
  1108. return blk_make_request(q, oii->bio, flags);
  1109. else {
  1110. struct request *req;
  1111. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1112. if (unlikely(!req))
  1113. return ERR_PTR(-ENOMEM);
  1114. return req;
  1115. }
  1116. }
  1117. static int _init_blk_request(struct osd_request *or,
  1118. bool has_in, bool has_out)
  1119. {
  1120. gfp_t flags = or->alloc_flags;
  1121. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1122. struct request_queue *q = scsi_device->request_queue;
  1123. struct request *req;
  1124. int ret;
  1125. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1126. if (IS_ERR(req)) {
  1127. ret = PTR_ERR(req);
  1128. goto out;
  1129. }
  1130. or->request = req;
  1131. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1132. req->cmd_flags |= REQ_QUIET;
  1133. req->timeout = or->timeout;
  1134. req->retries = or->retries;
  1135. req->sense = or->sense;
  1136. req->sense_len = 0;
  1137. if (has_out) {
  1138. or->out.req = req;
  1139. if (has_in) {
  1140. /* allocate bidi request */
  1141. req = _make_request(q, false, &or->in, flags);
  1142. if (IS_ERR(req)) {
  1143. OSD_DEBUG("blk_get_request for bidi failed\n");
  1144. ret = PTR_ERR(req);
  1145. goto out;
  1146. }
  1147. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1148. or->in.req = or->request->next_rq = req;
  1149. }
  1150. } else if (has_in)
  1151. or->in.req = req;
  1152. ret = 0;
  1153. out:
  1154. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1155. or, has_in, has_out, ret, or->request);
  1156. return ret;
  1157. }
  1158. int osd_finalize_request(struct osd_request *or,
  1159. u8 options, const void *cap, const u8 *cap_key)
  1160. {
  1161. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1162. bool has_in, has_out;
  1163. u64 out_data_bytes = or->out.total_bytes;
  1164. int ret;
  1165. if (options & OSD_REQ_FUA)
  1166. cdbh->options |= OSD_CDB_FUA;
  1167. if (options & OSD_REQ_DPO)
  1168. cdbh->options |= OSD_CDB_DPO;
  1169. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1170. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1171. osd_set_caps(&or->cdb, cap);
  1172. has_in = or->in.bio || or->get_attr.total_bytes;
  1173. has_out = or->out.bio || or->set_attr.total_bytes ||
  1174. or->enc_get_attr.total_bytes;
  1175. ret = _init_blk_request(or, has_in, has_out);
  1176. if (ret) {
  1177. OSD_DEBUG("_init_blk_request failed\n");
  1178. return ret;
  1179. }
  1180. or->out.pad_buff = sg_out_pad_buffer;
  1181. or->in.pad_buff = sg_in_pad_buffer;
  1182. if (!or->attributes_mode)
  1183. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1184. cdbh->command_specific_options |= or->attributes_mode;
  1185. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1186. ret = _osd_req_finalize_attr_page(or);
  1187. } else {
  1188. /* TODO: I think that for the GET_ATTR command these 2 should
  1189. * be reversed to keep them in execution order (for embeded
  1190. * targets with low memory footprint)
  1191. */
  1192. ret = _osd_req_finalize_set_attr_list(or);
  1193. if (ret) {
  1194. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1195. return ret;
  1196. }
  1197. ret = _osd_req_finalize_get_attr_list(or);
  1198. if (ret) {
  1199. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1200. return ret;
  1201. }
  1202. }
  1203. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1204. out_data_bytes, cap_key);
  1205. if (ret)
  1206. return ret;
  1207. osd_sec_sign_cdb(&or->cdb, cap_key);
  1208. or->request->cmd = or->cdb.buff;
  1209. or->request->cmd_len = _osd_req_cdb_len(or);
  1210. return 0;
  1211. }
  1212. EXPORT_SYMBOL(osd_finalize_request);
  1213. static bool _is_osd_security_code(int code)
  1214. {
  1215. return (code == osd_security_audit_value_frozen) ||
  1216. (code == osd_security_working_key_frozen) ||
  1217. (code == osd_nonce_not_unique) ||
  1218. (code == osd_nonce_timestamp_out_of_range) ||
  1219. (code == osd_invalid_dataout_buffer_integrity_check_value);
  1220. }
  1221. #define OSD_SENSE_PRINT1(fmt, a...) \
  1222. do { \
  1223. if (__cur_sense_need_output) \
  1224. OSD_ERR(fmt, ##a); \
  1225. } while (0)
  1226. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1227. int osd_req_decode_sense_full(struct osd_request *or,
  1228. struct osd_sense_info *osi, bool silent,
  1229. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1230. struct osd_attr *bad_attr_list, int max_attr)
  1231. {
  1232. int sense_len, original_sense_len;
  1233. struct osd_sense_info local_osi;
  1234. struct scsi_sense_descriptor_based *ssdb;
  1235. void *cur_descriptor;
  1236. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1237. const bool __cur_sense_need_output = false;
  1238. #else
  1239. bool __cur_sense_need_output = !silent;
  1240. #endif
  1241. int ret;
  1242. if (likely(!or->req_errors))
  1243. return 0;
  1244. osi = osi ? : &local_osi;
  1245. memset(osi, 0, sizeof(*osi));
  1246. ssdb = (typeof(ssdb))or->sense;
  1247. sense_len = or->sense_len;
  1248. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1249. OSD_ERR("Block-layer returned error(0x%x) but "
  1250. "sense_len(%u) || key(%d) is empty\n",
  1251. or->req_errors, sense_len, ssdb->sense_key);
  1252. goto analyze;
  1253. }
  1254. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1255. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1256. ssdb->response_code, sense_len);
  1257. goto analyze;
  1258. }
  1259. osi->key = ssdb->sense_key;
  1260. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1261. original_sense_len = ssdb->additional_sense_length + 8;
  1262. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1263. if (__cur_sense_need_output)
  1264. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1265. #endif
  1266. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1267. "additional_code=0x%x async_error=%d errors=0x%x\n",
  1268. osi->key, original_sense_len, sense_len,
  1269. osi->additional_code, or->async_error,
  1270. or->req_errors);
  1271. if (original_sense_len < sense_len)
  1272. sense_len = original_sense_len;
  1273. cur_descriptor = ssdb->ssd;
  1274. sense_len -= sizeof(*ssdb);
  1275. while (sense_len > 0) {
  1276. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1277. int cur_len = ssd->additional_length + 2;
  1278. sense_len -= cur_len;
  1279. if (sense_len < 0)
  1280. break; /* sense was truncated */
  1281. switch (ssd->descriptor_type) {
  1282. case scsi_sense_information:
  1283. case scsi_sense_command_specific_information:
  1284. {
  1285. struct scsi_sense_command_specific_data_descriptor
  1286. *sscd = cur_descriptor;
  1287. osi->command_info =
  1288. get_unaligned_be64(&sscd->information) ;
  1289. OSD_SENSE_PRINT2(
  1290. "command_specific_information 0x%llx \n",
  1291. _LLU(osi->command_info));
  1292. break;
  1293. }
  1294. case scsi_sense_key_specific:
  1295. {
  1296. struct scsi_sense_key_specific_data_descriptor
  1297. *ssks = cur_descriptor;
  1298. osi->sense_info = get_unaligned_be16(&ssks->value);
  1299. OSD_SENSE_PRINT2(
  1300. "sense_key_specific_information %u"
  1301. "sksv_cd_bpv_bp (0x%x)\n",
  1302. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1303. break;
  1304. }
  1305. case osd_sense_object_identification:
  1306. { /*FIXME: Keep first not last, Store in array*/
  1307. struct osd_sense_identification_data_descriptor
  1308. *osidd = cur_descriptor;
  1309. osi->not_initiated_command_functions =
  1310. le32_to_cpu(osidd->not_initiated_functions);
  1311. osi->completed_command_functions =
  1312. le32_to_cpu(osidd->completed_functions);
  1313. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1314. osi->obj.id = be64_to_cpu(osidd->object_id);
  1315. OSD_SENSE_PRINT2(
  1316. "object_identification pid=0x%llx oid=0x%llx\n",
  1317. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1318. OSD_SENSE_PRINT2(
  1319. "not_initiated_bits(%x) "
  1320. "completed_command_bits(%x)\n",
  1321. osi->not_initiated_command_functions,
  1322. osi->completed_command_functions);
  1323. break;
  1324. }
  1325. case osd_sense_response_integrity_check:
  1326. {
  1327. struct osd_sense_response_integrity_check_descriptor
  1328. *osricd = cur_descriptor;
  1329. const unsigned len =
  1330. sizeof(osricd->integrity_check_value);
  1331. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1332. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1333. 32, 1, key_dump, sizeof(key_dump), true);
  1334. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1335. }
  1336. case osd_sense_attribute_identification:
  1337. {
  1338. struct osd_sense_attributes_data_descriptor
  1339. *osadd = cur_descriptor;
  1340. unsigned len = min(cur_len, sense_len);
  1341. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1342. while (len >= sizeof(*pattr)) {
  1343. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1344. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1345. if (!osi->attr.attr_page) {
  1346. osi->attr.attr_page = attr_page;
  1347. osi->attr.attr_id = attr_id;
  1348. }
  1349. if (bad_attr_list && max_attr) {
  1350. bad_attr_list->attr_page = attr_page;
  1351. bad_attr_list->attr_id = attr_id;
  1352. bad_attr_list++;
  1353. max_attr--;
  1354. }
  1355. len -= sizeof(*pattr);
  1356. OSD_SENSE_PRINT2(
  1357. "osd_sense_attribute_identification"
  1358. "attr_page=0x%x attr_id=0x%x\n",
  1359. attr_page, attr_id);
  1360. }
  1361. }
  1362. /*These are not legal for OSD*/
  1363. case scsi_sense_field_replaceable_unit:
  1364. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1365. break;
  1366. case scsi_sense_stream_commands:
  1367. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1368. break;
  1369. case scsi_sense_block_commands:
  1370. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1371. break;
  1372. case scsi_sense_ata_return:
  1373. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1374. break;
  1375. default:
  1376. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1377. OSD_SENSE_PRINT2(
  1378. "scsi_sense Reserved descriptor (0x%x)",
  1379. ssd->descriptor_type);
  1380. else
  1381. OSD_SENSE_PRINT2(
  1382. "scsi_sense Vendor descriptor (0x%x)",
  1383. ssd->descriptor_type);
  1384. }
  1385. cur_descriptor += cur_len;
  1386. }
  1387. analyze:
  1388. if (!osi->key) {
  1389. /* scsi sense is Empty, the request was never issued to target
  1390. * linux return code might tell us what happened.
  1391. */
  1392. if (or->async_error == -ENOMEM)
  1393. osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
  1394. else
  1395. osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
  1396. ret = or->async_error;
  1397. } else if (osi->key <= scsi_sk_recovered_error) {
  1398. osi->osd_err_pri = 0;
  1399. ret = 0;
  1400. } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
  1401. if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
  1402. osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
  1403. ret = -EFAULT; /* caller should recover from this */
  1404. } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
  1405. osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
  1406. ret = -ENOENT;
  1407. } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
  1408. osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
  1409. ret = -EACCES;
  1410. } else {
  1411. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1412. ret = -EINVAL;
  1413. }
  1414. } else if (osi->additional_code == osd_quota_error) {
  1415. osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
  1416. ret = -ENOSPC;
  1417. } else if (_is_osd_security_code(osi->additional_code)) {
  1418. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1419. ret = -EINVAL;
  1420. } else {
  1421. osi->osd_err_pri = OSD_ERR_PRI_EIO;
  1422. ret = -EIO;
  1423. }
  1424. if (!or->out.residual)
  1425. or->out.residual = or->out.total_bytes;
  1426. if (!or->in.residual)
  1427. or->in.residual = or->in.total_bytes;
  1428. return ret;
  1429. }
  1430. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1431. /*
  1432. * Implementation of osd_sec.h API
  1433. * TODO: Move to a separate osd_sec.c file at a later stage.
  1434. */
  1435. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1436. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1437. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1438. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1439. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1440. };
  1441. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1442. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1443. };
  1444. void osd_sec_init_nosec_doall_caps(void *caps,
  1445. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1446. {
  1447. struct osd_capability *cap = caps;
  1448. u8 type;
  1449. u8 descriptor_type;
  1450. if (likely(obj->id)) {
  1451. if (unlikely(is_collection)) {
  1452. type = OSD_SEC_OBJ_COLLECTION;
  1453. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1454. OSD_SEC_OBJ_DESC_COL;
  1455. } else {
  1456. type = OSD_SEC_OBJ_USER;
  1457. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1458. }
  1459. WARN_ON(!obj->partition);
  1460. } else {
  1461. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1462. OSD_SEC_OBJ_ROOT;
  1463. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1464. }
  1465. memset(cap, 0, sizeof(*cap));
  1466. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1467. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1468. cap->h.security_method = OSD_SEC_NOSEC;
  1469. /* cap->expiration_time;
  1470. cap->AUDIT[30-10];
  1471. cap->discriminator[42-30];
  1472. cap->object_created_time; */
  1473. cap->h.object_type = type;
  1474. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1475. cap->h.object_descriptor_type = descriptor_type;
  1476. cap->od.obj_desc.policy_access_tag = 0;
  1477. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1478. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1479. }
  1480. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1481. /* FIXME: Extract version from caps pointer.
  1482. * Also Pete's target only supports caps from OSDv1 for now
  1483. */
  1484. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1485. {
  1486. bool is_ver1 = true;
  1487. /* NOTE: They start at same address */
  1488. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1489. }
  1490. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1491. {
  1492. return false;
  1493. }
  1494. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1495. {
  1496. }
  1497. void osd_sec_sign_data(void *data_integ __unused,
  1498. struct bio *bio __unused, const u8 *cap_key __unused)
  1499. {
  1500. }
  1501. /*
  1502. * Declared in osd_protocol.h
  1503. * 4.12.5 Data-In and Data-Out buffer offsets
  1504. * byte offset = mantissa * (2^(exponent+8))
  1505. * Returns the smallest allowed encoded offset that contains given @offset
  1506. * The actual encoded offset returned is @offset + *@padding.
  1507. */
  1508. osd_cdb_offset __osd_encode_offset(
  1509. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1510. {
  1511. u64 try_offset = -1, mod, align;
  1512. osd_cdb_offset be32_offset;
  1513. int shift;
  1514. *padding = 0;
  1515. if (!offset)
  1516. return 0;
  1517. for (shift = min_shift; shift < max_shift; ++shift) {
  1518. try_offset = offset >> shift;
  1519. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1520. break;
  1521. }
  1522. BUG_ON(shift == max_shift);
  1523. align = 1 << shift;
  1524. mod = offset & (align - 1);
  1525. if (mod) {
  1526. *padding = align - mod;
  1527. try_offset += 1;
  1528. }
  1529. try_offset |= ((shift - 8) & 0xf) << 28;
  1530. be32_offset = cpu_to_be32((u32)try_offset);
  1531. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1532. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1533. be32_offset, *padding);
  1534. return be32_offset;
  1535. }