osd_initiator.c 48 KB

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