Print this page
7367 blkdev: support block size larger than 512
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Hans Rosenfeld <hans.rosenfeld@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/blkdev/blkdev.c
+++ new/usr/src/uts/common/io/blkdev/blkdev.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
24 24 * Copyright 2012 Alexey Zaytsev <alexey.zaytsev@gmail.com> All rights reserved.
25 25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
26 + * Copyright 2017 The MathWorks, Inc. All rights reserved.
26 27 */
27 28
28 29 #include <sys/types.h>
29 30 #include <sys/ksynch.h>
30 31 #include <sys/kmem.h>
31 32 #include <sys/file.h>
32 33 #include <sys/errno.h>
33 34 #include <sys/open.h>
34 35 #include <sys/buf.h>
35 36 #include <sys/uio.h>
36 37 #include <sys/aio_req.h>
37 38 #include <sys/cred.h>
38 39 #include <sys/modctl.h>
39 40 #include <sys/cmlb.h>
40 41 #include <sys/conf.h>
41 42 #include <sys/devops.h>
42 43 #include <sys/list.h>
43 44 #include <sys/sysmacros.h>
44 45 #include <sys/dkio.h>
45 46 #include <sys/vtoc.h>
46 47 #include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
47 48 #include <sys/kstat.h>
48 49 #include <sys/fs/dv_node.h>
49 50 #include <sys/ddi.h>
50 51 #include <sys/sunddi.h>
51 52 #include <sys/note.h>
52 53 #include <sys/blkdev.h>
53 54 #include <sys/scsi/impl/inquiry.h>
54 55
55 56 #define BD_MAXPART 64
56 57 #define BDINST(dev) (getminor(dev) / BD_MAXPART)
57 58 #define BDPART(dev) (getminor(dev) % BD_MAXPART)
58 59
59 60 typedef struct bd bd_t;
60 61 typedef struct bd_xfer_impl bd_xfer_impl_t;
61 62
62 63 struct bd {
63 64 void *d_private;
64 65 dev_info_t *d_dip;
65 66 kmutex_t d_ocmutex;
66 67 kmutex_t d_iomutex;
67 68 kmutex_t *d_errmutex;
68 69 kmutex_t d_statemutex;
69 70 kcondvar_t d_statecv;
70 71 enum dkio_state d_state;
71 72 cmlb_handle_t d_cmlbh;
72 73 unsigned d_open_lyr[BD_MAXPART]; /* open count */
73 74 uint64_t d_open_excl; /* bit mask indexed by partition */
74 75 uint64_t d_open_reg[OTYPCNT]; /* bit mask */
75 76
76 77 uint32_t d_qsize;
77 78 uint32_t d_qactive;
78 79 uint32_t d_maxxfer;
79 80 uint32_t d_blkshift;
80 81 uint32_t d_pblkshift;
81 82 uint64_t d_numblks;
82 83 ddi_devid_t d_devid;
83 84
84 85 kmem_cache_t *d_cache;
85 86 list_t d_runq;
86 87 list_t d_waitq;
87 88 kstat_t *d_ksp;
88 89 kstat_io_t *d_kiop;
89 90 kstat_t *d_errstats;
90 91 struct bd_errstats *d_kerr;
91 92
92 93 boolean_t d_rdonly;
93 94 boolean_t d_ssd;
94 95 boolean_t d_removable;
95 96 boolean_t d_hotpluggable;
96 97 boolean_t d_use_dma;
97 98
98 99 ddi_dma_attr_t d_dma;
99 100 bd_ops_t d_ops;
100 101 bd_handle_t d_handle;
101 102 };
102 103
103 104 struct bd_handle {
104 105 bd_ops_t h_ops;
105 106 ddi_dma_attr_t *h_dma;
106 107 dev_info_t *h_parent;
107 108 dev_info_t *h_child;
108 109 void *h_private;
109 110 bd_t *h_bd;
110 111 char *h_name;
111 112 char h_addr[30]; /* enough for w%0.16x,%X */
112 113 };
113 114
114 115 struct bd_xfer_impl {
115 116 bd_xfer_t i_public;
116 117 list_node_t i_linkage;
117 118 bd_t *i_bd;
118 119 buf_t *i_bp;
119 120 uint_t i_num_win;
120 121 uint_t i_cur_win;
121 122 off_t i_offset;
122 123 int (*i_func)(void *, bd_xfer_t *);
123 124 uint32_t i_blkshift;
124 125 size_t i_len;
125 126 size_t i_resid;
126 127 };
127 128
128 129 #define i_dmah i_public.x_dmah
129 130 #define i_dmac i_public.x_dmac
130 131 #define i_ndmac i_public.x_ndmac
131 132 #define i_kaddr i_public.x_kaddr
132 133 #define i_nblks i_public.x_nblks
133 134 #define i_blkno i_public.x_blkno
134 135 #define i_flags i_public.x_flags
135 136
136 137
137 138 /*
138 139 * Private prototypes.
139 140 */
140 141
141 142 static void bd_prop_update_inqstring(dev_info_t *, char *, char *, size_t);
142 143 static void bd_create_inquiry_props(dev_info_t *, bd_drive_t *);
143 144 static void bd_create_errstats(bd_t *, int, bd_drive_t *);
144 145 static void bd_errstats_setstr(kstat_named_t *, char *, size_t, char *);
145 146 static void bd_init_errstats(bd_t *, bd_drive_t *);
146 147
147 148 static int bd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
148 149 static int bd_attach(dev_info_t *, ddi_attach_cmd_t);
149 150 static int bd_detach(dev_info_t *, ddi_detach_cmd_t);
150 151
151 152 static int bd_open(dev_t *, int, int, cred_t *);
152 153 static int bd_close(dev_t, int, int, cred_t *);
153 154 static int bd_strategy(struct buf *);
154 155 static int bd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
155 156 static int bd_dump(dev_t, caddr_t, daddr_t, int);
156 157 static int bd_read(dev_t, struct uio *, cred_t *);
157 158 static int bd_write(dev_t, struct uio *, cred_t *);
158 159 static int bd_aread(dev_t, struct aio_req *, cred_t *);
159 160 static int bd_awrite(dev_t, struct aio_req *, cred_t *);
160 161 static int bd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
161 162 caddr_t, int *);
162 163
163 164 static int bd_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t,
164 165 void *);
165 166 static int bd_tg_getinfo(dev_info_t *, int, void *, void *);
166 167 static int bd_xfer_ctor(void *, void *, int);
167 168 static void bd_xfer_dtor(void *, void *);
168 169 static void bd_sched(bd_t *);
169 170 static void bd_submit(bd_t *, bd_xfer_impl_t *);
170 171 static void bd_runq_exit(bd_xfer_impl_t *, int);
171 172 static void bd_update_state(bd_t *);
172 173 static int bd_check_state(bd_t *, enum dkio_state *);
173 174 static int bd_flush_write_cache(bd_t *, struct dk_callback *);
174 175
175 176 struct cmlb_tg_ops bd_tg_ops = {
176 177 TG_DK_OPS_VERSION_1,
177 178 bd_tg_rdwr,
178 179 bd_tg_getinfo,
179 180 };
180 181
181 182 static struct cb_ops bd_cb_ops = {
182 183 bd_open, /* open */
183 184 bd_close, /* close */
184 185 bd_strategy, /* strategy */
185 186 nodev, /* print */
186 187 bd_dump, /* dump */
187 188 bd_read, /* read */
188 189 bd_write, /* write */
189 190 bd_ioctl, /* ioctl */
190 191 nodev, /* devmap */
191 192 nodev, /* mmap */
192 193 nodev, /* segmap */
193 194 nochpoll, /* poll */
194 195 bd_prop_op, /* cb_prop_op */
195 196 0, /* streamtab */
196 197 D_64BIT | D_MP, /* Driver comaptibility flag */
197 198 CB_REV, /* cb_rev */
198 199 bd_aread, /* async read */
199 200 bd_awrite /* async write */
200 201 };
201 202
202 203 struct dev_ops bd_dev_ops = {
203 204 DEVO_REV, /* devo_rev, */
204 205 0, /* refcnt */
205 206 bd_getinfo, /* getinfo */
206 207 nulldev, /* identify */
207 208 nulldev, /* probe */
208 209 bd_attach, /* attach */
209 210 bd_detach, /* detach */
210 211 nodev, /* reset */
211 212 &bd_cb_ops, /* driver operations */
212 213 NULL, /* bus operations */
213 214 NULL, /* power */
214 215 ddi_quiesce_not_needed, /* quiesce */
215 216 };
216 217
217 218 static struct modldrv modldrv = {
218 219 &mod_driverops,
219 220 "Generic Block Device",
220 221 &bd_dev_ops,
221 222 };
222 223
223 224 static struct modlinkage modlinkage = {
224 225 MODREV_1, { &modldrv, NULL }
225 226 };
226 227
227 228 static void *bd_state;
228 229 static krwlock_t bd_lock;
229 230
230 231 int
231 232 _init(void)
232 233 {
233 234 int rv;
234 235
235 236 rv = ddi_soft_state_init(&bd_state, sizeof (struct bd), 2);
236 237 if (rv != DDI_SUCCESS) {
237 238 return (rv);
238 239 }
239 240 rw_init(&bd_lock, NULL, RW_DRIVER, NULL);
240 241 rv = mod_install(&modlinkage);
241 242 if (rv != DDI_SUCCESS) {
242 243 rw_destroy(&bd_lock);
243 244 ddi_soft_state_fini(&bd_state);
244 245 }
245 246 return (rv);
246 247 }
247 248
248 249 int
249 250 _fini(void)
250 251 {
251 252 int rv;
252 253
253 254 rv = mod_remove(&modlinkage);
254 255 if (rv == DDI_SUCCESS) {
255 256 rw_destroy(&bd_lock);
256 257 ddi_soft_state_fini(&bd_state);
257 258 }
258 259 return (rv);
259 260 }
260 261
261 262 int
262 263 _info(struct modinfo *modinfop)
263 264 {
264 265 return (mod_info(&modlinkage, modinfop));
265 266 }
266 267
267 268 static int
268 269 bd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
269 270 {
270 271 bd_t *bd;
271 272 minor_t inst;
272 273
273 274 _NOTE(ARGUNUSED(dip));
274 275
275 276 inst = BDINST((dev_t)arg);
276 277
277 278 switch (cmd) {
278 279 case DDI_INFO_DEVT2DEVINFO:
279 280 bd = ddi_get_soft_state(bd_state, inst);
280 281 if (bd == NULL) {
281 282 return (DDI_FAILURE);
282 283 }
283 284 *resultp = (void *)bd->d_dip;
284 285 break;
285 286
286 287 case DDI_INFO_DEVT2INSTANCE:
287 288 *resultp = (void *)(intptr_t)inst;
288 289 break;
289 290
290 291 default:
291 292 return (DDI_FAILURE);
292 293 }
293 294 return (DDI_SUCCESS);
294 295 }
295 296
296 297 static void
297 298 bd_prop_update_inqstring(dev_info_t *dip, char *name, char *data, size_t len)
298 299 {
299 300 int ilen;
300 301 char *data_string;
301 302
302 303 ilen = scsi_ascii_inquiry_len(data, len);
303 304 ASSERT3U(ilen, <=, len);
304 305 if (ilen <= 0)
305 306 return;
306 307 /* ensure null termination */
307 308 data_string = kmem_zalloc(ilen + 1, KM_SLEEP);
308 309 bcopy(data, data_string, ilen);
309 310 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, name, data_string);
310 311 kmem_free(data_string, ilen + 1);
311 312 }
312 313
313 314 static void
314 315 bd_create_inquiry_props(dev_info_t *dip, bd_drive_t *drive)
315 316 {
316 317 if (drive->d_vendor_len > 0)
317 318 bd_prop_update_inqstring(dip, INQUIRY_VENDOR_ID,
318 319 drive->d_vendor, drive->d_vendor_len);
319 320
320 321 if (drive->d_product_len > 0)
321 322 bd_prop_update_inqstring(dip, INQUIRY_PRODUCT_ID,
322 323 drive->d_product, drive->d_product_len);
323 324
324 325 if (drive->d_serial_len > 0)
325 326 bd_prop_update_inqstring(dip, INQUIRY_SERIAL_NO,
326 327 drive->d_serial, drive->d_serial_len);
327 328
328 329 if (drive->d_revision_len > 0)
329 330 bd_prop_update_inqstring(dip, INQUIRY_REVISION_ID,
330 331 drive->d_revision, drive->d_revision_len);
331 332 }
332 333
333 334 static void
334 335 bd_create_errstats(bd_t *bd, int inst, bd_drive_t *drive)
335 336 {
336 337 char ks_module[KSTAT_STRLEN];
337 338 char ks_name[KSTAT_STRLEN];
338 339 int ndata = sizeof (struct bd_errstats) / sizeof (kstat_named_t);
339 340
340 341 if (bd->d_errstats != NULL)
341 342 return;
342 343
343 344 (void) snprintf(ks_module, sizeof (ks_module), "%serr",
344 345 ddi_driver_name(bd->d_dip));
345 346 (void) snprintf(ks_name, sizeof (ks_name), "%s%d,err",
346 347 ddi_driver_name(bd->d_dip), inst);
347 348
348 349 bd->d_errstats = kstat_create(ks_module, inst, ks_name, "device_error",
349 350 KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
350 351
351 352 if (bd->d_errstats == NULL) {
352 353 /*
353 354 * Even if we cannot create the kstat, we create a
354 355 * scratch kstat. The reason for this is to ensure
355 356 * that we can update the kstat all of the time,
356 357 * without adding an extra branch instruction.
357 358 */
358 359 bd->d_kerr = kmem_zalloc(sizeof (struct bd_errstats),
359 360 KM_SLEEP);
360 361 bd->d_errmutex = kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
361 362 mutex_init(bd->d_errmutex, NULL, MUTEX_DRIVER, NULL);
362 363 } else {
363 364 if (bd->d_errstats->ks_lock == NULL) {
364 365 bd->d_errstats->ks_lock = kmem_zalloc(sizeof (kmutex_t),
365 366 KM_SLEEP);
366 367 mutex_init(bd->d_errstats->ks_lock, NULL, MUTEX_DRIVER,
367 368 NULL);
368 369 }
369 370
370 371 bd->d_errmutex = bd->d_errstats->ks_lock;
371 372 bd->d_kerr = (struct bd_errstats *)bd->d_errstats->ks_data;
372 373 }
373 374
374 375 kstat_named_init(&bd->d_kerr->bd_softerrs, "Soft Errors",
375 376 KSTAT_DATA_UINT32);
376 377 kstat_named_init(&bd->d_kerr->bd_harderrs, "Hard Errors",
377 378 KSTAT_DATA_UINT32);
378 379 kstat_named_init(&bd->d_kerr->bd_transerrs, "Transport Errors",
379 380 KSTAT_DATA_UINT32);
380 381
381 382 if (drive->d_model_len > 0) {
382 383 kstat_named_init(&bd->d_kerr->bd_model, "Model",
383 384 KSTAT_DATA_STRING);
384 385 } else {
385 386 kstat_named_init(&bd->d_kerr->bd_vid, "Vendor",
386 387 KSTAT_DATA_STRING);
387 388 kstat_named_init(&bd->d_kerr->bd_pid, "Product",
388 389 KSTAT_DATA_STRING);
389 390 }
390 391
391 392 kstat_named_init(&bd->d_kerr->bd_revision, "Revision",
392 393 KSTAT_DATA_STRING);
393 394 kstat_named_init(&bd->d_kerr->bd_serial, "Serial No",
394 395 KSTAT_DATA_STRING);
395 396 kstat_named_init(&bd->d_kerr->bd_capacity, "Size",
396 397 KSTAT_DATA_ULONGLONG);
397 398 kstat_named_init(&bd->d_kerr->bd_rq_media_err, "Media Error",
398 399 KSTAT_DATA_UINT32);
399 400 kstat_named_init(&bd->d_kerr->bd_rq_ntrdy_err, "Device Not Ready",
400 401 KSTAT_DATA_UINT32);
401 402 kstat_named_init(&bd->d_kerr->bd_rq_nodev_err, "No Device",
402 403 KSTAT_DATA_UINT32);
403 404 kstat_named_init(&bd->d_kerr->bd_rq_recov_err, "Recoverable",
404 405 KSTAT_DATA_UINT32);
405 406 kstat_named_init(&bd->d_kerr->bd_rq_illrq_err, "Illegal Request",
406 407 KSTAT_DATA_UINT32);
407 408 kstat_named_init(&bd->d_kerr->bd_rq_pfa_err,
408 409 "Predictive Failure Analysis", KSTAT_DATA_UINT32);
409 410
410 411 bd->d_errstats->ks_private = bd;
411 412
412 413 kstat_install(bd->d_errstats);
413 414 }
414 415
415 416 static void
416 417 bd_errstats_setstr(kstat_named_t *k, char *str, size_t len, char *alt)
417 418 {
418 419 char *tmp;
419 420
420 421 if (KSTAT_NAMED_STR_PTR(k) == NULL) {
421 422 if (len > 0) {
422 423 tmp = kmem_alloc(len + 1, KM_SLEEP);
423 424 (void) strlcpy(tmp, str, len + 1);
424 425 } else {
425 426 tmp = alt;
426 427 }
427 428
428 429 kstat_named_setstr(k, tmp);
429 430 }
430 431 }
431 432
432 433 static void
433 434 bd_init_errstats(bd_t *bd, bd_drive_t *drive)
434 435 {
435 436 struct bd_errstats *est = bd->d_kerr;
436 437
437 438 mutex_enter(bd->d_errmutex);
438 439
439 440 if (drive->d_model_len > 0 &&
440 441 KSTAT_NAMED_STR_PTR(&est->bd_model) == NULL) {
441 442 bd_errstats_setstr(&est->bd_model, drive->d_model,
442 443 drive->d_model_len, NULL);
443 444 } else {
444 445 bd_errstats_setstr(&est->bd_vid, drive->d_vendor,
445 446 drive->d_vendor_len, "Unknown ");
446 447 bd_errstats_setstr(&est->bd_pid, drive->d_product,
447 448 drive->d_product_len, "Unknown ");
448 449 }
449 450
450 451 bd_errstats_setstr(&est->bd_revision, drive->d_revision,
451 452 drive->d_revision_len, "0001");
452 453 bd_errstats_setstr(&est->bd_serial, drive->d_serial,
453 454 drive->d_serial_len, "0 ");
454 455
455 456 mutex_exit(bd->d_errmutex);
456 457 }
457 458
458 459 static int
459 460 bd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
460 461 {
461 462 int inst;
462 463 bd_handle_t hdl;
463 464 bd_t *bd;
464 465 bd_drive_t drive;
465 466 int rv;
466 467 char name[16];
467 468 char kcache[32];
468 469
469 470 switch (cmd) {
470 471 case DDI_ATTACH:
471 472 break;
472 473 case DDI_RESUME:
473 474 /* We don't do anything native for suspend/resume */
474 475 return (DDI_SUCCESS);
475 476 default:
476 477 return (DDI_FAILURE);
477 478 }
478 479
479 480 inst = ddi_get_instance(dip);
480 481 hdl = ddi_get_parent_data(dip);
481 482
482 483 (void) snprintf(name, sizeof (name), "%s%d",
483 484 ddi_driver_name(dip), ddi_get_instance(dip));
484 485 (void) snprintf(kcache, sizeof (kcache), "%s_xfer", name);
485 486
486 487 if (hdl == NULL) {
487 488 cmn_err(CE_WARN, "%s: missing parent data!", name);
488 489 return (DDI_FAILURE);
489 490 }
490 491
491 492 if (ddi_soft_state_zalloc(bd_state, inst) != DDI_SUCCESS) {
492 493 cmn_err(CE_WARN, "%s: unable to zalloc soft state!", name);
493 494 return (DDI_FAILURE);
494 495 }
495 496 bd = ddi_get_soft_state(bd_state, inst);
496 497
497 498 if (hdl->h_dma) {
498 499 bd->d_dma = *(hdl->h_dma);
499 500 bd->d_dma.dma_attr_granular =
500 501 max(DEV_BSIZE, bd->d_dma.dma_attr_granular);
501 502 bd->d_use_dma = B_TRUE;
502 503
503 504 if (bd->d_maxxfer &&
504 505 (bd->d_maxxfer != bd->d_dma.dma_attr_maxxfer)) {
505 506 cmn_err(CE_WARN,
506 507 "%s: inconsistent maximum transfer size!",
507 508 name);
508 509 /* We force it */
509 510 bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
510 511 } else {
511 512 bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
512 513 }
513 514 } else {
514 515 bd->d_use_dma = B_FALSE;
515 516 if (bd->d_maxxfer == 0) {
516 517 bd->d_maxxfer = 1024 * 1024;
517 518 }
518 519 }
519 520 bd->d_ops = hdl->h_ops;
520 521 bd->d_private = hdl->h_private;
521 522 bd->d_blkshift = 9; /* 512 bytes, to start */
522 523
523 524 if (bd->d_maxxfer % DEV_BSIZE) {
524 525 cmn_err(CE_WARN, "%s: maximum transfer misaligned!", name);
525 526 bd->d_maxxfer &= ~(DEV_BSIZE - 1);
526 527 }
527 528 if (bd->d_maxxfer < DEV_BSIZE) {
528 529 cmn_err(CE_WARN, "%s: maximum transfer size too small!", name);
529 530 ddi_soft_state_free(bd_state, inst);
530 531 return (DDI_FAILURE);
531 532 }
532 533
533 534 bd->d_dip = dip;
534 535 bd->d_handle = hdl;
535 536 hdl->h_bd = bd;
536 537 ddi_set_driver_private(dip, bd);
537 538
538 539 mutex_init(&bd->d_iomutex, NULL, MUTEX_DRIVER, NULL);
539 540 mutex_init(&bd->d_ocmutex, NULL, MUTEX_DRIVER, NULL);
540 541 mutex_init(&bd->d_statemutex, NULL, MUTEX_DRIVER, NULL);
541 542 cv_init(&bd->d_statecv, NULL, CV_DRIVER, NULL);
542 543
543 544 list_create(&bd->d_waitq, sizeof (bd_xfer_impl_t),
544 545 offsetof(struct bd_xfer_impl, i_linkage));
545 546 list_create(&bd->d_runq, sizeof (bd_xfer_impl_t),
546 547 offsetof(struct bd_xfer_impl, i_linkage));
547 548
548 549 bd->d_cache = kmem_cache_create(kcache, sizeof (bd_xfer_impl_t), 8,
549 550 bd_xfer_ctor, bd_xfer_dtor, NULL, bd, NULL, 0);
550 551
551 552 bd->d_ksp = kstat_create(ddi_driver_name(dip), inst, NULL, "disk",
552 553 KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
553 554 if (bd->d_ksp != NULL) {
554 555 bd->d_ksp->ks_lock = &bd->d_iomutex;
555 556 kstat_install(bd->d_ksp);
556 557 bd->d_kiop = bd->d_ksp->ks_data;
557 558 } else {
558 559 /*
559 560 * Even if we cannot create the kstat, we create a
560 561 * scratch kstat. The reason for this is to ensure
561 562 * that we can update the kstat all of the time,
562 563 * without adding an extra branch instruction.
563 564 */
564 565 bd->d_kiop = kmem_zalloc(sizeof (kstat_io_t), KM_SLEEP);
565 566 }
566 567
567 568 cmlb_alloc_handle(&bd->d_cmlbh);
568 569
569 570 bd->d_state = DKIO_NONE;
570 571
571 572 bzero(&drive, sizeof (drive));
572 573 bd->d_ops.o_drive_info(bd->d_private, &drive);
573 574 bd->d_qsize = drive.d_qsize;
574 575 bd->d_removable = drive.d_removable;
575 576 bd->d_hotpluggable = drive.d_hotpluggable;
576 577
577 578 if (drive.d_maxxfer && drive.d_maxxfer < bd->d_maxxfer)
578 579 bd->d_maxxfer = drive.d_maxxfer;
579 580
580 581 bd_create_inquiry_props(dip, &drive);
581 582
582 583 bd_create_errstats(bd, inst, &drive);
583 584 bd_init_errstats(bd, &drive);
584 585 bd_update_state(bd);
585 586
586 587 rv = cmlb_attach(dip, &bd_tg_ops, DTYPE_DIRECT,
587 588 bd->d_removable, bd->d_hotpluggable,
588 589 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
589 590 *(uint64_t *)drive.d_eui64 != 0 ? DDI_NT_BLOCK_BLKDEV :
590 591 drive.d_lun >= 0 ? DDI_NT_BLOCK_CHAN : DDI_NT_BLOCK,
591 592 CMLB_FAKE_LABEL_ONE_PARTITION, bd->d_cmlbh, 0);
592 593 if (rv != 0) {
593 594 cmlb_free_handle(&bd->d_cmlbh);
594 595 kmem_cache_destroy(bd->d_cache);
595 596 mutex_destroy(&bd->d_iomutex);
596 597 mutex_destroy(&bd->d_ocmutex);
597 598 mutex_destroy(&bd->d_statemutex);
598 599 cv_destroy(&bd->d_statecv);
599 600 list_destroy(&bd->d_waitq);
600 601 list_destroy(&bd->d_runq);
601 602 if (bd->d_ksp != NULL) {
602 603 kstat_delete(bd->d_ksp);
603 604 bd->d_ksp = NULL;
604 605 } else {
605 606 kmem_free(bd->d_kiop, sizeof (kstat_io_t));
606 607 }
607 608 ddi_soft_state_free(bd_state, inst);
608 609 return (DDI_FAILURE);
609 610 }
610 611
611 612 if (bd->d_ops.o_devid_init != NULL) {
612 613 rv = bd->d_ops.o_devid_init(bd->d_private, dip, &bd->d_devid);
613 614 if (rv == DDI_SUCCESS) {
614 615 if (ddi_devid_register(dip, bd->d_devid) !=
615 616 DDI_SUCCESS) {
616 617 cmn_err(CE_WARN,
617 618 "%s: unable to register devid", name);
618 619 }
619 620 }
620 621 }
621 622
622 623 /*
623 624 * Add a zero-length attribute to tell the world we support
624 625 * kernel ioctls (for layered drivers). Also set up properties
625 626 * used by HAL to identify removable media.
626 627 */
627 628 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
628 629 DDI_KERNEL_IOCTL, NULL, 0);
629 630 if (bd->d_removable) {
630 631 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
631 632 "removable-media", NULL, 0);
632 633 }
633 634 if (bd->d_hotpluggable) {
634 635 (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
635 636 "hotpluggable", NULL, 0);
636 637 }
637 638
638 639 ddi_report_dev(dip);
639 640
640 641 return (DDI_SUCCESS);
641 642 }
642 643
643 644 static int
644 645 bd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
645 646 {
646 647 bd_t *bd;
647 648
648 649 bd = ddi_get_driver_private(dip);
649 650
650 651 switch (cmd) {
651 652 case DDI_DETACH:
652 653 break;
653 654 case DDI_SUSPEND:
654 655 /* We don't suspend, but our parent does */
655 656 return (DDI_SUCCESS);
656 657 default:
657 658 return (DDI_FAILURE);
658 659 }
659 660 if (bd->d_ksp != NULL) {
660 661 kstat_delete(bd->d_ksp);
661 662 bd->d_ksp = NULL;
662 663 } else {
663 664 kmem_free(bd->d_kiop, sizeof (kstat_io_t));
664 665 }
665 666
666 667 if (bd->d_errstats != NULL) {
667 668 kstat_delete(bd->d_errstats);
668 669 bd->d_errstats = NULL;
669 670 } else {
670 671 kmem_free(bd->d_kerr, sizeof (struct bd_errstats));
671 672 mutex_destroy(bd->d_errmutex);
672 673 }
673 674
674 675 cmlb_detach(bd->d_cmlbh, 0);
675 676 cmlb_free_handle(&bd->d_cmlbh);
676 677 if (bd->d_devid)
677 678 ddi_devid_free(bd->d_devid);
678 679 kmem_cache_destroy(bd->d_cache);
679 680 mutex_destroy(&bd->d_iomutex);
680 681 mutex_destroy(&bd->d_ocmutex);
681 682 mutex_destroy(&bd->d_statemutex);
682 683 cv_destroy(&bd->d_statecv);
683 684 list_destroy(&bd->d_waitq);
684 685 list_destroy(&bd->d_runq);
685 686 ddi_soft_state_free(bd_state, ddi_get_instance(dip));
686 687 return (DDI_SUCCESS);
687 688 }
688 689
689 690 static int
690 691 bd_xfer_ctor(void *buf, void *arg, int kmflag)
691 692 {
692 693 bd_xfer_impl_t *xi;
693 694 bd_t *bd = arg;
694 695 int (*dcb)(caddr_t);
695 696
696 697 if (kmflag == KM_PUSHPAGE || kmflag == KM_SLEEP) {
697 698 dcb = DDI_DMA_SLEEP;
698 699 } else {
699 700 dcb = DDI_DMA_DONTWAIT;
700 701 }
701 702
702 703 xi = buf;
703 704 bzero(xi, sizeof (*xi));
704 705 xi->i_bd = bd;
705 706
706 707 if (bd->d_use_dma) {
707 708 if (ddi_dma_alloc_handle(bd->d_dip, &bd->d_dma, dcb, NULL,
708 709 &xi->i_dmah) != DDI_SUCCESS) {
709 710 return (-1);
710 711 }
711 712 }
712 713
713 714 return (0);
714 715 }
715 716
716 717 static void
717 718 bd_xfer_dtor(void *buf, void *arg)
718 719 {
719 720 bd_xfer_impl_t *xi = buf;
720 721
721 722 _NOTE(ARGUNUSED(arg));
722 723
723 724 if (xi->i_dmah)
724 725 ddi_dma_free_handle(&xi->i_dmah);
725 726 xi->i_dmah = NULL;
726 727 }
727 728
728 729 static bd_xfer_impl_t *
729 730 bd_xfer_alloc(bd_t *bd, struct buf *bp, int (*func)(void *, bd_xfer_t *),
730 731 int kmflag)
731 732 {
732 733 bd_xfer_impl_t *xi;
733 734 int rv = 0;
734 735 int status;
735 736 unsigned dir;
736 737 int (*cb)(caddr_t);
737 738 size_t len;
738 739 uint32_t shift;
739 740
740 741 if (kmflag == KM_SLEEP) {
741 742 cb = DDI_DMA_SLEEP;
742 743 } else {
743 744 cb = DDI_DMA_DONTWAIT;
744 745 }
745 746
↓ open down ↓ |
710 lines elided |
↑ open up ↑ |
746 747 xi = kmem_cache_alloc(bd->d_cache, kmflag);
747 748 if (xi == NULL) {
748 749 bioerror(bp, ENOMEM);
749 750 return (NULL);
750 751 }
751 752
752 753 ASSERT(bp);
753 754
754 755 xi->i_bp = bp;
755 756 xi->i_func = func;
756 - xi->i_blkno = bp->b_lblkno;
757 + xi->i_blkno = bp->b_lblkno >> (bd->d_blkshift - DEV_BSHIFT);
757 758
758 759 if (bp->b_bcount == 0) {
759 760 xi->i_len = 0;
760 761 xi->i_nblks = 0;
761 762 xi->i_kaddr = NULL;
762 763 xi->i_resid = 0;
763 764 xi->i_num_win = 0;
764 765 goto done;
765 766 }
766 767
767 768 if (bp->b_flags & B_READ) {
768 769 dir = DDI_DMA_READ;
769 770 xi->i_func = bd->d_ops.o_read;
770 771 } else {
771 772 dir = DDI_DMA_WRITE;
772 773 xi->i_func = bd->d_ops.o_write;
773 774 }
774 775
775 776 shift = bd->d_blkshift;
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
776 777 xi->i_blkshift = shift;
777 778
778 779 if (!bd->d_use_dma) {
779 780 bp_mapin(bp);
780 781 rv = 0;
781 782 xi->i_offset = 0;
782 783 xi->i_num_win =
783 784 (bp->b_bcount + (bd->d_maxxfer - 1)) / bd->d_maxxfer;
784 785 xi->i_cur_win = 0;
785 786 xi->i_len = min(bp->b_bcount, bd->d_maxxfer);
786 - xi->i_nblks = xi->i_len >> shift;
787 + xi->i_nblks = howmany(xi->i_len, (1U << shift));
787 788 xi->i_kaddr = bp->b_un.b_addr;
788 789 xi->i_resid = bp->b_bcount;
789 790 } else {
790 791
791 792 /*
792 793 * We have to use consistent DMA if the address is misaligned.
793 794 */
794 795 if (((bp->b_flags & (B_PAGEIO | B_REMAPPED)) != B_PAGEIO) &&
795 796 ((uintptr_t)bp->b_un.b_addr & 0x7)) {
796 797 dir |= DDI_DMA_CONSISTENT | DDI_DMA_PARTIAL;
797 798 } else {
798 799 dir |= DDI_DMA_STREAMING | DDI_DMA_PARTIAL;
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
799 800 }
800 801
801 802 status = ddi_dma_buf_bind_handle(xi->i_dmah, bp, dir, cb,
802 803 NULL, &xi->i_dmac, &xi->i_ndmac);
803 804 switch (status) {
804 805 case DDI_DMA_MAPPED:
805 806 xi->i_num_win = 1;
806 807 xi->i_cur_win = 0;
807 808 xi->i_offset = 0;
808 809 xi->i_len = bp->b_bcount;
809 - xi->i_nblks = xi->i_len >> shift;
810 + xi->i_nblks = howmany(xi->i_len, (1U << shift));
810 811 xi->i_resid = bp->b_bcount;
811 812 rv = 0;
812 813 break;
813 814 case DDI_DMA_PARTIAL_MAP:
814 815 xi->i_cur_win = 0;
815 816
816 817 if ((ddi_dma_numwin(xi->i_dmah, &xi->i_num_win) !=
817 818 DDI_SUCCESS) ||
818 819 (ddi_dma_getwin(xi->i_dmah, 0, &xi->i_offset,
819 820 &len, &xi->i_dmac, &xi->i_ndmac) !=
820 821 DDI_SUCCESS) ||
821 - (P2PHASE(len, shift) != 0)) {
822 + (P2PHASE(len, (1U << DEV_BSHIFT)) != 0)) {
822 823 (void) ddi_dma_unbind_handle(xi->i_dmah);
823 824 rv = EFAULT;
824 825 goto done;
825 826 }
826 827 xi->i_len = len;
827 - xi->i_nblks = xi->i_len >> shift;
828 + xi->i_nblks = howmany(xi->i_len, (1U << shift));
828 829 xi->i_resid = bp->b_bcount;
829 830 rv = 0;
830 831 break;
831 832 case DDI_DMA_NORESOURCES:
832 833 rv = EAGAIN;
833 834 goto done;
834 835 case DDI_DMA_TOOBIG:
835 836 rv = EINVAL;
836 837 goto done;
837 838 case DDI_DMA_NOMAPPING:
838 839 case DDI_DMA_INUSE:
839 840 default:
840 841 rv = EFAULT;
841 842 goto done;
842 843 }
843 844 }
844 845
845 846 done:
846 847 if (rv != 0) {
847 848 kmem_cache_free(bd->d_cache, xi);
848 849 bioerror(bp, rv);
849 850 return (NULL);
850 851 }
851 852
852 853 return (xi);
853 854 }
854 855
855 856 static void
856 857 bd_xfer_free(bd_xfer_impl_t *xi)
857 858 {
858 859 if (xi->i_dmah) {
859 860 (void) ddi_dma_unbind_handle(xi->i_dmah);
860 861 }
861 862 kmem_cache_free(xi->i_bd->d_cache, xi);
862 863 }
863 864
864 865 static int
865 866 bd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
866 867 {
867 868 dev_t dev = *devp;
868 869 bd_t *bd;
869 870 minor_t part;
870 871 minor_t inst;
871 872 uint64_t mask;
872 873 boolean_t ndelay;
873 874 int rv;
874 875 diskaddr_t nblks;
875 876 diskaddr_t lba;
876 877
877 878 _NOTE(ARGUNUSED(credp));
878 879
879 880 part = BDPART(dev);
880 881 inst = BDINST(dev);
881 882
882 883 if (otyp >= OTYPCNT)
883 884 return (EINVAL);
884 885
885 886 ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
886 887
887 888 /*
888 889 * Block any DR events from changing the set of registered
889 890 * devices while we function.
890 891 */
891 892 rw_enter(&bd_lock, RW_READER);
892 893 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
893 894 rw_exit(&bd_lock);
894 895 return (ENXIO);
895 896 }
896 897
897 898 mutex_enter(&bd->d_ocmutex);
898 899
899 900 ASSERT(part < 64);
900 901 mask = (1U << part);
901 902
902 903 bd_update_state(bd);
903 904
904 905 if (cmlb_validate(bd->d_cmlbh, 0, 0) != 0) {
905 906
906 907 /* non-blocking opens are allowed to succeed */
907 908 if (!ndelay) {
908 909 rv = ENXIO;
909 910 goto done;
910 911 }
911 912 } else if (cmlb_partinfo(bd->d_cmlbh, part, &nblks, &lba,
912 913 NULL, NULL, 0) == 0) {
913 914
914 915 /*
915 916 * We read the partinfo, verify valid ranges. If the
916 917 * partition is invalid, and we aren't blocking or
917 918 * doing a raw access, then fail. (Non-blocking and
918 919 * raw accesses can still succeed to allow a disk with
919 920 * bad partition data to opened by format and fdisk.)
920 921 */
921 922 if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
922 923 rv = ENXIO;
923 924 goto done;
924 925 }
925 926 } else if (!ndelay) {
926 927 /*
927 928 * cmlb_partinfo failed -- invalid partition or no
928 929 * disk label.
929 930 */
930 931 rv = ENXIO;
931 932 goto done;
932 933 }
933 934
934 935 if ((flag & FWRITE) && bd->d_rdonly) {
935 936 rv = EROFS;
936 937 goto done;
937 938 }
938 939
939 940 if ((bd->d_open_excl) & (mask)) {
940 941 rv = EBUSY;
941 942 goto done;
942 943 }
943 944 if (flag & FEXCL) {
944 945 if (bd->d_open_lyr[part]) {
945 946 rv = EBUSY;
946 947 goto done;
947 948 }
948 949 for (int i = 0; i < OTYP_LYR; i++) {
949 950 if (bd->d_open_reg[i] & mask) {
950 951 rv = EBUSY;
951 952 goto done;
952 953 }
953 954 }
954 955 }
955 956
956 957 if (otyp == OTYP_LYR) {
957 958 bd->d_open_lyr[part]++;
958 959 } else {
959 960 bd->d_open_reg[otyp] |= mask;
960 961 }
961 962 if (flag & FEXCL) {
962 963 bd->d_open_excl |= mask;
963 964 }
964 965
965 966 rv = 0;
966 967 done:
967 968 mutex_exit(&bd->d_ocmutex);
968 969 rw_exit(&bd_lock);
969 970
970 971 return (rv);
971 972 }
972 973
973 974 static int
974 975 bd_close(dev_t dev, int flag, int otyp, cred_t *credp)
975 976 {
976 977 bd_t *bd;
977 978 minor_t inst;
978 979 minor_t part;
979 980 uint64_t mask;
980 981 boolean_t last = B_TRUE;
981 982
982 983 _NOTE(ARGUNUSED(flag));
983 984 _NOTE(ARGUNUSED(credp));
984 985
985 986 part = BDPART(dev);
986 987 inst = BDINST(dev);
987 988
988 989 ASSERT(part < 64);
989 990 mask = (1U << part);
990 991
991 992 rw_enter(&bd_lock, RW_READER);
992 993
993 994 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
994 995 rw_exit(&bd_lock);
995 996 return (ENXIO);
996 997 }
997 998
998 999 mutex_enter(&bd->d_ocmutex);
999 1000 if (bd->d_open_excl & mask) {
1000 1001 bd->d_open_excl &= ~mask;
1001 1002 }
1002 1003 if (otyp == OTYP_LYR) {
1003 1004 bd->d_open_lyr[part]--;
1004 1005 } else {
1005 1006 bd->d_open_reg[otyp] &= ~mask;
1006 1007 }
1007 1008 for (int i = 0; i < 64; i++) {
1008 1009 if (bd->d_open_lyr[part]) {
1009 1010 last = B_FALSE;
1010 1011 }
1011 1012 }
1012 1013 for (int i = 0; last && (i < OTYP_LYR); i++) {
1013 1014 if (bd->d_open_reg[i]) {
1014 1015 last = B_FALSE;
1015 1016 }
1016 1017 }
1017 1018 mutex_exit(&bd->d_ocmutex);
1018 1019
1019 1020 if (last) {
1020 1021 cmlb_invalidate(bd->d_cmlbh, 0);
1021 1022 }
1022 1023 rw_exit(&bd_lock);
1023 1024
1024 1025 return (0);
1025 1026 }
1026 1027
1027 1028 static int
↓ open down ↓ |
190 lines elided |
↑ open up ↑ |
1028 1029 bd_dump(dev_t dev, caddr_t caddr, daddr_t blkno, int nblk)
1029 1030 {
1030 1031 minor_t inst;
1031 1032 minor_t part;
1032 1033 diskaddr_t pstart;
1033 1034 diskaddr_t psize;
1034 1035 bd_t *bd;
1035 1036 bd_xfer_impl_t *xi;
1036 1037 buf_t *bp;
1037 1038 int rv;
1039 + uint32_t shift;
1040 + daddr_t d_blkno;
1041 + int d_nblk;
1038 1042
1039 1043 rw_enter(&bd_lock, RW_READER);
1040 1044
1041 1045 part = BDPART(dev);
1042 1046 inst = BDINST(dev);
1043 1047
1044 1048 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
1045 1049 rw_exit(&bd_lock);
1046 1050 return (ENXIO);
1047 1051 }
1052 + shift = bd->d_blkshift;
1053 + d_blkno = blkno >> (shift - DEV_BSHIFT);
1054 + d_nblk = howmany((nblk << DEV_BSHIFT), (1U << shift));
1048 1055 /*
1049 1056 * do cmlb, but do it synchronously unless we already have the
1050 1057 * partition (which we probably should.)
1051 1058 */
1052 1059 if (cmlb_partinfo(bd->d_cmlbh, part, &psize, &pstart, NULL, NULL,
1053 1060 (void *)1)) {
1054 1061 rw_exit(&bd_lock);
1055 1062 return (ENXIO);
1056 1063 }
1057 1064
1058 - if ((blkno + nblk) > psize) {
1065 + if ((d_blkno + d_nblk) > psize) {
1059 1066 rw_exit(&bd_lock);
1060 1067 return (EINVAL);
1061 1068 }
1062 1069 bp = getrbuf(KM_NOSLEEP);
1063 1070 if (bp == NULL) {
1064 1071 rw_exit(&bd_lock);
1065 1072 return (ENOMEM);
1066 1073 }
1067 1074
1068 - bp->b_bcount = nblk << bd->d_blkshift;
1075 + bp->b_bcount = nblk << DEV_BSHIFT;
1069 1076 bp->b_resid = bp->b_bcount;
1070 1077 bp->b_lblkno = blkno;
1071 1078 bp->b_un.b_addr = caddr;
1072 1079
1073 1080 xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_write, KM_NOSLEEP);
1074 1081 if (xi == NULL) {
1075 1082 rw_exit(&bd_lock);
1076 1083 freerbuf(bp);
1077 1084 return (ENOMEM);
1078 1085 }
1079 - xi->i_blkno = blkno + pstart;
1086 + xi->i_blkno = d_blkno + pstart;
1080 1087 xi->i_flags = BD_XFER_POLL;
1081 1088 bd_submit(bd, xi);
1082 1089 rw_exit(&bd_lock);
1083 1090
1084 1091 /*
1085 1092 * Generally, we should have run this entirely synchronously
1086 1093 * at this point and the biowait call should be a no-op. If
1087 1094 * it didn't happen this way, it's a bug in the underlying
1088 1095 * driver not honoring BD_XFER_POLL.
1089 1096 */
1090 1097 (void) biowait(bp);
1091 1098 rv = geterror(bp);
1092 1099 freerbuf(bp);
1093 1100 return (rv);
1094 1101 }
1095 1102
1096 1103 void
1097 1104 bd_minphys(struct buf *bp)
1098 1105 {
1099 1106 minor_t inst;
1100 1107 bd_t *bd;
1101 1108 inst = BDINST(bp->b_edev);
1102 1109
1103 1110 bd = ddi_get_soft_state(bd_state, inst);
1104 1111
1105 1112 /*
1106 1113 * In a non-debug kernel, bd_strategy will catch !bd as
1107 1114 * well, and will fail nicely.
1108 1115 */
1109 1116 ASSERT(bd);
1110 1117
1111 1118 if (bp->b_bcount > bd->d_maxxfer)
1112 1119 bp->b_bcount = bd->d_maxxfer;
1113 1120 }
1114 1121
1115 1122 static int
1116 1123 bd_read(dev_t dev, struct uio *uio, cred_t *credp)
1117 1124 {
1118 1125 _NOTE(ARGUNUSED(credp));
1119 1126 return (physio(bd_strategy, NULL, dev, B_READ, bd_minphys, uio));
1120 1127 }
1121 1128
1122 1129 static int
1123 1130 bd_write(dev_t dev, struct uio *uio, cred_t *credp)
1124 1131 {
1125 1132 _NOTE(ARGUNUSED(credp));
1126 1133 return (physio(bd_strategy, NULL, dev, B_WRITE, bd_minphys, uio));
1127 1134 }
1128 1135
1129 1136 static int
1130 1137 bd_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
1131 1138 {
1132 1139 _NOTE(ARGUNUSED(credp));
1133 1140 return (aphysio(bd_strategy, anocancel, dev, B_READ, bd_minphys, aio));
1134 1141 }
1135 1142
1136 1143 static int
1137 1144 bd_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
1138 1145 {
1139 1146 _NOTE(ARGUNUSED(credp));
1140 1147 return (aphysio(bd_strategy, anocancel, dev, B_WRITE, bd_minphys, aio));
1141 1148 }
1142 1149
1143 1150 static int
1144 1151 bd_strategy(struct buf *bp)
↓ open down ↓ |
55 lines elided |
↑ open up ↑ |
1145 1152 {
1146 1153 minor_t inst;
1147 1154 minor_t part;
1148 1155 bd_t *bd;
1149 1156 diskaddr_t p_lba;
1150 1157 diskaddr_t p_nblks;
1151 1158 diskaddr_t b_nblks;
1152 1159 bd_xfer_impl_t *xi;
1153 1160 uint32_t shift;
1154 1161 int (*func)(void *, bd_xfer_t *);
1162 + diskaddr_t lblkno;
1155 1163
1156 1164 part = BDPART(bp->b_edev);
1157 1165 inst = BDINST(bp->b_edev);
1158 1166
1159 1167 ASSERT(bp);
1160 1168
1161 1169 bp->b_resid = bp->b_bcount;
1162 1170
1163 1171 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
1164 1172 bioerror(bp, ENXIO);
1165 1173 biodone(bp);
1166 1174 return (0);
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
1167 1175 }
1168 1176
1169 1177 if (cmlb_partinfo(bd->d_cmlbh, part, &p_nblks, &p_lba,
1170 1178 NULL, NULL, 0)) {
1171 1179 bioerror(bp, ENXIO);
1172 1180 biodone(bp);
1173 1181 return (0);
1174 1182 }
1175 1183
1176 1184 shift = bd->d_blkshift;
1177 -
1178 - if ((P2PHASE(bp->b_bcount, (1U << shift)) != 0) ||
1179 - (bp->b_lblkno > p_nblks)) {
1185 + lblkno = bp->b_lblkno >> (shift - DEV_BSHIFT);
1186 + if ((P2PHASE(bp->b_bcount, (1U << DEV_BSHIFT)) != 0) ||
1187 + (lblkno > p_nblks)) {
1180 1188 bioerror(bp, ENXIO);
1181 1189 biodone(bp);
1182 1190 return (0);
1183 1191 }
1184 - b_nblks = bp->b_bcount >> shift;
1185 - if ((bp->b_lblkno == p_nblks) || (bp->b_bcount == 0)) {
1192 + b_nblks = howmany(bp->b_bcount, (1U << shift));
1193 + if ((lblkno == p_nblks) || (bp->b_bcount == 0)) {
1186 1194 biodone(bp);
1187 1195 return (0);
1188 1196 }
1189 1197
1190 - if ((b_nblks + bp->b_lblkno) > p_nblks) {
1191 - bp->b_resid = ((bp->b_lblkno + b_nblks - p_nblks) << shift);
1198 + if ((b_nblks + lblkno) > p_nblks) {
1199 + bp->b_resid = ((lblkno + b_nblks - p_nblks) << shift);
1192 1200 bp->b_bcount -= bp->b_resid;
1193 1201 } else {
1194 1202 bp->b_resid = 0;
1195 1203 }
1196 1204 func = (bp->b_flags & B_READ) ? bd->d_ops.o_read : bd->d_ops.o_write;
1197 1205
1198 1206 xi = bd_xfer_alloc(bd, bp, func, KM_NOSLEEP);
1199 1207 if (xi == NULL) {
1200 1208 xi = bd_xfer_alloc(bd, bp, func, KM_PUSHPAGE);
1201 1209 }
1202 1210 if (xi == NULL) {
1203 1211 /* bd_request_alloc will have done bioerror */
1204 1212 biodone(bp);
1205 1213 return (0);
1206 1214 }
1207 - xi->i_blkno = bp->b_lblkno + p_lba;
1215 + xi->i_blkno = lblkno + p_lba;
1208 1216
1209 1217 bd_submit(bd, xi);
1210 1218
1211 1219 return (0);
1212 1220 }
1213 1221
1214 1222 static int
1215 1223 bd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, int *rvalp)
1216 1224 {
1217 1225 minor_t inst;
1218 1226 uint16_t part;
1219 1227 bd_t *bd;
1220 1228 void *ptr = (void *)arg;
1221 1229 int rv;
1222 1230
1223 1231 part = BDPART(dev);
1224 1232 inst = BDINST(dev);
1225 1233
1226 1234 if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
1227 1235 return (ENXIO);
1228 1236 }
1229 1237
1230 1238 rv = cmlb_ioctl(bd->d_cmlbh, dev, cmd, arg, flag, credp, rvalp, 0);
1231 1239 if (rv != ENOTTY)
1232 1240 return (rv);
1233 1241
1234 1242 if (rvalp != NULL) {
1235 1243 /* the return value of the ioctl is 0 by default */
1236 1244 *rvalp = 0;
1237 1245 }
1238 1246
1239 1247 switch (cmd) {
1240 1248 case DKIOCGMEDIAINFO: {
1241 1249 struct dk_minfo minfo;
1242 1250
1243 1251 /* make sure our state information is current */
1244 1252 bd_update_state(bd);
1245 1253 bzero(&minfo, sizeof (minfo));
1246 1254 minfo.dki_media_type = DK_FIXED_DISK;
1247 1255 minfo.dki_lbsize = (1U << bd->d_blkshift);
1248 1256 minfo.dki_capacity = bd->d_numblks;
1249 1257 if (ddi_copyout(&minfo, ptr, sizeof (minfo), flag)) {
1250 1258 return (EFAULT);
1251 1259 }
1252 1260 return (0);
1253 1261 }
1254 1262 case DKIOCGMEDIAINFOEXT: {
1255 1263 struct dk_minfo_ext miext;
1256 1264
1257 1265 /* make sure our state information is current */
1258 1266 bd_update_state(bd);
1259 1267 bzero(&miext, sizeof (miext));
1260 1268 miext.dki_media_type = DK_FIXED_DISK;
1261 1269 miext.dki_lbsize = (1U << bd->d_blkshift);
1262 1270 miext.dki_pbsize = (1U << bd->d_pblkshift);
1263 1271 miext.dki_capacity = bd->d_numblks;
1264 1272 if (ddi_copyout(&miext, ptr, sizeof (miext), flag)) {
1265 1273 return (EFAULT);
1266 1274 }
1267 1275 return (0);
1268 1276 }
1269 1277 case DKIOCINFO: {
1270 1278 struct dk_cinfo cinfo;
1271 1279 bzero(&cinfo, sizeof (cinfo));
1272 1280 cinfo.dki_ctype = DKC_BLKDEV;
1273 1281 cinfo.dki_cnum = ddi_get_instance(ddi_get_parent(bd->d_dip));
1274 1282 (void) snprintf(cinfo.dki_cname, sizeof (cinfo.dki_cname),
1275 1283 "%s", ddi_driver_name(ddi_get_parent(bd->d_dip)));
1276 1284 (void) snprintf(cinfo.dki_dname, sizeof (cinfo.dki_dname),
1277 1285 "%s", ddi_driver_name(bd->d_dip));
1278 1286 cinfo.dki_unit = inst;
1279 1287 cinfo.dki_flags = DKI_FMTVOL;
1280 1288 cinfo.dki_partition = part;
1281 1289 cinfo.dki_maxtransfer = bd->d_maxxfer / DEV_BSIZE;
1282 1290 cinfo.dki_addr = 0;
1283 1291 cinfo.dki_slave = 0;
1284 1292 cinfo.dki_space = 0;
1285 1293 cinfo.dki_prio = 0;
1286 1294 cinfo.dki_vec = 0;
1287 1295 if (ddi_copyout(&cinfo, ptr, sizeof (cinfo), flag)) {
1288 1296 return (EFAULT);
1289 1297 }
1290 1298 return (0);
1291 1299 }
1292 1300 case DKIOCREMOVABLE: {
1293 1301 int i;
1294 1302 i = bd->d_removable ? 1 : 0;
1295 1303 if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
1296 1304 return (EFAULT);
1297 1305 }
1298 1306 return (0);
1299 1307 }
1300 1308 case DKIOCHOTPLUGGABLE: {
1301 1309 int i;
1302 1310 i = bd->d_hotpluggable ? 1 : 0;
1303 1311 if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
1304 1312 return (EFAULT);
1305 1313 }
1306 1314 return (0);
1307 1315 }
1308 1316 case DKIOCREADONLY: {
1309 1317 int i;
1310 1318 i = bd->d_rdonly ? 1 : 0;
1311 1319 if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
1312 1320 return (EFAULT);
1313 1321 }
1314 1322 return (0);
1315 1323 }
1316 1324 case DKIOCSOLIDSTATE: {
1317 1325 int i;
1318 1326 i = bd->d_ssd ? 1 : 0;
1319 1327 if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
1320 1328 return (EFAULT);
1321 1329 }
1322 1330 return (0);
1323 1331 }
1324 1332 case DKIOCSTATE: {
1325 1333 enum dkio_state state;
1326 1334 if (ddi_copyin(ptr, &state, sizeof (state), flag)) {
1327 1335 return (EFAULT);
1328 1336 }
1329 1337 if ((rv = bd_check_state(bd, &state)) != 0) {
1330 1338 return (rv);
1331 1339 }
1332 1340 if (ddi_copyout(&state, ptr, sizeof (state), flag)) {
1333 1341 return (EFAULT);
1334 1342 }
1335 1343 return (0);
1336 1344 }
1337 1345 case DKIOCFLUSHWRITECACHE: {
1338 1346 struct dk_callback *dkc = NULL;
1339 1347
1340 1348 if (flag & FKIOCTL)
1341 1349 dkc = (void *)arg;
1342 1350
1343 1351 rv = bd_flush_write_cache(bd, dkc);
1344 1352 return (rv);
1345 1353 }
1346 1354
1347 1355 default:
1348 1356 break;
1349 1357
1350 1358 }
1351 1359 return (ENOTTY);
1352 1360 }
1353 1361
1354 1362 static int
1355 1363 bd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1356 1364 char *name, caddr_t valuep, int *lengthp)
1357 1365 {
1358 1366 bd_t *bd;
1359 1367
1360 1368 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
1361 1369 if (bd == NULL)
1362 1370 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1363 1371 name, valuep, lengthp));
1364 1372
1365 1373 return (cmlb_prop_op(bd->d_cmlbh, dev, dip, prop_op, mod_flags, name,
1366 1374 valuep, lengthp, BDPART(dev), 0));
1367 1375 }
1368 1376
1369 1377
1370 1378 static int
1371 1379 bd_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
1372 1380 size_t length, void *tg_cookie)
1373 1381 {
1374 1382 bd_t *bd;
1375 1383 buf_t *bp;
1376 1384 bd_xfer_impl_t *xi;
1377 1385 int rv;
1378 1386 int (*func)(void *, bd_xfer_t *);
↓ open down ↓ |
161 lines elided |
↑ open up ↑ |
1379 1387 int kmflag;
1380 1388
1381 1389 /*
1382 1390 * If we are running in polled mode (such as during dump(9e)
1383 1391 * execution), then we cannot sleep for kernel allocations.
1384 1392 */
1385 1393 kmflag = tg_cookie ? KM_NOSLEEP : KM_SLEEP;
1386 1394
1387 1395 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
1388 1396
1389 - if (P2PHASE(length, (1U << bd->d_blkshift)) != 0) {
1397 + if (P2PHASE(length, (1U << DEV_BSHIFT)) != 0) {
1390 1398 /* We can only transfer whole blocks at a time! */
1391 1399 return (EINVAL);
1392 1400 }
1393 1401
1394 1402 if ((bp = getrbuf(kmflag)) == NULL) {
1395 1403 return (ENOMEM);
1396 1404 }
1397 1405
1398 1406 switch (cmd) {
1399 1407 case TG_READ:
1400 1408 bp->b_flags = B_READ;
1401 1409 func = bd->d_ops.o_read;
1402 1410 break;
1403 1411 case TG_WRITE:
1404 1412 bp->b_flags = B_WRITE;
1405 1413 func = bd->d_ops.o_write;
1406 1414 break;
1407 1415 default:
1408 1416 freerbuf(bp);
1409 1417 return (EINVAL);
1410 1418 }
1411 1419
1412 1420 bp->b_un.b_addr = bufaddr;
1413 1421 bp->b_bcount = length;
1414 1422 xi = bd_xfer_alloc(bd, bp, func, kmflag);
1415 1423 if (xi == NULL) {
1416 1424 rv = geterror(bp);
1417 1425 freerbuf(bp);
1418 1426 return (rv);
1419 1427 }
1420 1428 xi->i_flags = tg_cookie ? BD_XFER_POLL : 0;
1421 1429 xi->i_blkno = start;
1422 1430 bd_submit(bd, xi);
1423 1431 (void) biowait(bp);
1424 1432 rv = geterror(bp);
1425 1433 freerbuf(bp);
1426 1434
1427 1435 return (rv);
1428 1436 }
1429 1437
1430 1438 static int
1431 1439 bd_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
1432 1440 {
1433 1441 bd_t *bd;
1434 1442
1435 1443 _NOTE(ARGUNUSED(tg_cookie));
1436 1444 bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
1437 1445
1438 1446 switch (cmd) {
1439 1447 case TG_GETPHYGEOM:
1440 1448 case TG_GETVIRTGEOM:
1441 1449 /*
1442 1450 * We don't have any "geometry" as such, let cmlb
1443 1451 * fabricate something.
1444 1452 */
1445 1453 return (ENOTTY);
1446 1454
1447 1455 case TG_GETCAPACITY:
1448 1456 bd_update_state(bd);
1449 1457 *(diskaddr_t *)arg = bd->d_numblks;
1450 1458 return (0);
1451 1459
1452 1460 case TG_GETBLOCKSIZE:
1453 1461 *(uint32_t *)arg = (1U << bd->d_blkshift);
1454 1462 return (0);
1455 1463
1456 1464 case TG_GETATTR:
1457 1465 /*
1458 1466 * It turns out that cmlb really doesn't do much for
1459 1467 * non-writable media, but lets make the information
1460 1468 * available for it in case it does more in the
1461 1469 * future. (The value is currently used for
1462 1470 * triggering special behavior for CD-ROMs.)
1463 1471 */
1464 1472 bd_update_state(bd);
1465 1473 ((tg_attribute_t *)arg)->media_is_writable =
1466 1474 bd->d_rdonly ? B_FALSE : B_TRUE;
1467 1475 ((tg_attribute_t *)arg)->media_is_solid_state = bd->d_ssd;
1468 1476 return (0);
1469 1477
1470 1478 default:
1471 1479 return (EINVAL);
1472 1480 }
1473 1481 }
1474 1482
1475 1483
1476 1484 static void
1477 1485 bd_sched(bd_t *bd)
1478 1486 {
1479 1487 bd_xfer_impl_t *xi;
1480 1488 struct buf *bp;
1481 1489 int rv;
1482 1490
1483 1491 mutex_enter(&bd->d_iomutex);
1484 1492
1485 1493 while ((bd->d_qactive < bd->d_qsize) &&
1486 1494 ((xi = list_remove_head(&bd->d_waitq)) != NULL)) {
1487 1495 bd->d_qactive++;
1488 1496 kstat_waitq_to_runq(bd->d_kiop);
1489 1497 list_insert_tail(&bd->d_runq, xi);
1490 1498
1491 1499 /*
1492 1500 * Submit the job to the driver. We drop the I/O mutex
1493 1501 * so that we can deal with the case where the driver
1494 1502 * completion routine calls back into us synchronously.
1495 1503 */
1496 1504
1497 1505 mutex_exit(&bd->d_iomutex);
1498 1506
1499 1507 rv = xi->i_func(bd->d_private, &xi->i_public);
1500 1508 if (rv != 0) {
1501 1509 bp = xi->i_bp;
1502 1510 bioerror(bp, rv);
1503 1511 biodone(bp);
1504 1512
1505 1513 atomic_inc_32(&bd->d_kerr->bd_transerrs.value.ui32);
1506 1514
1507 1515 mutex_enter(&bd->d_iomutex);
1508 1516 bd->d_qactive--;
1509 1517 kstat_runq_exit(bd->d_kiop);
1510 1518 list_remove(&bd->d_runq, xi);
1511 1519 bd_xfer_free(xi);
1512 1520 } else {
1513 1521 mutex_enter(&bd->d_iomutex);
1514 1522 }
1515 1523 }
1516 1524
1517 1525 mutex_exit(&bd->d_iomutex);
1518 1526 }
1519 1527
1520 1528 static void
1521 1529 bd_submit(bd_t *bd, bd_xfer_impl_t *xi)
1522 1530 {
1523 1531 mutex_enter(&bd->d_iomutex);
1524 1532 list_insert_tail(&bd->d_waitq, xi);
1525 1533 kstat_waitq_enter(bd->d_kiop);
1526 1534 mutex_exit(&bd->d_iomutex);
1527 1535
1528 1536 bd_sched(bd);
1529 1537 }
1530 1538
1531 1539 static void
1532 1540 bd_runq_exit(bd_xfer_impl_t *xi, int err)
1533 1541 {
1534 1542 bd_t *bd = xi->i_bd;
1535 1543 buf_t *bp = xi->i_bp;
1536 1544
1537 1545 mutex_enter(&bd->d_iomutex);
1538 1546 bd->d_qactive--;
1539 1547 kstat_runq_exit(bd->d_kiop);
1540 1548 list_remove(&bd->d_runq, xi);
1541 1549 mutex_exit(&bd->d_iomutex);
1542 1550
1543 1551 if (err == 0) {
1544 1552 if (bp->b_flags & B_READ) {
1545 1553 bd->d_kiop->reads++;
1546 1554 bd->d_kiop->nread += (bp->b_bcount - xi->i_resid);
1547 1555 } else {
1548 1556 bd->d_kiop->writes++;
1549 1557 bd->d_kiop->nwritten += (bp->b_bcount - xi->i_resid);
1550 1558 }
1551 1559 }
1552 1560 bd_sched(bd);
1553 1561 }
1554 1562
1555 1563 static void
1556 1564 bd_update_state(bd_t *bd)
1557 1565 {
1558 1566 enum dkio_state state = DKIO_INSERTED;
1559 1567 boolean_t docmlb = B_FALSE;
1560 1568 bd_media_t media;
1561 1569
1562 1570 bzero(&media, sizeof (media));
1563 1571
1564 1572 mutex_enter(&bd->d_statemutex);
1565 1573 if (bd->d_ops.o_media_info(bd->d_private, &media) != 0) {
1566 1574 bd->d_numblks = 0;
1567 1575 state = DKIO_EJECTED;
1568 1576 goto done;
1569 1577 }
1570 1578
1571 1579 if ((media.m_blksize < 512) ||
1572 1580 (!ISP2(media.m_blksize)) ||
1573 1581 (P2PHASE(bd->d_maxxfer, media.m_blksize))) {
1574 1582 cmn_err(CE_WARN, "%s%d: Invalid media block size (%d)",
1575 1583 ddi_driver_name(bd->d_dip), ddi_get_instance(bd->d_dip),
1576 1584 media.m_blksize);
1577 1585 /*
1578 1586 * We can't use the media, treat it as not present.
1579 1587 */
1580 1588 state = DKIO_EJECTED;
1581 1589 bd->d_numblks = 0;
1582 1590 goto done;
1583 1591 }
1584 1592
1585 1593 if (((1U << bd->d_blkshift) != media.m_blksize) ||
1586 1594 (bd->d_numblks != media.m_nblks)) {
1587 1595 /* Device size changed */
1588 1596 docmlb = B_TRUE;
1589 1597 }
1590 1598
1591 1599 bd->d_blkshift = ddi_ffs(media.m_blksize) - 1;
1592 1600 bd->d_pblkshift = bd->d_blkshift;
1593 1601 bd->d_numblks = media.m_nblks;
1594 1602 bd->d_rdonly = media.m_readonly;
1595 1603 bd->d_ssd = media.m_solidstate;
1596 1604
1597 1605 /*
1598 1606 * Only use the supplied physical block size if it is non-zero,
1599 1607 * greater or equal to the block size, and a power of 2. Ignore it
1600 1608 * if not, it's just informational and we can still use the media.
1601 1609 */
1602 1610 if ((media.m_pblksize != 0) &&
1603 1611 (media.m_pblksize >= media.m_blksize) &&
1604 1612 (ISP2(media.m_pblksize)))
1605 1613 bd->d_pblkshift = ddi_ffs(media.m_pblksize) - 1;
1606 1614
1607 1615 done:
1608 1616 if (state != bd->d_state) {
1609 1617 bd->d_state = state;
1610 1618 cv_broadcast(&bd->d_statecv);
1611 1619 docmlb = B_TRUE;
1612 1620 }
1613 1621 mutex_exit(&bd->d_statemutex);
1614 1622
1615 1623 bd->d_kerr->bd_capacity.value.ui64 = bd->d_numblks << bd->d_blkshift;
1616 1624
1617 1625 if (docmlb) {
1618 1626 if (state == DKIO_INSERTED) {
1619 1627 (void) cmlb_validate(bd->d_cmlbh, 0, 0);
1620 1628 } else {
1621 1629 cmlb_invalidate(bd->d_cmlbh, 0);
1622 1630 }
1623 1631 }
1624 1632 }
1625 1633
1626 1634 static int
1627 1635 bd_check_state(bd_t *bd, enum dkio_state *state)
1628 1636 {
1629 1637 clock_t when;
1630 1638
1631 1639 for (;;) {
1632 1640
1633 1641 bd_update_state(bd);
1634 1642
1635 1643 mutex_enter(&bd->d_statemutex);
1636 1644
1637 1645 if (bd->d_state != *state) {
1638 1646 *state = bd->d_state;
1639 1647 mutex_exit(&bd->d_statemutex);
1640 1648 break;
1641 1649 }
1642 1650
1643 1651 when = drv_usectohz(1000000);
1644 1652 if (cv_reltimedwait_sig(&bd->d_statecv, &bd->d_statemutex,
1645 1653 when, TR_CLOCK_TICK) == 0) {
1646 1654 mutex_exit(&bd->d_statemutex);
1647 1655 return (EINTR);
1648 1656 }
1649 1657
1650 1658 mutex_exit(&bd->d_statemutex);
1651 1659 }
1652 1660
1653 1661 return (0);
1654 1662 }
1655 1663
1656 1664 static int
1657 1665 bd_flush_write_cache_done(struct buf *bp)
1658 1666 {
1659 1667 struct dk_callback *dc = (void *)bp->b_private;
1660 1668
1661 1669 (*dc->dkc_callback)(dc->dkc_cookie, geterror(bp));
1662 1670 kmem_free(dc, sizeof (*dc));
1663 1671 freerbuf(bp);
1664 1672 return (0);
1665 1673 }
1666 1674
1667 1675 static int
1668 1676 bd_flush_write_cache(bd_t *bd, struct dk_callback *dkc)
1669 1677 {
1670 1678 buf_t *bp;
1671 1679 struct dk_callback *dc;
1672 1680 bd_xfer_impl_t *xi;
1673 1681 int rv;
1674 1682
1675 1683 if (bd->d_ops.o_sync_cache == NULL) {
1676 1684 return (ENOTSUP);
1677 1685 }
1678 1686 if ((bp = getrbuf(KM_SLEEP)) == NULL) {
1679 1687 return (ENOMEM);
1680 1688 }
1681 1689 bp->b_resid = 0;
1682 1690 bp->b_bcount = 0;
1683 1691
1684 1692 xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_sync_cache, KM_SLEEP);
1685 1693 if (xi == NULL) {
1686 1694 rv = geterror(bp);
1687 1695 freerbuf(bp);
1688 1696 return (rv);
1689 1697 }
1690 1698
1691 1699 /* Make an asynchronous flush, but only if there is a callback */
1692 1700 if (dkc != NULL && dkc->dkc_callback != NULL) {
1693 1701 /* Make a private copy of the callback structure */
1694 1702 dc = kmem_alloc(sizeof (*dc), KM_SLEEP);
1695 1703 *dc = *dkc;
1696 1704 bp->b_private = dc;
1697 1705 bp->b_iodone = bd_flush_write_cache_done;
1698 1706
1699 1707 bd_submit(bd, xi);
1700 1708 return (0);
1701 1709 }
1702 1710
1703 1711 /* In case there is no callback, perform a synchronous flush */
1704 1712 bd_submit(bd, xi);
1705 1713 (void) biowait(bp);
1706 1714 rv = geterror(bp);
1707 1715 freerbuf(bp);
1708 1716
1709 1717 return (rv);
1710 1718 }
1711 1719
1712 1720 /*
1713 1721 * Nexus support.
1714 1722 */
1715 1723 int
1716 1724 bd_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
1717 1725 void *arg, void *result)
1718 1726 {
1719 1727 bd_handle_t hdl;
1720 1728
1721 1729 switch (ctlop) {
1722 1730 case DDI_CTLOPS_REPORTDEV:
1723 1731 cmn_err(CE_CONT, "?Block device: %s@%s, %s%d\n",
1724 1732 ddi_node_name(rdip), ddi_get_name_addr(rdip),
1725 1733 ddi_driver_name(rdip), ddi_get_instance(rdip));
1726 1734 return (DDI_SUCCESS);
1727 1735
1728 1736 case DDI_CTLOPS_INITCHILD:
1729 1737 hdl = ddi_get_parent_data((dev_info_t *)arg);
1730 1738 if (hdl == NULL) {
1731 1739 return (DDI_NOT_WELL_FORMED);
1732 1740 }
1733 1741 ddi_set_name_addr((dev_info_t *)arg, hdl->h_addr);
1734 1742 return (DDI_SUCCESS);
1735 1743
1736 1744 case DDI_CTLOPS_UNINITCHILD:
1737 1745 ddi_set_name_addr((dev_info_t *)arg, NULL);
1738 1746 ndi_prop_remove_all((dev_info_t *)arg);
1739 1747 return (DDI_SUCCESS);
1740 1748
1741 1749 default:
1742 1750 return (ddi_ctlops(dip, rdip, ctlop, arg, result));
1743 1751 }
1744 1752 }
1745 1753
1746 1754 /*
1747 1755 * Functions for device drivers.
1748 1756 */
1749 1757 bd_handle_t
1750 1758 bd_alloc_handle(void *private, bd_ops_t *ops, ddi_dma_attr_t *dma, int kmflag)
1751 1759 {
1752 1760 bd_handle_t hdl;
1753 1761
1754 1762 hdl = kmem_zalloc(sizeof (*hdl), kmflag);
1755 1763 if (hdl != NULL) {
1756 1764 hdl->h_ops = *ops;
1757 1765 hdl->h_dma = dma;
1758 1766 hdl->h_private = private;
1759 1767 }
1760 1768
1761 1769 return (hdl);
1762 1770 }
1763 1771
1764 1772 void
1765 1773 bd_free_handle(bd_handle_t hdl)
1766 1774 {
1767 1775 kmem_free(hdl, sizeof (*hdl));
1768 1776 }
1769 1777
1770 1778 int
1771 1779 bd_attach_handle(dev_info_t *dip, bd_handle_t hdl)
1772 1780 {
1773 1781 dev_info_t *child;
1774 1782 bd_drive_t drive = { 0 };
1775 1783
1776 1784 /* if drivers don't override this, make it assume none */
1777 1785 drive.d_lun = -1;
1778 1786 hdl->h_ops.o_drive_info(hdl->h_private, &drive);
1779 1787
1780 1788 hdl->h_parent = dip;
1781 1789 hdl->h_name = "blkdev";
1782 1790
1783 1791 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
1784 1792 if (*(uint64_t *)drive.d_eui64 != 0) {
1785 1793 if (drive.d_lun >= 0) {
1786 1794 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr),
1787 1795 "w%02X%02X%02X%02X%02X%02X%02X%02X,%X",
1788 1796 drive.d_eui64[0], drive.d_eui64[1],
1789 1797 drive.d_eui64[2], drive.d_eui64[3],
1790 1798 drive.d_eui64[4], drive.d_eui64[5],
1791 1799 drive.d_eui64[6], drive.d_eui64[7], drive.d_lun);
1792 1800 } else {
1793 1801 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr),
1794 1802 "w%02X%02X%02X%02X%02X%02X%02X%02X",
1795 1803 drive.d_eui64[0], drive.d_eui64[1],
1796 1804 drive.d_eui64[2], drive.d_eui64[3],
1797 1805 drive.d_eui64[4], drive.d_eui64[5],
1798 1806 drive.d_eui64[6], drive.d_eui64[7]);
1799 1807 }
1800 1808 } else {
1801 1809 if (drive.d_lun >= 0) {
1802 1810 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr),
1803 1811 "%X,%X", drive.d_target, drive.d_lun);
1804 1812 } else {
1805 1813 (void) snprintf(hdl->h_addr, sizeof (hdl->h_addr),
1806 1814 "%X", drive.d_target);
1807 1815 }
1808 1816 }
1809 1817
1810 1818 if (ndi_devi_alloc(dip, hdl->h_name, (pnode_t)DEVI_SID_NODEID,
1811 1819 &child) != NDI_SUCCESS) {
1812 1820 cmn_err(CE_WARN, "%s%d: unable to allocate node %s@%s",
1813 1821 ddi_driver_name(dip), ddi_get_instance(dip),
1814 1822 "blkdev", hdl->h_addr);
1815 1823 return (DDI_FAILURE);
1816 1824 }
1817 1825
1818 1826 ddi_set_parent_data(child, hdl);
1819 1827 hdl->h_child = child;
1820 1828
1821 1829 if (ndi_devi_online(child, 0) == NDI_FAILURE) {
1822 1830 cmn_err(CE_WARN, "%s%d: failed bringing node %s@%s online",
1823 1831 ddi_driver_name(dip), ddi_get_instance(dip),
1824 1832 hdl->h_name, hdl->h_addr);
1825 1833 (void) ndi_devi_free(child);
1826 1834 return (DDI_FAILURE);
1827 1835 }
1828 1836
1829 1837 return (DDI_SUCCESS);
1830 1838 }
1831 1839
1832 1840 int
1833 1841 bd_detach_handle(bd_handle_t hdl)
1834 1842 {
1835 1843 int circ;
1836 1844 int rv;
1837 1845 char *devnm;
1838 1846
1839 1847 if (hdl->h_child == NULL) {
1840 1848 return (DDI_SUCCESS);
1841 1849 }
1842 1850 ndi_devi_enter(hdl->h_parent, &circ);
1843 1851 if (i_ddi_node_state(hdl->h_child) < DS_INITIALIZED) {
1844 1852 rv = ddi_remove_child(hdl->h_child, 0);
1845 1853 } else {
1846 1854 devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
1847 1855 (void) ddi_deviname(hdl->h_child, devnm);
1848 1856 (void) devfs_clean(hdl->h_parent, devnm + 1, DV_CLEAN_FORCE);
1849 1857 rv = ndi_devi_unconfig_one(hdl->h_parent, devnm + 1, NULL,
1850 1858 NDI_DEVI_REMOVE | NDI_UNCONFIG);
1851 1859 kmem_free(devnm, MAXNAMELEN + 1);
1852 1860 }
1853 1861 if (rv == 0) {
1854 1862 hdl->h_child = NULL;
1855 1863 }
1856 1864
1857 1865 ndi_devi_exit(hdl->h_parent, circ);
1858 1866 return (rv == NDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
1859 1867 }
1860 1868
1861 1869 void
1862 1870 bd_xfer_done(bd_xfer_t *xfer, int err)
1863 1871 {
1864 1872 bd_xfer_impl_t *xi = (void *)xfer;
1865 1873 buf_t *bp = xi->i_bp;
1866 1874 int rv = DDI_SUCCESS;
1867 1875 bd_t *bd = xi->i_bd;
1868 1876 size_t len;
1869 1877
1870 1878 if (err != 0) {
1871 1879 bd_runq_exit(xi, err);
1872 1880 atomic_inc_32(&bd->d_kerr->bd_harderrs.value.ui32);
1873 1881
1874 1882 bp->b_resid += xi->i_resid;
1875 1883 bd_xfer_free(xi);
1876 1884 bioerror(bp, err);
1877 1885 biodone(bp);
1878 1886 return;
1879 1887 }
1880 1888
1881 1889 xi->i_cur_win++;
1882 1890 xi->i_resid -= xi->i_len;
1883 1891
1884 1892 if (xi->i_resid == 0) {
1885 1893 /* Job completed succcessfully! */
1886 1894 bd_runq_exit(xi, 0);
1887 1895
1888 1896 bd_xfer_free(xi);
1889 1897 biodone(bp);
1890 1898 return;
1891 1899 }
1892 1900
1893 1901 xi->i_blkno += xi->i_nblks;
1894 1902
1895 1903 if (bd->d_use_dma) {
1896 1904 /* More transfer still pending... advance to next DMA window. */
1897 1905 rv = ddi_dma_getwin(xi->i_dmah, xi->i_cur_win,
↓ open down ↓ |
498 lines elided |
↑ open up ↑ |
1898 1906 &xi->i_offset, &len, &xi->i_dmac, &xi->i_ndmac);
1899 1907 } else {
1900 1908 /* Advance memory window. */
1901 1909 xi->i_kaddr += xi->i_len;
1902 1910 xi->i_offset += xi->i_len;
1903 1911 len = min(bp->b_bcount - xi->i_offset, bd->d_maxxfer);
1904 1912 }
1905 1913
1906 1914
1907 1915 if ((rv != DDI_SUCCESS) ||
1908 - (P2PHASE(len, (1U << xi->i_blkshift) != 0))) {
1916 + (P2PHASE(len, (1U << DEV_BSHIFT) != 0))) {
1909 1917 bd_runq_exit(xi, EFAULT);
1910 1918
1911 1919 bp->b_resid += xi->i_resid;
1912 1920 bd_xfer_free(xi);
1913 1921 bioerror(bp, EFAULT);
1914 1922 biodone(bp);
1915 1923 return;
1916 1924 }
1917 1925 xi->i_len = len;
1918 - xi->i_nblks = len >> xi->i_blkshift;
1926 + xi->i_nblks = howmany(len, (1U << xi->i_blkshift));
1919 1927
1920 1928 /* Submit next window to hardware. */
1921 1929 rv = xi->i_func(bd->d_private, &xi->i_public);
1922 1930 if (rv != 0) {
1923 1931 bd_runq_exit(xi, rv);
1924 1932
1925 1933 atomic_inc_32(&bd->d_kerr->bd_transerrs.value.ui32);
1926 1934
1927 1935 bp->b_resid += xi->i_resid;
1928 1936 bd_xfer_free(xi);
1929 1937 bioerror(bp, rv);
1930 1938 biodone(bp);
1931 1939 }
1932 1940 }
1933 1941
1934 1942 void
1935 1943 bd_error(bd_xfer_t *xfer, int error)
1936 1944 {
1937 1945 bd_xfer_impl_t *xi = (void *)xfer;
1938 1946 bd_t *bd = xi->i_bd;
1939 1947
1940 1948 switch (error) {
1941 1949 case BD_ERR_MEDIA:
1942 1950 atomic_inc_32(&bd->d_kerr->bd_rq_media_err.value.ui32);
1943 1951 break;
1944 1952 case BD_ERR_NTRDY:
1945 1953 atomic_inc_32(&bd->d_kerr->bd_rq_ntrdy_err.value.ui32);
1946 1954 break;
1947 1955 case BD_ERR_NODEV:
1948 1956 atomic_inc_32(&bd->d_kerr->bd_rq_nodev_err.value.ui32);
1949 1957 break;
1950 1958 case BD_ERR_RECOV:
1951 1959 atomic_inc_32(&bd->d_kerr->bd_rq_recov_err.value.ui32);
1952 1960 break;
1953 1961 case BD_ERR_ILLRQ:
1954 1962 atomic_inc_32(&bd->d_kerr->bd_rq_illrq_err.value.ui32);
1955 1963 break;
1956 1964 case BD_ERR_PFA:
1957 1965 atomic_inc_32(&bd->d_kerr->bd_rq_pfa_err.value.ui32);
1958 1966 break;
1959 1967 default:
1960 1968 cmn_err(CE_PANIC, "bd_error: unknown error type %d", error);
1961 1969 break;
1962 1970 }
1963 1971 }
1964 1972
1965 1973 void
1966 1974 bd_state_change(bd_handle_t hdl)
1967 1975 {
1968 1976 bd_t *bd;
1969 1977
1970 1978 if ((bd = hdl->h_bd) != NULL) {
1971 1979 bd_update_state(bd);
1972 1980 }
1973 1981 }
1974 1982
1975 1983 void
1976 1984 bd_mod_init(struct dev_ops *devops)
1977 1985 {
1978 1986 static struct bus_ops bd_bus_ops = {
1979 1987 BUSO_REV, /* busops_rev */
1980 1988 nullbusmap, /* bus_map */
1981 1989 NULL, /* bus_get_intrspec (OBSOLETE) */
1982 1990 NULL, /* bus_add_intrspec (OBSOLETE) */
1983 1991 NULL, /* bus_remove_intrspec (OBSOLETE) */
1984 1992 i_ddi_map_fault, /* bus_map_fault */
1985 1993 NULL, /* bus_dma_map (OBSOLETE) */
1986 1994 ddi_dma_allochdl, /* bus_dma_allochdl */
1987 1995 ddi_dma_freehdl, /* bus_dma_freehdl */
1988 1996 ddi_dma_bindhdl, /* bus_dma_bindhdl */
1989 1997 ddi_dma_unbindhdl, /* bus_dma_unbindhdl */
1990 1998 ddi_dma_flush, /* bus_dma_flush */
1991 1999 ddi_dma_win, /* bus_dma_win */
1992 2000 ddi_dma_mctl, /* bus_dma_ctl */
1993 2001 bd_bus_ctl, /* bus_ctl */
1994 2002 ddi_bus_prop_op, /* bus_prop_op */
1995 2003 NULL, /* bus_get_eventcookie */
1996 2004 NULL, /* bus_add_eventcall */
1997 2005 NULL, /* bus_remove_eventcall */
1998 2006 NULL, /* bus_post_event */
1999 2007 NULL, /* bus_intr_ctl (OBSOLETE) */
2000 2008 NULL, /* bus_config */
2001 2009 NULL, /* bus_unconfig */
2002 2010 NULL, /* bus_fm_init */
2003 2011 NULL, /* bus_fm_fini */
2004 2012 NULL, /* bus_fm_access_enter */
2005 2013 NULL, /* bus_fm_access_exit */
2006 2014 NULL, /* bus_power */
2007 2015 NULL, /* bus_intr_op */
2008 2016 };
2009 2017
2010 2018 devops->devo_bus_ops = &bd_bus_ops;
2011 2019
2012 2020 /*
2013 2021 * NB: The device driver is free to supply its own
2014 2022 * character entry device support.
2015 2023 */
2016 2024 }
2017 2025
2018 2026 void
2019 2027 bd_mod_fini(struct dev_ops *devops)
2020 2028 {
2021 2029 devops->devo_bus_ops = NULL;
2022 2030 }
↓ open down ↓ |
94 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX