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