]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/cam/ctl/ctl_private.h
MFC r268581:
[FreeBSD/stable/10.git] / sys / cam / ctl / ctl_private.h
1 /*-
2  * Copyright (c) 2003, 2004, 2005, 2008 Silicon Graphics International Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_private.h#7 $
31  * $FreeBSD$
32  */
33 /*
34  * CAM Target Layer driver private data structures/definitions.
35  *
36  * Author: Ken Merry <ken@FreeBSD.org>
37  */
38
39 #ifndef _CTL_PRIVATE_H_
40 #define _CTL_PRIVATE_H_
41
42 /*
43  * SCSI vendor and product names.
44  */
45 #define CTL_VENDOR              "FREEBSD "
46 #define CTL_DIRECT_PRODUCT      "CTLDISK         "
47 #define CTL_PROCESSOR_PRODUCT   "CTLPROCESSOR    "
48 #define CTL_UNKNOWN_PRODUCT     "CTLDEVICE       "
49
50 struct ctl_fe_ioctl_startstop_info {
51         struct cv                       sem;
52         struct ctl_hard_startstop_info  hs_info;
53 };
54
55 struct ctl_fe_ioctl_bbrread_info {
56         struct cv                       sem;
57         struct ctl_bbrread_info         *bbr_info;
58         int                             wakeup_done;
59         struct mtx                      *lock;
60 };
61
62 typedef enum {
63         CTL_IOCTL_INPROG,
64         CTL_IOCTL_DATAMOVE,
65         CTL_IOCTL_DONE
66 } ctl_fe_ioctl_state;
67
68 struct ctl_fe_ioctl_params {
69         struct cv               sem;
70         struct mtx              ioctl_mtx;
71         ctl_fe_ioctl_state      state;
72 };
73
74 #define CTL_POOL_ENTRIES_INTERNAL       200
75 #define CTL_POOL_ENTRIES_EMERGENCY      300
76 #define CTL_POOL_ENTRIES_OTHER_SC   200
77
78 typedef enum {
79         CTL_POOL_INTERNAL,
80         CTL_POOL_FETD,
81         CTL_POOL_EMERGENCY,
82         CTL_POOL_4OTHERSC
83 } ctl_pool_type;
84
85 typedef enum {
86         CTL_POOL_FLAG_NONE      = 0x00,
87         CTL_POOL_FLAG_INVALID   = 0x01
88 } ctl_pool_flags;
89
90 struct ctl_io_pool {
91         ctl_pool_type                   type;
92         ctl_pool_flags                  flags;
93         uint32_t                        id;
94         struct ctl_softc                *ctl_softc;
95         uint32_t                        refcount;
96         uint64_t                        total_allocated;
97         uint64_t                        total_freed;
98         int32_t                         total_ctl_io;
99         int32_t                         free_ctl_io;
100         STAILQ_HEAD(, ctl_io_hdr)       free_queue;
101         STAILQ_ENTRY(ctl_io_pool)       links;
102 };
103
104 typedef enum {
105         CTL_IOCTL_FLAG_NONE     = 0x00,
106         CTL_IOCTL_FLAG_ENABLED  = 0x01
107 } ctl_ioctl_flags;
108
109 struct ctl_ioctl_info {
110         ctl_ioctl_flags         flags;
111         uint32_t                cur_tag_num;
112         struct ctl_port         port;
113         char                    port_name[24];
114 };
115
116 typedef enum {
117         CTL_SER_BLOCK,
118         CTL_SER_EXTENT,
119         CTL_SER_PASS,
120         CTL_SER_SKIP
121 } ctl_serialize_action;
122
123 typedef enum {
124         CTL_ACTION_BLOCK,
125         CTL_ACTION_OVERLAP,
126         CTL_ACTION_OVERLAP_TAG,
127         CTL_ACTION_PASS,
128         CTL_ACTION_SKIP,
129         CTL_ACTION_ERROR
130 } ctl_action;
131
132 /*
133  * WARNING:  Keep the bottom nibble here free, we OR in the data direction
134  * flags for each command.
135  *
136  * Note:  "OK_ON_ALL_LUNS" == we don't have to have a lun configured
137  *        "OK_ON_BOTH"     == we have to have a lun configured
138  *        "SA5"            == command has 5-bit service action at byte 1
139  */
140 typedef enum {
141         CTL_CMD_FLAG_NONE               = 0x0000,
142         CTL_CMD_FLAG_NO_SENSE           = 0x0010,
143         CTL_CMD_FLAG_OK_ON_ALL_LUNS     = 0x0020,
144         CTL_CMD_FLAG_ALLOW_ON_RESV      = 0x0040,
145         CTL_CMD_FLAG_OK_ON_PROC         = 0x0100,
146         CTL_CMD_FLAG_OK_ON_SLUN         = 0x0200,
147         CTL_CMD_FLAG_OK_ON_BOTH         = 0x0300,
148         CTL_CMD_FLAG_OK_ON_STOPPED      = 0x0400,
149         CTL_CMD_FLAG_OK_ON_INOPERABLE   = 0x0800,
150         CTL_CMD_FLAG_OK_ON_OFFLINE      = 0x1000,
151         CTL_CMD_FLAG_OK_ON_SECONDARY    = 0x2000,
152         CTL_CMD_FLAG_ALLOW_ON_PR_RESV   = 0x4000,
153         CTL_CMD_FLAG_SA5                = 0x8000
154 } ctl_cmd_flags;
155
156 typedef enum {
157         CTL_SERIDX_TUR  = 0,
158         CTL_SERIDX_READ,
159         CTL_SERIDX_WRITE,
160         CTL_SERIDX_UNMAP,
161         CTL_SERIDX_MD_SNS,
162         CTL_SERIDX_MD_SEL,
163         CTL_SERIDX_RQ_SNS,
164         CTL_SERIDX_INQ,
165         CTL_SERIDX_RD_CAP,
166         CTL_SERIDX_RES,
167         CTL_SERIDX_LOG_SNS,
168         CTL_SERIDX_FORMAT,
169         CTL_SERIDX_START,
170         /* TBD: others to be filled in as needed */
171         CTL_SERIDX_COUNT, /* LAST, not a normal code, provides # codes */
172         CTL_SERIDX_INVLD = CTL_SERIDX_COUNT
173 } ctl_seridx;
174
175 typedef int     ctl_opfunc(struct ctl_scsiio *ctsio);
176
177 struct ctl_cmd_entry {
178         ctl_opfunc              *execute;
179         ctl_seridx              seridx;
180         ctl_cmd_flags           flags;
181         ctl_lun_error_pattern   pattern;
182         uint8_t                 length;         /* CDB length */
183         uint8_t                 usage[15];      /* Mask of allowed CDB bits
184                                                  * after the opcode byte. */
185 };
186
187 typedef enum {
188         CTL_LUN_NONE            = 0x000,
189         CTL_LUN_CONTROL         = 0x001,
190         CTL_LUN_RESERVED        = 0x002,
191         CTL_LUN_INVALID         = 0x004,
192         CTL_LUN_DISABLED        = 0x008,
193         CTL_LUN_MALLOCED        = 0x010,
194         CTL_LUN_STOPPED         = 0x020,
195         CTL_LUN_INOPERABLE      = 0x040,
196         CTL_LUN_OFFLINE         = 0x080,
197         CTL_LUN_PR_RESERVED     = 0x100,
198         CTL_LUN_PRIMARY_SC      = 0x200,
199         CTL_LUN_SENSE_DESC      = 0x400
200 } ctl_lun_flags;
201
202 typedef enum {
203         CTLBLOCK_FLAG_NONE      = 0x00,
204         CTLBLOCK_FLAG_INVALID   = 0x01
205 } ctlblock_flags;
206
207 union ctl_softcs {
208         struct ctl_softc        *ctl_softc;
209         struct ctlblock_softc   *ctlblock_softc;
210 };
211
212 /*
213  * Mode page defaults.
214  */
215 #if 0
216 /*
217  * These values make Solaris trim off some of the capacity.
218  */
219 #define CTL_DEFAULT_SECTORS_PER_TRACK   63
220 #define CTL_DEFAULT_HEADS               255
221 /*
222  * These values seem to work okay.
223  */
224 #define CTL_DEFAULT_SECTORS_PER_TRACK   63
225 #define CTL_DEFAULT_HEADS               16
226 /*
227  * These values work reasonably well.
228  */
229 #define CTL_DEFAULT_SECTORS_PER_TRACK   512
230 #define CTL_DEFAULT_HEADS               64
231 #endif
232
233 /*
234  * Solaris is somewhat picky about how many heads and sectors per track you
235  * have defined in mode pages 3 and 4.  These values seem to cause Solaris
236  * to get the capacity more or less right when you run the format tool.
237  * They still have problems when dealing with devices larger than 1TB,
238  * but there isn't anything we can do about that.
239  *
240  * For smaller LUN sizes, this ends up causing the number of cylinders to
241  * work out to 0.  Solaris actually recognizes that and comes up with its
242  * own bogus geometry to fit the actual capacity of the drive.  They really
243  * should just give up on geometry and stick to the read capacity
244  * information alone for modern disk drives.
245  *
246  * One thing worth mentioning about Solaris' mkfs command is that it
247  * doesn't like sectors per track values larger than 256.  512 seems to
248  * work okay for format, but causes problems when you try to make a
249  * filesystem.
250  *
251  * Another caveat about these values:  the product of these two values
252  * really should be a power of 2.  This is because of the simplistic
253  * shift-based calculation that we have to use on the i386 platform to
254  * calculate the number of cylinders here.  (If you use a divide, you end
255  * up calling __udivdi3(), which is a hardware FP call on the PC.  On the
256  * XScale, it is done in software, so you can do that from inside the
257  * kernel.)
258  *
259  * So for the current values (256 S/T, 128 H), we get 32768, which works
260  * very nicely for calculating cylinders.
261  *
262  * If you want to change these values so that their product is no longer a
263  * power of 2, re-visit the calculation in ctl_init_page_index().  You may
264  * need to make it a bit more complicated to get the number of cylinders
265  * right.
266  */
267 #define CTL_DEFAULT_SECTORS_PER_TRACK   256
268 #define CTL_DEFAULT_HEADS               128
269
270 #define CTL_DEFAULT_ROTATION_RATE       10000
271
272 struct ctl_page_index;
273
274 typedef int     ctl_modesen_handler(struct ctl_scsiio *ctsio,
275                                     struct ctl_page_index *page_index,
276                                     int pc);
277 typedef int     ctl_modesel_handler(struct ctl_scsiio *ctsio,
278                                     struct ctl_page_index *page_index,
279                                     uint8_t *page_ptr);
280
281 typedef enum {
282         CTL_PAGE_FLAG_NONE       = 0x00,
283         CTL_PAGE_FLAG_DISK_ONLY  = 0x01
284 } ctl_page_flags;
285
286 struct ctl_page_index {
287         uint8_t                 page_code;
288         uint8_t                 subpage;
289         uint16_t                page_len;
290         uint8_t                 *page_data;
291         ctl_page_flags          page_flags;
292         ctl_modesen_handler     *sense_handler;
293         ctl_modesel_handler     *select_handler;
294 };
295
296 #define CTL_PAGE_CURRENT        0x00
297 #define CTL_PAGE_CHANGEABLE     0x01
298 #define CTL_PAGE_DEFAULT        0x02
299 #define CTL_PAGE_SAVED          0x03
300
301 static const struct ctl_page_index page_index_template[] = {
302         {SMS_FORMAT_DEVICE_PAGE, 0, sizeof(struct scsi_format_page), NULL,
303          CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
304         {SMS_RIGID_DISK_PAGE, 0, sizeof(struct scsi_rigid_disk_page), NULL,
305          CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
306         {SMS_CACHING_PAGE, 0, sizeof(struct scsi_caching_page), NULL,
307          CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
308         {SMS_CONTROL_MODE_PAGE, 0, sizeof(struct scsi_control_page), NULL,
309          CTL_PAGE_FLAG_NONE, NULL, ctl_control_page_handler},
310         {SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, PWR_SUBPAGE_CODE,
311          sizeof(struct copan_power_subpage), NULL, CTL_PAGE_FLAG_NONE,
312          ctl_power_sp_sense_handler, ctl_power_sp_handler},
313         {SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, APS_SUBPAGE_CODE,
314          sizeof(struct copan_aps_subpage), NULL, CTL_PAGE_FLAG_NONE,
315          NULL, ctl_aps_sp_handler},
316         {SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, DBGCNF_SUBPAGE_CODE,
317          sizeof(struct copan_debugconf_subpage), NULL, CTL_PAGE_FLAG_NONE,
318          ctl_debugconf_sp_sense_handler, ctl_debugconf_sp_select_handler},
319 };
320
321 #define CTL_NUM_MODE_PAGES sizeof(page_index_template)/   \
322                            sizeof(page_index_template[0])
323
324 struct ctl_mode_pages {
325         struct scsi_format_page         format_page[4];
326         struct scsi_rigid_disk_page     rigid_disk_page[4];
327         struct scsi_caching_page        caching_page[4];
328         struct scsi_control_page        control_page[4];
329         struct copan_power_subpage      power_subpage[4];
330         struct copan_aps_subpage        aps_subpage[4];
331         struct copan_debugconf_subpage  debugconf_subpage[4];
332         struct ctl_page_index           index[CTL_NUM_MODE_PAGES];
333 };
334
335 struct ctl_pending_sense {
336         ctl_ua_type             ua_pending;
337         struct scsi_sense_data  sense;
338 };
339
340 struct ctl_lun_delay_info {
341         ctl_delay_type          datamove_type;
342         uint32_t                datamove_delay;
343         ctl_delay_type          done_type;
344         uint32_t                done_delay;
345 };
346
347 typedef enum {
348         CTL_ERR_INJ_NONE        = 0x00,
349         CTL_ERR_INJ_ABORTED     = 0x01
350 } ctl_err_inject_flags;
351
352 typedef enum {
353         CTL_PR_FLAG_NONE        = 0x00,
354         CTL_PR_FLAG_REGISTERED  = 0x01,
355         CTL_PR_FLAG_ACTIVE_RES  = 0x02
356 } ctl_per_res_flags;
357
358 struct ctl_per_res_info {
359         struct scsi_per_res_key res_key;
360         uint8_t  registered;
361 };
362
363 #define CTL_PR_ALL_REGISTRANTS  0xFFFF
364 #define CTL_PR_NO_RESERVATION   0xFFF0
365
366 struct ctl_devid {
367         int             len;
368         uint8_t         data[];
369 };
370
371 /*
372  * For report target port groups.
373  */
374 #define NUM_TARGET_PORT_GROUPS  2
375
376 struct ctl_lun {
377         struct mtx                      lun_lock;
378         struct ctl_id                   target;
379         uint64_t                        lun;
380         ctl_lun_flags                   flags;
381         STAILQ_HEAD(,ctl_error_desc)    error_list;
382         uint64_t                        error_serial;
383         struct ctl_softc                *ctl_softc;
384         struct ctl_be_lun               *be_lun;
385         struct ctl_backend_driver       *backend;
386         int                             io_count;
387         struct ctl_lun_delay_info       delay_info;
388         int                             sync_interval;
389         int                             sync_count;
390         TAILQ_HEAD(ctl_ooaq, ctl_io_hdr)  ooa_queue;
391         TAILQ_HEAD(ctl_blockq,ctl_io_hdr) blocked_queue;
392         STAILQ_ENTRY(ctl_lun)           links;
393         STAILQ_ENTRY(ctl_lun)           run_links;
394         struct ctl_nexus                rsv_nexus;
395         uint32_t                        have_ca[CTL_MAX_INITIATORS >> 5];
396         struct ctl_pending_sense        pending_sense[CTL_MAX_INITIATORS];
397         struct ctl_mode_pages           mode_pages;
398         struct ctl_lun_io_stats         stats;
399         struct ctl_per_res_info         per_res[2*CTL_MAX_INITIATORS];
400         unsigned int                    PRGeneration;
401         int                             pr_key_count;
402         uint16_t                        pr_res_idx;
403         uint8_t                         res_type;
404         uint8_t                         write_buffer[524288];
405         struct ctl_devid                *lun_devid;
406 };
407
408 typedef enum {
409         CTL_FLAG_REAL_SYNC      = 0x02,
410         CTL_FLAG_MASTER_SHELF   = 0x04
411 } ctl_gen_flags;
412
413 #define CTL_MAX_THREADS         16
414
415 struct ctl_thread {
416         struct mtx_padalign queue_lock;
417         struct ctl_softc        *ctl_softc;
418         struct thread           *thread;
419         STAILQ_HEAD(, ctl_io_hdr) incoming_queue;
420         STAILQ_HEAD(, ctl_io_hdr) rtr_queue;
421         STAILQ_HEAD(, ctl_io_hdr) done_queue;
422         STAILQ_HEAD(, ctl_io_hdr) isc_queue;
423 };
424
425 struct ctl_softc {
426         struct mtx ctl_lock;
427         struct cdev *dev;
428         int open_count;
429         struct ctl_id target;
430         int num_disks;
431         int num_luns;
432         ctl_gen_flags flags;
433         ctl_ha_mode ha_mode;
434         int inquiry_pq_no_lun;
435         struct sysctl_ctx_list sysctl_ctx;
436         struct sysctl_oid *sysctl_tree;
437         struct ctl_ioctl_info ioctl_info;
438         struct ctl_io_pool *internal_pool;
439         struct ctl_io_pool *emergency_pool;
440         struct ctl_io_pool *othersc_pool;
441         struct proc *ctl_proc;
442         int targ_online;
443         uint32_t ctl_lun_mask[CTL_MAX_LUNS >> 5];
444         struct ctl_lun *ctl_luns[CTL_MAX_LUNS];
445         uint32_t ctl_port_mask;
446         uint64_t aps_locked_lun;
447         STAILQ_HEAD(, ctl_lun) lun_list;
448         STAILQ_HEAD(, ctl_be_lun) pending_lun_queue;
449         uint32_t num_frontends;
450         STAILQ_HEAD(, ctl_frontend) fe_list;
451         uint32_t num_ports;
452         STAILQ_HEAD(, ctl_port) port_list;
453         struct ctl_port *ctl_ports[CTL_MAX_PORTS];
454         uint32_t num_backends;
455         STAILQ_HEAD(, ctl_backend_driver) be_list;
456         struct mtx pool_lock;
457         uint32_t num_pools;
458         uint32_t cur_pool_id;
459         STAILQ_HEAD(, ctl_io_pool) io_pools;
460         time_t last_print_jiffies;
461         uint32_t skipped_prints;
462         struct ctl_thread threads[CTL_MAX_THREADS];
463 };
464
465 #ifdef _KERNEL
466
467 extern const struct ctl_cmd_entry ctl_cmd_table[256];
468
469 uint32_t ctl_get_initindex(struct ctl_nexus *nexus);
470 int ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
471                     uint32_t total_ctl_io, struct ctl_io_pool **npool);
472 void ctl_pool_free(struct ctl_io_pool *pool);
473 int ctl_scsi_release(struct ctl_scsiio *ctsio);
474 int ctl_scsi_reserve(struct ctl_scsiio *ctsio);
475 int ctl_start_stop(struct ctl_scsiio *ctsio);
476 int ctl_sync_cache(struct ctl_scsiio *ctsio);
477 int ctl_format(struct ctl_scsiio *ctsio);
478 int ctl_read_buffer(struct ctl_scsiio *ctsio);
479 int ctl_write_buffer(struct ctl_scsiio *ctsio);
480 int ctl_write_same(struct ctl_scsiio *ctsio);
481 int ctl_unmap(struct ctl_scsiio *ctsio);
482 int ctl_mode_select(struct ctl_scsiio *ctsio);
483 int ctl_mode_sense(struct ctl_scsiio *ctsio);
484 int ctl_read_capacity(struct ctl_scsiio *ctsio);
485 int ctl_read_capacity_16(struct ctl_scsiio *ctsio);
486 int ctl_read_write(struct ctl_scsiio *ctsio);
487 int ctl_cnw(struct ctl_scsiio *ctsio);
488 int ctl_report_luns(struct ctl_scsiio *ctsio);
489 int ctl_request_sense(struct ctl_scsiio *ctsio);
490 int ctl_tur(struct ctl_scsiio *ctsio);
491 int ctl_verify(struct ctl_scsiio *ctsio);
492 int ctl_inquiry(struct ctl_scsiio *ctsio);
493 int ctl_persistent_reserve_in(struct ctl_scsiio *ctsio);
494 int ctl_persistent_reserve_out(struct ctl_scsiio *ctsio);
495 int ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio);
496 int ctl_report_supported_opcodes(struct ctl_scsiio *ctsio);
497 int ctl_report_supported_tmf(struct ctl_scsiio *ctsio);
498 int ctl_report_timestamp(struct ctl_scsiio *ctsio);
499 int ctl_isc(struct ctl_scsiio *ctsio);
500
501 #endif  /* _KERNEL */
502
503 #endif  /* _CTL_PRIVATE_H_ */
504
505 /*
506  * vim: ts=8
507  */