/*1* osd_initiator.h - OSD initiator API definition2*3* Copyright (C) 2008 Panasas Inc. All rights reserved.4*5* Authors:6* Boaz Harrosh <[email protected]>7* Benny Halevy <[email protected]>8*9* This program is free software; you can redistribute it and/or modify10* it under the terms of the GNU General Public License version 211*12*/13#ifndef __OSD_INITIATOR_H__14#define __OSD_INITIATOR_H__1516#include "osd_protocol.h"17#include "osd_types.h"1819#include <linux/blkdev.h>20#include <scsi/scsi_device.h>2122/* Note: "NI" in comments below means "Not Implemented yet" */2324/* Configure of code:25* #undef if you *don't* want OSD v1 support in runtime.26* If #defined the initiator will dynamically configure to encode OSD v127* CDB's if the target is detected to be OSD v1 only.28* OSD v2 only commands, options, and attributes will be ignored if target29* is v1 only.30* If #defined will result in bigger/slower code (OK Slower maybe not)31* Q: Should this be CONFIG_SCSI_OSD_VER1_SUPPORT and set from Kconfig?32*/33#define OSD_VER1_SUPPORT y3435enum osd_std_version {36OSD_VER_NONE = 0,37OSD_VER1 = 1,38OSD_VER2 = 2,39};4041/*42* Object-based Storage Device.43* This object represents an OSD device.44* It is not a full linux device in any way. It is only45* a place to hang resources associated with a Linux46* request Q and some default properties.47*/48struct osd_dev {49struct scsi_device *scsi_device;50unsigned def_timeout;5152#ifdef OSD_VER1_SUPPORT53enum osd_std_version version;54#endif55};5657/* Unique Identification of an OSD device */58struct osd_dev_info {59unsigned systemid_len;60u8 systemid[OSD_SYSTEMID_LEN];61unsigned osdname_len;62u8 *osdname;63};6465/* Retrieve/return osd_dev(s) for use by Kernel clients66* Use IS_ERR/ERR_PTR on returned "osd_dev *".67*/68struct osd_dev *osduld_path_lookup(const char *dev_name);69struct osd_dev *osduld_info_lookup(const struct osd_dev_info *odi);70void osduld_put_device(struct osd_dev *od);7172const struct osd_dev_info *osduld_device_info(struct osd_dev *od);73bool osduld_device_same(struct osd_dev *od, const struct osd_dev_info *odi);7475/* Add/remove test ioctls from external modules */76typedef int (do_test_fn)(struct osd_dev *od, unsigned cmd, unsigned long arg);77int osduld_register_test(unsigned ioctl, do_test_fn *do_test);78void osduld_unregister_test(unsigned ioctl);7980/* These are called by uld at probe time */81void osd_dev_init(struct osd_dev *od, struct scsi_device *scsi_device);82void osd_dev_fini(struct osd_dev *od);8384/**85* osd_auto_detect_ver - Detect the OSD version, return Unique Identification86*87* @od: OSD target lun handle88* @caps: Capabilities authorizing OSD root read attributes access89* @odi: Retrieved information uniquely identifying the osd target lun90* Note: odi->osdname must be kfreed by caller.91*92* Auto detects the OSD version of the OSD target and sets the @od93* accordingly. Meanwhile also returns the "system id" and "osd name" root94* attributes which uniquely identify the OSD target. This member is usually95* called by the ULD. ULD users should call osduld_device_info().96* This rutine allocates osd requests and memory at GFP_KERNEL level and might97* sleep.98*/99int osd_auto_detect_ver(struct osd_dev *od,100void *caps, struct osd_dev_info *odi);101102static inline struct request_queue *osd_request_queue(struct osd_dev *od)103{104return od->scsi_device->request_queue;105}106107/* we might want to use function vector in the future */108static inline void osd_dev_set_ver(struct osd_dev *od, enum osd_std_version v)109{110#ifdef OSD_VER1_SUPPORT111od->version = v;112#endif113}114115static inline bool osd_dev_is_ver1(struct osd_dev *od)116{117#ifdef OSD_VER1_SUPPORT118return od->version == OSD_VER1;119#else120return false;121#endif122}123124struct osd_request;125typedef void (osd_req_done_fn)(struct osd_request *or, void *private);126127struct osd_request {128struct osd_cdb cdb;129struct osd_data_out_integrity_info out_data_integ;130struct osd_data_in_integrity_info in_data_integ;131132struct osd_dev *osd_dev;133struct request *request;134135struct _osd_req_data_segment {136void *buff;137unsigned alloc_size; /* 0 here means: don't call kfree */138unsigned total_bytes;139} cdb_cont, set_attr, enc_get_attr, get_attr;140141struct _osd_io_info {142struct bio *bio;143u64 total_bytes;144u64 residual;145struct request *req;146struct _osd_req_data_segment *last_seg;147u8 *pad_buff;148} out, in;149150gfp_t alloc_flags;151unsigned timeout;152unsigned retries;153unsigned sense_len;154u8 sense[OSD_MAX_SENSE_LEN];155enum osd_attributes_mode attributes_mode;156157osd_req_done_fn *async_done;158void *async_private;159int async_error;160int req_errors;161};162163static inline bool osd_req_is_ver1(struct osd_request *or)164{165return osd_dev_is_ver1(or->osd_dev);166}167168/*169* How to use the osd library:170*171* osd_start_request172* Allocates a request.173*174* osd_req_*175* Call one of, to encode the desired operation.176*177* osd_add_{get,set}_attr178* Optionally add attributes to the CDB, list or page mode.179*180* osd_finalize_request181* Computes final data out/in offsets and signs the request,182* making it ready for execution.183*184* osd_execute_request185* May be called to execute it through the block layer. Other wise submit186* the associated block request in some other way.187*188* After execution:189* osd_req_decode_sense190* Decodes sense information to verify execution results.191*192* osd_req_decode_get_attr193* Retrieve osd_add_get_attr_list() values if used.194*195* osd_end_request196* Must be called to deallocate the request.197*/198199/**200* osd_start_request - Allocate and initialize an osd_request201*202* @osd_dev: OSD device that holds the scsi-device and default values203* that the request is associated with.204* @gfp: The allocation flags to use for request allocation, and all205* subsequent allocations. This will be stored at206* osd_request->alloc_flags, can be changed by user later207*208* Allocate osd_request and initialize all members to the209* default/initial state.210*/211struct osd_request *osd_start_request(struct osd_dev *od, gfp_t gfp);212213enum osd_req_options {214OSD_REQ_FUA = 0x08, /* Force Unit Access */215OSD_REQ_DPO = 0x10, /* Disable Page Out */216217OSD_REQ_BYPASS_TIMESTAMPS = 0x80,218};219220/**221* osd_finalize_request - Sign request and prepare request for execution222*223* @or: osd_request to prepare224* @options: combination of osd_req_options bit flags or 0.225* @cap: A Pointer to an OSD_CAP_LEN bytes buffer that is received from226* The security manager as capabilities for this cdb.227* @cap_key: The cryptographic key used to sign the cdb/data. Can be null228* if NOSEC is used.229*230* The actual request and bios are only allocated here, so are the get_attr231* buffers that will receive the returned attributes. Copy's @cap to cdb.232* Sign the cdb/data with @cap_key.233*/234int osd_finalize_request(struct osd_request *or,235u8 options, const void *cap, const u8 *cap_key);236237/**238* osd_execute_request - Execute the request synchronously through block-layer239*240* @or: osd_request to Executed241*242* Calls blk_execute_rq to q the command and waits for completion.243*/244int osd_execute_request(struct osd_request *or);245246/**247* osd_execute_request_async - Execute the request without waitting.248*249* @or: - osd_request to Executed250* @done: (Optional) - Called at end of execution251* @private: - Will be passed to @done function252*253* Calls blk_execute_rq_nowait to queue the command. When execution is done254* optionally calls @done with @private as parameter. @or->async_error will255* have the return code256*/257int osd_execute_request_async(struct osd_request *or,258osd_req_done_fn *done, void *private);259260/**261* osd_req_decode_sense_full - Decode sense information after execution.262*263* @or: - osd_request to examine264* @osi - Recievs a more detailed error report information (optional).265* @silent - Do not print to dmsg (Even if enabled)266* @bad_obj_list - Some commands act on multiple objects. Failed objects will267* be received here (optional)268* @max_obj - Size of @bad_obj_list.269* @bad_attr_list - List of failing attributes (optional)270* @max_attr - Size of @bad_attr_list.271*272* After execution, osd_request results are analyzed using this function. The273* return code is the final disposition on the error. So it is possible that a274* CHECK_CONDITION was returned from target but this will return NO_ERROR, for275* example on recovered errors. All parameters are optional if caller does276* not need any returned information.277* Note: This function will also dump the error to dmsg according to settings278* of the SCSI_OSD_DPRINT_SENSE Kconfig value. Set @silent if you know the279* command would routinely fail, to not spam the dmsg file.280*/281282/**283* osd_err_priority - osd categorized return codes in ascending severity.284*285* The categories are borrowed from the pnfs_osd_errno enum.286* See comments for translated Linux codes returned by osd_req_decode_sense.287*/288enum osd_err_priority {289OSD_ERR_PRI_NO_ERROR = 0,290/* Recoverable, caller should clear_highpage() all pages */291OSD_ERR_PRI_CLEAR_PAGES = 1, /* -EFAULT */292OSD_ERR_PRI_RESOURCE = 2, /* -ENOMEM */293OSD_ERR_PRI_BAD_CRED = 3, /* -EINVAL */294OSD_ERR_PRI_NO_ACCESS = 4, /* -EACCES */295OSD_ERR_PRI_UNREACHABLE = 5, /* any other */296OSD_ERR_PRI_NOT_FOUND = 6, /* -ENOENT */297OSD_ERR_PRI_NO_SPACE = 7, /* -ENOSPC */298OSD_ERR_PRI_EIO = 8, /* -EIO */299};300301struct osd_sense_info {302enum osd_err_priority osd_err_pri;303304int key; /* one of enum scsi_sense_keys */305int additional_code ; /* enum osd_additional_sense_codes */306union { /* Sense specific information */307u16 sense_info;308u16 cdb_field_offset; /* scsi_invalid_field_in_cdb */309};310union { /* Command specific information */311u64 command_info;312};313314u32 not_initiated_command_functions; /* osd_command_functions_bits */315u32 completed_command_functions; /* osd_command_functions_bits */316struct osd_obj_id obj;317struct osd_attr attr;318};319320int osd_req_decode_sense_full(struct osd_request *or,321struct osd_sense_info *osi, bool silent,322struct osd_obj_id *bad_obj_list, int max_obj,323struct osd_attr *bad_attr_list, int max_attr);324325static inline int osd_req_decode_sense(struct osd_request *or,326struct osd_sense_info *osi)327{328return osd_req_decode_sense_full(or, osi, false, NULL, 0, NULL, 0);329}330331/**332* osd_end_request - return osd_request to free store333*334* @or: osd_request to free335*336* Deallocate all osd_request resources (struct req's, BIOs, buffers, etc.)337*/338void osd_end_request(struct osd_request *or);339340/*341* CDB Encoding342*343* Note: call only one of the following methods.344*/345346/*347* Device commands348*/349void osd_req_set_master_seed_xchg(struct osd_request *or, ...);/* NI */350void osd_req_set_master_key(struct osd_request *or, ...);/* NI */351352void osd_req_format(struct osd_request *or, u64 tot_capacity);353354/* list all partitions355* @list header must be initialized to zero on first run.356*357* Call osd_is_obj_list_done() to find if we got the complete list.358*/359int osd_req_list_dev_partitions(struct osd_request *or,360osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem);361362void osd_req_flush_obsd(struct osd_request *or,363enum osd_options_flush_scope_values);364365void osd_req_perform_scsi_command(struct osd_request *or,366const u8 *cdb, ...);/* NI */367void osd_req_task_management(struct osd_request *or, ...);/* NI */368369/*370* Partition commands371*/372void osd_req_create_partition(struct osd_request *or, osd_id partition);373void osd_req_remove_partition(struct osd_request *or, osd_id partition);374375void osd_req_set_partition_key(struct osd_request *or,376osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],377u8 seed[OSD_CRYPTO_SEED_SIZE]);/* NI */378379/* list all collections in the partition380* @list header must be init to zero on first run.381*382* Call osd_is_obj_list_done() to find if we got the complete list.383*/384int osd_req_list_partition_collections(struct osd_request *or,385osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,386unsigned nelem);387388/* list all objects in the partition389* @list header must be init to zero on first run.390*391* Call osd_is_obj_list_done() to find if we got the complete list.392*/393int osd_req_list_partition_objects(struct osd_request *or,394osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,395unsigned nelem);396397void osd_req_flush_partition(struct osd_request *or,398osd_id partition, enum osd_options_flush_scope_values);399400/*401* Collection commands402*/403void osd_req_create_collection(struct osd_request *or,404const struct osd_obj_id *);/* NI */405void osd_req_remove_collection(struct osd_request *or,406const struct osd_obj_id *);/* NI */407408/* list all objects in the collection */409int osd_req_list_collection_objects(struct osd_request *or,410const struct osd_obj_id *, osd_id initial_id,411struct osd_obj_id_list *list, unsigned nelem);412413/* V2 only filtered list of objects in the collection */414void osd_req_query(struct osd_request *or, ...);/* NI */415416void osd_req_flush_collection(struct osd_request *or,417const struct osd_obj_id *, enum osd_options_flush_scope_values);418419void osd_req_get_member_attrs(struct osd_request *or, ...);/* V2-only NI */420void osd_req_set_member_attrs(struct osd_request *or, ...);/* V2-only NI */421422/*423* Object commands424*/425void osd_req_create_object(struct osd_request *or, struct osd_obj_id *);426void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *);427428void osd_req_write(struct osd_request *or,429const struct osd_obj_id *obj, u64 offset, struct bio *bio, u64 len);430int osd_req_write_kern(struct osd_request *or,431const struct osd_obj_id *obj, u64 offset, void *buff, u64 len);432void osd_req_append(struct osd_request *or,433const struct osd_obj_id *, struct bio *data_out);/* NI */434void osd_req_create_write(struct osd_request *or,435const struct osd_obj_id *, struct bio *data_out, u64 offset);/* NI */436void osd_req_clear(struct osd_request *or,437const struct osd_obj_id *, u64 offset, u64 len);/* NI */438void osd_req_punch(struct osd_request *or,439const struct osd_obj_id *, u64 offset, u64 len);/* V2-only NI */440441void osd_req_flush_object(struct osd_request *or,442const struct osd_obj_id *, enum osd_options_flush_scope_values,443/*V2*/ u64 offset, /*V2*/ u64 len);444445void osd_req_read(struct osd_request *or,446const struct osd_obj_id *obj, u64 offset, struct bio *bio, u64 len);447int osd_req_read_kern(struct osd_request *or,448const struct osd_obj_id *obj, u64 offset, void *buff, u64 len);449450/* Scatter/Gather write/read commands */451int osd_req_write_sg(struct osd_request *or,452const struct osd_obj_id *obj, struct bio *bio,453const struct osd_sg_entry *sglist, unsigned numentries);454int osd_req_read_sg(struct osd_request *or,455const struct osd_obj_id *obj, struct bio *bio,456const struct osd_sg_entry *sglist, unsigned numentries);457int osd_req_write_sg_kern(struct osd_request *or,458const struct osd_obj_id *obj, void **buff,459const struct osd_sg_entry *sglist, unsigned numentries);460int osd_req_read_sg_kern(struct osd_request *or,461const struct osd_obj_id *obj, void **buff,462const struct osd_sg_entry *sglist, unsigned numentries);463464/*465* Root/Partition/Collection/Object Attributes commands466*/467468/* get before set */469void osd_req_get_attributes(struct osd_request *or, const struct osd_obj_id *);470471/* set before get */472void osd_req_set_attributes(struct osd_request *or, const struct osd_obj_id *);473474/*475* Attributes appended to most commands476*/477478/* Attributes List mode (or V2 CDB) */479/*480* TODO: In ver2 if at finalize time only one attr was set and no gets,481* then the Attributes CDB mode is used automatically to save IO.482*/483484/* set a list of attributes. */485int osd_req_add_set_attr_list(struct osd_request *or,486const struct osd_attr *, unsigned nelem);487488/* get a list of attributes */489int osd_req_add_get_attr_list(struct osd_request *or,490const struct osd_attr *, unsigned nelem);491492/*493* Attributes list decoding494* Must be called after osd_request.request was executed495* It is called in a loop to decode the returned get_attr496* (see osd_add_get_attr)497*/498int osd_req_decode_get_attr_list(struct osd_request *or,499struct osd_attr *, int *nelem, void **iterator);500501/* Attributes Page mode */502503/*504* Read an attribute page and optionally set one attribute505*506* Retrieves the attribute page directly to a user buffer.507* @attr_page_data shall stay valid until end of execution.508* See osd_attributes.h for common page structures509*/510int osd_req_add_get_attr_page(struct osd_request *or,511u32 page_id, void *attr_page_data, unsigned max_page_len,512const struct osd_attr *set_one);513514#endif /* __OSD_LIB_H__ */515516517