osd_initiator.c 44 KB

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