osd_initiator.c 44 KB

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