blob: 1ce6b24abab297f70937a43cfd414bbe60349f26 [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
Boaz Harroshe9da4d72009-04-19 19:17:54 +0300266 /* OSD2: For convenience, on empty attributes, we return 8 bytes
267 * of zeros here. This keeps the same behaviour with OSD2r04,
268 * and is nice with null terminating ASCII fields.
269 * oa->val_ptr == NULL marks the end-of-list, or error.
270 */
271 oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300272 }
273 return inc;
274}
275
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200276static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
277{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200278 return osd_req_is_ver1(or) ?
279 osdv1_list_size(list_head) :
280 osdv2_list_size(list_head);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200281}
282
283static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
284{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200285 return osd_req_is_ver1(or) ?
286 sizeof(struct osdv1_attributes_list_header) :
287 sizeof(struct osdv2_attributes_list_header);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200288}
289
290static void _osd_req_set_alist_type(struct osd_request *or,
291 void *list, int list_type)
292{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200293 if (osd_req_is_ver1(or)) {
294 struct osdv1_attributes_list_header *attr_list = list;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200295
Boaz Harroshc6572c92009-01-25 17:09:40 +0200296 memset(attr_list, 0, sizeof(*attr_list));
297 attr_list->type = list_type;
298 } else {
299 struct osdv2_attributes_list_header *attr_list = list;
300
301 memset(attr_list, 0, sizeof(*attr_list));
302 attr_list->type = list_type;
303 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200304}
305
306static bool _osd_req_is_alist_type(struct osd_request *or,
307 void *list, int list_type)
308{
309 if (!list)
310 return false;
311
Boaz Harroshc6572c92009-01-25 17:09:40 +0200312 if (osd_req_is_ver1(or)) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200313 struct osdv1_attributes_list_header *attr_list = list;
314
315 return attr_list->type == list_type;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200316 } else {
317 struct osdv2_attributes_list_header *attr_list = list;
318
319 return attr_list->type == list_type;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200320 }
321}
322
Boaz Harrosh3e086132009-01-25 17:05:07 +0200323/* This is for List-objects not Attributes-Lists */
324static void _osd_req_encode_olist(struct osd_request *or,
325 struct osd_obj_id_list *list)
326{
327 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
328
Boaz Harroshc6572c92009-01-25 17:09:40 +0200329 if (osd_req_is_ver1(or)) {
330 cdbh->v1.list_identifier = list->list_identifier;
331 cdbh->v1.start_address = list->continuation_id;
332 } else {
333 cdbh->v2.list_identifier = list->list_identifier;
334 cdbh->v2.start_address = list->continuation_id;
335 }
Boaz Harrosh3e086132009-01-25 17:05:07 +0200336}
337
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200338static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
339 u64 offset, unsigned *padding)
340{
341 return __osd_encode_offset(offset, padding,
Boaz Harroshc6572c92009-01-25 17:09:40 +0200342 osd_req_is_ver1(or) ?
343 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
344 OSD_OFFSET_MAX_SHIFT);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200345}
346
Boaz Harrosh345c4352009-01-25 17:03:07 +0200347static struct osd_security_parameters *
348_osd_req_sec_params(struct osd_request *or)
349{
350 struct osd_cdb *ocdb = &or->cdb;
351
Boaz Harroshc6572c92009-01-25 17:09:40 +0200352 if (osd_req_is_ver1(or))
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300353 return (struct osd_security_parameters *)&ocdb->v1.sec_params;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200354 else
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300355 return (struct osd_security_parameters *)&ocdb->v2.sec_params;
Boaz Harrosh345c4352009-01-25 17:03:07 +0200356}
357
Boaz Harrosh02941a52009-01-25 16:55:30 +0200358void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
359{
360 memset(osdd, 0, sizeof(*osdd));
361 osdd->scsi_device = scsi_device;
362 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200363#ifdef OSD_VER1_SUPPORT
364 osdd->version = OSD_VER2;
365#endif
Boaz Harrosh02941a52009-01-25 16:55:30 +0200366 /* TODO: Allocate pools for osd_request attributes ... */
367}
368EXPORT_SYMBOL(osd_dev_init);
369
370void osd_dev_fini(struct osd_dev *osdd)
371{
372 /* TODO: De-allocate pools */
373
374 osdd->scsi_device = NULL;
375}
376EXPORT_SYMBOL(osd_dev_fini);
377
378static struct osd_request *_osd_request_alloc(gfp_t gfp)
379{
380 struct osd_request *or;
381
382 /* TODO: Use mempool with one saved request */
383 or = kzalloc(sizeof(*or), gfp);
384 return or;
385}
386
387static void _osd_request_free(struct osd_request *or)
388{
389 kfree(or);
390}
391
392struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
393{
394 struct osd_request *or;
395
396 or = _osd_request_alloc(gfp);
397 if (!or)
398 return NULL;
399
400 or->osd_dev = dev;
401 or->alloc_flags = gfp;
402 or->timeout = dev->def_timeout;
403 or->retries = OSD_REQ_RETRIES;
404
405 return or;
406}
407EXPORT_SYMBOL(osd_start_request);
408
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200409static void _osd_free_seg(struct osd_request *or __unused,
410 struct _osd_req_data_segment *seg)
411{
412 if (!seg->buff || !seg->alloc_size)
413 return;
414
415 kfree(seg->buff);
416 seg->buff = NULL;
417 seg->alloc_size = 0;
418}
419
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300420static void _put_request(struct request *rq , bool is_async)
421{
422 if (is_async) {
423 WARN_ON(rq->bio);
424 __blk_put_request(rq->q, rq);
425 } else {
426 /*
427 * If osd_finalize_request() was called but the request was not
428 * executed through the block layer, then we must release BIOs.
429 * TODO: Keep error code in or->async_error. Need to audit all
430 * code paths.
431 */
432 if (unlikely(rq->bio))
433 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
434 else
435 blk_put_request(rq);
436 }
437}
438
Boaz Harrosh02941a52009-01-25 16:55:30 +0200439void osd_end_request(struct osd_request *or)
440{
441 struct request *rq = or->request;
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300442 /* IMPORTANT: make sure this agrees with osd_execute_request_async */
443 bool is_async = (or->request->end_io_data == or);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200444
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200445 _osd_free_seg(or, &or->set_attr);
446 _osd_free_seg(or, &or->enc_get_attr);
447 _osd_free_seg(or, &or->get_attr);
448
Boaz Harrosh02941a52009-01-25 16:55:30 +0200449 if (rq) {
450 if (rq->next_rq) {
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300451 _put_request(rq->next_rq, is_async);
452 rq->next_rq = NULL;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200453 }
454
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300455 _put_request(rq, is_async);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200456 }
457 _osd_request_free(or);
458}
459EXPORT_SYMBOL(osd_end_request);
460
461int osd_execute_request(struct osd_request *or)
462{
463 return blk_execute_rq(or->request->q, NULL, or->request, 0);
464}
465EXPORT_SYMBOL(osd_execute_request);
466
467static void osd_request_async_done(struct request *req, int error)
468{
469 struct osd_request *or = req->end_io_data;
470
471 or->async_error = error;
472
473 if (error)
474 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
475
476 if (or->async_done)
477 or->async_done(or, or->async_private);
478 else
479 osd_end_request(or);
480}
481
482int osd_execute_request_async(struct osd_request *or,
483 osd_req_done_fn *done, void *private)
484{
485 or->request->end_io_data = or;
486 or->async_private = private;
487 or->async_done = done;
488
489 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
490 osd_request_async_done);
491 return 0;
492}
493EXPORT_SYMBOL(osd_execute_request_async);
494
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200495u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
496u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
497
498static int _osd_realloc_seg(struct osd_request *or,
499 struct _osd_req_data_segment *seg, unsigned max_bytes)
500{
501 void *buff;
502
503 if (seg->alloc_size >= max_bytes)
504 return 0;
505
506 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
507 if (!buff) {
508 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
509 seg->alloc_size);
510 return -ENOMEM;
511 }
512
513 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
514 seg->buff = buff;
515 seg->alloc_size = max_bytes;
516 return 0;
517}
518
519static int _alloc_set_attr_list(struct osd_request *or,
520 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
521{
522 unsigned total_bytes = add_bytes;
523
524 for (; nelem; --nelem, ++oa)
525 total_bytes += _osd_req_alist_elem_size(or, oa->len);
526
527 OSD_DEBUG("total_bytes=%d\n", total_bytes);
528 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
529}
530
531static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
532{
533 OSD_DEBUG("total_bytes=%d\n", max_bytes);
534 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
535}
536
537static int _alloc_get_attr_list(struct osd_request *or)
538{
539 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
540 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
541}
542
Boaz Harrosh02941a52009-01-25 16:55:30 +0200543/*
544 * Common to all OSD commands
545 */
546
547static void _osdv1_req_encode_common(struct osd_request *or,
548 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
549{
550 struct osdv1_cdb *ocdb = &or->cdb.v1;
551
552 /*
553 * For speed, the commands
554 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
555 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
556 * are not supported here. Should pass zero and set after the call
557 */
558 act &= cpu_to_be16(~0x0080); /* V1 action code */
559
560 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
561
562 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
563 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
564 ocdb->h.varlen_cdb.service_action = act;
565
566 ocdb->h.partition = cpu_to_be64(obj->partition);
567 ocdb->h.object = cpu_to_be64(obj->id);
568 ocdb->h.v1.length = cpu_to_be64(len);
569 ocdb->h.v1.start_address = cpu_to_be64(offset);
570}
571
Boaz Harroshc6572c92009-01-25 17:09:40 +0200572static void _osdv2_req_encode_common(struct osd_request *or,
573 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
574{
575 struct osdv2_cdb *ocdb = &or->cdb.v2;
576
577 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
578
579 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
580 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
581 ocdb->h.varlen_cdb.service_action = act;
582
583 ocdb->h.partition = cpu_to_be64(obj->partition);
584 ocdb->h.object = cpu_to_be64(obj->id);
585 ocdb->h.v2.length = cpu_to_be64(len);
586 ocdb->h.v2.start_address = cpu_to_be64(offset);
587}
588
Boaz Harrosh02941a52009-01-25 16:55:30 +0200589static void _osd_req_encode_common(struct osd_request *or,
590 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
591{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200592 if (osd_req_is_ver1(or))
593 _osdv1_req_encode_common(or, act, obj, offset, len);
594 else
595 _osdv2_req_encode_common(or, act, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200596}
597
598/*
599 * Device commands
600 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200601/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
602/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
603
Boaz Harrosh02941a52009-01-25 16:55:30 +0200604void osd_req_format(struct osd_request *or, u64 tot_capacity)
605{
606 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
607 tot_capacity);
608}
609EXPORT_SYMBOL(osd_req_format);
610
Boaz Harrosh3e086132009-01-25 17:05:07 +0200611int osd_req_list_dev_partitions(struct osd_request *or,
612 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
613{
614 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
615}
616EXPORT_SYMBOL(osd_req_list_dev_partitions);
617
618static void _osd_req_encode_flush(struct osd_request *or,
619 enum osd_options_flush_scope_values op)
620{
621 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
622
623 ocdb->command_specific_options = op;
624}
625
626void osd_req_flush_obsd(struct osd_request *or,
627 enum osd_options_flush_scope_values op)
628{
629 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
630 _osd_req_encode_flush(or, op);
631}
632EXPORT_SYMBOL(osd_req_flush_obsd);
633
Boaz Harroshae30c992009-01-25 17:07:14 +0200634/*TODO: void osd_req_perform_scsi_command(struct osd_request *,
635 const u8 *cdb, ...); */
636/*TODO: void osd_req_task_management(struct osd_request *, ...); */
637
Boaz Harrosh02941a52009-01-25 16:55:30 +0200638/*
639 * Partition commands
640 */
641static void _osd_req_encode_partition(struct osd_request *or,
642 __be16 act, osd_id partition)
643{
644 struct osd_obj_id par = {
645 .partition = partition,
646 .id = 0,
647 };
648
649 _osd_req_encode_common(or, act, &par, 0, 0);
650}
651
652void osd_req_create_partition(struct osd_request *or, osd_id partition)
653{
654 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
655}
656EXPORT_SYMBOL(osd_req_create_partition);
657
658void osd_req_remove_partition(struct osd_request *or, osd_id partition)
659{
660 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
661}
662EXPORT_SYMBOL(osd_req_remove_partition);
663
Boaz Harroshae30c992009-01-25 17:07:14 +0200664/*TODO: void osd_req_set_partition_key(struct osd_request *,
665 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
666 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
667
Boaz Harrosh3e086132009-01-25 17:05:07 +0200668static int _osd_req_list_objects(struct osd_request *or,
669 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
670 struct osd_obj_id_list *list, unsigned nelem)
671{
672 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
673 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
674 struct bio *bio;
675
676 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
677
678 if (list->list_identifier)
679 _osd_req_encode_olist(or, list);
680
681 WARN_ON(or->in.bio);
682 bio = bio_map_kern(q, list, len, or->alloc_flags);
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300683 if (IS_ERR(bio)) {
Boaz Harrosh3e086132009-01-25 17:05:07 +0200684 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300685 return PTR_ERR(bio);
Boaz Harrosh3e086132009-01-25 17:05:07 +0200686 }
687
688 bio->bi_rw &= ~(1 << BIO_RW);
689 or->in.bio = bio;
690 or->in.total_bytes = bio->bi_size;
691 return 0;
692}
693
694int osd_req_list_partition_collections(struct osd_request *or,
695 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
696 unsigned nelem)
697{
698 struct osd_obj_id par = {
699 .partition = partition,
700 .id = 0,
701 };
702
703 return osd_req_list_collection_objects(or, &par, initial_id, list,
704 nelem);
705}
706EXPORT_SYMBOL(osd_req_list_partition_collections);
707
708int osd_req_list_partition_objects(struct osd_request *or,
709 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
710 unsigned nelem)
711{
712 struct osd_obj_id par = {
713 .partition = partition,
714 .id = 0,
715 };
716
717 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
718 nelem);
719}
720EXPORT_SYMBOL(osd_req_list_partition_objects);
721
722void osd_req_flush_partition(struct osd_request *or,
723 osd_id partition, enum osd_options_flush_scope_values op)
724{
725 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
726 _osd_req_encode_flush(or, op);
727}
728EXPORT_SYMBOL(osd_req_flush_partition);
729
730/*
731 * Collection commands
732 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200733/*TODO: void osd_req_create_collection(struct osd_request *,
734 const struct osd_obj_id *); */
735/*TODO: void osd_req_remove_collection(struct osd_request *,
736 const struct osd_obj_id *); */
737
Boaz Harrosh3e086132009-01-25 17:05:07 +0200738int osd_req_list_collection_objects(struct osd_request *or,
739 const struct osd_obj_id *obj, osd_id initial_id,
740 struct osd_obj_id_list *list, unsigned nelem)
741{
742 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
743 initial_id, list, nelem);
744}
745EXPORT_SYMBOL(osd_req_list_collection_objects);
746
Boaz Harroshae30c992009-01-25 17:07:14 +0200747/*TODO: void query(struct osd_request *, ...); V2 */
748
Boaz Harrosh3e086132009-01-25 17:05:07 +0200749void osd_req_flush_collection(struct osd_request *or,
750 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
751{
752 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
753 _osd_req_encode_flush(or, op);
754}
755EXPORT_SYMBOL(osd_req_flush_collection);
756
Boaz Harroshae30c992009-01-25 17:07:14 +0200757/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
758/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
759
Boaz Harrosh02941a52009-01-25 16:55:30 +0200760/*
761 * Object commands
762 */
763void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
764{
765 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
766}
767EXPORT_SYMBOL(osd_req_create_object);
768
769void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
770{
771 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
772}
773EXPORT_SYMBOL(osd_req_remove_object);
774
Boaz Harroshae30c992009-01-25 17:07:14 +0200775
776/*TODO: void osd_req_create_multi(struct osd_request *or,
777 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
778*/
779
Boaz Harrosh02941a52009-01-25 16:55:30 +0200780void osd_req_write(struct osd_request *or,
781 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
782{
783 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
784 WARN_ON(or->out.bio || or->out.total_bytes);
785 bio->bi_rw |= (1 << BIO_RW);
786 or->out.bio = bio;
787 or->out.total_bytes = bio->bi_size;
788}
789EXPORT_SYMBOL(osd_req_write);
790
Boaz Harroshae30c992009-01-25 17:07:14 +0200791/*TODO: void osd_req_append(struct osd_request *,
792 const struct osd_obj_id *, struct bio *data_out); */
793/*TODO: void osd_req_create_write(struct osd_request *,
794 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
795/*TODO: void osd_req_clear(struct osd_request *,
796 const struct osd_obj_id *, u64 offset, u64 len); */
797/*TODO: void osd_req_punch(struct osd_request *,
798 const struct osd_obj_id *, u64 offset, u64 len); V2 */
799
Boaz Harrosh3e086132009-01-25 17:05:07 +0200800void osd_req_flush_object(struct osd_request *or,
801 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
802 /*V2*/ u64 offset, /*V2*/ u64 len)
803{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200804 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
805 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
806 offset = 0;
807 len = 0;
808 }
809
Boaz Harrosh3e086132009-01-25 17:05:07 +0200810 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
811 _osd_req_encode_flush(or, op);
812}
813EXPORT_SYMBOL(osd_req_flush_object);
814
Boaz Harrosh02941a52009-01-25 16:55:30 +0200815void osd_req_read(struct osd_request *or,
816 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
817{
818 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
819 WARN_ON(or->in.bio || or->in.total_bytes);
820 bio->bi_rw &= ~(1 << BIO_RW);
821 or->in.bio = bio;
822 or->in.total_bytes = bio->bi_size;
823}
824EXPORT_SYMBOL(osd_req_read);
825
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200826void osd_req_get_attributes(struct osd_request *or,
827 const struct osd_obj_id *obj)
828{
829 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
830}
831EXPORT_SYMBOL(osd_req_get_attributes);
832
833void osd_req_set_attributes(struct osd_request *or,
834 const struct osd_obj_id *obj)
835{
836 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
837}
838EXPORT_SYMBOL(osd_req_set_attributes);
839
840/*
841 * Attributes List-mode
842 */
843
844int osd_req_add_set_attr_list(struct osd_request *or,
845 const struct osd_attr *oa, unsigned nelem)
846{
847 unsigned total_bytes = or->set_attr.total_bytes;
848 void *attr_last;
849 int ret;
850
851 if (or->attributes_mode &&
852 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
853 WARN_ON(1);
854 return -EINVAL;
855 }
856 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
857
858 if (!total_bytes) { /* first-time: allocate and put list header */
859 total_bytes = _osd_req_sizeof_alist_header(or);
860 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
861 if (ret)
862 return ret;
863 _osd_req_set_alist_type(or, or->set_attr.buff,
864 OSD_ATTR_LIST_SET_RETRIEVE);
865 }
866 attr_last = or->set_attr.buff + total_bytes;
867
868 for (; nelem; --nelem) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200869 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
870
871 total_bytes += elem_size;
872 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
873 or->set_attr.total_bytes = total_bytes - elem_size;
874 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
875 if (ret)
876 return ret;
877 attr_last =
878 or->set_attr.buff + or->set_attr.total_bytes;
879 }
880
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300881 _osd_req_alist_elem_encode(or, attr_last, oa);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200882
883 attr_last += elem_size;
884 ++oa;
885 }
886
887 or->set_attr.total_bytes = total_bytes;
888 return 0;
889}
890EXPORT_SYMBOL(osd_req_add_set_attr_list);
891
892static int _append_map_kern(struct request *req,
893 void *buff, unsigned len, gfp_t flags)
894{
895 struct bio *bio;
896 int ret;
897
898 bio = bio_map_kern(req->q, buff, len, flags);
899 if (IS_ERR(bio)) {
900 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
901 PTR_ERR(bio));
902 return PTR_ERR(bio);
903 }
904 ret = blk_rq_append_bio(req->q, req, bio);
905 if (ret) {
906 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
907 bio_put(bio);
908 }
909 return ret;
910}
911
912static int _req_append_segment(struct osd_request *or,
913 unsigned padding, struct _osd_req_data_segment *seg,
914 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
915{
916 void *pad_buff;
917 int ret;
918
919 if (padding) {
920 /* check if we can just add it to last buffer */
921 if (last_seg &&
922 (padding <= last_seg->alloc_size - last_seg->total_bytes))
923 pad_buff = last_seg->buff + last_seg->total_bytes;
924 else
925 pad_buff = io->pad_buff;
926
927 ret = _append_map_kern(io->req, pad_buff, padding,
928 or->alloc_flags);
929 if (ret)
930 return ret;
931 io->total_bytes += padding;
932 }
933
934 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
935 or->alloc_flags);
936 if (ret)
937 return ret;
938
939 io->total_bytes += seg->total_bytes;
940 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
941 seg->total_bytes);
942 return 0;
943}
944
945static int _osd_req_finalize_set_attr_list(struct osd_request *or)
946{
947 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
948 unsigned padding;
949 int ret;
950
951 if (!or->set_attr.total_bytes) {
952 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
953 return 0;
954 }
955
956 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
957 cdbh->attrs_list.set_attr_offset =
958 osd_req_encode_offset(or, or->out.total_bytes, &padding);
959
960 ret = _req_append_segment(or, padding, &or->set_attr,
961 or->out.last_seg, &or->out);
962 if (ret)
963 return ret;
964
965 or->out.last_seg = &or->set_attr;
966 return 0;
967}
968
969int osd_req_add_get_attr_list(struct osd_request *or,
970 const struct osd_attr *oa, unsigned nelem)
971{
972 unsigned total_bytes = or->enc_get_attr.total_bytes;
973 void *attr_last;
974 int ret;
975
976 if (or->attributes_mode &&
977 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
978 WARN_ON(1);
979 return -EINVAL;
980 }
981 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
982
983 /* first time calc data-in list header size */
984 if (!or->get_attr.total_bytes)
985 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
986
987 /* calc data-out info */
988 if (!total_bytes) { /* first-time: allocate and put list header */
989 unsigned max_bytes;
990
991 total_bytes = _osd_req_sizeof_alist_header(or);
992 max_bytes = total_bytes +
993 nelem * sizeof(struct osd_attributes_list_attrid);
994 ret = _alloc_get_attr_desc(or, max_bytes);
995 if (ret)
996 return ret;
997
998 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
999 OSD_ATTR_LIST_GET);
1000 }
1001 attr_last = or->enc_get_attr.buff + total_bytes;
1002
1003 for (; nelem; --nelem) {
1004 struct osd_attributes_list_attrid *attrid;
1005 const unsigned cur_size = sizeof(*attrid);
1006
1007 total_bytes += cur_size;
1008 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
1009 or->enc_get_attr.total_bytes = total_bytes - cur_size;
1010 ret = _alloc_get_attr_desc(or,
1011 total_bytes + nelem * sizeof(*attrid));
1012 if (ret)
1013 return ret;
1014 attr_last = or->enc_get_attr.buff +
1015 or->enc_get_attr.total_bytes;
1016 }
1017
1018 attrid = attr_last;
1019 attrid->attr_page = cpu_to_be32(oa->attr_page);
1020 attrid->attr_id = cpu_to_be32(oa->attr_id);
1021
1022 attr_last += cur_size;
1023
1024 /* calc data-in size */
1025 or->get_attr.total_bytes +=
1026 _osd_req_alist_elem_size(or, oa->len);
1027 ++oa;
1028 }
1029
1030 or->enc_get_attr.total_bytes = total_bytes;
1031
1032 OSD_DEBUG(
1033 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1034 or->get_attr.total_bytes,
1035 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1036 or->enc_get_attr.total_bytes,
1037 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1038 / sizeof(struct osd_attributes_list_attrid));
1039
1040 return 0;
1041}
1042EXPORT_SYMBOL(osd_req_add_get_attr_list);
1043
1044static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1045{
1046 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1047 unsigned out_padding;
1048 unsigned in_padding;
1049 int ret;
1050
1051 if (!or->enc_get_attr.total_bytes) {
1052 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
1053 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
1054 return 0;
1055 }
1056
1057 ret = _alloc_get_attr_list(or);
1058 if (ret)
1059 return ret;
1060
1061 /* The out-going buffer info update */
1062 OSD_DEBUG("out-going\n");
1063 cdbh->attrs_list.get_attr_desc_bytes =
1064 cpu_to_be32(or->enc_get_attr.total_bytes);
1065
1066 cdbh->attrs_list.get_attr_desc_offset =
1067 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1068
1069 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1070 or->out.last_seg, &or->out);
1071 if (ret)
1072 return ret;
1073 or->out.last_seg = &or->enc_get_attr;
1074
1075 /* The incoming buffer info update */
1076 OSD_DEBUG("in-coming\n");
1077 cdbh->attrs_list.get_attr_alloc_length =
1078 cpu_to_be32(or->get_attr.total_bytes);
1079
1080 cdbh->attrs_list.get_attr_offset =
1081 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1082
1083 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1084 &or->in);
1085 if (ret)
1086 return ret;
1087 or->in.last_seg = &or->get_attr;
1088
1089 return 0;
1090}
1091
1092int osd_req_decode_get_attr_list(struct osd_request *or,
1093 struct osd_attr *oa, int *nelem, void **iterator)
1094{
1095 unsigned cur_bytes, returned_bytes;
1096 int n;
1097 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1098 void *cur_p;
1099
1100 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1101 OSD_ATTR_LIST_SET_RETRIEVE)) {
1102 oa->attr_page = 0;
1103 oa->attr_id = 0;
1104 oa->val_ptr = NULL;
1105 oa->len = 0;
1106 *iterator = NULL;
1107 return 0;
1108 }
1109
1110 if (*iterator) {
1111 BUG_ON((*iterator < or->get_attr.buff) ||
1112 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1113 cur_p = *iterator;
1114 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1115 returned_bytes = or->get_attr.total_bytes;
1116 } else { /* first time decode the list header */
1117 cur_bytes = sizeof_attr_list;
1118 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1119 sizeof_attr_list;
1120
1121 cur_p = or->get_attr.buff + sizeof_attr_list;
1122
1123 if (returned_bytes > or->get_attr.alloc_size) {
1124 OSD_DEBUG("target report: space was not big enough! "
1125 "Allocate=%u Needed=%u\n",
1126 or->get_attr.alloc_size,
1127 returned_bytes + sizeof_attr_list);
1128
1129 returned_bytes =
1130 or->get_attr.alloc_size - sizeof_attr_list;
1131 }
1132 or->get_attr.total_bytes = returned_bytes;
1133 }
1134
1135 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001136 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1137 returned_bytes - cur_bytes);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001138
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001139 if (inc < 0) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001140 OSD_ERR("BAD FOOD from target. list not valid!"
1141 "c=%d r=%d n=%d\n",
1142 cur_bytes, returned_bytes, n);
1143 oa->val_ptr = NULL;
1144 break;
1145 }
1146
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001147 cur_bytes += inc;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001148 cur_p += inc;
1149 ++oa;
1150 }
1151
1152 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1153 *nelem = n;
1154 return returned_bytes - cur_bytes;
1155}
1156EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1157
1158/*
1159 * Attributes Page-mode
1160 */
1161
1162int osd_req_add_get_attr_page(struct osd_request *or,
1163 u32 page_id, void *attar_page, unsigned max_page_len,
1164 const struct osd_attr *set_one_attr)
1165{
1166 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1167
1168 if (or->attributes_mode &&
1169 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1170 WARN_ON(1);
1171 return -EINVAL;
1172 }
1173 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1174
1175 or->get_attr.buff = attar_page;
1176 or->get_attr.total_bytes = max_page_len;
1177
1178 or->set_attr.buff = set_one_attr->val_ptr;
1179 or->set_attr.total_bytes = set_one_attr->len;
1180
1181 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1182 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1183 /* ocdb->attrs_page.get_attr_offset; */
1184
1185 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1186 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1187 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1188 /* ocdb->attrs_page.set_attr_offset; */
1189 return 0;
1190}
1191EXPORT_SYMBOL(osd_req_add_get_attr_page);
1192
1193static int _osd_req_finalize_attr_page(struct osd_request *or)
1194{
1195 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1196 unsigned in_padding, out_padding;
1197 int ret;
1198
1199 /* returned page */
1200 cdbh->attrs_page.get_attr_offset =
1201 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1202
1203 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1204 &or->in);
1205 if (ret)
1206 return ret;
1207
1208 /* set one value */
1209 cdbh->attrs_page.set_attr_offset =
1210 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1211
1212 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1213 &or->out);
1214 return ret;
1215}
1216
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001217static inline void osd_sec_parms_set_out_offset(bool is_v1,
1218 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1219{
1220 if (is_v1)
1221 sec_parms->v1.data_out_integrity_check_offset = offset;
1222 else
1223 sec_parms->v2.data_out_integrity_check_offset = offset;
1224}
1225
1226static inline void osd_sec_parms_set_in_offset(bool is_v1,
1227 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1228{
1229 if (is_v1)
1230 sec_parms->v1.data_in_integrity_check_offset = offset;
1231 else
1232 sec_parms->v2.data_in_integrity_check_offset = offset;
1233}
1234
Boaz Harrosh345c4352009-01-25 17:03:07 +02001235static int _osd_req_finalize_data_integrity(struct osd_request *or,
1236 bool has_in, bool has_out, const u8 *cap_key)
1237{
1238 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1239 int ret;
1240
1241 if (!osd_is_sec_alldata(sec_parms))
1242 return 0;
1243
1244 if (has_out) {
1245 struct _osd_req_data_segment seg = {
1246 .buff = &or->out_data_integ,
1247 .total_bytes = sizeof(or->out_data_integ),
1248 };
1249 unsigned pad;
1250
1251 or->out_data_integ.data_bytes = cpu_to_be64(
1252 or->out.bio ? or->out.bio->bi_size : 0);
1253 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1254 or->set_attr.total_bytes);
1255 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1256 or->enc_get_attr.total_bytes);
1257
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001258 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1259 osd_req_encode_offset(or, or->out.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001260
1261 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1262 &or->out);
1263 if (ret)
1264 return ret;
1265 or->out.last_seg = NULL;
1266
1267 /* they are now all chained to request sign them all together */
1268 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1269 cap_key);
1270 }
1271
1272 if (has_in) {
1273 struct _osd_req_data_segment seg = {
1274 .buff = &or->in_data_integ,
1275 .total_bytes = sizeof(or->in_data_integ),
1276 };
1277 unsigned pad;
1278
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001279 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1280 osd_req_encode_offset(or, or->in.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001281
1282 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1283 &or->in);
1284 if (ret)
1285 return ret;
1286
1287 or->in.last_seg = NULL;
1288 }
1289
1290 return 0;
1291}
1292
Boaz Harrosh02941a52009-01-25 16:55:30 +02001293/*
1294 * osd_finalize_request and helpers
1295 */
1296
1297static int _init_blk_request(struct osd_request *or,
1298 bool has_in, bool has_out)
1299{
1300 gfp_t flags = or->alloc_flags;
1301 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1302 struct request_queue *q = scsi_device->request_queue;
1303 struct request *req;
1304 int ret = -ENOMEM;
1305
1306 req = blk_get_request(q, has_out, flags);
1307 if (!req)
1308 goto out;
1309
1310 or->request = req;
1311 req->cmd_type = REQ_TYPE_BLOCK_PC;
1312 req->timeout = or->timeout;
1313 req->retries = or->retries;
1314 req->sense = or->sense;
1315 req->sense_len = 0;
1316
1317 if (has_out) {
1318 or->out.req = req;
1319 if (has_in) {
1320 /* allocate bidi request */
1321 req = blk_get_request(q, READ, flags);
1322 if (!req) {
1323 OSD_DEBUG("blk_get_request for bidi failed\n");
1324 goto out;
1325 }
1326 req->cmd_type = REQ_TYPE_BLOCK_PC;
1327 or->in.req = or->request->next_rq = req;
1328 }
1329 } else if (has_in)
1330 or->in.req = req;
1331
1332 ret = 0;
1333out:
1334 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1335 or, has_in, has_out, ret, or->request);
1336 return ret;
1337}
1338
1339int osd_finalize_request(struct osd_request *or,
1340 u8 options, const void *cap, const u8 *cap_key)
1341{
1342 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1343 bool has_in, has_out;
1344 int ret;
1345
1346 if (options & OSD_REQ_FUA)
1347 cdbh->options |= OSD_CDB_FUA;
1348
1349 if (options & OSD_REQ_DPO)
1350 cdbh->options |= OSD_CDB_DPO;
1351
1352 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1353 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1354
1355 osd_set_caps(&or->cdb, cap);
1356
1357 has_in = or->in.bio || or->get_attr.total_bytes;
1358 has_out = or->out.bio || or->set_attr.total_bytes ||
1359 or->enc_get_attr.total_bytes;
1360
1361 ret = _init_blk_request(or, has_in, has_out);
1362 if (ret) {
1363 OSD_DEBUG("_init_blk_request failed\n");
1364 return ret;
1365 }
1366
1367 if (or->out.bio) {
1368 ret = blk_rq_append_bio(or->request->q, or->out.req,
1369 or->out.bio);
1370 if (ret) {
1371 OSD_DEBUG("blk_rq_append_bio out failed\n");
1372 return ret;
1373 }
1374 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1375 _LLU(or->out.total_bytes), or->out.req->data_len);
1376 }
1377 if (or->in.bio) {
1378 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1379 if (ret) {
1380 OSD_DEBUG("blk_rq_append_bio in failed\n");
1381 return ret;
1382 }
1383 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1384 _LLU(or->in.total_bytes), or->in.req->data_len);
1385 }
1386
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001387 or->out.pad_buff = sg_out_pad_buffer;
1388 or->in.pad_buff = sg_in_pad_buffer;
1389
Boaz Harrosh02941a52009-01-25 16:55:30 +02001390 if (!or->attributes_mode)
1391 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1392 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001393 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1394 ret = _osd_req_finalize_attr_page(or);
1395 } else {
1396 /* TODO: I think that for the GET_ATTR command these 2 should
1397 * be reversed to keep them in execution order (for embeded
1398 * targets with low memory footprint)
1399 */
1400 ret = _osd_req_finalize_set_attr_list(or);
1401 if (ret) {
1402 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1403 return ret;
1404 }
1405
1406 ret = _osd_req_finalize_get_attr_list(or);
1407 if (ret) {
1408 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1409 return ret;
1410 }
1411 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001412
Boaz Harrosh345c4352009-01-25 17:03:07 +02001413 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1414 if (ret)
1415 return ret;
1416
1417 osd_sec_sign_cdb(&or->cdb, cap_key);
1418
Boaz Harrosh02941a52009-01-25 16:55:30 +02001419 or->request->cmd = or->cdb.buff;
1420 or->request->cmd_len = _osd_req_cdb_len(or);
1421
1422 return 0;
1423}
1424EXPORT_SYMBOL(osd_finalize_request);
1425
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001426#define OSD_SENSE_PRINT1(fmt, a...) \
1427 do { \
1428 if (__cur_sense_need_output) \
1429 OSD_ERR(fmt, ##a); \
1430 } while (0)
1431
1432#define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1433
1434int osd_req_decode_sense_full(struct osd_request *or,
1435 struct osd_sense_info *osi, bool silent,
1436 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1437 struct osd_attr *bad_attr_list, int max_attr)
1438{
1439 int sense_len, original_sense_len;
1440 struct osd_sense_info local_osi;
1441 struct scsi_sense_descriptor_based *ssdb;
1442 void *cur_descriptor;
1443#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1444 const bool __cur_sense_need_output = false;
1445#else
1446 bool __cur_sense_need_output = !silent;
1447#endif
1448
1449 if (!or->request->errors)
1450 return 0;
1451
1452 ssdb = or->request->sense;
1453 sense_len = or->request->sense_len;
1454 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1455 OSD_ERR("Block-layer returned error(0x%x) but "
1456 "sense_len(%u) || key(%d) is empty\n",
1457 or->request->errors, sense_len, ssdb->sense_key);
1458 return -EIO;
1459 }
1460
1461 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1462 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1463 ssdb->response_code, sense_len);
1464 return -EIO;
1465 }
1466
1467 osi = osi ? : &local_osi;
1468 memset(osi, 0, sizeof(*osi));
1469 osi->key = ssdb->sense_key;
1470 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1471 original_sense_len = ssdb->additional_sense_length + 8;
1472
1473#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1474 if (__cur_sense_need_output)
1475 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1476#endif
1477 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
1478 "additional_code=0x%x\n",
1479 osi->key, original_sense_len, sense_len,
1480 osi->additional_code);
1481
1482 if (original_sense_len < sense_len)
1483 sense_len = original_sense_len;
1484
1485 cur_descriptor = ssdb->ssd;
1486 sense_len -= sizeof(*ssdb);
1487 while (sense_len > 0) {
1488 struct scsi_sense_descriptor *ssd = cur_descriptor;
1489 int cur_len = ssd->additional_length + 2;
1490
1491 sense_len -= cur_len;
1492
1493 if (sense_len < 0)
1494 break; /* sense was truncated */
1495
1496 switch (ssd->descriptor_type) {
1497 case scsi_sense_information:
1498 case scsi_sense_command_specific_information:
1499 {
1500 struct scsi_sense_command_specific_data_descriptor
1501 *sscd = cur_descriptor;
1502
1503 osi->command_info =
1504 get_unaligned_be64(&sscd->information) ;
1505 OSD_SENSE_PRINT2(
1506 "command_specific_information 0x%llx \n",
1507 _LLU(osi->command_info));
1508 break;
1509 }
1510 case scsi_sense_key_specific:
1511 {
1512 struct scsi_sense_key_specific_data_descriptor
1513 *ssks = cur_descriptor;
1514
1515 osi->sense_info = get_unaligned_be16(&ssks->value);
1516 OSD_SENSE_PRINT2(
1517 "sense_key_specific_information %u"
1518 "sksv_cd_bpv_bp (0x%x)\n",
1519 osi->sense_info, ssks->sksv_cd_bpv_bp);
1520 break;
1521 }
1522 case osd_sense_object_identification:
1523 { /*FIXME: Keep first not last, Store in array*/
1524 struct osd_sense_identification_data_descriptor
1525 *osidd = cur_descriptor;
1526
1527 osi->not_initiated_command_functions =
1528 le32_to_cpu(osidd->not_initiated_functions);
1529 osi->completed_command_functions =
1530 le32_to_cpu(osidd->completed_functions);
1531 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1532 osi->obj.id = be64_to_cpu(osidd->object_id);
1533 OSD_SENSE_PRINT2(
1534 "object_identification pid=0x%llx oid=0x%llx\n",
1535 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1536 OSD_SENSE_PRINT2(
1537 "not_initiated_bits(%x) "
1538 "completed_command_bits(%x)\n",
1539 osi->not_initiated_command_functions,
1540 osi->completed_command_functions);
1541 break;
1542 }
1543 case osd_sense_response_integrity_check:
1544 {
1545 struct osd_sense_response_integrity_check_descriptor
1546 *osricd = cur_descriptor;
1547 const unsigned len =
1548 sizeof(osricd->integrity_check_value);
1549 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1550
1551 hex_dump_to_buffer(osricd->integrity_check_value, len,
1552 32, 1, key_dump, sizeof(key_dump), true);
1553 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1554 }
1555 case osd_sense_attribute_identification:
1556 {
1557 struct osd_sense_attributes_data_descriptor
1558 *osadd = cur_descriptor;
1559 int len = min(cur_len, sense_len);
1560 int i = 0;
1561 struct osd_sense_attr *pattr = osadd->sense_attrs;
1562
1563 while (len < 0) {
1564 u32 attr_page = be32_to_cpu(pattr->attr_page);
1565 u32 attr_id = be32_to_cpu(pattr->attr_id);
1566
1567 if (i++ == 0) {
1568 osi->attr.attr_page = attr_page;
1569 osi->attr.attr_id = attr_id;
1570 }
1571
1572 if (bad_attr_list && max_attr) {
1573 bad_attr_list->attr_page = attr_page;
1574 bad_attr_list->attr_id = attr_id;
1575 bad_attr_list++;
1576 max_attr--;
1577 }
1578 OSD_SENSE_PRINT2(
1579 "osd_sense_attribute_identification"
1580 "attr_page=0x%x attr_id=0x%x\n",
1581 attr_page, attr_id);
1582 }
1583 }
1584 /*These are not legal for OSD*/
1585 case scsi_sense_field_replaceable_unit:
1586 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1587 break;
1588 case scsi_sense_stream_commands:
1589 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1590 break;
1591 case scsi_sense_block_commands:
1592 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1593 break;
1594 case scsi_sense_ata_return:
1595 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1596 break;
1597 default:
1598 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1599 OSD_SENSE_PRINT2(
1600 "scsi_sense Reserved descriptor (0x%x)",
1601 ssd->descriptor_type);
1602 else
1603 OSD_SENSE_PRINT2(
1604 "scsi_sense Vendor descriptor (0x%x)",
1605 ssd->descriptor_type);
1606 }
1607
1608 cur_descriptor += cur_len;
1609 }
1610
1611 return (osi->key > scsi_sk_recovered_error) ? -EIO : 0;
1612}
1613EXPORT_SYMBOL(osd_req_decode_sense_full);
1614
Boaz Harrosh02941a52009-01-25 16:55:30 +02001615/*
1616 * Implementation of osd_sec.h API
1617 * TODO: Move to a separate osd_sec.c file at a later stage.
1618 */
1619
1620enum { OSD_SEC_CAP_V1_ALL_CAPS =
1621 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1622 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1623 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1624 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1625};
1626
Boaz Harroshc6572c92009-01-25 17:09:40 +02001627enum { OSD_SEC_CAP_V2_ALL_CAPS =
1628 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1629};
1630
Boaz Harrosh02941a52009-01-25 16:55:30 +02001631void osd_sec_init_nosec_doall_caps(void *caps,
1632 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1633{
1634 struct osd_capability *cap = caps;
1635 u8 type;
1636 u8 descriptor_type;
1637
1638 if (likely(obj->id)) {
1639 if (unlikely(is_collection)) {
1640 type = OSD_SEC_OBJ_COLLECTION;
1641 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1642 OSD_SEC_OBJ_DESC_COL;
1643 } else {
1644 type = OSD_SEC_OBJ_USER;
1645 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1646 }
1647 WARN_ON(!obj->partition);
1648 } else {
1649 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1650 OSD_SEC_OBJ_ROOT;
1651 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1652 }
1653
1654 memset(cap, 0, sizeof(*cap));
1655
1656 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1657 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1658 cap->h.security_method = OSD_SEC_NOSEC;
1659/* cap->expiration_time;
1660 cap->AUDIT[30-10];
1661 cap->discriminator[42-30];
1662 cap->object_created_time; */
1663 cap->h.object_type = type;
1664 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1665 cap->h.object_descriptor_type = descriptor_type;
1666 cap->od.obj_desc.policy_access_tag = 0;
1667 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1668 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1669}
1670EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1671
Boaz Harroshc6572c92009-01-25 17:09:40 +02001672/* FIXME: Extract version from caps pointer.
1673 * Also Pete's target only supports caps from OSDv1 for now
1674 */
Boaz Harrosh02941a52009-01-25 16:55:30 +02001675void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1676{
Boaz Harroshc6572c92009-01-25 17:09:40 +02001677 bool is_ver1 = true;
1678 /* NOTE: They start at same address */
1679 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001680}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001681
Boaz Harrosh345c4352009-01-25 17:03:07 +02001682bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1683{
1684 return false;
1685}
1686
1687void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1688{
1689}
1690
1691void osd_sec_sign_data(void *data_integ __unused,
1692 struct bio *bio __unused, const u8 *cap_key __unused)
1693{
1694}
1695
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001696/*
1697 * Declared in osd_protocol.h
1698 * 4.12.5 Data-In and Data-Out buffer offsets
1699 * byte offset = mantissa * (2^(exponent+8))
1700 * Returns the smallest allowed encoded offset that contains given @offset
1701 * The actual encoded offset returned is @offset + *@padding.
1702 */
1703osd_cdb_offset __osd_encode_offset(
1704 u64 offset, unsigned *padding, int min_shift, int max_shift)
1705{
1706 u64 try_offset = -1, mod, align;
1707 osd_cdb_offset be32_offset;
1708 int shift;
1709
1710 *padding = 0;
1711 if (!offset)
1712 return 0;
1713
1714 for (shift = min_shift; shift < max_shift; ++shift) {
1715 try_offset = offset >> shift;
1716 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1717 break;
1718 }
1719
1720 BUG_ON(shift == max_shift);
1721
1722 align = 1 << shift;
1723 mod = offset & (align - 1);
1724 if (mod) {
1725 *padding = align - mod;
1726 try_offset += 1;
1727 }
1728
1729 try_offset |= ((shift - 8) & 0xf) << 28;
1730 be32_offset = cpu_to_be32((u32)try_offset);
1731
1732 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1733 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1734 be32_offset, *padding);
1735 return be32_offset;
1736}