osd_initiator.c 45 KB

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