xref: /freebsd/sys/dev/smartpqi/smartpqi_request.c (revision d0b2dbfa)
1 /*-
2  * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 
27 #include "smartpqi_includes.h"
28 
29 /*
30  * Attempt to perform offload RAID mapping for a logical volume I/O.
31  */
32 
33 #define HPSA_RAID_0		0
34 #define HPSA_RAID_4		1
35 #define HPSA_RAID_1		2	/* also used for RAID 10 */
36 #define HPSA_RAID_5		3	/* also used for RAID 50 */
37 #define HPSA_RAID_51		4
38 #define HPSA_RAID_6		5	/* also used for RAID 60 */
39 #define HPSA_RAID_ADM		6	/* also used for RAID 1+0 ADM */
40 #define HPSA_RAID_MAX		HPSA_RAID_ADM
41 #define HPSA_RAID_UNKNOWN	0xff
42 
43 #define SG_FLAG_LAST	0x40000000
44 #define SG_FLAG_CHAIN	0x80000000
45 
46 /* Subroutine to find out embedded sgl count in IU */
47 static inline uint32_t
48 pqisrc_embedded_sgl_count(uint32_t elem_alloted)
49 {
50 	uint32_t embedded_sgl_count = MAX_EMBEDDED_SG_IN_FIRST_IU;
51 	DBG_FUNC(" IN ");
52 	/**
53 	calculate embedded sgl count using num_elem_alloted for IO
54 	**/
55 	if(elem_alloted - 1)
56 		embedded_sgl_count += ((elem_alloted - 1) * MAX_EMBEDDED_SG_IN_IU);
57 	DBG_IO("embedded_sgl_count :%d\n",embedded_sgl_count);
58 
59 	DBG_FUNC(" OUT ");
60 
61 	return embedded_sgl_count;
62 
63 }
64 
65 /* Subroutine to find out contiguous free elem in IU */
66 static inline uint32_t
67 pqisrc_contiguous_free_elem(uint32_t pi, uint32_t ci, uint32_t elem_in_q)
68 {
69 	uint32_t contiguous_free_elem = 0;
70 
71 	DBG_FUNC(" IN ");
72 
73 	if(pi >= ci) {
74 		contiguous_free_elem = (elem_in_q - pi);
75 		if(ci == 0)
76 			contiguous_free_elem -= 1;
77 	} else {
78 		contiguous_free_elem = (ci - pi - 1);
79 	}
80 
81 	DBG_FUNC(" OUT ");
82 
83 	return contiguous_free_elem;
84 }
85 
86 /* Subroutine to find out num of elements need for the request */
87 static uint32_t
88 pqisrc_num_elem_needed(pqisrc_softstate_t *softs, uint32_t SG_Count)
89 {
90 	uint32_t num_sg;
91 	uint32_t num_elem_required = 1;
92 	DBG_FUNC(" IN ");
93 	DBG_IO("SGL_Count :%d",SG_Count);
94 	/********
95 	If SG_Count greater than max sg per IU i.e 4 or 68
96 	(4 is with out spanning or 68 is with spanning) chaining is required.
97 	OR, If SG_Count <= MAX_EMBEDDED_SG_IN_FIRST_IU then,
98 	on these two cases one element is enough.
99 	********/
100 	if(SG_Count > softs->max_sg_per_iu || SG_Count <= MAX_EMBEDDED_SG_IN_FIRST_IU)
101 		return num_elem_required;
102 	/*
103 	SGL Count Other Than First IU
104 	 */
105 	num_sg = SG_Count - MAX_EMBEDDED_SG_IN_FIRST_IU;
106 	num_elem_required += PQISRC_DIV_ROUND_UP(num_sg, MAX_EMBEDDED_SG_IN_IU);
107 	DBG_FUNC(" OUT ");
108 	return num_elem_required;
109 }
110 
111 /* Subroutine to build SG list for the IU submission*/
112 static boolean_t
113 pqisrc_build_sgl(sgt_t *sg_array, rcb_t *rcb, iu_header_t *iu_hdr,
114 			uint32_t num_elem_alloted)
115 {
116 	uint32_t i;
117 	uint32_t num_sg = OS_GET_IO_SG_COUNT(rcb);
118 	sgt_t *sgt = sg_array;
119 	sgt_t *sg_chain = NULL;
120 	boolean_t partial = false;
121 
122 	DBG_FUNC(" IN ");
123 
124 	DBG_IO("SGL_Count :%d",num_sg);
125 	if (0 == num_sg) {
126 		goto out;
127 	}
128 
129 	if (num_sg <= pqisrc_embedded_sgl_count(num_elem_alloted)) {
130 		for (i = 0; i < num_sg; i++, sgt++) {
131                         sgt->addr= OS_GET_IO_SG_ADDR(rcb,i);
132                         sgt->len= OS_GET_IO_SG_LEN(rcb,i);
133                         sgt->flags= 0;
134                 }
135 
136 		sg_array[num_sg - 1].flags = SG_FLAG_LAST;
137 	} else {
138 	/**
139 	SGL Chaining
140 	**/
141 		sg_chain = rcb->sg_chain_virt;
142 		sgt->addr = rcb->sg_chain_dma;
143 		sgt->len = num_sg * sizeof(sgt_t);
144 		sgt->flags = SG_FLAG_CHAIN;
145 
146 		sgt = sg_chain;
147 		for (i = 0; i < num_sg; i++, sgt++) {
148 			sgt->addr = OS_GET_IO_SG_ADDR(rcb,i);
149 			sgt->len = OS_GET_IO_SG_LEN(rcb,i);
150 			sgt->flags = 0;
151 		}
152 
153 		sg_chain[num_sg - 1].flags = SG_FLAG_LAST;
154 		num_sg = 1;
155 		partial = true;
156 
157 	}
158 out:
159 	iu_hdr->iu_length = num_sg * sizeof(sgt_t);
160 	DBG_FUNC(" OUT ");
161 	return partial;
162 
163 }
164 
165 /*Subroutine used to Build the RAID request */
166 static void
167 pqisrc_build_raid_io(pqisrc_softstate_t *softs, rcb_t *rcb,
168  	pqisrc_raid_req_t *raid_req, uint32_t num_elem_alloted)
169 {
170 	DBG_FUNC(" IN ");
171 
172 	raid_req->header.iu_type = PQI_IU_TYPE_RAID_PATH_IO_REQUEST;
173 	raid_req->header.comp_feature = 0;
174 	raid_req->response_queue_id = OS_GET_IO_RESP_QID(softs, rcb);
175 	raid_req->work_area[0] = 0;
176 	raid_req->work_area[1] = 0;
177 	raid_req->request_id = rcb->tag;
178 	raid_req->nexus_id = 0;
179 	raid_req->buffer_length = GET_SCSI_BUFFLEN(rcb);
180 	memcpy(raid_req->lun_number, rcb->dvp->scsi3addr,
181 		sizeof(raid_req->lun_number));
182 	raid_req->protocol_spec = 0;
183 	raid_req->data_direction = rcb->data_dir;
184 	raid_req->reserved1 = 0;
185 	raid_req->fence = 0;
186 	raid_req->error_index = raid_req->request_id;
187 	raid_req->reserved2 = 0;
188   	raid_req->task_attribute = OS_GET_TASK_ATTR(rcb);
189   	raid_req->command_priority = 0;
190 	raid_req->reserved3 = 0;
191 	raid_req->reserved4 = 0;
192 	raid_req->reserved5 = 0;
193 
194 	/* As cdb and additional_cdb_bytes are contiguous,
195 	   update them in a single statement */
196 	memcpy(raid_req->cdb, rcb->cdbp, rcb->cmdlen);
197 #if 0
198 	DBG_IO("CDB :");
199 	for(i = 0; i < rcb->cmdlen ; i++)
200 		DBG_IO(" 0x%x \n ",raid_req->cdb[i]);
201 #endif
202 
203 	switch (rcb->cmdlen) {
204 		case 6:
205 		case 10:
206 		case 12:
207 		case 16:
208 			raid_req->additional_cdb_bytes_usage =
209 				PQI_ADDITIONAL_CDB_BYTES_0;
210 			break;
211 		case 20:
212 			raid_req->additional_cdb_bytes_usage =
213 				PQI_ADDITIONAL_CDB_BYTES_4;
214 			break;
215 		case 24:
216 			raid_req->additional_cdb_bytes_usage =
217 				PQI_ADDITIONAL_CDB_BYTES_8;
218 			break;
219 		case 28:
220 			raid_req->additional_cdb_bytes_usage =
221 				PQI_ADDITIONAL_CDB_BYTES_12;
222 			break;
223 		case 32:
224 		default: /* todo:review again */
225 			raid_req->additional_cdb_bytes_usage =
226 				PQI_ADDITIONAL_CDB_BYTES_16;
227 			break;
228 	}
229 
230 	/* Frame SGL Descriptor */
231 	raid_req->partial = pqisrc_build_sgl(&raid_req->sg_descriptors[0], rcb,
232 		&raid_req->header, num_elem_alloted);
233 
234 	raid_req->header.iu_length +=
235 			offsetof(pqisrc_raid_req_t, sg_descriptors) - sizeof(iu_header_t);
236 
237 #if 0
238 	DBG_IO("raid_req->header.iu_type : 0x%x", raid_req->header.iu_type);
239 	DBG_IO("raid_req->response_queue_id :%d\n"raid_req->response_queue_id);
240 	DBG_IO("raid_req->request_id : 0x%x", raid_req->request_id);
241 	DBG_IO("raid_req->buffer_length : 0x%x", raid_req->buffer_length);
242 	DBG_IO("raid_req->task_attribute : 0x%x", raid_req->task_attribute);
243 	DBG_IO("raid_req->lun_number  : 0x%x", raid_req->lun_number);
244 	DBG_IO("raid_req->error_index : 0x%x", raid_req->error_index);
245 	DBG_IO("raid_req->sg_descriptors[0].addr : %p", (void*)raid_req->sg_descriptors[0].addr);
246 	DBG_IO("raid_req->sg_descriptors[0].len : 0x%x", raid_req->sg_descriptors[0].len);
247 	DBG_IO("raid_req->sg_descriptors[0].flags : 0%x", raid_req->sg_descriptors[0].flags);
248 #endif
249 	rcb->success_cmp_callback = pqisrc_process_io_response_success;
250 	rcb->error_cmp_callback = pqisrc_process_raid_response_error;
251 	rcb->resp_qid = raid_req->response_queue_id;
252 
253  	DBG_FUNC(" OUT ");
254 
255 }
256 
257 /*Subroutine used to Build the AIO request */
258 static void
259 pqisrc_build_aio_io(pqisrc_softstate_t *softs, rcb_t *rcb,
260  				pqi_aio_req_t *aio_req, uint32_t num_elem_alloted)
261 {
262 	DBG_FUNC(" IN ");
263 
264 	aio_req->header.iu_type = PQI_IU_TYPE_AIO_PATH_IO_REQUEST;
265 	aio_req->header.comp_feature = 0;
266 	aio_req->response_queue_id = OS_GET_IO_RESP_QID(softs, rcb);
267 	aio_req->work_area[0] = 0;
268 	aio_req->work_area[1] = 0;
269 	aio_req->req_id = rcb->tag;
270 	aio_req->res1[0] = 0;
271 	aio_req->res1[1] = 0;
272 	aio_req->nexus = rcb->ioaccel_handle;
273 	aio_req->buf_len = GET_SCSI_BUFFLEN(rcb);
274 	aio_req->data_dir = rcb->data_dir;
275 	aio_req->mem_type = 0;
276 	aio_req->fence = 0;
277 	aio_req->res2 = 0;
278 	aio_req->task_attr = OS_GET_TASK_ATTR(rcb);
279 	aio_req->cmd_prio = 0;
280 	aio_req->res3 = 0;
281 	aio_req->err_idx = aio_req->req_id;
282 	aio_req->cdb_len = rcb->cmdlen;
283 
284 	if(rcb->cmdlen > sizeof(aio_req->cdb))
285 		rcb->cmdlen = sizeof(aio_req->cdb);
286 	memcpy(aio_req->cdb, rcb->cdbp, rcb->cmdlen);
287 #if 0
288 	DBG_IO("CDB : \n");
289 	for(int i = 0; i < rcb->cmdlen ; i++)
290 		 DBG_IO(" 0x%x \n",aio_req->cdb[i]);
291 #endif
292 	memset(aio_req->lun,0,sizeof(aio_req->lun));
293 	memset(aio_req->res4,0,sizeof(aio_req->res4));
294 
295 	if(rcb->encrypt_enable == true) {
296 		aio_req->encrypt_enable = true;
297 		aio_req->encrypt_key_index = LE_16(rcb->enc_info.data_enc_key_index);
298 		aio_req->encrypt_twk_low = LE_32(rcb->enc_info.encrypt_tweak_lower);
299 		aio_req->encrypt_twk_high = LE_32(rcb->enc_info.encrypt_tweak_upper);
300 	} else {
301 		aio_req->encrypt_enable = 0;
302 		aio_req->encrypt_key_index = 0;
303 		aio_req->encrypt_twk_high = 0;
304 		aio_req->encrypt_twk_low = 0;
305 	}
306 
307 	/* Frame SGL Descriptor */
308 	aio_req->partial = pqisrc_build_sgl(&aio_req->sg_desc[0], rcb,
309  		&aio_req->header, num_elem_alloted);
310 
311 	aio_req->num_sg = aio_req->header.iu_length / sizeof(sgt_t);
312 
313 	DBG_INFO("aio_req->num_sg :%d",aio_req->num_sg);
314 
315 	aio_req->header.iu_length += offsetof(pqi_aio_req_t, sg_desc) -
316 		sizeof(iu_header_t);
317 #if 0
318 	DBG_IO("aio_req->header.iu_type : 0x%x \n",aio_req->header.iu_type);
319 	DBG_IO("aio_req->resp_qid :0x%x",aio_req->resp_qid);
320 	DBG_IO("aio_req->req_id : 0x%x \n",aio_req->req_id);
321 	DBG_IO("aio_req->nexus : 0x%x  \n",aio_req->nexus);
322 	DBG_IO("aio_req->buf_len : 0x%x \n",aio_req->buf_len);
323 	DBG_IO("aio_req->data_dir : 0x%x \n",aio_req->data_dir);
324 	DBG_IO("aio_req->task_attr : 0x%x \n",aio_req->task_attr);
325 	DBG_IO("aio_req->err_idx : 0x%x \n",aio_req->err_idx);
326 	DBG_IO("aio_req->num_sg :%d",aio_req->num_sg);
327 	DBG_IO("aio_req->sg_desc[0].addr : %p \n", (void*)aio_req->sg_desc[0].addr);
328 	DBG_IO("aio_req->sg_desc[0].len : 0%x \n", aio_req->sg_desc[0].len);
329 	DBG_IO("aio_req->sg_desc[0].flags : 0%x \n", aio_req->sg_desc[0].flags);
330 #endif
331 
332 	rcb->success_cmp_callback = pqisrc_process_io_response_success;
333 	rcb->error_cmp_callback = pqisrc_process_aio_response_error;
334 	rcb->resp_qid = aio_req->response_queue_id;
335 
336 	DBG_FUNC(" OUT ");
337 
338 }
339 
340 /*Function used to build and send RAID/AIO */
341 int
342 pqisrc_build_send_io(pqisrc_softstate_t *softs,rcb_t *rcb)
343 {
344 	ib_queue_t *ib_q_array = softs->op_aio_ib_q;
345 	ib_queue_t *ib_q = NULL;
346 	char *ib_iu = NULL;
347 	IO_PATH_T io_path = AIO_PATH;
348 	uint32_t TraverseCount = 0;
349 	int first_qindex = OS_GET_IO_REQ_QINDEX(softs, rcb);
350 	int qindex = first_qindex;
351 	uint32_t num_op_ib_q = softs->num_op_aio_ibq;
352 	uint32_t num_elem_needed;
353 	uint32_t num_elem_alloted = 0;
354 	pqi_scsi_dev_t *devp = rcb->dvp;
355 	uint8_t raidbypass_cdb[16];
356 
357 	DBG_FUNC(" IN ");
358 
359 	if(!rcb->aio_retry) {
360 		rcb->cdbp = OS_GET_CDBP(rcb);
361 		if(IS_AIO_PATH(devp)) {
362 			/**  IO for Physical Drive  **/
363 			/** Send in AIO PATH**/
364 			rcb->ioaccel_handle = devp->ioaccel_handle;
365 		} else {
366 			int ret = PQI_STATUS_FAILURE;
367 			/** IO for RAID Volume **/
368 			if (devp->offload_enabled) {
369 				/** ByPass IO ,Send in AIO PATH **/
370 				ret = pqisrc_send_scsi_cmd_raidbypass(softs,
371 					devp, rcb, raidbypass_cdb);
372 			}
373 			if (PQI_STATUS_FAILURE == ret) {
374 				/** Send in RAID PATH **/
375 				io_path = RAID_PATH;
376 				num_op_ib_q = softs->num_op_raid_ibq;
377 				ib_q_array = softs->op_raid_ib_q;
378 			} else {
379 				rcb->cdbp = raidbypass_cdb;
380 			}
381 		}
382 	} else {
383 		/* Retrying failed AIO IO */
384 		io_path = RAID_PATH;
385 		rcb->cdbp = OS_GET_CDBP(rcb);
386 		num_op_ib_q = softs->num_op_raid_ibq;
387 		ib_q_array = softs->op_raid_ib_q;
388 	}
389 
390 	num_elem_needed = pqisrc_num_elem_needed(softs, OS_GET_IO_SG_COUNT(rcb));
391 	DBG_IO("num_elem_needed :%d",num_elem_needed);
392 
393 	do {
394 		uint32_t num_elem_available;
395 		ib_q = (ib_q_array + qindex);
396 		PQI_LOCK(&ib_q->lock);
397 		num_elem_available = pqisrc_contiguous_free_elem(ib_q->pi_local,
398 					*(ib_q->ci_virt_addr), ib_q->num_elem);
399 
400 		DBG_IO("num_elem_avialable :%d\n",num_elem_available);
401 		if(num_elem_available >= num_elem_needed) {
402 			num_elem_alloted = num_elem_needed;
403 			break;
404 		}
405 		DBG_IO("Current queue is busy! Hop to next queue\n");
406 
407 		PQI_UNLOCK(&ib_q->lock);
408 		qindex = (qindex + 1) % num_op_ib_q;
409 		if(qindex == first_qindex) {
410 			if (num_elem_needed == 1)
411 				break;
412 			TraverseCount += 1;
413 			num_elem_needed = 1;
414 		}
415 	}while(TraverseCount < 2);
416 
417 	DBG_IO("num_elem_alloted :%d",num_elem_alloted);
418 	if (num_elem_alloted == 0) {
419 		DBG_WARN("OUT: IB Queues were full\n");
420 		return PQI_STATUS_QFULL;
421 	}
422 
423 	pqisrc_increment_device_active_io(softs,devp);
424 
425 	/* Get IB Queue Slot address to build IU */
426 	ib_iu = ib_q->array_virt_addr + (ib_q->pi_local * ib_q->elem_size);
427 
428 	if(io_path == AIO_PATH) {
429 		/** Build AIO structure **/
430  		pqisrc_build_aio_io(softs, rcb, (pqi_aio_req_t*)ib_iu,
431  			num_elem_alloted);
432 	} else {
433 		/** Build RAID structure **/
434 		pqisrc_build_raid_io(softs, rcb, (pqisrc_raid_req_t*)ib_iu,
435 			num_elem_alloted);
436 	}
437 
438 	rcb->req_pending = true;
439 	rcb->req_q = ib_q;
440 	rcb->path = io_path;
441 
442 	/* Update the local PI */
443 	ib_q->pi_local = (ib_q->pi_local + num_elem_alloted) % ib_q->num_elem;
444 
445 	DBG_INFO("ib_q->pi_local : %x\n", ib_q->pi_local);
446 	DBG_INFO("*ib_q->ci_virt_addr: %x\n",*(ib_q->ci_virt_addr));
447 
448 	/* Inform the fw about the new IU */
449 	PCI_MEM_PUT32(softs, ib_q->pi_register_abs, ib_q->pi_register_offset, ib_q->pi_local);
450 
451 	PQI_UNLOCK(&ib_q->lock);
452 	DBG_FUNC(" OUT ");
453 	return PQI_STATUS_SUCCESS;
454 }
455 
456 /* Subroutine used to set encryption info as part of RAID bypass IO*/
457 static inline void
458 pqisrc_set_enc_info(struct pqi_enc_info *enc_info,
459 		struct raid_map *raid_map, uint64_t first_block)
460 {
461 	uint32_t volume_blk_size;
462 
463 	/*
464 	 * Set the encryption tweak values based on logical block address.
465 	 * If the block size is 512, the tweak value is equal to the LBA.
466 	 * For other block sizes, tweak value is (LBA * block size) / 512.
467 	 */
468 	volume_blk_size = GET_LE32((uint8_t *)&raid_map->volume_blk_size);
469 	if (volume_blk_size != 512)
470 		first_block = (first_block * volume_blk_size) / 512;
471 
472 	enc_info->data_enc_key_index =
473 		GET_LE16((uint8_t *)&raid_map->data_encryption_key_index);
474 	enc_info->encrypt_tweak_upper = ((uint32_t)(((first_block) >> 16) >> 16));
475 	enc_info->encrypt_tweak_lower = ((uint32_t)(first_block));
476 }
477 
478 /* Subroutine used to parse the scsi opcode and build the CDB for RAID bypass*/
479 int
480 check_for_scsi_opcode(uint8_t *cdb, boolean_t *is_write, uint64_t *fst_blk,
481 				uint32_t *blk_cnt)
482 {
483 
484 	switch (cdb[0]) {
485 	case SCMD_WRITE_6:
486 		*is_write = true;
487 	case SCMD_READ_6:
488 		*fst_blk = (uint64_t)(((cdb[1] & 0x1F) << 16) |
489 				(cdb[2] << 8) | cdb[3]);
490 		*blk_cnt = (uint32_t)cdb[4];
491 		if (*blk_cnt == 0)
492 			*blk_cnt = 256;
493 		break;
494 	case SCMD_WRITE_10:
495 		*is_write = true;
496 	case SCMD_READ_10:
497 		*fst_blk = (uint64_t)GET_BE32(&cdb[2]);
498 		*blk_cnt = (uint32_t)GET_BE16(&cdb[7]);
499 		break;
500 	case SCMD_WRITE_12:
501 		*is_write = true;
502 	case SCMD_READ_12:
503 		*fst_blk = (uint64_t)GET_BE32(&cdb[2]);
504 		*blk_cnt = GET_BE32(&cdb[6]);
505 		break;
506 	case SCMD_WRITE_16:
507 		*is_write = true;
508 	case SCMD_READ_16:
509 		*fst_blk = GET_BE64(&cdb[2]);
510 		*blk_cnt = GET_BE32(&cdb[10]);
511 		break;
512 	default:
513 		/* Process via normal I/O path. */
514 		return PQI_STATUS_FAILURE;
515 	}
516 	return PQI_STATUS_SUCCESS;
517 }
518 
519 /* print any arbitrary buffer of length total_len */
520 void
521 pqisrc_print_buffer(pqisrc_softstate_t *softs, char *msg, void *user_buf,
522 		uint32_t total_len, uint32_t flags)
523 {
524 #define LINE_BUF_LEN 60
525 #define INDEX_PER_LINE 16
526 	uint32_t buf_consumed = 0;
527 	int ii;
528 	char line_buf[LINE_BUF_LEN];
529 	int line_len; /* written length per line */
530 	uint8_t this_char;
531 
532 	if (user_buf == NULL)
533 		return;
534 
535 	/* Print index columns */
536 	if (flags & PRINT_FLAG_HDR_COLUMN)
537 	{
538 		for (ii = 0, line_len = 0; ii < MIN(total_len, 16); ii++)
539 		{
540 			line_len += snprintf(line_buf + line_len, (LINE_BUF_LEN - line_len), "%02d ", ii);
541 			if ((line_len + 4) >= LINE_BUF_LEN)
542 			break;
543 		}
544 		DBG_NOTE("%15.15s:[ %s ]\n", "header", line_buf);
545 	}
546 
547 	/* Print index columns */
548 	while(buf_consumed < total_len)
549 	{
550 		memset(line_buf, 0, LINE_BUF_LEN);
551 
552 		for (ii = 0, line_len = 0; ii < INDEX_PER_LINE; ii++)
553 		{
554 			this_char = *((char*)(user_buf) + buf_consumed);
555 			line_len += snprintf(line_buf + line_len, (LINE_BUF_LEN - line_len), "%02x ", this_char);
556 
557 			buf_consumed++;
558 			if (buf_consumed >= total_len || (line_len + 4) >= LINE_BUF_LEN)
559 			break;
560 		}
561 		DBG_NOTE("%15.15s:[ %s ]\n", msg, line_buf);
562 	}
563 }
564 
565 
566 /*
567  * Function used to build and send RAID bypass request to the adapter
568  */
569 int
570 pqisrc_send_scsi_cmd_raidbypass(pqisrc_softstate_t *softs,
571 				pqi_scsi_dev_t *device, rcb_t *rcb, uint8_t *cdb)
572 {
573 	struct raid_map *raid_map;
574 	boolean_t is_write = false;
575 	uint32_t map_idx;
576 	uint64_t fst_blk, lst_blk;
577 	uint32_t blk_cnt, blks_per_row;
578 	uint64_t fst_row, lst_row;
579 	uint32_t fst_row_offset, lst_row_offset;
580 	uint32_t fst_col, lst_col;
581 	uint32_t r5or6_blks_per_row;
582 	uint64_t r5or6_fst_row, r5or6_lst_row;
583 	uint32_t r5or6_fst_row_offset, r5or6_lst_row_offset;
584 	uint32_t r5or6_fst_col, r5or6_lst_col;
585 	uint16_t data_disks_per_row, total_disks_per_row;
586 	uint16_t layout_map_count;
587 	uint32_t stripesz;
588 	uint16_t strip_sz;
589 	uint32_t fst_grp, lst_grp, cur_grp;
590 	uint32_t map_row;
591 	uint64_t disk_block;
592 	uint32_t disk_blk_cnt;
593 	uint8_t cdb_length;
594 	int offload_to_mirror;
595 	int i;
596 	DBG_FUNC(" IN \n");
597 	DBG_IO("!!!!!\n");
598 
599 	/* Check for eligible opcode, get LBA and block count. */
600 	memcpy(cdb, OS_GET_CDBP(rcb), rcb->cmdlen);
601 
602 	for(i = 0; i < rcb->cmdlen ; i++)
603 		DBG_IO(" CDB [ %d ] : %x\n",i,cdb[i]);
604 	if(check_for_scsi_opcode(cdb, &is_write,
605 		&fst_blk, &blk_cnt) == PQI_STATUS_FAILURE)
606 			return PQI_STATUS_FAILURE;
607 	/* Check for write to non-RAID-0. */
608 	if (is_write && device->raid_level != SA_RAID_0)
609 		return PQI_STATUS_FAILURE;
610 
611 	if(blk_cnt == 0)
612 		return PQI_STATUS_FAILURE;
613 
614 	lst_blk = fst_blk + blk_cnt - 1;
615 	raid_map = device->raid_map;
616 
617 	/* Check for invalid block or wraparound. */
618 	if (lst_blk >= GET_LE64((uint8_t *)&raid_map->volume_blk_cnt) ||
619 		lst_blk < fst_blk)
620 		return PQI_STATUS_FAILURE;
621 
622 	data_disks_per_row = GET_LE16((uint8_t *)&raid_map->data_disks_per_row);
623 	strip_sz = GET_LE16((uint8_t *)(&raid_map->strip_size));
624 	layout_map_count = GET_LE16((uint8_t *)(&raid_map->layout_map_count));
625 
626 	/* Calculate stripe information for the request. */
627 	blks_per_row = data_disks_per_row * strip_sz;
628 	if (!blks_per_row)
629 		return PQI_STATUS_FAILURE;  /*Send the IO in raid path itself, not AIO or raidbypass*/
630 
631 	/* use __udivdi3 ? */
632 	fst_row = fst_blk / blks_per_row;
633 	lst_row = lst_blk / blks_per_row;
634 	fst_row_offset = (uint32_t)(fst_blk - (fst_row * blks_per_row));
635 	lst_row_offset = (uint32_t)(lst_blk - (lst_row * blks_per_row));
636 	fst_col = fst_row_offset / strip_sz;
637 	lst_col = lst_row_offset / strip_sz;
638 
639 	/* If this isn't a single row/column then give to the controller. */
640 	if (fst_row != lst_row || fst_col != lst_col)
641 		return PQI_STATUS_FAILURE;
642 
643 	/* Proceeding with driver mapping. */
644 	total_disks_per_row = data_disks_per_row +
645 		GET_LE16((uint8_t *)(&raid_map->metadata_disks_per_row));
646 	map_row = ((uint32_t)(fst_row >> raid_map->parity_rotation_shift)) %
647 		GET_LE16((uint8_t *)(&raid_map->row_cnt));
648 	map_idx = (map_row * total_disks_per_row) + fst_col;
649 
650 	/* RAID 1 */
651 	if (device->raid_level == SA_RAID_1) {
652 		if (device->offload_to_mirror)
653 			map_idx += data_disks_per_row;
654 		device->offload_to_mirror = !device->offload_to_mirror;
655 	} else if (device->raid_level == SA_RAID_ADM) {
656 		/* RAID ADM */
657 		/*
658 		 * Handles N-way mirrors  (R1-ADM) and R10 with # of drives
659 		 * divisible by 3.
660 		 */
661 		offload_to_mirror = device->offload_to_mirror;
662 		if (offload_to_mirror == 0)  {
663 			/* use physical disk in the first mirrored group. */
664 			map_idx %= data_disks_per_row;
665 		} else {
666 			do {
667 				/*
668 				 * Determine mirror group that map_idx
669 				 * indicates.
670 				 */
671 				cur_grp = map_idx / data_disks_per_row;
672 
673 				if (offload_to_mirror != cur_grp) {
674 					if (cur_grp <
675 						layout_map_count - 1) {
676 						/*
677 						 * Select raid index from
678 						 * next group.
679 						 */
680 						map_idx += data_disks_per_row;
681 						cur_grp++;
682 					} else {
683 						/*
684 						 * Select raid index from first
685 						 * group.
686 						 */
687 						map_idx %= data_disks_per_row;
688 						cur_grp = 0;
689 					}
690 				}
691 			} while (offload_to_mirror != cur_grp);
692 		}
693 
694 		/* Set mirror group to use next time. */
695 		offload_to_mirror =
696 			(offload_to_mirror >= layout_map_count - 1) ?
697 				0 : offload_to_mirror + 1;
698 		if(offload_to_mirror >= layout_map_count)
699 			return PQI_STATUS_FAILURE;
700 
701 		device->offload_to_mirror = offload_to_mirror;
702 		/*
703 		 * Avoid direct use of device->offload_to_mirror within this
704 		 * function since multiple threads might simultaneously
705 		 * increment it beyond the range of device->layout_map_count -1.
706 		 */
707 	} else if ((device->raid_level == SA_RAID_5 ||
708 		device->raid_level == SA_RAID_6) && layout_map_count > 1) {
709 		/* RAID 50/60 */
710 		/* Verify first and last block are in same RAID group */
711 		r5or6_blks_per_row = strip_sz * data_disks_per_row;
712 		stripesz = r5or6_blks_per_row * layout_map_count;
713 
714 		fst_grp = (fst_blk % stripesz) / r5or6_blks_per_row;
715 		lst_grp = (lst_blk % stripesz) / r5or6_blks_per_row;
716 
717 		if (fst_grp != lst_grp)
718 			return PQI_STATUS_FAILURE;
719 
720 		/* Verify request is in a single row of RAID 5/6 */
721 		fst_row = r5or6_fst_row =
722 			fst_blk / stripesz;
723 		r5or6_lst_row = lst_blk / stripesz;
724 
725 		if (r5or6_fst_row != r5or6_lst_row)
726 			return PQI_STATUS_FAILURE;
727 
728 		/* Verify request is in a single column */
729 		fst_row_offset = r5or6_fst_row_offset =
730 			(uint32_t)((fst_blk % stripesz) %
731 			r5or6_blks_per_row);
732 
733 		r5or6_lst_row_offset =
734 			(uint32_t)((lst_blk % stripesz) %
735 			r5or6_blks_per_row);
736 
737 		fst_col = r5or6_fst_row_offset / strip_sz;
738 		r5or6_fst_col = fst_col;
739 		r5or6_lst_col = r5or6_lst_row_offset / strip_sz;
740 
741 		if (r5or6_fst_col != r5or6_lst_col)
742 			return PQI_STATUS_FAILURE;
743 
744 		/* Request is eligible */
745 		map_row =
746 			((uint32_t)(fst_row >> raid_map->parity_rotation_shift)) %
747 			GET_LE16((uint8_t *)(&raid_map->row_cnt));
748 
749 		map_idx = (fst_grp *
750 			(GET_LE16((uint8_t *)(&raid_map->row_cnt)) *
751 			total_disks_per_row)) +
752 			(map_row * total_disks_per_row) + fst_col;
753 	}
754 
755 	rcb->ioaccel_handle = raid_map->dev_data[map_idx].ioaccel_handle;
756 	disk_block = GET_LE64((uint8_t *)(&raid_map->disk_starting_blk)) +
757 		fst_row * strip_sz +
758 		(fst_row_offset - fst_col * strip_sz);
759 	disk_blk_cnt = blk_cnt;
760 
761 	/* Handle differing logical/physical block sizes. */
762 	if (raid_map->phys_blk_shift) {
763 		disk_block <<= raid_map->phys_blk_shift;
764 		disk_blk_cnt <<= raid_map->phys_blk_shift;
765 	}
766 
767 	if (disk_blk_cnt > 0xffff)
768 		return PQI_STATUS_FAILURE;
769 
770 	/* Build the new CDB for the physical disk I/O. */
771 	if (disk_block > 0xffffffff) {
772 		cdb[0] = is_write ? SCMD_WRITE_16 : SCMD_READ_16;
773 		cdb[1] = 0;
774 		PUT_BE64(disk_block, &cdb[2]);
775 		PUT_BE32(disk_blk_cnt, &cdb[10]);
776 		cdb[14] = 0;
777 		cdb[15] = 0;
778 		cdb_length = 16;
779 	} else {
780 		cdb[0] = is_write ? SCMD_WRITE_10 : SCMD_READ_10;
781 		cdb[1] = 0;
782 		PUT_BE32(disk_block, &cdb[2]);
783 		cdb[6] = 0;
784 		PUT_BE16(disk_blk_cnt, &cdb[7]);
785 		cdb[9] = 0;
786 		cdb_length = 10;
787 	}
788 
789 	if (GET_LE16((uint8_t *)(&raid_map->flags)) &
790 		RAID_MAP_ENCRYPTION_ENABLED) {
791 		pqisrc_set_enc_info(&rcb->enc_info, raid_map,
792 			fst_blk);
793 		rcb->encrypt_enable = true;
794 	} else {
795 		rcb->encrypt_enable = false;
796 	}
797 
798 	rcb->cmdlen = cdb_length;
799 
800 
801 	DBG_FUNC("OUT");
802 
803 	return PQI_STATUS_SUCCESS;
804 }
805 
806 /* Function used to submit an AIO TMF to the adapter
807  * DEVICE_RESET is not supported.
808  */
809 static int
810 pqisrc_send_aio_tmf(pqisrc_softstate_t *softs, pqi_scsi_dev_t *devp,
811                     rcb_t *rcb, rcb_t *rcb_to_manage, int tmf_type)
812 {
813 	int rval = PQI_STATUS_SUCCESS;
814 	pqi_aio_tmf_req_t tmf_req;
815 	ib_queue_t *op_ib_q = NULL;
816 
817 	memset(&tmf_req, 0, sizeof(pqi_aio_tmf_req_t));
818 
819 	DBG_FUNC("IN");
820 
821 	tmf_req.header.iu_type = PQI_REQUEST_IU_AIO_TASK_MANAGEMENT;
822 	tmf_req.header.iu_length = sizeof(tmf_req) - sizeof(iu_header_t);
823 	tmf_req.req_id = rcb->tag;
824 	tmf_req.error_idx = rcb->tag;
825 	tmf_req.nexus = devp->ioaccel_handle;
826 	//memcpy(tmf_req.lun, devp->scsi3addr, sizeof(tmf_req.lun));
827 	tmf_req.tmf = tmf_type;
828 	tmf_req.resp_qid = OS_GET_TMF_RESP_QID(softs, rcb);
829 	op_ib_q = &softs->op_aio_ib_q[0];
830 
831 	if (tmf_type == SOP_TASK_MANAGEMENT_FUNCTION_ABORT_TASK) {
832 		tmf_req.req_id_to_manage = rcb_to_manage->tag;
833 		tmf_req.nexus = rcb_to_manage->ioaccel_handle;
834 	}
835 
836 	DBG_INFO("tmf_req.header.iu_type : %x tmf_req.req_id_to_manage :%d \n",tmf_req.header.iu_type,tmf_req.req_id_to_manage);
837 	DBG_INFO("tmf_req.req_id : %d tmf_req.nexus : %x tmf_req.tmf %x QID : %d\n",tmf_req.req_id,tmf_req.nexus,tmf_req.tmf,op_ib_q->q_id);
838 
839 	DBG_WARN("aio tmf: iu_type=0x%x req_id_to_manage=0x%x\n",
840 		tmf_req.header.iu_type, tmf_req.req_id_to_manage);
841 	DBG_WARN("aio tmf: req_id=0x%x nexus=0x%x tmf=0x%x QID=%d\n",
842 		tmf_req.req_id, tmf_req.nexus, tmf_req.tmf, op_ib_q->q_id);
843 
844 	rcb->path = AIO_PATH;
845 	rcb->req_pending = true;
846 	/* Timedout tmf response goes here */
847 	rcb->error_cmp_callback = pqisrc_process_aio_response_error;
848 
849 	rval = pqisrc_submit_cmnd(softs, op_ib_q, &tmf_req);
850 	if (rval != PQI_STATUS_SUCCESS) {
851 		DBG_ERR("Unable to submit command rval=%d\n", rval);
852 		return rval;
853 	}
854 
855 	rval = pqisrc_wait_on_condition(softs, rcb, PQISRC_TMF_TIMEOUT);
856 	if (rval != PQI_STATUS_SUCCESS){
857 		DBG_ERR("Task Management tmf_type : %d timeout\n", tmf_type);
858 		rcb->status = rval;
859 	}
860 
861 	if (rcb->status  != REQUEST_SUCCESS) {
862 		DBG_ERR_BTL(devp, "Task Management failed tmf_type:%d "
863 				"stat:0x%x\n", tmf_type, rcb->status);
864 		rval = PQI_STATUS_FAILURE;
865 	}
866 
867 	DBG_FUNC("OUT");
868 	return rval;
869 }
870 
871 /* Function used to submit a Raid TMF to the adapter */
872 static int
873 pqisrc_send_raid_tmf(pqisrc_softstate_t *softs, pqi_scsi_dev_t *devp,
874                     rcb_t *rcb, rcb_t *rcb_to_manage, int tmf_type)
875 {
876 	int rval = PQI_STATUS_SUCCESS;
877 	pqi_raid_tmf_req_t tmf_req;
878 	ib_queue_t *op_ib_q = NULL;
879 
880 	memset(&tmf_req, 0, sizeof(pqi_raid_tmf_req_t));
881 
882 	DBG_FUNC("IN");
883 
884 	tmf_req.header.iu_type = PQI_REQUEST_IU_RAID_TASK_MANAGEMENT;
885 	tmf_req.header.iu_length = sizeof(tmf_req) - sizeof(iu_header_t);
886 	tmf_req.req_id = rcb->tag;
887 
888 	memcpy(tmf_req.lun, devp->scsi3addr, sizeof(tmf_req.lun));
889 	tmf_req.tmf = tmf_type;
890 	tmf_req.resp_qid = OS_GET_TMF_RESP_QID(softs, rcb);
891 
892 	/* Decide the queue where the tmf request should be submitted */
893 	if (tmf_type == SOP_TASK_MANAGEMENT_FUNCTION_ABORT_TASK) {
894 		tmf_req.obq_id_to_manage = rcb_to_manage->resp_qid;
895 		tmf_req.req_id_to_manage = rcb_to_manage->tag;
896 	}
897 
898 	if (softs->timeout_in_tmf &&
899 			tmf_type == SOP_TASK_MANAGEMENT_LUN_RESET) {
900 		/* OS_TMF_TIMEOUT_SEC - 1 to accommodate driver processing */
901 		tmf_req.timeout_in_sec = OS_TMF_TIMEOUT_SEC - 1;
902 		/* if OS tmf timeout is 0, set minimum value for timeout */
903 		if (!tmf_req.timeout_in_sec)
904 			tmf_req.timeout_in_sec = 1;
905 	}
906 
907 	op_ib_q = &softs->op_raid_ib_q[0];
908 	rcb->path = RAID_PATH;
909 	rcb->req_pending = true;
910 	/* Timedout tmf response goes here */
911 	rcb->error_cmp_callback = pqisrc_process_raid_response_error;
912 
913 	rval = pqisrc_submit_cmnd(softs, op_ib_q, &tmf_req);
914 	if (rval != PQI_STATUS_SUCCESS) {
915 		DBG_ERR("Unable to submit command rval=%d\n", rval);
916 		return rval;
917 	}
918 
919 	rval = pqisrc_wait_on_condition(softs, rcb, PQISRC_TMF_TIMEOUT);
920 	if (rval != PQI_STATUS_SUCCESS) {
921 		DBG_ERR("Task Management tmf_type : %d timeout\n", tmf_type);
922 		rcb->status = rval;
923 	}
924 
925 	if (rcb->status  != REQUEST_SUCCESS) {
926 		DBG_NOTE("Task Management failed tmf_type:%d "
927 				"stat:0x%x\n", tmf_type, rcb->status);
928 		rval = PQI_STATUS_FAILURE;
929 	}
930 
931 	DBG_FUNC("OUT");
932 	return rval;
933 }
934 
935 int
936 pqisrc_send_tmf(pqisrc_softstate_t *softs, pqi_scsi_dev_t *devp,
937                     rcb_t *rcb, rcb_t *rcb_to_manage, int tmf_type)
938 {
939 	int ret = PQI_STATUS_SUCCESS;
940 
941 	DBG_FUNC("IN");
942 	rcb->softs = softs;
943 
944 	if(!devp->is_physical_device) {
945 		if (tmf_type == SOP_TASK_MANAGEMENT_FUNCTION_ABORT_TASK) {
946 			if(rcb_to_manage->path == AIO_PATH) {
947 				if(devp->offload_enabled)
948 					ret = pqisrc_send_aio_tmf(softs, devp, rcb, rcb_to_manage, tmf_type);
949 			}
950 			else {
951 				DBG_INFO("TASK ABORT not supported in raid\n");
952 				ret = PQI_STATUS_FAILURE;
953 			}
954 		}
955 		else {
956 			ret = pqisrc_send_raid_tmf(softs, devp, rcb, rcb_to_manage, tmf_type);
957 		}
958 	} else {
959 		if (tmf_type == SOP_TASK_MANAGEMENT_FUNCTION_ABORT_TASK)
960 			ret = pqisrc_send_aio_tmf(softs, devp, rcb, rcb_to_manage, tmf_type);
961 		else
962 			ret = pqisrc_send_raid_tmf(softs, devp, rcb, rcb_to_manage, tmf_type);
963 	}
964 
965 	DBG_FUNC("IN");
966 
967 	return ret;
968 }
969 
970 /*
971  * Function used to build and send the vendor general request
972  * Used for configuring PQI feature bits between firmware and driver
973  */
974 int
975 pqisrc_build_send_vendor_request(
976 	pqisrc_softstate_t *softs,
977 	pqi_vendor_general_request_t *request,
978 	raid_path_error_info_elem_t *error_info)
979 {
980 	int ret = PQI_STATUS_SUCCESS;
981 	ib_queue_t *op_ib_q = &softs->op_raid_ib_q[PQI_DEFAULT_IB_QUEUE];
982 	ob_queue_t *ob_q = &softs->op_ob_q[PQI_DEFAULT_IB_QUEUE];
983 
984 	rcb_t *rcb = NULL;
985 
986 	uint16_t request_id = 0;
987 
988 	/* Get the tag */
989 	request_id = pqisrc_get_tag(&softs->taglist);
990 	if (INVALID_ELEM == request_id) {
991 		DBG_ERR("Tag not available\n");
992 		ret = PQI_STATUS_FAILURE;
993 		goto err_notag;
994 	}
995 
996 	((pqi_vendor_general_request_t *)request)->request_id = request_id;
997 	((pqi_vendor_general_request_t *)request)->response_queue_id = ob_q->q_id;
998 
999 	rcb = &softs->rcb[request_id];
1000 
1001 	rcb->req_pending = true;
1002 	rcb->tag = request_id;
1003 
1004 	ret = pqisrc_submit_cmnd(softs, op_ib_q, request);
1005 
1006 	if (ret != PQI_STATUS_SUCCESS) {
1007 		DBG_ERR("Unable to submit command\n");
1008 		goto err_out;
1009 	}
1010 
1011 	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
1012 	if (ret != PQI_STATUS_SUCCESS) {
1013 		DBG_ERR("Management request timed out!\n");
1014 		goto err_out;
1015 	}
1016 
1017 	ret = rcb->status;
1018 	if (ret) {
1019 		ret = PQI_STATUS_FAILURE;
1020 		if(error_info) {
1021 			// TODO: config table err handling.
1022 		}
1023 	} else {
1024 		if(error_info) {
1025 			ret = PQI_STATUS_SUCCESS;
1026 			memset(error_info, 0, sizeof(*error_info));
1027 		}
1028 	}
1029 
1030 	os_reset_rcb(rcb);
1031 	pqisrc_put_tag(&softs->taglist, ((pqi_vendor_general_request_t *)request)->request_id);
1032 	DBG_FUNC("OUT\n");
1033 	return ret;
1034 
1035 err_out:
1036 	DBG_ERR("Vender general request submission failed.\n");
1037 	os_reset_rcb(rcb);
1038 	pqisrc_put_tag(&softs->taglist, ((pqi_vendor_general_request_t *)request)->request_id);
1039 err_notag:
1040 	DBG_FUNC("FAILED \n");
1041 	return ret;
1042 }
1043 
1044 /* return the path as ASCII-string */
1045 char *
1046 io_path_to_ascii(IO_PATH_T path)
1047 {
1048 	switch (path)
1049 	{
1050 		case AIO_PATH:		return "Aio";
1051 		case RAID_PATH:		return "Raid";
1052 		default:		return "Unknown";
1053 	}
1054 }
1055