blob: f61ab84ad20bbffa316752692d8bdbeb114bb6f1 [file] [log] [blame]
Boaz Harrosh02941a52009-01-25 16:55:30 +02001/*
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
42#include <scsi/osd_initiator.h>
43#include <scsi/osd_sec.h>
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020044#include <scsi/osd_attributes.h>
Boaz Harrosh98f3aea2009-01-25 17:15:16 +020045#include <scsi/osd_sense.h>
46
Boaz Harrosh02941a52009-01-25 16:55:30 +020047#include <scsi/scsi_device.h>
48
49#include "osd_debug.h"
50
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020051#ifndef __unused
52# define __unused __attribute__((unused))
53#endif
54
Boaz Harrosh02941a52009-01-25 16:55:30 +020055enum { OSD_REQ_RETRIES = 1 };
56
57MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
58MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
59MODULE_LICENSE("GPL");
60
61static inline void build_test(void)
62{
63 /* structures were not packed */
64 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
Boaz Harroshc6572c92009-01-25 17:09:40 +020065 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +020066 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
67}
68
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020069static const char *_osd_ver_desc(struct osd_request *or)
70{
71 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
72}
73
74#define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
75
76static int _osd_print_system_info(struct osd_dev *od, void *caps)
77{
78 struct osd_request *or;
79 struct osd_attr get_attrs[] = {
80 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
81 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
82 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
83 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
84 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
85 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
86 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
87 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
88 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
89 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
90 /* IBM-OSD-SIM Has a bug with this one put it last */
91 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
92 };
93 void *iter = NULL, *pFirst;
94 int nelem = ARRAY_SIZE(get_attrs), a = 0;
95 int ret;
96
97 or = osd_start_request(od, GFP_KERNEL);
98 if (!or)
99 return -ENOMEM;
100
101 /* get attrs */
102 osd_req_get_attributes(or, &osd_root_object);
103 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
104
105 ret = osd_finalize_request(or, 0, caps, NULL);
106 if (ret)
107 goto out;
108
109 ret = osd_execute_request(or);
110 if (ret) {
111 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
112 goto out;
113 }
114
115 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
116
117 OSD_INFO("Detected %s device\n",
118 _osd_ver_desc(or));
119
120 pFirst = get_attrs[a++].val_ptr;
121 OSD_INFO("OSD_ATTR_RI_VENDOR_IDENTIFICATION [%s]\n",
122 (char *)pFirst);
123
124 pFirst = get_attrs[a++].val_ptr;
125 OSD_INFO("OSD_ATTR_RI_PRODUCT_IDENTIFICATION [%s]\n",
126 (char *)pFirst);
127
128 pFirst = get_attrs[a++].val_ptr;
129 OSD_INFO("OSD_ATTR_RI_PRODUCT_MODEL [%s]\n",
130 (char *)pFirst);
131
132 pFirst = get_attrs[a++].val_ptr;
133 OSD_INFO("OSD_ATTR_RI_PRODUCT_REVISION_LEVEL [%u]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200134 pFirst ? get_unaligned_be32(pFirst) : ~0U);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200135
136 pFirst = get_attrs[a++].val_ptr;
137 OSD_INFO("OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER [%s]\n",
138 (char *)pFirst);
139
140 pFirst = get_attrs[a].val_ptr;
141 OSD_INFO("OSD_ATTR_RI_OSD_NAME [%s]\n", (char *)pFirst);
142 a++;
143
144 pFirst = get_attrs[a++].val_ptr;
145 OSD_INFO("OSD_ATTR_RI_TOTAL_CAPACITY [0x%llx]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200146 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200147
148 pFirst = get_attrs[a++].val_ptr;
149 OSD_INFO("OSD_ATTR_RI_USED_CAPACITY [0x%llx]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200150 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200151
152 pFirst = get_attrs[a++].val_ptr;
153 OSD_INFO("OSD_ATTR_RI_NUMBER_OF_PARTITIONS [%llu]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200154 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
155
156 if (a >= nelem)
157 goto out;
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200158
159 /* FIXME: Where are the time utilities */
160 pFirst = get_attrs[a++].val_ptr;
161 OSD_INFO("OSD_ATTR_RI_CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
162 ((char *)pFirst)[0], ((char *)pFirst)[1],
163 ((char *)pFirst)[2], ((char *)pFirst)[3],
164 ((char *)pFirst)[4], ((char *)pFirst)[5]);
165
166 if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
167 unsigned len = get_attrs[a].len;
168 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
169
170 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
171 sid_dump, sizeof(sid_dump), true);
172 OSD_INFO("OSD_ATTR_RI_OSD_SYSTEM_ID(%d) [%s]\n", len, sid_dump);
173 a++;
174 }
175out:
176 osd_end_request(or);
177 return ret;
178}
179
180int osd_auto_detect_ver(struct osd_dev *od, void *caps)
181{
182 int ret;
183
184 /* Auto-detect the osd version */
185 ret = _osd_print_system_info(od, caps);
186 if (ret) {
187 osd_dev_set_ver(od, OSD_VER1);
188 OSD_DEBUG("converting to OSD1\n");
189 ret = _osd_print_system_info(od, caps);
190 }
191
192 return ret;
193}
194EXPORT_SYMBOL(osd_auto_detect_ver);
195
Boaz Harrosh02941a52009-01-25 16:55:30 +0200196static unsigned _osd_req_cdb_len(struct osd_request *or)
197{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200198 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200199}
200
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200201static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
202{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200203 return osd_req_is_ver1(or) ?
204 osdv1_attr_list_elem_size(len) :
205 osdv2_attr_list_elem_size(len);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200206}
207
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300208static void _osd_req_alist_elem_encode(struct osd_request *or,
209 void *attr_last, const struct osd_attr *oa)
210{
211 if (osd_req_is_ver1(or)) {
212 struct osdv1_attributes_list_element *attr = attr_last;
213
214 attr->attr_page = cpu_to_be32(oa->attr_page);
215 attr->attr_id = cpu_to_be32(oa->attr_id);
216 attr->attr_bytes = cpu_to_be16(oa->len);
217 memcpy(attr->attr_val, oa->val_ptr, oa->len);
218 } else {
219 struct osdv2_attributes_list_element *attr = attr_last;
220
221 attr->attr_page = cpu_to_be32(oa->attr_page);
222 attr->attr_id = cpu_to_be32(oa->attr_id);
223 attr->attr_bytes = cpu_to_be16(oa->len);
224 memcpy(attr->attr_val, oa->val_ptr, oa->len);
225 }
226}
227
228static int _osd_req_alist_elem_decode(struct osd_request *or,
229 void *cur_p, struct osd_attr *oa, unsigned max_bytes)
230{
231 unsigned inc;
232 if (osd_req_is_ver1(or)) {
233 struct osdv1_attributes_list_element *attr = cur_p;
234
235 if (max_bytes < sizeof(*attr))
236 return -1;
237
238 oa->len = be16_to_cpu(attr->attr_bytes);
239 inc = _osd_req_alist_elem_size(or, oa->len);
240 if (inc > max_bytes)
241 return -1;
242
243 oa->attr_page = be32_to_cpu(attr->attr_page);
244 oa->attr_id = be32_to_cpu(attr->attr_id);
245
246 /* OSD1: On empty attributes we return a pointer to 2 bytes
247 * of zeros. This keeps similar behaviour with OSD2.
248 * (See below)
249 */
250 oa->val_ptr = likely(oa->len) ? attr->attr_val :
251 (u8 *)&attr->attr_bytes;
252 } else {
253 struct osdv2_attributes_list_element *attr = cur_p;
254
255 if (max_bytes < sizeof(*attr))
256 return -1;
257
258 oa->len = be16_to_cpu(attr->attr_bytes);
259 inc = _osd_req_alist_elem_size(or, oa->len);
260 if (inc > max_bytes)
261 return -1;
262
263 oa->attr_page = be32_to_cpu(attr->attr_page);
264 oa->attr_id = be32_to_cpu(attr->attr_id);
265
266 oa->val_ptr = attr->attr_val;
267 }
268 return inc;
269}
270
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200271static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
272{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200273 return osd_req_is_ver1(or) ?
274 osdv1_list_size(list_head) :
275 osdv2_list_size(list_head);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200276}
277
278static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
279{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200280 return osd_req_is_ver1(or) ?
281 sizeof(struct osdv1_attributes_list_header) :
282 sizeof(struct osdv2_attributes_list_header);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200283}
284
285static void _osd_req_set_alist_type(struct osd_request *or,
286 void *list, int list_type)
287{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200288 if (osd_req_is_ver1(or)) {
289 struct osdv1_attributes_list_header *attr_list = list;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200290
Boaz Harroshc6572c92009-01-25 17:09:40 +0200291 memset(attr_list, 0, sizeof(*attr_list));
292 attr_list->type = list_type;
293 } else {
294 struct osdv2_attributes_list_header *attr_list = list;
295
296 memset(attr_list, 0, sizeof(*attr_list));
297 attr_list->type = list_type;
298 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200299}
300
301static bool _osd_req_is_alist_type(struct osd_request *or,
302 void *list, int list_type)
303{
304 if (!list)
305 return false;
306
Boaz Harroshc6572c92009-01-25 17:09:40 +0200307 if (osd_req_is_ver1(or)) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200308 struct osdv1_attributes_list_header *attr_list = list;
309
310 return attr_list->type == list_type;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200311 } else {
312 struct osdv2_attributes_list_header *attr_list = list;
313
314 return attr_list->type == list_type;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200315 }
316}
317
Boaz Harrosh3e086132009-01-25 17:05:07 +0200318/* This is for List-objects not Attributes-Lists */
319static void _osd_req_encode_olist(struct osd_request *or,
320 struct osd_obj_id_list *list)
321{
322 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
323
Boaz Harroshc6572c92009-01-25 17:09:40 +0200324 if (osd_req_is_ver1(or)) {
325 cdbh->v1.list_identifier = list->list_identifier;
326 cdbh->v1.start_address = list->continuation_id;
327 } else {
328 cdbh->v2.list_identifier = list->list_identifier;
329 cdbh->v2.start_address = list->continuation_id;
330 }
Boaz Harrosh3e086132009-01-25 17:05:07 +0200331}
332
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200333static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
334 u64 offset, unsigned *padding)
335{
336 return __osd_encode_offset(offset, padding,
Boaz Harroshc6572c92009-01-25 17:09:40 +0200337 osd_req_is_ver1(or) ?
338 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
339 OSD_OFFSET_MAX_SHIFT);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200340}
341
Boaz Harrosh345c4352009-01-25 17:03:07 +0200342static struct osd_security_parameters *
343_osd_req_sec_params(struct osd_request *or)
344{
345 struct osd_cdb *ocdb = &or->cdb;
346
Boaz Harroshc6572c92009-01-25 17:09:40 +0200347 if (osd_req_is_ver1(or))
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300348 return (struct osd_security_parameters *)&ocdb->v1.sec_params;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200349 else
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300350 return (struct osd_security_parameters *)&ocdb->v2.sec_params;
Boaz Harrosh345c4352009-01-25 17:03:07 +0200351}
352
Boaz Harrosh02941a52009-01-25 16:55:30 +0200353void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
354{
355 memset(osdd, 0, sizeof(*osdd));
356 osdd->scsi_device = scsi_device;
357 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200358#ifdef OSD_VER1_SUPPORT
359 osdd->version = OSD_VER2;
360#endif
Boaz Harrosh02941a52009-01-25 16:55:30 +0200361 /* TODO: Allocate pools for osd_request attributes ... */
362}
363EXPORT_SYMBOL(osd_dev_init);
364
365void osd_dev_fini(struct osd_dev *osdd)
366{
367 /* TODO: De-allocate pools */
368
369 osdd->scsi_device = NULL;
370}
371EXPORT_SYMBOL(osd_dev_fini);
372
373static struct osd_request *_osd_request_alloc(gfp_t gfp)
374{
375 struct osd_request *or;
376
377 /* TODO: Use mempool with one saved request */
378 or = kzalloc(sizeof(*or), gfp);
379 return or;
380}
381
382static void _osd_request_free(struct osd_request *or)
383{
384 kfree(or);
385}
386
387struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
388{
389 struct osd_request *or;
390
391 or = _osd_request_alloc(gfp);
392 if (!or)
393 return NULL;
394
395 or->osd_dev = dev;
396 or->alloc_flags = gfp;
397 or->timeout = dev->def_timeout;
398 or->retries = OSD_REQ_RETRIES;
399
400 return or;
401}
402EXPORT_SYMBOL(osd_start_request);
403
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200404static void _osd_free_seg(struct osd_request *or __unused,
405 struct _osd_req_data_segment *seg)
406{
407 if (!seg->buff || !seg->alloc_size)
408 return;
409
410 kfree(seg->buff);
411 seg->buff = NULL;
412 seg->alloc_size = 0;
413}
414
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300415static void _put_request(struct request *rq , bool is_async)
416{
417 if (is_async) {
418 WARN_ON(rq->bio);
419 __blk_put_request(rq->q, rq);
420 } else {
421 /*
422 * If osd_finalize_request() was called but the request was not
423 * executed through the block layer, then we must release BIOs.
424 * TODO: Keep error code in or->async_error. Need to audit all
425 * code paths.
426 */
427 if (unlikely(rq->bio))
428 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
429 else
430 blk_put_request(rq);
431 }
432}
433
Boaz Harrosh02941a52009-01-25 16:55:30 +0200434void osd_end_request(struct osd_request *or)
435{
436 struct request *rq = or->request;
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300437 /* IMPORTANT: make sure this agrees with osd_execute_request_async */
438 bool is_async = (or->request->end_io_data == or);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200439
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200440 _osd_free_seg(or, &or->set_attr);
441 _osd_free_seg(or, &or->enc_get_attr);
442 _osd_free_seg(or, &or->get_attr);
443
Boaz Harrosh02941a52009-01-25 16:55:30 +0200444 if (rq) {
445 if (rq->next_rq) {
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300446 _put_request(rq->next_rq, is_async);
447 rq->next_rq = NULL;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200448 }
449
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300450 _put_request(rq, is_async);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200451 }
452 _osd_request_free(or);
453}
454EXPORT_SYMBOL(osd_end_request);
455
456int osd_execute_request(struct osd_request *or)
457{
458 return blk_execute_rq(or->request->q, NULL, or->request, 0);
459}
460EXPORT_SYMBOL(osd_execute_request);
461
462static void osd_request_async_done(struct request *req, int error)
463{
464 struct osd_request *or = req->end_io_data;
465
466 or->async_error = error;
467
468 if (error)
469 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
470
471 if (or->async_done)
472 or->async_done(or, or->async_private);
473 else
474 osd_end_request(or);
475}
476
477int osd_execute_request_async(struct osd_request *or,
478 osd_req_done_fn *done, void *private)
479{
480 or->request->end_io_data = or;
481 or->async_private = private;
482 or->async_done = done;
483
484 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
485 osd_request_async_done);
486 return 0;
487}
488EXPORT_SYMBOL(osd_execute_request_async);
489
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200490u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
491u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
492
493static int _osd_realloc_seg(struct osd_request *or,
494 struct _osd_req_data_segment *seg, unsigned max_bytes)
495{
496 void *buff;
497
498 if (seg->alloc_size >= max_bytes)
499 return 0;
500
501 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
502 if (!buff) {
503 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
504 seg->alloc_size);
505 return -ENOMEM;
506 }
507
508 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
509 seg->buff = buff;
510 seg->alloc_size = max_bytes;
511 return 0;
512}
513
514static int _alloc_set_attr_list(struct osd_request *or,
515 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
516{
517 unsigned total_bytes = add_bytes;
518
519 for (; nelem; --nelem, ++oa)
520 total_bytes += _osd_req_alist_elem_size(or, oa->len);
521
522 OSD_DEBUG("total_bytes=%d\n", total_bytes);
523 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
524}
525
526static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
527{
528 OSD_DEBUG("total_bytes=%d\n", max_bytes);
529 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
530}
531
532static int _alloc_get_attr_list(struct osd_request *or)
533{
534 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
535 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
536}
537
Boaz Harrosh02941a52009-01-25 16:55:30 +0200538/*
539 * Common to all OSD commands
540 */
541
542static void _osdv1_req_encode_common(struct osd_request *or,
543 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
544{
545 struct osdv1_cdb *ocdb = &or->cdb.v1;
546
547 /*
548 * For speed, the commands
549 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
550 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
551 * are not supported here. Should pass zero and set after the call
552 */
553 act &= cpu_to_be16(~0x0080); /* V1 action code */
554
555 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
556
557 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
558 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
559 ocdb->h.varlen_cdb.service_action = act;
560
561 ocdb->h.partition = cpu_to_be64(obj->partition);
562 ocdb->h.object = cpu_to_be64(obj->id);
563 ocdb->h.v1.length = cpu_to_be64(len);
564 ocdb->h.v1.start_address = cpu_to_be64(offset);
565}
566
Boaz Harroshc6572c92009-01-25 17:09:40 +0200567static void _osdv2_req_encode_common(struct osd_request *or,
568 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
569{
570 struct osdv2_cdb *ocdb = &or->cdb.v2;
571
572 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
573
574 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
575 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
576 ocdb->h.varlen_cdb.service_action = act;
577
578 ocdb->h.partition = cpu_to_be64(obj->partition);
579 ocdb->h.object = cpu_to_be64(obj->id);
580 ocdb->h.v2.length = cpu_to_be64(len);
581 ocdb->h.v2.start_address = cpu_to_be64(offset);
582}
583
Boaz Harrosh02941a52009-01-25 16:55:30 +0200584static void _osd_req_encode_common(struct osd_request *or,
585 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
586{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200587 if (osd_req_is_ver1(or))
588 _osdv1_req_encode_common(or, act, obj, offset, len);
589 else
590 _osdv2_req_encode_common(or, act, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200591}
592
593/*
594 * Device commands
595 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200596/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
597/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
598
Boaz Harrosh02941a52009-01-25 16:55:30 +0200599void osd_req_format(struct osd_request *or, u64 tot_capacity)
600{
601 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
602 tot_capacity);
603}
604EXPORT_SYMBOL(osd_req_format);
605
Boaz Harrosh3e086132009-01-25 17:05:07 +0200606int osd_req_list_dev_partitions(struct osd_request *or,
607 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
608{
609 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
610}
611EXPORT_SYMBOL(osd_req_list_dev_partitions);
612
613static void _osd_req_encode_flush(struct osd_request *or,
614 enum osd_options_flush_scope_values op)
615{
616 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
617
618 ocdb->command_specific_options = op;
619}
620
621void osd_req_flush_obsd(struct osd_request *or,
622 enum osd_options_flush_scope_values op)
623{
624 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
625 _osd_req_encode_flush(or, op);
626}
627EXPORT_SYMBOL(osd_req_flush_obsd);
628
Boaz Harroshae30c992009-01-25 17:07:14 +0200629/*TODO: void osd_req_perform_scsi_command(struct osd_request *,
630 const u8 *cdb, ...); */
631/*TODO: void osd_req_task_management(struct osd_request *, ...); */
632
Boaz Harrosh02941a52009-01-25 16:55:30 +0200633/*
634 * Partition commands
635 */
636static void _osd_req_encode_partition(struct osd_request *or,
637 __be16 act, osd_id partition)
638{
639 struct osd_obj_id par = {
640 .partition = partition,
641 .id = 0,
642 };
643
644 _osd_req_encode_common(or, act, &par, 0, 0);
645}
646
647void osd_req_create_partition(struct osd_request *or, osd_id partition)
648{
649 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
650}
651EXPORT_SYMBOL(osd_req_create_partition);
652
653void osd_req_remove_partition(struct osd_request *or, osd_id partition)
654{
655 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
656}
657EXPORT_SYMBOL(osd_req_remove_partition);
658
Boaz Harroshae30c992009-01-25 17:07:14 +0200659/*TODO: void osd_req_set_partition_key(struct osd_request *,
660 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
661 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
662
Boaz Harrosh3e086132009-01-25 17:05:07 +0200663static int _osd_req_list_objects(struct osd_request *or,
664 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
665 struct osd_obj_id_list *list, unsigned nelem)
666{
667 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
668 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
669 struct bio *bio;
670
671 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
672
673 if (list->list_identifier)
674 _osd_req_encode_olist(or, list);
675
676 WARN_ON(or->in.bio);
677 bio = bio_map_kern(q, list, len, or->alloc_flags);
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300678 if (IS_ERR(bio)) {
Boaz Harrosh3e086132009-01-25 17:05:07 +0200679 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300680 return PTR_ERR(bio);
Boaz Harrosh3e086132009-01-25 17:05:07 +0200681 }
682
683 bio->bi_rw &= ~(1 << BIO_RW);
684 or->in.bio = bio;
685 or->in.total_bytes = bio->bi_size;
686 return 0;
687}
688
689int osd_req_list_partition_collections(struct osd_request *or,
690 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
691 unsigned nelem)
692{
693 struct osd_obj_id par = {
694 .partition = partition,
695 .id = 0,
696 };
697
698 return osd_req_list_collection_objects(or, &par, initial_id, list,
699 nelem);
700}
701EXPORT_SYMBOL(osd_req_list_partition_collections);
702
703int osd_req_list_partition_objects(struct osd_request *or,
704 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
705 unsigned nelem)
706{
707 struct osd_obj_id par = {
708 .partition = partition,
709 .id = 0,
710 };
711
712 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
713 nelem);
714}
715EXPORT_SYMBOL(osd_req_list_partition_objects);
716
717void osd_req_flush_partition(struct osd_request *or,
718 osd_id partition, enum osd_options_flush_scope_values op)
719{
720 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
721 _osd_req_encode_flush(or, op);
722}
723EXPORT_SYMBOL(osd_req_flush_partition);
724
725/*
726 * Collection commands
727 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200728/*TODO: void osd_req_create_collection(struct osd_request *,
729 const struct osd_obj_id *); */
730/*TODO: void osd_req_remove_collection(struct osd_request *,
731 const struct osd_obj_id *); */
732
Boaz Harrosh3e086132009-01-25 17:05:07 +0200733int osd_req_list_collection_objects(struct osd_request *or,
734 const struct osd_obj_id *obj, osd_id initial_id,
735 struct osd_obj_id_list *list, unsigned nelem)
736{
737 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
738 initial_id, list, nelem);
739}
740EXPORT_SYMBOL(osd_req_list_collection_objects);
741
Boaz Harroshae30c992009-01-25 17:07:14 +0200742/*TODO: void query(struct osd_request *, ...); V2 */
743
Boaz Harrosh3e086132009-01-25 17:05:07 +0200744void osd_req_flush_collection(struct osd_request *or,
745 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
746{
747 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
748 _osd_req_encode_flush(or, op);
749}
750EXPORT_SYMBOL(osd_req_flush_collection);
751
Boaz Harroshae30c992009-01-25 17:07:14 +0200752/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
753/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
754
Boaz Harrosh02941a52009-01-25 16:55:30 +0200755/*
756 * Object commands
757 */
758void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
759{
760 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
761}
762EXPORT_SYMBOL(osd_req_create_object);
763
764void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
765{
766 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
767}
768EXPORT_SYMBOL(osd_req_remove_object);
769
Boaz Harroshae30c992009-01-25 17:07:14 +0200770
771/*TODO: void osd_req_create_multi(struct osd_request *or,
772 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
773*/
774
Boaz Harrosh02941a52009-01-25 16:55:30 +0200775void osd_req_write(struct osd_request *or,
776 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
777{
778 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
779 WARN_ON(or->out.bio || or->out.total_bytes);
780 bio->bi_rw |= (1 << BIO_RW);
781 or->out.bio = bio;
782 or->out.total_bytes = bio->bi_size;
783}
784EXPORT_SYMBOL(osd_req_write);
785
Boaz Harroshae30c992009-01-25 17:07:14 +0200786/*TODO: void osd_req_append(struct osd_request *,
787 const struct osd_obj_id *, struct bio *data_out); */
788/*TODO: void osd_req_create_write(struct osd_request *,
789 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
790/*TODO: void osd_req_clear(struct osd_request *,
791 const struct osd_obj_id *, u64 offset, u64 len); */
792/*TODO: void osd_req_punch(struct osd_request *,
793 const struct osd_obj_id *, u64 offset, u64 len); V2 */
794
Boaz Harrosh3e086132009-01-25 17:05:07 +0200795void osd_req_flush_object(struct osd_request *or,
796 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
797 /*V2*/ u64 offset, /*V2*/ u64 len)
798{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200799 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
800 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
801 offset = 0;
802 len = 0;
803 }
804
Boaz Harrosh3e086132009-01-25 17:05:07 +0200805 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
806 _osd_req_encode_flush(or, op);
807}
808EXPORT_SYMBOL(osd_req_flush_object);
809
Boaz Harrosh02941a52009-01-25 16:55:30 +0200810void osd_req_read(struct osd_request *or,
811 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
812{
813 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
814 WARN_ON(or->in.bio || or->in.total_bytes);
815 bio->bi_rw &= ~(1 << BIO_RW);
816 or->in.bio = bio;
817 or->in.total_bytes = bio->bi_size;
818}
819EXPORT_SYMBOL(osd_req_read);
820
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200821void osd_req_get_attributes(struct osd_request *or,
822 const struct osd_obj_id *obj)
823{
824 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
825}
826EXPORT_SYMBOL(osd_req_get_attributes);
827
828void osd_req_set_attributes(struct osd_request *or,
829 const struct osd_obj_id *obj)
830{
831 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
832}
833EXPORT_SYMBOL(osd_req_set_attributes);
834
835/*
836 * Attributes List-mode
837 */
838
839int osd_req_add_set_attr_list(struct osd_request *or,
840 const struct osd_attr *oa, unsigned nelem)
841{
842 unsigned total_bytes = or->set_attr.total_bytes;
843 void *attr_last;
844 int ret;
845
846 if (or->attributes_mode &&
847 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
848 WARN_ON(1);
849 return -EINVAL;
850 }
851 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
852
853 if (!total_bytes) { /* first-time: allocate and put list header */
854 total_bytes = _osd_req_sizeof_alist_header(or);
855 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
856 if (ret)
857 return ret;
858 _osd_req_set_alist_type(or, or->set_attr.buff,
859 OSD_ATTR_LIST_SET_RETRIEVE);
860 }
861 attr_last = or->set_attr.buff + total_bytes;
862
863 for (; nelem; --nelem) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200864 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
865
866 total_bytes += elem_size;
867 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
868 or->set_attr.total_bytes = total_bytes - elem_size;
869 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
870 if (ret)
871 return ret;
872 attr_last =
873 or->set_attr.buff + or->set_attr.total_bytes;
874 }
875
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300876 _osd_req_alist_elem_encode(or, attr_last, oa);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200877
878 attr_last += elem_size;
879 ++oa;
880 }
881
882 or->set_attr.total_bytes = total_bytes;
883 return 0;
884}
885EXPORT_SYMBOL(osd_req_add_set_attr_list);
886
887static int _append_map_kern(struct request *req,
888 void *buff, unsigned len, gfp_t flags)
889{
890 struct bio *bio;
891 int ret;
892
893 bio = bio_map_kern(req->q, buff, len, flags);
894 if (IS_ERR(bio)) {
895 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
896 PTR_ERR(bio));
897 return PTR_ERR(bio);
898 }
899 ret = blk_rq_append_bio(req->q, req, bio);
900 if (ret) {
901 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
902 bio_put(bio);
903 }
904 return ret;
905}
906
907static int _req_append_segment(struct osd_request *or,
908 unsigned padding, struct _osd_req_data_segment *seg,
909 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
910{
911 void *pad_buff;
912 int ret;
913
914 if (padding) {
915 /* check if we can just add it to last buffer */
916 if (last_seg &&
917 (padding <= last_seg->alloc_size - last_seg->total_bytes))
918 pad_buff = last_seg->buff + last_seg->total_bytes;
919 else
920 pad_buff = io->pad_buff;
921
922 ret = _append_map_kern(io->req, pad_buff, padding,
923 or->alloc_flags);
924 if (ret)
925 return ret;
926 io->total_bytes += padding;
927 }
928
929 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
930 or->alloc_flags);
931 if (ret)
932 return ret;
933
934 io->total_bytes += seg->total_bytes;
935 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
936 seg->total_bytes);
937 return 0;
938}
939
940static int _osd_req_finalize_set_attr_list(struct osd_request *or)
941{
942 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
943 unsigned padding;
944 int ret;
945
946 if (!or->set_attr.total_bytes) {
947 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
948 return 0;
949 }
950
951 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
952 cdbh->attrs_list.set_attr_offset =
953 osd_req_encode_offset(or, or->out.total_bytes, &padding);
954
955 ret = _req_append_segment(or, padding, &or->set_attr,
956 or->out.last_seg, &or->out);
957 if (ret)
958 return ret;
959
960 or->out.last_seg = &or->set_attr;
961 return 0;
962}
963
964int osd_req_add_get_attr_list(struct osd_request *or,
965 const struct osd_attr *oa, unsigned nelem)
966{
967 unsigned total_bytes = or->enc_get_attr.total_bytes;
968 void *attr_last;
969 int ret;
970
971 if (or->attributes_mode &&
972 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
973 WARN_ON(1);
974 return -EINVAL;
975 }
976 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
977
978 /* first time calc data-in list header size */
979 if (!or->get_attr.total_bytes)
980 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
981
982 /* calc data-out info */
983 if (!total_bytes) { /* first-time: allocate and put list header */
984 unsigned max_bytes;
985
986 total_bytes = _osd_req_sizeof_alist_header(or);
987 max_bytes = total_bytes +
988 nelem * sizeof(struct osd_attributes_list_attrid);
989 ret = _alloc_get_attr_desc(or, max_bytes);
990 if (ret)
991 return ret;
992
993 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
994 OSD_ATTR_LIST_GET);
995 }
996 attr_last = or->enc_get_attr.buff + total_bytes;
997
998 for (; nelem; --nelem) {
999 struct osd_attributes_list_attrid *attrid;
1000 const unsigned cur_size = sizeof(*attrid);
1001
1002 total_bytes += cur_size;
1003 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
1004 or->enc_get_attr.total_bytes = total_bytes - cur_size;
1005 ret = _alloc_get_attr_desc(or,
1006 total_bytes + nelem * sizeof(*attrid));
1007 if (ret)
1008 return ret;
1009 attr_last = or->enc_get_attr.buff +
1010 or->enc_get_attr.total_bytes;
1011 }
1012
1013 attrid = attr_last;
1014 attrid->attr_page = cpu_to_be32(oa->attr_page);
1015 attrid->attr_id = cpu_to_be32(oa->attr_id);
1016
1017 attr_last += cur_size;
1018
1019 /* calc data-in size */
1020 or->get_attr.total_bytes +=
1021 _osd_req_alist_elem_size(or, oa->len);
1022 ++oa;
1023 }
1024
1025 or->enc_get_attr.total_bytes = total_bytes;
1026
1027 OSD_DEBUG(
1028 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1029 or->get_attr.total_bytes,
1030 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1031 or->enc_get_attr.total_bytes,
1032 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1033 / sizeof(struct osd_attributes_list_attrid));
1034
1035 return 0;
1036}
1037EXPORT_SYMBOL(osd_req_add_get_attr_list);
1038
1039static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1040{
1041 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1042 unsigned out_padding;
1043 unsigned in_padding;
1044 int ret;
1045
1046 if (!or->enc_get_attr.total_bytes) {
1047 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
1048 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
1049 return 0;
1050 }
1051
1052 ret = _alloc_get_attr_list(or);
1053 if (ret)
1054 return ret;
1055
1056 /* The out-going buffer info update */
1057 OSD_DEBUG("out-going\n");
1058 cdbh->attrs_list.get_attr_desc_bytes =
1059 cpu_to_be32(or->enc_get_attr.total_bytes);
1060
1061 cdbh->attrs_list.get_attr_desc_offset =
1062 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1063
1064 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1065 or->out.last_seg, &or->out);
1066 if (ret)
1067 return ret;
1068 or->out.last_seg = &or->enc_get_attr;
1069
1070 /* The incoming buffer info update */
1071 OSD_DEBUG("in-coming\n");
1072 cdbh->attrs_list.get_attr_alloc_length =
1073 cpu_to_be32(or->get_attr.total_bytes);
1074
1075 cdbh->attrs_list.get_attr_offset =
1076 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1077
1078 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1079 &or->in);
1080 if (ret)
1081 return ret;
1082 or->in.last_seg = &or->get_attr;
1083
1084 return 0;
1085}
1086
1087int osd_req_decode_get_attr_list(struct osd_request *or,
1088 struct osd_attr *oa, int *nelem, void **iterator)
1089{
1090 unsigned cur_bytes, returned_bytes;
1091 int n;
1092 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1093 void *cur_p;
1094
1095 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1096 OSD_ATTR_LIST_SET_RETRIEVE)) {
1097 oa->attr_page = 0;
1098 oa->attr_id = 0;
1099 oa->val_ptr = NULL;
1100 oa->len = 0;
1101 *iterator = NULL;
1102 return 0;
1103 }
1104
1105 if (*iterator) {
1106 BUG_ON((*iterator < or->get_attr.buff) ||
1107 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1108 cur_p = *iterator;
1109 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1110 returned_bytes = or->get_attr.total_bytes;
1111 } else { /* first time decode the list header */
1112 cur_bytes = sizeof_attr_list;
1113 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1114 sizeof_attr_list;
1115
1116 cur_p = or->get_attr.buff + sizeof_attr_list;
1117
1118 if (returned_bytes > or->get_attr.alloc_size) {
1119 OSD_DEBUG("target report: space was not big enough! "
1120 "Allocate=%u Needed=%u\n",
1121 or->get_attr.alloc_size,
1122 returned_bytes + sizeof_attr_list);
1123
1124 returned_bytes =
1125 or->get_attr.alloc_size - sizeof_attr_list;
1126 }
1127 or->get_attr.total_bytes = returned_bytes;
1128 }
1129
1130 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001131 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1132 returned_bytes - cur_bytes);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001133
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001134 if (inc < 0) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001135 OSD_ERR("BAD FOOD from target. list not valid!"
1136 "c=%d r=%d n=%d\n",
1137 cur_bytes, returned_bytes, n);
1138 oa->val_ptr = NULL;
1139 break;
1140 }
1141
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001142 cur_bytes += inc;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001143 cur_p += inc;
1144 ++oa;
1145 }
1146
1147 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1148 *nelem = n;
1149 return returned_bytes - cur_bytes;
1150}
1151EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1152
1153/*
1154 * Attributes Page-mode
1155 */
1156
1157int osd_req_add_get_attr_page(struct osd_request *or,
1158 u32 page_id, void *attar_page, unsigned max_page_len,
1159 const struct osd_attr *set_one_attr)
1160{
1161 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1162
1163 if (or->attributes_mode &&
1164 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1165 WARN_ON(1);
1166 return -EINVAL;
1167 }
1168 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1169
1170 or->get_attr.buff = attar_page;
1171 or->get_attr.total_bytes = max_page_len;
1172
1173 or->set_attr.buff = set_one_attr->val_ptr;
1174 or->set_attr.total_bytes = set_one_attr->len;
1175
1176 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1177 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1178 /* ocdb->attrs_page.get_attr_offset; */
1179
1180 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1181 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1182 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1183 /* ocdb->attrs_page.set_attr_offset; */
1184 return 0;
1185}
1186EXPORT_SYMBOL(osd_req_add_get_attr_page);
1187
1188static int _osd_req_finalize_attr_page(struct osd_request *or)
1189{
1190 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1191 unsigned in_padding, out_padding;
1192 int ret;
1193
1194 /* returned page */
1195 cdbh->attrs_page.get_attr_offset =
1196 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1197
1198 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1199 &or->in);
1200 if (ret)
1201 return ret;
1202
1203 /* set one value */
1204 cdbh->attrs_page.set_attr_offset =
1205 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1206
1207 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1208 &or->out);
1209 return ret;
1210}
1211
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001212static inline void osd_sec_parms_set_out_offset(bool is_v1,
1213 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1214{
1215 if (is_v1)
1216 sec_parms->v1.data_out_integrity_check_offset = offset;
1217 else
1218 sec_parms->v2.data_out_integrity_check_offset = offset;
1219}
1220
1221static inline void osd_sec_parms_set_in_offset(bool is_v1,
1222 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1223{
1224 if (is_v1)
1225 sec_parms->v1.data_in_integrity_check_offset = offset;
1226 else
1227 sec_parms->v2.data_in_integrity_check_offset = offset;
1228}
1229
Boaz Harrosh345c4352009-01-25 17:03:07 +02001230static int _osd_req_finalize_data_integrity(struct osd_request *or,
1231 bool has_in, bool has_out, const u8 *cap_key)
1232{
1233 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1234 int ret;
1235
1236 if (!osd_is_sec_alldata(sec_parms))
1237 return 0;
1238
1239 if (has_out) {
1240 struct _osd_req_data_segment seg = {
1241 .buff = &or->out_data_integ,
1242 .total_bytes = sizeof(or->out_data_integ),
1243 };
1244 unsigned pad;
1245
1246 or->out_data_integ.data_bytes = cpu_to_be64(
1247 or->out.bio ? or->out.bio->bi_size : 0);
1248 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1249 or->set_attr.total_bytes);
1250 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1251 or->enc_get_attr.total_bytes);
1252
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001253 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1254 osd_req_encode_offset(or, or->out.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001255
1256 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1257 &or->out);
1258 if (ret)
1259 return ret;
1260 or->out.last_seg = NULL;
1261
1262 /* they are now all chained to request sign them all together */
1263 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1264 cap_key);
1265 }
1266
1267 if (has_in) {
1268 struct _osd_req_data_segment seg = {
1269 .buff = &or->in_data_integ,
1270 .total_bytes = sizeof(or->in_data_integ),
1271 };
1272 unsigned pad;
1273
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001274 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1275 osd_req_encode_offset(or, or->in.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001276
1277 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1278 &or->in);
1279 if (ret)
1280 return ret;
1281
1282 or->in.last_seg = NULL;
1283 }
1284
1285 return 0;
1286}
1287
Boaz Harrosh02941a52009-01-25 16:55:30 +02001288/*
1289 * osd_finalize_request and helpers
1290 */
1291
1292static int _init_blk_request(struct osd_request *or,
1293 bool has_in, bool has_out)
1294{
1295 gfp_t flags = or->alloc_flags;
1296 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1297 struct request_queue *q = scsi_device->request_queue;
1298 struct request *req;
1299 int ret = -ENOMEM;
1300
1301 req = blk_get_request(q, has_out, flags);
1302 if (!req)
1303 goto out;
1304
1305 or->request = req;
1306 req->cmd_type = REQ_TYPE_BLOCK_PC;
1307 req->timeout = or->timeout;
1308 req->retries = or->retries;
1309 req->sense = or->sense;
1310 req->sense_len = 0;
1311
1312 if (has_out) {
1313 or->out.req = req;
1314 if (has_in) {
1315 /* allocate bidi request */
1316 req = blk_get_request(q, READ, flags);
1317 if (!req) {
1318 OSD_DEBUG("blk_get_request for bidi failed\n");
1319 goto out;
1320 }
1321 req->cmd_type = REQ_TYPE_BLOCK_PC;
1322 or->in.req = or->request->next_rq = req;
1323 }
1324 } else if (has_in)
1325 or->in.req = req;
1326
1327 ret = 0;
1328out:
1329 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1330 or, has_in, has_out, ret, or->request);
1331 return ret;
1332}
1333
1334int osd_finalize_request(struct osd_request *or,
1335 u8 options, const void *cap, const u8 *cap_key)
1336{
1337 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1338 bool has_in, has_out;
1339 int ret;
1340
1341 if (options & OSD_REQ_FUA)
1342 cdbh->options |= OSD_CDB_FUA;
1343
1344 if (options & OSD_REQ_DPO)
1345 cdbh->options |= OSD_CDB_DPO;
1346
1347 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1348 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1349
1350 osd_set_caps(&or->cdb, cap);
1351
1352 has_in = or->in.bio || or->get_attr.total_bytes;
1353 has_out = or->out.bio || or->set_attr.total_bytes ||
1354 or->enc_get_attr.total_bytes;
1355
1356 ret = _init_blk_request(or, has_in, has_out);
1357 if (ret) {
1358 OSD_DEBUG("_init_blk_request failed\n");
1359 return ret;
1360 }
1361
1362 if (or->out.bio) {
1363 ret = blk_rq_append_bio(or->request->q, or->out.req,
1364 or->out.bio);
1365 if (ret) {
1366 OSD_DEBUG("blk_rq_append_bio out failed\n");
1367 return ret;
1368 }
1369 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1370 _LLU(or->out.total_bytes), or->out.req->data_len);
1371 }
1372 if (or->in.bio) {
1373 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1374 if (ret) {
1375 OSD_DEBUG("blk_rq_append_bio in failed\n");
1376 return ret;
1377 }
1378 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1379 _LLU(or->in.total_bytes), or->in.req->data_len);
1380 }
1381
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001382 or->out.pad_buff = sg_out_pad_buffer;
1383 or->in.pad_buff = sg_in_pad_buffer;
1384
Boaz Harrosh02941a52009-01-25 16:55:30 +02001385 if (!or->attributes_mode)
1386 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1387 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001388 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1389 ret = _osd_req_finalize_attr_page(or);
1390 } else {
1391 /* TODO: I think that for the GET_ATTR command these 2 should
1392 * be reversed to keep them in execution order (for embeded
1393 * targets with low memory footprint)
1394 */
1395 ret = _osd_req_finalize_set_attr_list(or);
1396 if (ret) {
1397 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1398 return ret;
1399 }
1400
1401 ret = _osd_req_finalize_get_attr_list(or);
1402 if (ret) {
1403 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1404 return ret;
1405 }
1406 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001407
Boaz Harrosh345c4352009-01-25 17:03:07 +02001408 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1409 if (ret)
1410 return ret;
1411
1412 osd_sec_sign_cdb(&or->cdb, cap_key);
1413
Boaz Harrosh02941a52009-01-25 16:55:30 +02001414 or->request->cmd = or->cdb.buff;
1415 or->request->cmd_len = _osd_req_cdb_len(or);
1416
1417 return 0;
1418}
1419EXPORT_SYMBOL(osd_finalize_request);
1420
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001421#define OSD_SENSE_PRINT1(fmt, a...) \
1422 do { \
1423 if (__cur_sense_need_output) \
1424 OSD_ERR(fmt, ##a); \
1425 } while (0)
1426
1427#define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1428
1429int osd_req_decode_sense_full(struct osd_request *or,
1430 struct osd_sense_info *osi, bool silent,
1431 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1432 struct osd_attr *bad_attr_list, int max_attr)
1433{
1434 int sense_len, original_sense_len;
1435 struct osd_sense_info local_osi;
1436 struct scsi_sense_descriptor_based *ssdb;
1437 void *cur_descriptor;
1438#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1439 const bool __cur_sense_need_output = false;
1440#else
1441 bool __cur_sense_need_output = !silent;
1442#endif
1443
1444 if (!or->request->errors)
1445 return 0;
1446
1447 ssdb = or->request->sense;
1448 sense_len = or->request->sense_len;
1449 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1450 OSD_ERR("Block-layer returned error(0x%x) but "
1451 "sense_len(%u) || key(%d) is empty\n",
1452 or->request->errors, sense_len, ssdb->sense_key);
1453 return -EIO;
1454 }
1455
1456 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1457 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1458 ssdb->response_code, sense_len);
1459 return -EIO;
1460 }
1461
1462 osi = osi ? : &local_osi;
1463 memset(osi, 0, sizeof(*osi));
1464 osi->key = ssdb->sense_key;
1465 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1466 original_sense_len = ssdb->additional_sense_length + 8;
1467
1468#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1469 if (__cur_sense_need_output)
1470 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1471#endif
1472 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1473 "additional_code=0x%x\n",
1474 osi->key, original_sense_len, sense_len,
1475 osi->additional_code);
1476
1477 if (original_sense_len < sense_len)
1478 sense_len = original_sense_len;
1479
1480 cur_descriptor = ssdb->ssd;
1481 sense_len -= sizeof(*ssdb);
1482 while (sense_len > 0) {
1483 struct scsi_sense_descriptor *ssd = cur_descriptor;
1484 int cur_len = ssd->additional_length + 2;
1485
1486 sense_len -= cur_len;
1487
1488 if (sense_len < 0)
1489 break; /* sense was truncated */
1490
1491 switch (ssd->descriptor_type) {
1492 case scsi_sense_information:
1493 case scsi_sense_command_specific_information:
1494 {
1495 struct scsi_sense_command_specific_data_descriptor
1496 *sscd = cur_descriptor;
1497
1498 osi->command_info =
1499 get_unaligned_be64(&sscd->information) ;
1500 OSD_SENSE_PRINT2(
1501 "command_specific_information 0x%llx \n",
1502 _LLU(osi->command_info));
1503 break;
1504 }
1505 case scsi_sense_key_specific:
1506 {
1507 struct scsi_sense_key_specific_data_descriptor
1508 *ssks = cur_descriptor;
1509
1510 osi->sense_info = get_unaligned_be16(&ssks->value);
1511 OSD_SENSE_PRINT2(
1512 "sense_key_specific_information %u"
1513 "sksv_cd_bpv_bp (0x%x)\n",
1514 osi->sense_info, ssks->sksv_cd_bpv_bp);
1515 break;
1516 }
1517 case osd_sense_object_identification:
1518 { /*FIXME: Keep first not last, Store in array*/
1519 struct osd_sense_identification_data_descriptor
1520 *osidd = cur_descriptor;
1521
1522 osi->not_initiated_command_functions =
1523 le32_to_cpu(osidd->not_initiated_functions);
1524 osi->completed_command_functions =
1525 le32_to_cpu(osidd->completed_functions);
1526 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1527 osi->obj.id = be64_to_cpu(osidd->object_id);
1528 OSD_SENSE_PRINT2(
1529 "object_identification pid=0x%llx oid=0x%llx\n",
1530 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1531 OSD_SENSE_PRINT2(
1532 "not_initiated_bits(%x) "
1533 "completed_command_bits(%x)\n",
1534 osi->not_initiated_command_functions,
1535 osi->completed_command_functions);
1536 break;
1537 }
1538 case osd_sense_response_integrity_check:
1539 {
1540 struct osd_sense_response_integrity_check_descriptor
1541 *osricd = cur_descriptor;
1542 const unsigned len =
1543 sizeof(osricd->integrity_check_value);
1544 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1545
1546 hex_dump_to_buffer(osricd->integrity_check_value, len,
1547 32, 1, key_dump, sizeof(key_dump), true);
1548 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1549 }
1550 case osd_sense_attribute_identification:
1551 {
1552 struct osd_sense_attributes_data_descriptor
1553 *osadd = cur_descriptor;
1554 int len = min(cur_len, sense_len);
1555 int i = 0;
1556 struct osd_sense_attr *pattr = osadd->sense_attrs;
1557
1558 while (len < 0) {
1559 u32 attr_page = be32_to_cpu(pattr->attr_page);
1560 u32 attr_id = be32_to_cpu(pattr->attr_id);
1561
1562 if (i++ == 0) {
1563 osi->attr.attr_page = attr_page;
1564 osi->attr.attr_id = attr_id;
1565 }
1566
1567 if (bad_attr_list && max_attr) {
1568 bad_attr_list->attr_page = attr_page;
1569 bad_attr_list->attr_id = attr_id;
1570 bad_attr_list++;
1571 max_attr--;
1572 }
1573 OSD_SENSE_PRINT2(
1574 "osd_sense_attribute_identification"
1575 "attr_page=0x%x attr_id=0x%x\n",
1576 attr_page, attr_id);
1577 }
1578 }
1579 /*These are not legal for OSD*/
1580 case scsi_sense_field_replaceable_unit:
1581 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1582 break;
1583 case scsi_sense_stream_commands:
1584 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1585 break;
1586 case scsi_sense_block_commands:
1587 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1588 break;
1589 case scsi_sense_ata_return:
1590 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1591 break;
1592 default:
1593 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1594 OSD_SENSE_PRINT2(
1595 "scsi_sense Reserved descriptor (0x%x)",
1596 ssd->descriptor_type);
1597 else
1598 OSD_SENSE_PRINT2(
1599 "scsi_sense Vendor descriptor (0x%x)",
1600 ssd->descriptor_type);
1601 }
1602
1603 cur_descriptor += cur_len;
1604 }
1605
1606 return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1607}
1608EXPORT_SYMBOL(osd_req_decode_sense_full);
1609
Boaz Harrosh02941a52009-01-25 16:55:30 +02001610/*
1611 * Implementation of osd_sec.h API
1612 * TODO: Move to a separate osd_sec.c file at a later stage.
1613 */
1614
1615enum { OSD_SEC_CAP_V1_ALL_CAPS =
1616 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1617 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1618 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1619 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1620};
1621
Boaz Harroshc6572c92009-01-25 17:09:40 +02001622enum { OSD_SEC_CAP_V2_ALL_CAPS =
1623 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1624};
1625
Boaz Harrosh02941a52009-01-25 16:55:30 +02001626void osd_sec_init_nosec_doall_caps(void *caps,
1627 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1628{
1629 struct osd_capability *cap = caps;
1630 u8 type;
1631 u8 descriptor_type;
1632
1633 if (likely(obj->id)) {
1634 if (unlikely(is_collection)) {
1635 type = OSD_SEC_OBJ_COLLECTION;
1636 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1637 OSD_SEC_OBJ_DESC_COL;
1638 } else {
1639 type = OSD_SEC_OBJ_USER;
1640 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1641 }
1642 WARN_ON(!obj->partition);
1643 } else {
1644 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1645 OSD_SEC_OBJ_ROOT;
1646 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1647 }
1648
1649 memset(cap, 0, sizeof(*cap));
1650
1651 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1652 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1653 cap->h.security_method = OSD_SEC_NOSEC;
1654/* cap->expiration_time;
1655 cap->AUDIT[30-10];
1656 cap->discriminator[42-30];
1657 cap->object_created_time; */
1658 cap->h.object_type = type;
1659 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1660 cap->h.object_descriptor_type = descriptor_type;
1661 cap->od.obj_desc.policy_access_tag = 0;
1662 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1663 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1664}
1665EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1666
Boaz Harroshc6572c92009-01-25 17:09:40 +02001667/* FIXME: Extract version from caps pointer.
1668 * Also Pete's target only supports caps from OSDv1 for now
1669 */
Boaz Harrosh02941a52009-01-25 16:55:30 +02001670void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1671{
Boaz Harroshc6572c92009-01-25 17:09:40 +02001672 bool is_ver1 = true;
1673 /* NOTE: They start at same address */
1674 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001675}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001676
Boaz Harrosh345c4352009-01-25 17:03:07 +02001677bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1678{
1679 return false;
1680}
1681
1682void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1683{
1684}
1685
1686void osd_sec_sign_data(void *data_integ __unused,
1687 struct bio *bio __unused, const u8 *cap_key __unused)
1688{
1689}
1690
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001691/*
1692 * Declared in osd_protocol.h
1693 * 4.12.5 Data-In and Data-Out buffer offsets
1694 * byte offset = mantissa * (2^(exponent+8))
1695 * Returns the smallest allowed encoded offset that contains given @offset
1696 * The actual encoded offset returned is @offset + *@padding.
1697 */
1698osd_cdb_offset __osd_encode_offset(
1699 u64 offset, unsigned *padding, int min_shift, int max_shift)
1700{
1701 u64 try_offset = -1, mod, align;
1702 osd_cdb_offset be32_offset;
1703 int shift;
1704
1705 *padding = 0;
1706 if (!offset)
1707 return 0;
1708
1709 for (shift = min_shift; shift < max_shift; ++shift) {
1710 try_offset = offset >> shift;
1711 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1712 break;
1713 }
1714
1715 BUG_ON(shift == max_shift);
1716
1717 align = 1 << shift;
1718 mod = offset & (align - 1);
1719 if (mod) {
1720 *padding = align - mod;
1721 try_offset += 1;
1722 }
1723
1724 try_offset |= ((shift - 8) & 0xf) << 28;
1725 be32_offset = cpu_to_be32((u32)try_offset);
1726
1727 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1728 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1729 be32_offset, *padding);
1730 return be32_offset;
1731}