1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/scsi/scsi.h> 30 #include <sys/vtrace.h> 31 32 33 #define A_TO_TRAN(ap) ((ap)->a_hba_tran) 34 #define P_TO_TRAN(pkt) ((pkt)->pkt_address.a_hba_tran) 35 #define P_TO_ADDR(pkt) (&((pkt)->pkt_address)) 36 37 /* 38 * Callback id 39 */ 40 uintptr_t scsi_callback_id = 0; 41 42 extern ddi_dma_attr_t scsi_alloc_attr; 43 44 struct buf * 45 scsi_alloc_consistent_buf(struct scsi_address *ap, 46 struct buf *in_bp, size_t datalen, uint_t bflags, 47 int (*callback)(caddr_t), caddr_t callback_arg) 48 { 49 dev_info_t *pdip; 50 struct buf *bp; 51 int kmflag; 52 size_t rlen; 53 54 TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_ALLOC_CONSISTENT_BUF_START, 55 "scsi_alloc_consistent_buf_start"); 56 57 if (!in_bp) { 58 kmflag = (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP; 59 if ((bp = getrbuf(kmflag)) == NULL) { 60 goto no_resource; 61 } 62 } else { 63 bp = in_bp; 64 65 /* we are establishing a new buffer memory association */ 66 bp->b_flags &= ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW); 67 bp->b_proc = NULL; 68 bp->b_pages = NULL; 69 bp->b_shadow = NULL; 70 } 71 72 /* limit bits that can be set by bflags argument */ 73 ASSERT(!(bflags & ~(B_READ | B_WRITE))); 74 bflags &= (B_READ | B_WRITE); 75 bp->b_un.b_addr = 0; 76 77 if (datalen) { 78 pdip = (A_TO_TRAN(ap))->tran_hba_dip; 79 80 /* 81 * use i_ddi_mem_alloc() for now until we have an interface to 82 * allocate memory for DMA which doesn't require a DMA handle. 83 * ddi_iopb_alloc() is obsolete and we want more flexibility in 84 * controlling the DMA address constraints. 85 */ 86 while (i_ddi_mem_alloc(pdip, &scsi_alloc_attr, datalen, 87 ((callback == SLEEP_FUNC) ? 1 : 0), 0, NULL, 88 &bp->b_un.b_addr, &rlen, NULL) != DDI_SUCCESS) { 89 if (callback == SLEEP_FUNC) { 90 delay(drv_usectohz(10000)); 91 } else { 92 if (!in_bp) 93 freerbuf(bp); 94 goto no_resource; 95 } 96 } 97 bp->b_flags |= bflags; 98 } 99 bp->b_bcount = datalen; 100 bp->b_resid = 0; 101 102 TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_ALLOC_CONSISTENT_BUF_END, 103 "scsi_alloc_consistent_buf_end"); 104 return (bp); 105 106 no_resource: 107 108 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 109 ddi_set_callback(callback, callback_arg, 110 &scsi_callback_id); 111 } 112 TRACE_0(TR_FAC_SCSI_RES, 113 TR_SCSI_ALLOC_CONSISTENT_BUF_RETURN1_END, 114 "scsi_alloc_consistent_buf_end (return1)"); 115 return (NULL); 116 } 117 118 void 119 scsi_free_consistent_buf(struct buf *bp) 120 { 121 TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_FREE_CONSISTENT_BUF_START, 122 "scsi_free_consistent_buf_start"); 123 if (!bp) 124 return; 125 if (bp->b_un.b_addr) 126 i_ddi_mem_free((caddr_t)bp->b_un.b_addr, 0); 127 freerbuf(bp); 128 if (scsi_callback_id != 0) { 129 ddi_run_callback(&scsi_callback_id); 130 } 131 TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_FREE_CONSISTENT_BUF_END, 132 "scsi_free_consistent_buf_end"); 133 } 134 135 void scsi_free_cache_pkt(struct scsi_address *, struct scsi_pkt *); 136 137 struct scsi_pkt * 138 scsi_init_cache_pkt(struct scsi_address *ap, struct scsi_pkt *in_pktp, 139 struct buf *bp, int cmdlen, int statuslen, int pplen, 140 int flags, int (*callback)(caddr_t), caddr_t callback_arg) 141 { 142 struct scsi_pkt_cache_wrapper *pktw; 143 scsi_hba_tran_t *tranp = ap->a_hba_tran; 144 int (*func)(caddr_t); 145 146 func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC; 147 148 if (in_pktp == NULL) { 149 int kf; 150 151 if (callback == SLEEP_FUNC) 152 kf = KM_SLEEP; 153 else 154 kf = KM_NOSLEEP; 155 pktw = kmem_cache_alloc(tranp->tran_pkt_cache_ptr, 156 kf); 157 if (pktw == NULL) 158 goto fail1; 159 160 pktw->pcw_kmflags = 0; 161 in_pktp = &(pktw->pcw_pkt); 162 /* 163 * target driver initializes pkt_comp, pkt_flags, 164 * and pkt_time 165 */ 166 in_pktp->pkt_address = *ap; 167 in_pktp->pkt_resid = 0; 168 in_pktp->pkt_state = 0; 169 in_pktp->pkt_statistics = 0; 170 in_pktp->pkt_reason = 0; 171 172 in_pktp->pkt_cdblen = cmdlen; 173 if ((tranp->tran_hba_flags & SCSI_HBA_TRAN_CDB) && 174 (cmdlen > DEFAULT_CDBLEN)) { 175 pktw->pcw_kmflags |= NEED_EXT_CDB; 176 in_pktp->pkt_cdbp = kmem_zalloc(cmdlen, kf); 177 if (in_pktp->pkt_cdbp == NULL) 178 goto fail2; 179 } 180 in_pktp->pkt_tgtlen = pplen; 181 if (pplen > DEFAULT_PRIVLEN) { 182 pktw->pcw_kmflags |= NEED_EXT_TGT; 183 in_pktp->pkt_private = kmem_zalloc(pplen, kf); 184 if (in_pktp->pkt_private == NULL) 185 goto fail3; 186 } 187 in_pktp->pkt_scblen = statuslen; 188 if ((tranp->tran_hba_flags & SCSI_HBA_TRAN_SCB) && 189 (statuslen > DEFAULT_SCBLEN)) { 190 pktw->pcw_kmflags |= NEED_EXT_SCB; 191 in_pktp->pkt_scbp = kmem_zalloc(statuslen, kf); 192 if (in_pktp->pkt_scbp == NULL) 193 goto fail4; 194 } 195 if ((*tranp->tran_setup_pkt) (in_pktp, 196 func, NULL) == -1) { 197 goto fail5; 198 } 199 } 200 if (bp && bp->b_bcount) { 201 if ((*tranp->tran_setup_bp) (in_pktp, bp, 202 flags, func, NULL) == -1) { 203 scsi_free_cache_pkt(ap, in_pktp); 204 in_pktp = NULL; 205 } 206 } 207 return (in_pktp); 208 209 fail5: 210 if (pktw->pcw_kmflags & NEED_EXT_SCB) { 211 kmem_free(in_pktp->pkt_scbp, statuslen); 212 in_pktp->pkt_scbp = (opaque_t)((char *)in_pktp + 213 tranp->tran_hba_len + DEFAULT_PRIVLEN + 214 sizeof (struct scsi_pkt)); 215 if ((A_TO_TRAN(ap))->tran_hba_flags & SCSI_HBA_TRAN_CDB) 216 in_pktp->pkt_scbp = (opaque_t)((in_pktp->pkt_scbp) + 217 DEFAULT_CDBLEN); 218 in_pktp->pkt_scblen = 0; 219 } 220 fail4: 221 if (pktw->pcw_kmflags & NEED_EXT_TGT) { 222 kmem_free(in_pktp->pkt_private, pplen); 223 in_pktp->pkt_tgtlen = 0; 224 in_pktp->pkt_private = NULL; 225 } 226 fail3: 227 if (pktw->pcw_kmflags & NEED_EXT_CDB) { 228 kmem_free(in_pktp->pkt_cdbp, cmdlen); 229 in_pktp->pkt_cdbp = (opaque_t)((char *)in_pktp + 230 tranp->tran_hba_len + 231 sizeof (struct scsi_pkt)); 232 in_pktp->pkt_cdblen = 0; 233 } 234 pktw->pcw_kmflags &= 235 ~(NEED_EXT_CDB|NEED_EXT_TGT|NEED_EXT_SCB); 236 fail2: 237 kmem_cache_free(tranp->tran_pkt_cache_ptr, pktw); 238 fail1: 239 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 240 ddi_set_callback(callback, callback_arg, 241 &scsi_callback_id); 242 } 243 244 return (NULL); 245 } 246 247 void 248 scsi_free_cache_pkt(struct scsi_address *ap, struct scsi_pkt *pktp) 249 { 250 struct scsi_pkt_cache_wrapper *pktw; 251 252 (*A_TO_TRAN(ap)->tran_teardown_pkt)(pktp); 253 pktw = (struct scsi_pkt_cache_wrapper *)pktp; 254 255 /* 256 * if we allocated memory for anything that wouldn't fit, free 257 * the memory and restore the pointers 258 */ 259 if (pktw->pcw_kmflags & NEED_EXT_SCB) { 260 kmem_free(pktp->pkt_scbp, pktp->pkt_scblen); 261 pktp->pkt_scbp = (opaque_t)((char *)pktp + 262 (A_TO_TRAN(ap))->tran_hba_len + 263 DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper)); 264 if ((A_TO_TRAN(ap))->tran_hba_flags & SCSI_HBA_TRAN_CDB) 265 pktp->pkt_scbp = (opaque_t)((pktp->pkt_scbp) + 266 DEFAULT_CDBLEN); 267 pktp->pkt_scblen = 0; 268 } 269 if (pktw->pcw_kmflags & NEED_EXT_TGT) { 270 kmem_free(pktp->pkt_private, pktp->pkt_tgtlen); 271 pktp->pkt_tgtlen = 0; 272 pktp->pkt_private = NULL; 273 } 274 if (pktw->pcw_kmflags & NEED_EXT_CDB) { 275 kmem_free(pktp->pkt_cdbp, pktp->pkt_cdblen); 276 pktp->pkt_cdbp = (opaque_t)((char *)pktp + 277 (A_TO_TRAN(ap))->tran_hba_len + 278 sizeof (struct scsi_pkt_cache_wrapper)); 279 pktp->pkt_cdblen = 0; 280 } 281 pktw->pcw_kmflags &= 282 ~(NEED_EXT_CDB|NEED_EXT_TGT|NEED_EXT_SCB); 283 ASSERT(pktw->pcw_kmflags == 0); 284 kmem_cache_free(A_TO_TRAN(ap)->tran_pkt_cache_ptr, pktw); 285 286 if (scsi_callback_id != 0) { 287 ddi_run_callback(&scsi_callback_id); 288 } 289 290 } 291 292 struct scsi_pkt * 293 scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *in_pktp, 294 struct buf *bp, int cmdlen, int statuslen, int pplen, 295 int flags, int (*callback)(caddr_t), caddr_t callback_arg) 296 { 297 struct scsi_pkt *pktp; 298 scsi_hba_tran_t *tranp = ap->a_hba_tran; 299 int (*func)(caddr_t); 300 301 TRACE_5(TR_FAC_SCSI_RES, TR_SCSI_INIT_PKT_START, 302 "scsi_init_pkt_start: addr %p in_pktp %p cmdlen %d statuslen %d pplen %d", 303 ap, in_pktp, cmdlen, statuslen, pplen); 304 305 #if defined(__i386) || defined(__amd64) 306 if (flags & PKT_CONSISTENT_OLD) { 307 flags &= ~PKT_CONSISTENT_OLD; 308 flags |= PKT_CONSISTENT; 309 } 310 #endif 311 312 func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC; 313 314 pktp = (*tranp->tran_init_pkt) (ap, in_pktp, bp, cmdlen, 315 statuslen, pplen, flags, func, NULL); 316 if (pktp == NULL) { 317 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 318 ddi_set_callback(callback, callback_arg, 319 &scsi_callback_id); 320 } 321 } 322 323 TRACE_1(TR_FAC_SCSI_RES, TR_SCSI_INIT_PKT_END, 324 "scsi_init_pkt_end: pktp %p", pktp); 325 return (pktp); 326 } 327 328 void 329 scsi_destroy_pkt(struct scsi_pkt *pkt) 330 { 331 struct scsi_address *ap = P_TO_ADDR(pkt); 332 333 TRACE_1(TR_FAC_SCSI_RES, TR_SCSI_DESTROY_PKT_START, 334 "scsi_destroy_pkt_start: pkt %p", pkt); 335 336 (*A_TO_TRAN(ap)->tran_destroy_pkt)(ap, pkt); 337 338 if (scsi_callback_id != 0) { 339 ddi_run_callback(&scsi_callback_id); 340 } 341 342 TRACE_0(TR_FAC_SCSI_RES, TR_SCSI_DESTROY_PKT_END, 343 "scsi_destroy_pkt_end"); 344 } 345 346 347 /* 348 * Generic Resource Allocation Routines 349 */ 350 351 struct scsi_pkt * 352 scsi_resalloc(struct scsi_address *ap, int cmdlen, int statuslen, 353 opaque_t dmatoken, int (*callback)()) 354 { 355 register struct scsi_pkt *pkt; 356 register scsi_hba_tran_t *tranp = ap->a_hba_tran; 357 register int (*func)(caddr_t); 358 359 func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC; 360 361 pkt = (*tranp->tran_init_pkt) (ap, NULL, (struct buf *)dmatoken, 362 cmdlen, statuslen, 0, 0, func, NULL); 363 if (pkt == NULL) { 364 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 365 ddi_set_callback(callback, NULL, &scsi_callback_id); 366 } 367 } 368 369 return (pkt); 370 } 371 372 struct scsi_pkt * 373 scsi_pktalloc(struct scsi_address *ap, int cmdlen, int statuslen, 374 int (*callback)()) 375 { 376 struct scsi_pkt *pkt; 377 struct scsi_hba_tran *tran = ap->a_hba_tran; 378 register int (*func)(caddr_t); 379 380 func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC; 381 382 pkt = (*tran->tran_init_pkt) (ap, NULL, NULL, cmdlen, 383 statuslen, 0, 0, func, NULL); 384 if (pkt == NULL) { 385 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 386 ddi_set_callback(callback, NULL, &scsi_callback_id); 387 } 388 } 389 390 return (pkt); 391 } 392 393 struct scsi_pkt * 394 scsi_dmaget(struct scsi_pkt *pkt, opaque_t dmatoken, int (*callback)()) 395 { 396 struct scsi_pkt *new_pkt; 397 register int (*func)(caddr_t); 398 399 func = (callback == SLEEP_FUNC) ? SLEEP_FUNC : NULL_FUNC; 400 401 new_pkt = (*P_TO_TRAN(pkt)->tran_init_pkt) (&pkt->pkt_address, 402 pkt, (struct buf *)dmatoken, 403 0, 0, 0, 0, func, NULL); 404 ASSERT(new_pkt == pkt || new_pkt == NULL); 405 if (new_pkt == NULL) { 406 if (callback != NULL_FUNC && callback != SLEEP_FUNC) { 407 ddi_set_callback(callback, NULL, &scsi_callback_id); 408 } 409 } 410 411 return (new_pkt); 412 } 413 414 415 /* 416 * Generic Resource Deallocation Routines 417 */ 418 419 void 420 scsi_dmafree(struct scsi_pkt *pkt) 421 { 422 register struct scsi_address *ap = P_TO_ADDR(pkt); 423 (*A_TO_TRAN(ap)->tran_dmafree)(ap, pkt); 424 425 if (scsi_callback_id != 0) { 426 ddi_run_callback(&scsi_callback_id); 427 } 428 } 429 430 void 431 scsi_sync_pkt(struct scsi_pkt *pkt) 432 { 433 register struct scsi_address *ap = P_TO_ADDR(pkt); 434 (*A_TO_TRAN(ap)->tran_sync_pkt)(ap, pkt); 435 } 436 437 void 438 scsi_resfree(struct scsi_pkt *pkt) 439 { 440 register struct scsi_address *ap = P_TO_ADDR(pkt); 441 (*A_TO_TRAN(ap)->tran_destroy_pkt)(ap, pkt); 442 443 if (scsi_callback_id != 0) { 444 ddi_run_callback(&scsi_callback_id); 445 } 446 } 447