osd_initiator.c 47 KB

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