blob: f6340c2ceb2516f4b922e1db9576bdbf3291b828 [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 Harrosh02941a52009-01-25 16:55:30 +020045#include <scsi/scsi_device.h>
46
47#include "osd_debug.h"
48
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020049#ifndef __unused
50# define __unused __attribute__((unused))
51#endif
52
Boaz Harrosh02941a52009-01-25 16:55:30 +020053enum { OSD_REQ_RETRIES = 1 };
54
55MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
56MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
57MODULE_LICENSE("GPL");
58
59static inline void build_test(void)
60{
61 /* structures were not packed */
62 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
Boaz Harroshc6572c92009-01-25 17:09:40 +020063 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +020064 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
65}
66
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020067static const char *_osd_ver_desc(struct osd_request *or)
68{
69 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
70}
71
72#define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
73
74static int _osd_print_system_info(struct osd_dev *od, void *caps)
75{
76 struct osd_request *or;
77 struct osd_attr get_attrs[] = {
78 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
79 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
80 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
81 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
82 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
83 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
84 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
85 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
86 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
87 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
88 /* IBM-OSD-SIM Has a bug with this one put it last */
89 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
90 };
91 void *iter = NULL, *pFirst;
92 int nelem = ARRAY_SIZE(get_attrs), a = 0;
93 int ret;
94
95 or = osd_start_request(od, GFP_KERNEL);
96 if (!or)
97 return -ENOMEM;
98
99 /* get attrs */
100 osd_req_get_attributes(or, &osd_root_object);
101 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
102
103 ret = osd_finalize_request(or, 0, caps, NULL);
104 if (ret)
105 goto out;
106
107 ret = osd_execute_request(or);
108 if (ret) {
109 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
110 goto out;
111 }
112
113 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
114
115 OSD_INFO("Detected %s device\n",
116 _osd_ver_desc(or));
117
118 pFirst = get_attrs[a++].val_ptr;
119 OSD_INFO("OSD_ATTR_RI_VENDOR_IDENTIFICATION [%s]\n",
120 (char *)pFirst);
121
122 pFirst = get_attrs[a++].val_ptr;
123 OSD_INFO("OSD_ATTR_RI_PRODUCT_IDENTIFICATION [%s]\n",
124 (char *)pFirst);
125
126 pFirst = get_attrs[a++].val_ptr;
127 OSD_INFO("OSD_ATTR_RI_PRODUCT_MODEL [%s]\n",
128 (char *)pFirst);
129
130 pFirst = get_attrs[a++].val_ptr;
131 OSD_INFO("OSD_ATTR_RI_PRODUCT_REVISION_LEVEL [%u]\n",
132 get_unaligned_be32(pFirst));
133
134 pFirst = get_attrs[a++].val_ptr;
135 OSD_INFO("OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER [%s]\n",
136 (char *)pFirst);
137
138 pFirst = get_attrs[a].val_ptr;
139 OSD_INFO("OSD_ATTR_RI_OSD_NAME [%s]\n", (char *)pFirst);
140 a++;
141
142 pFirst = get_attrs[a++].val_ptr;
143 OSD_INFO("OSD_ATTR_RI_TOTAL_CAPACITY [0x%llx]\n",
144 _LLU(get_unaligned_be64(pFirst)));
145
146 pFirst = get_attrs[a++].val_ptr;
147 OSD_INFO("OSD_ATTR_RI_USED_CAPACITY [0x%llx]\n",
148 _LLU(get_unaligned_be64(pFirst)));
149
150 pFirst = get_attrs[a++].val_ptr;
151 OSD_INFO("OSD_ATTR_RI_NUMBER_OF_PARTITIONS [%llu]\n",
152 _LLU(get_unaligned_be64(pFirst)));
153
154 /* FIXME: Where are the time utilities */
155 pFirst = get_attrs[a++].val_ptr;
156 OSD_INFO("OSD_ATTR_RI_CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
157 ((char *)pFirst)[0], ((char *)pFirst)[1],
158 ((char *)pFirst)[2], ((char *)pFirst)[3],
159 ((char *)pFirst)[4], ((char *)pFirst)[5]);
160
161 if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
162 unsigned len = get_attrs[a].len;
163 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
164
165 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
166 sid_dump, sizeof(sid_dump), true);
167 OSD_INFO("OSD_ATTR_RI_OSD_SYSTEM_ID(%d) [%s]\n", len, sid_dump);
168 a++;
169 }
170out:
171 osd_end_request(or);
172 return ret;
173}
174
175int osd_auto_detect_ver(struct osd_dev *od, void *caps)
176{
177 int ret;
178
179 /* Auto-detect the osd version */
180 ret = _osd_print_system_info(od, caps);
181 if (ret) {
182 osd_dev_set_ver(od, OSD_VER1);
183 OSD_DEBUG("converting to OSD1\n");
184 ret = _osd_print_system_info(od, caps);
185 }
186
187 return ret;
188}
189EXPORT_SYMBOL(osd_auto_detect_ver);
190
Boaz Harrosh02941a52009-01-25 16:55:30 +0200191static unsigned _osd_req_cdb_len(struct osd_request *or)
192{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200193 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200194}
195
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200196static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
197{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200198 return osd_req_is_ver1(or) ?
199 osdv1_attr_list_elem_size(len) :
200 osdv2_attr_list_elem_size(len);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200201}
202
203static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
204{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200205 return osd_req_is_ver1(or) ?
206 osdv1_list_size(list_head) :
207 osdv2_list_size(list_head);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200208}
209
210static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
211{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200212 return osd_req_is_ver1(or) ?
213 sizeof(struct osdv1_attributes_list_header) :
214 sizeof(struct osdv2_attributes_list_header);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200215}
216
217static void _osd_req_set_alist_type(struct osd_request *or,
218 void *list, int list_type)
219{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200220 if (osd_req_is_ver1(or)) {
221 struct osdv1_attributes_list_header *attr_list = list;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200222
Boaz Harroshc6572c92009-01-25 17:09:40 +0200223 memset(attr_list, 0, sizeof(*attr_list));
224 attr_list->type = list_type;
225 } else {
226 struct osdv2_attributes_list_header *attr_list = list;
227
228 memset(attr_list, 0, sizeof(*attr_list));
229 attr_list->type = list_type;
230 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200231}
232
233static bool _osd_req_is_alist_type(struct osd_request *or,
234 void *list, int list_type)
235{
236 if (!list)
237 return false;
238
Boaz Harroshc6572c92009-01-25 17:09:40 +0200239 if (osd_req_is_ver1(or)) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200240 struct osdv1_attributes_list_header *attr_list = list;
241
242 return attr_list->type == list_type;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200243 } else {
244 struct osdv2_attributes_list_header *attr_list = list;
245
246 return attr_list->type == list_type;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200247 }
248}
249
Boaz Harrosh3e086132009-01-25 17:05:07 +0200250/* This is for List-objects not Attributes-Lists */
251static void _osd_req_encode_olist(struct osd_request *or,
252 struct osd_obj_id_list *list)
253{
254 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
255
Boaz Harroshc6572c92009-01-25 17:09:40 +0200256 if (osd_req_is_ver1(or)) {
257 cdbh->v1.list_identifier = list->list_identifier;
258 cdbh->v1.start_address = list->continuation_id;
259 } else {
260 cdbh->v2.list_identifier = list->list_identifier;
261 cdbh->v2.start_address = list->continuation_id;
262 }
Boaz Harrosh3e086132009-01-25 17:05:07 +0200263}
264
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200265static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
266 u64 offset, unsigned *padding)
267{
268 return __osd_encode_offset(offset, padding,
Boaz Harroshc6572c92009-01-25 17:09:40 +0200269 osd_req_is_ver1(or) ?
270 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
271 OSD_OFFSET_MAX_SHIFT);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200272}
273
Boaz Harrosh345c4352009-01-25 17:03:07 +0200274static struct osd_security_parameters *
275_osd_req_sec_params(struct osd_request *or)
276{
277 struct osd_cdb *ocdb = &or->cdb;
278
Boaz Harroshc6572c92009-01-25 17:09:40 +0200279 if (osd_req_is_ver1(or))
280 return &ocdb->v1.sec_params;
281 else
282 return &ocdb->v2.sec_params;
Boaz Harrosh345c4352009-01-25 17:03:07 +0200283}
284
Boaz Harrosh02941a52009-01-25 16:55:30 +0200285void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
286{
287 memset(osdd, 0, sizeof(*osdd));
288 osdd->scsi_device = scsi_device;
289 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200290#ifdef OSD_VER1_SUPPORT
291 osdd->version = OSD_VER2;
292#endif
Boaz Harrosh02941a52009-01-25 16:55:30 +0200293 /* TODO: Allocate pools for osd_request attributes ... */
294}
295EXPORT_SYMBOL(osd_dev_init);
296
297void osd_dev_fini(struct osd_dev *osdd)
298{
299 /* TODO: De-allocate pools */
300
301 osdd->scsi_device = NULL;
302}
303EXPORT_SYMBOL(osd_dev_fini);
304
305static struct osd_request *_osd_request_alloc(gfp_t gfp)
306{
307 struct osd_request *or;
308
309 /* TODO: Use mempool with one saved request */
310 or = kzalloc(sizeof(*or), gfp);
311 return or;
312}
313
314static void _osd_request_free(struct osd_request *or)
315{
316 kfree(or);
317}
318
319struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
320{
321 struct osd_request *or;
322
323 or = _osd_request_alloc(gfp);
324 if (!or)
325 return NULL;
326
327 or->osd_dev = dev;
328 or->alloc_flags = gfp;
329 or->timeout = dev->def_timeout;
330 or->retries = OSD_REQ_RETRIES;
331
332 return or;
333}
334EXPORT_SYMBOL(osd_start_request);
335
336/*
337 * If osd_finalize_request() was called but the request was not executed through
338 * the block layer, then we must release BIOs.
339 */
340static void _abort_unexecuted_bios(struct request *rq)
341{
342 struct bio *bio;
343
344 while ((bio = rq->bio) != NULL) {
345 rq->bio = bio->bi_next;
346 bio_endio(bio, 0);
347 }
348}
349
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200350static void _osd_free_seg(struct osd_request *or __unused,
351 struct _osd_req_data_segment *seg)
352{
353 if (!seg->buff || !seg->alloc_size)
354 return;
355
356 kfree(seg->buff);
357 seg->buff = NULL;
358 seg->alloc_size = 0;
359}
360
Boaz Harrosh02941a52009-01-25 16:55:30 +0200361void osd_end_request(struct osd_request *or)
362{
363 struct request *rq = or->request;
364
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200365 _osd_free_seg(or, &or->set_attr);
366 _osd_free_seg(or, &or->enc_get_attr);
367 _osd_free_seg(or, &or->get_attr);
368
Boaz Harrosh02941a52009-01-25 16:55:30 +0200369 if (rq) {
370 if (rq->next_rq) {
371 _abort_unexecuted_bios(rq->next_rq);
372 blk_put_request(rq->next_rq);
373 }
374
375 _abort_unexecuted_bios(rq);
376 blk_put_request(rq);
377 }
378 _osd_request_free(or);
379}
380EXPORT_SYMBOL(osd_end_request);
381
382int osd_execute_request(struct osd_request *or)
383{
384 return blk_execute_rq(or->request->q, NULL, or->request, 0);
385}
386EXPORT_SYMBOL(osd_execute_request);
387
388static void osd_request_async_done(struct request *req, int error)
389{
390 struct osd_request *or = req->end_io_data;
391
392 or->async_error = error;
393
394 if (error)
395 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
396
397 if (or->async_done)
398 or->async_done(or, or->async_private);
399 else
400 osd_end_request(or);
401}
402
403int osd_execute_request_async(struct osd_request *or,
404 osd_req_done_fn *done, void *private)
405{
406 or->request->end_io_data = or;
407 or->async_private = private;
408 or->async_done = done;
409
410 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
411 osd_request_async_done);
412 return 0;
413}
414EXPORT_SYMBOL(osd_execute_request_async);
415
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200416u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
417u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
418
419static int _osd_realloc_seg(struct osd_request *or,
420 struct _osd_req_data_segment *seg, unsigned max_bytes)
421{
422 void *buff;
423
424 if (seg->alloc_size >= max_bytes)
425 return 0;
426
427 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
428 if (!buff) {
429 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
430 seg->alloc_size);
431 return -ENOMEM;
432 }
433
434 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
435 seg->buff = buff;
436 seg->alloc_size = max_bytes;
437 return 0;
438}
439
440static int _alloc_set_attr_list(struct osd_request *or,
441 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
442{
443 unsigned total_bytes = add_bytes;
444
445 for (; nelem; --nelem, ++oa)
446 total_bytes += _osd_req_alist_elem_size(or, oa->len);
447
448 OSD_DEBUG("total_bytes=%d\n", total_bytes);
449 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
450}
451
452static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
453{
454 OSD_DEBUG("total_bytes=%d\n", max_bytes);
455 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
456}
457
458static int _alloc_get_attr_list(struct osd_request *or)
459{
460 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
461 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
462}
463
Boaz Harrosh02941a52009-01-25 16:55:30 +0200464/*
465 * Common to all OSD commands
466 */
467
468static void _osdv1_req_encode_common(struct osd_request *or,
469 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
470{
471 struct osdv1_cdb *ocdb = &or->cdb.v1;
472
473 /*
474 * For speed, the commands
475 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
476 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
477 * are not supported here. Should pass zero and set after the call
478 */
479 act &= cpu_to_be16(~0x0080); /* V1 action code */
480
481 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
482
483 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
484 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
485 ocdb->h.varlen_cdb.service_action = act;
486
487 ocdb->h.partition = cpu_to_be64(obj->partition);
488 ocdb->h.object = cpu_to_be64(obj->id);
489 ocdb->h.v1.length = cpu_to_be64(len);
490 ocdb->h.v1.start_address = cpu_to_be64(offset);
491}
492
Boaz Harroshc6572c92009-01-25 17:09:40 +0200493static void _osdv2_req_encode_common(struct osd_request *or,
494 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
495{
496 struct osdv2_cdb *ocdb = &or->cdb.v2;
497
498 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
499
500 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
501 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
502 ocdb->h.varlen_cdb.service_action = act;
503
504 ocdb->h.partition = cpu_to_be64(obj->partition);
505 ocdb->h.object = cpu_to_be64(obj->id);
506 ocdb->h.v2.length = cpu_to_be64(len);
507 ocdb->h.v2.start_address = cpu_to_be64(offset);
508}
509
Boaz Harrosh02941a52009-01-25 16:55:30 +0200510static void _osd_req_encode_common(struct osd_request *or,
511 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
512{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200513 if (osd_req_is_ver1(or))
514 _osdv1_req_encode_common(or, act, obj, offset, len);
515 else
516 _osdv2_req_encode_common(or, act, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200517}
518
519/*
520 * Device commands
521 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200522/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
523/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
524
Boaz Harrosh02941a52009-01-25 16:55:30 +0200525void osd_req_format(struct osd_request *or, u64 tot_capacity)
526{
527 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
528 tot_capacity);
529}
530EXPORT_SYMBOL(osd_req_format);
531
Boaz Harrosh3e086132009-01-25 17:05:07 +0200532int osd_req_list_dev_partitions(struct osd_request *or,
533 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
534{
535 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
536}
537EXPORT_SYMBOL(osd_req_list_dev_partitions);
538
539static void _osd_req_encode_flush(struct osd_request *or,
540 enum osd_options_flush_scope_values op)
541{
542 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
543
544 ocdb->command_specific_options = op;
545}
546
547void osd_req_flush_obsd(struct osd_request *or,
548 enum osd_options_flush_scope_values op)
549{
550 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
551 _osd_req_encode_flush(or, op);
552}
553EXPORT_SYMBOL(osd_req_flush_obsd);
554
Boaz Harroshae30c992009-01-25 17:07:14 +0200555/*TODO: void osd_req_perform_scsi_command(struct osd_request *,
556 const u8 *cdb, ...); */
557/*TODO: void osd_req_task_management(struct osd_request *, ...); */
558
Boaz Harrosh02941a52009-01-25 16:55:30 +0200559/*
560 * Partition commands
561 */
562static void _osd_req_encode_partition(struct osd_request *or,
563 __be16 act, osd_id partition)
564{
565 struct osd_obj_id par = {
566 .partition = partition,
567 .id = 0,
568 };
569
570 _osd_req_encode_common(or, act, &par, 0, 0);
571}
572
573void osd_req_create_partition(struct osd_request *or, osd_id partition)
574{
575 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
576}
577EXPORT_SYMBOL(osd_req_create_partition);
578
579void osd_req_remove_partition(struct osd_request *or, osd_id partition)
580{
581 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
582}
583EXPORT_SYMBOL(osd_req_remove_partition);
584
Boaz Harroshae30c992009-01-25 17:07:14 +0200585/*TODO: void osd_req_set_partition_key(struct osd_request *,
586 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
587 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
588
Boaz Harrosh3e086132009-01-25 17:05:07 +0200589static int _osd_req_list_objects(struct osd_request *or,
590 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
591 struct osd_obj_id_list *list, unsigned nelem)
592{
593 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
594 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
595 struct bio *bio;
596
597 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
598
599 if (list->list_identifier)
600 _osd_req_encode_olist(or, list);
601
602 WARN_ON(or->in.bio);
603 bio = bio_map_kern(q, list, len, or->alloc_flags);
604 if (!bio) {
605 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
606 return -ENOMEM;
607 }
608
609 bio->bi_rw &= ~(1 << BIO_RW);
610 or->in.bio = bio;
611 or->in.total_bytes = bio->bi_size;
612 return 0;
613}
614
615int osd_req_list_partition_collections(struct osd_request *or,
616 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
617 unsigned nelem)
618{
619 struct osd_obj_id par = {
620 .partition = partition,
621 .id = 0,
622 };
623
624 return osd_req_list_collection_objects(or, &par, initial_id, list,
625 nelem);
626}
627EXPORT_SYMBOL(osd_req_list_partition_collections);
628
629int osd_req_list_partition_objects(struct osd_request *or,
630 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
631 unsigned nelem)
632{
633 struct osd_obj_id par = {
634 .partition = partition,
635 .id = 0,
636 };
637
638 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
639 nelem);
640}
641EXPORT_SYMBOL(osd_req_list_partition_objects);
642
643void osd_req_flush_partition(struct osd_request *or,
644 osd_id partition, enum osd_options_flush_scope_values op)
645{
646 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
647 _osd_req_encode_flush(or, op);
648}
649EXPORT_SYMBOL(osd_req_flush_partition);
650
651/*
652 * Collection commands
653 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200654/*TODO: void osd_req_create_collection(struct osd_request *,
655 const struct osd_obj_id *); */
656/*TODO: void osd_req_remove_collection(struct osd_request *,
657 const struct osd_obj_id *); */
658
Boaz Harrosh3e086132009-01-25 17:05:07 +0200659int osd_req_list_collection_objects(struct osd_request *or,
660 const struct osd_obj_id *obj, osd_id initial_id,
661 struct osd_obj_id_list *list, unsigned nelem)
662{
663 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
664 initial_id, list, nelem);
665}
666EXPORT_SYMBOL(osd_req_list_collection_objects);
667
Boaz Harroshae30c992009-01-25 17:07:14 +0200668/*TODO: void query(struct osd_request *, ...); V2 */
669
Boaz Harrosh3e086132009-01-25 17:05:07 +0200670void osd_req_flush_collection(struct osd_request *or,
671 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
672{
673 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
674 _osd_req_encode_flush(or, op);
675}
676EXPORT_SYMBOL(osd_req_flush_collection);
677
Boaz Harroshae30c992009-01-25 17:07:14 +0200678/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
679/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
680
Boaz Harrosh02941a52009-01-25 16:55:30 +0200681/*
682 * Object commands
683 */
684void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
685{
686 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
687}
688EXPORT_SYMBOL(osd_req_create_object);
689
690void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
691{
692 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
693}
694EXPORT_SYMBOL(osd_req_remove_object);
695
Boaz Harroshae30c992009-01-25 17:07:14 +0200696
697/*TODO: void osd_req_create_multi(struct osd_request *or,
698 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
699*/
700
Boaz Harrosh02941a52009-01-25 16:55:30 +0200701void osd_req_write(struct osd_request *or,
702 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
703{
704 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
705 WARN_ON(or->out.bio || or->out.total_bytes);
706 bio->bi_rw |= (1 << BIO_RW);
707 or->out.bio = bio;
708 or->out.total_bytes = bio->bi_size;
709}
710EXPORT_SYMBOL(osd_req_write);
711
Boaz Harroshae30c992009-01-25 17:07:14 +0200712/*TODO: void osd_req_append(struct osd_request *,
713 const struct osd_obj_id *, struct bio *data_out); */
714/*TODO: void osd_req_create_write(struct osd_request *,
715 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
716/*TODO: void osd_req_clear(struct osd_request *,
717 const struct osd_obj_id *, u64 offset, u64 len); */
718/*TODO: void osd_req_punch(struct osd_request *,
719 const struct osd_obj_id *, u64 offset, u64 len); V2 */
720
Boaz Harrosh3e086132009-01-25 17:05:07 +0200721void osd_req_flush_object(struct osd_request *or,
722 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
723 /*V2*/ u64 offset, /*V2*/ u64 len)
724{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200725 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
726 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
727 offset = 0;
728 len = 0;
729 }
730
Boaz Harrosh3e086132009-01-25 17:05:07 +0200731 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
732 _osd_req_encode_flush(or, op);
733}
734EXPORT_SYMBOL(osd_req_flush_object);
735
Boaz Harrosh02941a52009-01-25 16:55:30 +0200736void osd_req_read(struct osd_request *or,
737 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
738{
739 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
740 WARN_ON(or->in.bio || or->in.total_bytes);
741 bio->bi_rw &= ~(1 << BIO_RW);
742 or->in.bio = bio;
743 or->in.total_bytes = bio->bi_size;
744}
745EXPORT_SYMBOL(osd_req_read);
746
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200747void osd_req_get_attributes(struct osd_request *or,
748 const struct osd_obj_id *obj)
749{
750 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
751}
752EXPORT_SYMBOL(osd_req_get_attributes);
753
754void osd_req_set_attributes(struct osd_request *or,
755 const struct osd_obj_id *obj)
756{
757 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
758}
759EXPORT_SYMBOL(osd_req_set_attributes);
760
761/*
762 * Attributes List-mode
763 */
764
765int osd_req_add_set_attr_list(struct osd_request *or,
766 const struct osd_attr *oa, unsigned nelem)
767{
768 unsigned total_bytes = or->set_attr.total_bytes;
769 void *attr_last;
770 int ret;
771
772 if (or->attributes_mode &&
773 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
774 WARN_ON(1);
775 return -EINVAL;
776 }
777 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
778
779 if (!total_bytes) { /* first-time: allocate and put list header */
780 total_bytes = _osd_req_sizeof_alist_header(or);
781 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
782 if (ret)
783 return ret;
784 _osd_req_set_alist_type(or, or->set_attr.buff,
785 OSD_ATTR_LIST_SET_RETRIEVE);
786 }
787 attr_last = or->set_attr.buff + total_bytes;
788
789 for (; nelem; --nelem) {
790 struct osd_attributes_list_element *attr;
791 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
792
793 total_bytes += elem_size;
794 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
795 or->set_attr.total_bytes = total_bytes - elem_size;
796 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
797 if (ret)
798 return ret;
799 attr_last =
800 or->set_attr.buff + or->set_attr.total_bytes;
801 }
802
803 attr = attr_last;
804 attr->attr_page = cpu_to_be32(oa->attr_page);
805 attr->attr_id = cpu_to_be32(oa->attr_id);
806 attr->attr_bytes = cpu_to_be16(oa->len);
807 memcpy(attr->attr_val, oa->val_ptr, oa->len);
808
809 attr_last += elem_size;
810 ++oa;
811 }
812
813 or->set_attr.total_bytes = total_bytes;
814 return 0;
815}
816EXPORT_SYMBOL(osd_req_add_set_attr_list);
817
818static int _append_map_kern(struct request *req,
819 void *buff, unsigned len, gfp_t flags)
820{
821 struct bio *bio;
822 int ret;
823
824 bio = bio_map_kern(req->q, buff, len, flags);
825 if (IS_ERR(bio)) {
826 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
827 PTR_ERR(bio));
828 return PTR_ERR(bio);
829 }
830 ret = blk_rq_append_bio(req->q, req, bio);
831 if (ret) {
832 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
833 bio_put(bio);
834 }
835 return ret;
836}
837
838static int _req_append_segment(struct osd_request *or,
839 unsigned padding, struct _osd_req_data_segment *seg,
840 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
841{
842 void *pad_buff;
843 int ret;
844
845 if (padding) {
846 /* check if we can just add it to last buffer */
847 if (last_seg &&
848 (padding <= last_seg->alloc_size - last_seg->total_bytes))
849 pad_buff = last_seg->buff + last_seg->total_bytes;
850 else
851 pad_buff = io->pad_buff;
852
853 ret = _append_map_kern(io->req, pad_buff, padding,
854 or->alloc_flags);
855 if (ret)
856 return ret;
857 io->total_bytes += padding;
858 }
859
860 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
861 or->alloc_flags);
862 if (ret)
863 return ret;
864
865 io->total_bytes += seg->total_bytes;
866 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
867 seg->total_bytes);
868 return 0;
869}
870
871static int _osd_req_finalize_set_attr_list(struct osd_request *or)
872{
873 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
874 unsigned padding;
875 int ret;
876
877 if (!or->set_attr.total_bytes) {
878 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
879 return 0;
880 }
881
882 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
883 cdbh->attrs_list.set_attr_offset =
884 osd_req_encode_offset(or, or->out.total_bytes, &padding);
885
886 ret = _req_append_segment(or, padding, &or->set_attr,
887 or->out.last_seg, &or->out);
888 if (ret)
889 return ret;
890
891 or->out.last_seg = &or->set_attr;
892 return 0;
893}
894
895int osd_req_add_get_attr_list(struct osd_request *or,
896 const struct osd_attr *oa, unsigned nelem)
897{
898 unsigned total_bytes = or->enc_get_attr.total_bytes;
899 void *attr_last;
900 int ret;
901
902 if (or->attributes_mode &&
903 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
904 WARN_ON(1);
905 return -EINVAL;
906 }
907 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
908
909 /* first time calc data-in list header size */
910 if (!or->get_attr.total_bytes)
911 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
912
913 /* calc data-out info */
914 if (!total_bytes) { /* first-time: allocate and put list header */
915 unsigned max_bytes;
916
917 total_bytes = _osd_req_sizeof_alist_header(or);
918 max_bytes = total_bytes +
919 nelem * sizeof(struct osd_attributes_list_attrid);
920 ret = _alloc_get_attr_desc(or, max_bytes);
921 if (ret)
922 return ret;
923
924 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
925 OSD_ATTR_LIST_GET);
926 }
927 attr_last = or->enc_get_attr.buff + total_bytes;
928
929 for (; nelem; --nelem) {
930 struct osd_attributes_list_attrid *attrid;
931 const unsigned cur_size = sizeof(*attrid);
932
933 total_bytes += cur_size;
934 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
935 or->enc_get_attr.total_bytes = total_bytes - cur_size;
936 ret = _alloc_get_attr_desc(or,
937 total_bytes + nelem * sizeof(*attrid));
938 if (ret)
939 return ret;
940 attr_last = or->enc_get_attr.buff +
941 or->enc_get_attr.total_bytes;
942 }
943
944 attrid = attr_last;
945 attrid->attr_page = cpu_to_be32(oa->attr_page);
946 attrid->attr_id = cpu_to_be32(oa->attr_id);
947
948 attr_last += cur_size;
949
950 /* calc data-in size */
951 or->get_attr.total_bytes +=
952 _osd_req_alist_elem_size(or, oa->len);
953 ++oa;
954 }
955
956 or->enc_get_attr.total_bytes = total_bytes;
957
958 OSD_DEBUG(
959 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
960 or->get_attr.total_bytes,
961 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
962 or->enc_get_attr.total_bytes,
963 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
964 / sizeof(struct osd_attributes_list_attrid));
965
966 return 0;
967}
968EXPORT_SYMBOL(osd_req_add_get_attr_list);
969
970static int _osd_req_finalize_get_attr_list(struct osd_request *or)
971{
972 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
973 unsigned out_padding;
974 unsigned in_padding;
975 int ret;
976
977 if (!or->enc_get_attr.total_bytes) {
978 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
979 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
980 return 0;
981 }
982
983 ret = _alloc_get_attr_list(or);
984 if (ret)
985 return ret;
986
987 /* The out-going buffer info update */
988 OSD_DEBUG("out-going\n");
989 cdbh->attrs_list.get_attr_desc_bytes =
990 cpu_to_be32(or->enc_get_attr.total_bytes);
991
992 cdbh->attrs_list.get_attr_desc_offset =
993 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
994
995 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
996 or->out.last_seg, &or->out);
997 if (ret)
998 return ret;
999 or->out.last_seg = &or->enc_get_attr;
1000
1001 /* The incoming buffer info update */
1002 OSD_DEBUG("in-coming\n");
1003 cdbh->attrs_list.get_attr_alloc_length =
1004 cpu_to_be32(or->get_attr.total_bytes);
1005
1006 cdbh->attrs_list.get_attr_offset =
1007 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1008
1009 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1010 &or->in);
1011 if (ret)
1012 return ret;
1013 or->in.last_seg = &or->get_attr;
1014
1015 return 0;
1016}
1017
1018int osd_req_decode_get_attr_list(struct osd_request *or,
1019 struct osd_attr *oa, int *nelem, void **iterator)
1020{
1021 unsigned cur_bytes, returned_bytes;
1022 int n;
1023 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1024 void *cur_p;
1025
1026 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1027 OSD_ATTR_LIST_SET_RETRIEVE)) {
1028 oa->attr_page = 0;
1029 oa->attr_id = 0;
1030 oa->val_ptr = NULL;
1031 oa->len = 0;
1032 *iterator = NULL;
1033 return 0;
1034 }
1035
1036 if (*iterator) {
1037 BUG_ON((*iterator < or->get_attr.buff) ||
1038 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1039 cur_p = *iterator;
1040 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1041 returned_bytes = or->get_attr.total_bytes;
1042 } else { /* first time decode the list header */
1043 cur_bytes = sizeof_attr_list;
1044 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1045 sizeof_attr_list;
1046
1047 cur_p = or->get_attr.buff + sizeof_attr_list;
1048
1049 if (returned_bytes > or->get_attr.alloc_size) {
1050 OSD_DEBUG("target report: space was not big enough! "
1051 "Allocate=%u Needed=%u\n",
1052 or->get_attr.alloc_size,
1053 returned_bytes + sizeof_attr_list);
1054
1055 returned_bytes =
1056 or->get_attr.alloc_size - sizeof_attr_list;
1057 }
1058 or->get_attr.total_bytes = returned_bytes;
1059 }
1060
1061 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
1062 struct osd_attributes_list_element *attr = cur_p;
1063 unsigned inc;
1064
1065 oa->len = be16_to_cpu(attr->attr_bytes);
1066 inc = _osd_req_alist_elem_size(or, oa->len);
1067 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
1068 oa->len, inc, cur_bytes);
1069 cur_bytes += inc;
1070 if (cur_bytes > returned_bytes) {
1071 OSD_ERR("BAD FOOD from target. list not valid!"
1072 "c=%d r=%d n=%d\n",
1073 cur_bytes, returned_bytes, n);
1074 oa->val_ptr = NULL;
1075 break;
1076 }
1077
1078 oa->attr_page = be32_to_cpu(attr->attr_page);
1079 oa->attr_id = be32_to_cpu(attr->attr_id);
1080 oa->val_ptr = attr->attr_val;
1081
1082 cur_p += inc;
1083 ++oa;
1084 }
1085
1086 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1087 *nelem = n;
1088 return returned_bytes - cur_bytes;
1089}
1090EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1091
1092/*
1093 * Attributes Page-mode
1094 */
1095
1096int osd_req_add_get_attr_page(struct osd_request *or,
1097 u32 page_id, void *attar_page, unsigned max_page_len,
1098 const struct osd_attr *set_one_attr)
1099{
1100 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1101
1102 if (or->attributes_mode &&
1103 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1104 WARN_ON(1);
1105 return -EINVAL;
1106 }
1107 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1108
1109 or->get_attr.buff = attar_page;
1110 or->get_attr.total_bytes = max_page_len;
1111
1112 or->set_attr.buff = set_one_attr->val_ptr;
1113 or->set_attr.total_bytes = set_one_attr->len;
1114
1115 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1116 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
1117 /* ocdb->attrs_page.get_attr_offset; */
1118
1119 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1120 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1121 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
1122 /* ocdb->attrs_page.set_attr_offset; */
1123 return 0;
1124}
1125EXPORT_SYMBOL(osd_req_add_get_attr_page);
1126
1127static int _osd_req_finalize_attr_page(struct osd_request *or)
1128{
1129 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1130 unsigned in_padding, out_padding;
1131 int ret;
1132
1133 /* returned page */
1134 cdbh->attrs_page.get_attr_offset =
1135 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1136
1137 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1138 &or->in);
1139 if (ret)
1140 return ret;
1141
1142 /* set one value */
1143 cdbh->attrs_page.set_attr_offset =
1144 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1145
1146 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1147 &or->out);
1148 return ret;
1149}
1150
Boaz Harrosh345c4352009-01-25 17:03:07 +02001151static int _osd_req_finalize_data_integrity(struct osd_request *or,
1152 bool has_in, bool has_out, const u8 *cap_key)
1153{
1154 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1155 int ret;
1156
1157 if (!osd_is_sec_alldata(sec_parms))
1158 return 0;
1159
1160 if (has_out) {
1161 struct _osd_req_data_segment seg = {
1162 .buff = &or->out_data_integ,
1163 .total_bytes = sizeof(or->out_data_integ),
1164 };
1165 unsigned pad;
1166
1167 or->out_data_integ.data_bytes = cpu_to_be64(
1168 or->out.bio ? or->out.bio->bi_size : 0);
1169 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1170 or->set_attr.total_bytes);
1171 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1172 or->enc_get_attr.total_bytes);
1173
1174 sec_parms->data_out_integrity_check_offset =
1175 osd_req_encode_offset(or, or->out.total_bytes, &pad);
1176
1177 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1178 &or->out);
1179 if (ret)
1180 return ret;
1181 or->out.last_seg = NULL;
1182
1183 /* they are now all chained to request sign them all together */
1184 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1185 cap_key);
1186 }
1187
1188 if (has_in) {
1189 struct _osd_req_data_segment seg = {
1190 .buff = &or->in_data_integ,
1191 .total_bytes = sizeof(or->in_data_integ),
1192 };
1193 unsigned pad;
1194
1195 sec_parms->data_in_integrity_check_offset =
1196 osd_req_encode_offset(or, or->in.total_bytes, &pad);
1197
1198 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1199 &or->in);
1200 if (ret)
1201 return ret;
1202
1203 or->in.last_seg = NULL;
1204 }
1205
1206 return 0;
1207}
1208
Boaz Harrosh02941a52009-01-25 16:55:30 +02001209/*
1210 * osd_finalize_request and helpers
1211 */
1212
1213static int _init_blk_request(struct osd_request *or,
1214 bool has_in, bool has_out)
1215{
1216 gfp_t flags = or->alloc_flags;
1217 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1218 struct request_queue *q = scsi_device->request_queue;
1219 struct request *req;
1220 int ret = -ENOMEM;
1221
1222 req = blk_get_request(q, has_out, flags);
1223 if (!req)
1224 goto out;
1225
1226 or->request = req;
1227 req->cmd_type = REQ_TYPE_BLOCK_PC;
1228 req->timeout = or->timeout;
1229 req->retries = or->retries;
1230 req->sense = or->sense;
1231 req->sense_len = 0;
1232
1233 if (has_out) {
1234 or->out.req = req;
1235 if (has_in) {
1236 /* allocate bidi request */
1237 req = blk_get_request(q, READ, flags);
1238 if (!req) {
1239 OSD_DEBUG("blk_get_request for bidi failed\n");
1240 goto out;
1241 }
1242 req->cmd_type = REQ_TYPE_BLOCK_PC;
1243 or->in.req = or->request->next_rq = req;
1244 }
1245 } else if (has_in)
1246 or->in.req = req;
1247
1248 ret = 0;
1249out:
1250 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1251 or, has_in, has_out, ret, or->request);
1252 return ret;
1253}
1254
1255int osd_finalize_request(struct osd_request *or,
1256 u8 options, const void *cap, const u8 *cap_key)
1257{
1258 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1259 bool has_in, has_out;
1260 int ret;
1261
1262 if (options & OSD_REQ_FUA)
1263 cdbh->options |= OSD_CDB_FUA;
1264
1265 if (options & OSD_REQ_DPO)
1266 cdbh->options |= OSD_CDB_DPO;
1267
1268 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1269 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1270
1271 osd_set_caps(&or->cdb, cap);
1272
1273 has_in = or->in.bio || or->get_attr.total_bytes;
1274 has_out = or->out.bio || or->set_attr.total_bytes ||
1275 or->enc_get_attr.total_bytes;
1276
1277 ret = _init_blk_request(or, has_in, has_out);
1278 if (ret) {
1279 OSD_DEBUG("_init_blk_request failed\n");
1280 return ret;
1281 }
1282
1283 if (or->out.bio) {
1284 ret = blk_rq_append_bio(or->request->q, or->out.req,
1285 or->out.bio);
1286 if (ret) {
1287 OSD_DEBUG("blk_rq_append_bio out failed\n");
1288 return ret;
1289 }
1290 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1291 _LLU(or->out.total_bytes), or->out.req->data_len);
1292 }
1293 if (or->in.bio) {
1294 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1295 if (ret) {
1296 OSD_DEBUG("blk_rq_append_bio in failed\n");
1297 return ret;
1298 }
1299 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1300 _LLU(or->in.total_bytes), or->in.req->data_len);
1301 }
1302
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001303 or->out.pad_buff = sg_out_pad_buffer;
1304 or->in.pad_buff = sg_in_pad_buffer;
1305
Boaz Harrosh02941a52009-01-25 16:55:30 +02001306 if (!or->attributes_mode)
1307 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1308 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001309 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1310 ret = _osd_req_finalize_attr_page(or);
1311 } else {
1312 /* TODO: I think that for the GET_ATTR command these 2 should
1313 * be reversed to keep them in execution order (for embeded
1314 * targets with low memory footprint)
1315 */
1316 ret = _osd_req_finalize_set_attr_list(or);
1317 if (ret) {
1318 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1319 return ret;
1320 }
1321
1322 ret = _osd_req_finalize_get_attr_list(or);
1323 if (ret) {
1324 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1325 return ret;
1326 }
1327 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001328
Boaz Harrosh345c4352009-01-25 17:03:07 +02001329 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1330 if (ret)
1331 return ret;
1332
1333 osd_sec_sign_cdb(&or->cdb, cap_key);
1334
Boaz Harrosh02941a52009-01-25 16:55:30 +02001335 or->request->cmd = or->cdb.buff;
1336 or->request->cmd_len = _osd_req_cdb_len(or);
1337
1338 return 0;
1339}
1340EXPORT_SYMBOL(osd_finalize_request);
1341
1342/*
1343 * Implementation of osd_sec.h API
1344 * TODO: Move to a separate osd_sec.c file at a later stage.
1345 */
1346
1347enum { OSD_SEC_CAP_V1_ALL_CAPS =
1348 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1349 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1350 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1351 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1352};
1353
Boaz Harroshc6572c92009-01-25 17:09:40 +02001354enum { OSD_SEC_CAP_V2_ALL_CAPS =
1355 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1356};
1357
Boaz Harrosh02941a52009-01-25 16:55:30 +02001358void osd_sec_init_nosec_doall_caps(void *caps,
1359 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1360{
1361 struct osd_capability *cap = caps;
1362 u8 type;
1363 u8 descriptor_type;
1364
1365 if (likely(obj->id)) {
1366 if (unlikely(is_collection)) {
1367 type = OSD_SEC_OBJ_COLLECTION;
1368 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1369 OSD_SEC_OBJ_DESC_COL;
1370 } else {
1371 type = OSD_SEC_OBJ_USER;
1372 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1373 }
1374 WARN_ON(!obj->partition);
1375 } else {
1376 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1377 OSD_SEC_OBJ_ROOT;
1378 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1379 }
1380
1381 memset(cap, 0, sizeof(*cap));
1382
1383 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1384 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1385 cap->h.security_method = OSD_SEC_NOSEC;
1386/* cap->expiration_time;
1387 cap->AUDIT[30-10];
1388 cap->discriminator[42-30];
1389 cap->object_created_time; */
1390 cap->h.object_type = type;
1391 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1392 cap->h.object_descriptor_type = descriptor_type;
1393 cap->od.obj_desc.policy_access_tag = 0;
1394 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1395 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1396}
1397EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1398
Boaz Harroshc6572c92009-01-25 17:09:40 +02001399/* FIXME: Extract version from caps pointer.
1400 * Also Pete's target only supports caps from OSDv1 for now
1401 */
Boaz Harrosh02941a52009-01-25 16:55:30 +02001402void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1403{
Boaz Harroshc6572c92009-01-25 17:09:40 +02001404 bool is_ver1 = true;
1405 /* NOTE: They start at same address */
1406 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001407}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001408
Boaz Harrosh345c4352009-01-25 17:03:07 +02001409bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1410{
1411 return false;
1412}
1413
1414void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1415{
1416}
1417
1418void osd_sec_sign_data(void *data_integ __unused,
1419 struct bio *bio __unused, const u8 *cap_key __unused)
1420{
1421}
1422
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001423/*
1424 * Declared in osd_protocol.h
1425 * 4.12.5 Data-In and Data-Out buffer offsets
1426 * byte offset = mantissa * (2^(exponent+8))
1427 * Returns the smallest allowed encoded offset that contains given @offset
1428 * The actual encoded offset returned is @offset + *@padding.
1429 */
1430osd_cdb_offset __osd_encode_offset(
1431 u64 offset, unsigned *padding, int min_shift, int max_shift)
1432{
1433 u64 try_offset = -1, mod, align;
1434 osd_cdb_offset be32_offset;
1435 int shift;
1436
1437 *padding = 0;
1438 if (!offset)
1439 return 0;
1440
1441 for (shift = min_shift; shift < max_shift; ++shift) {
1442 try_offset = offset >> shift;
1443 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1444 break;
1445 }
1446
1447 BUG_ON(shift == max_shift);
1448
1449 align = 1 << shift;
1450 mod = offset & (align - 1);
1451 if (mod) {
1452 *padding = align - mod;
1453 try_offset += 1;
1454 }
1455
1456 try_offset |= ((shift - 8) & 0xf) << 28;
1457 be32_offset = cpu_to_be32((u32)try_offset);
1458
1459 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1460 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1461 be32_offset, *padding);
1462 return be32_offset;
1463}