xref: /freebsd/sys/dev/smartpqi/smartpqi_discovery.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * Copyright 2016-2023 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  * Populate the controller's advanced aio features via BMIC cmd.
31  */
32 int
33 pqisrc_QuerySenseFeatures(pqisrc_softstate_t *softs)
34 {
35 	bmic_sense_feature_aio_buffer_t *features;
36 	int ret;
37 	pqisrc_raid_req_t request;
38 
39 	/* Initializing defaults for AIO support subpage */
40 	softs->max_aio_write_raid5_6 =
41 		PQISRC_MAX_AIO_RAID5_OR_6_WRITE;
42 	softs->max_aio_write_raid1_10_2drv =
43 		PQISRC_MAX_AIO_RAID1_OR_10_WRITE_2DRV;
44 	softs->max_aio_write_raid1_10_3drv =
45 		PQISRC_MAX_AIO_RAID1_OR_10_WRITE_3DRV;
46 	softs->max_aio_rw_xfer_crypto_nvme =
47 		PQISRC_MAX_AIO_RW_XFER_NVME_CRYPTO;
48 	softs->max_aio_rw_xfer_crypto_sas_sata =
49 		PQISRC_MAX_AIO_RW_XFER_SAS_SATA_CRYPTO;
50 
51 #ifdef DEVICE_HINT
52 	softs->enable_stream_detection = softs->hint.stream_status;
53 #endif
54 
55 	/* Implement SENSE_FEATURE BMIC to populate AIO limits */
56 	features = os_mem_alloc(softs, sizeof(*features));
57 	if (!features) {
58 		DBG_ERR("Failed to allocate memory for sense aio features.\n");
59 		goto err;
60 	}
61 	memset(features, 0, sizeof(*features));
62 
63 	memset(&request, 0, sizeof(request));
64 	request.data_direction = SOP_DATA_DIR_TO_DEVICE;
65 	request.cmd.bmic_cdb.op_code = BMIC_READ;
66 	request.cmd.cdb[2] = IO_SENSE_FEATURES_PAGE;
67 	request.cmd.cdb[3] = SENSE_FEATURES_AIO_SUBPAGE;
68 	request.cmd.bmic_cdb.cmd = BMIC_SENSE_FEATURE;
69 	request.cmd.bmic_cdb.xfer_len = BE_16(sizeof(*features));
70 	ret = pqisrc_prepare_send_ctrlr_request(softs, &request,
71 		features, sizeof(*features));
72 
73 	if (ret)
74 		goto free_err;
75 
76 	/* If AIO subpage was valid, use values from that page */
77 	if (features->aio_subpage.header.total_length >=
78 		MINIMUM_AIO_SUBPAGE_LENGTH) {
79 		DBG_INIT("AIO support subpage valid. total_length = 0x%0x.\n",
80 			features->aio_subpage.header.total_length);
81 		softs->adv_aio_capable = true;
82 
83 		/* AIO transfer limits are reported in  kbytes, so x 1024.
84  		 * Values of 0 mean 'no limit'.
85  		 */
86 
87 		softs->max_aio_write_raid5_6 =
88 			(features->aio_subpage.max_aio_write_raid5_6 == 0) ?
89 			PQISRC_MAX_AIO_NO_LIMIT :
90 			features->aio_subpage.max_aio_write_raid5_6 * 1024;
91 		softs->max_aio_write_raid1_10_2drv =
92 			(features->aio_subpage.max_aio_write_raid1_10_2drv
93 			== 0) ?  PQISRC_MAX_AIO_NO_LIMIT :
94 			features->aio_subpage.max_aio_write_raid1_10_2drv
95 			* 1024;
96 		softs->max_aio_write_raid1_10_3drv =
97 			(features->aio_subpage.max_aio_write_raid1_10_3drv
98 			== 0) ?  PQISRC_MAX_AIO_NO_LIMIT :
99 			features->aio_subpage.max_aio_write_raid1_10_3drv
100 			* 1024;
101 		softs->max_aio_rw_xfer_crypto_nvme =
102 			(features->aio_subpage.max_aio_rw_xfer_crypto_nvme
103 			== 0) ?  PQISRC_MAX_AIO_NO_LIMIT :
104 			features->aio_subpage.max_aio_rw_xfer_crypto_nvme
105 			* 1024;
106 		softs->max_aio_rw_xfer_crypto_sas_sata =
107 			(features->aio_subpage.max_aio_rw_xfer_crypto_sas_sata
108 			== 0) ?  PQISRC_MAX_AIO_NO_LIMIT :
109 			features->aio_subpage.max_aio_rw_xfer_crypto_sas_sata
110 			* 1024;
111 
112 		DBG_INIT("softs->max_aio_write_raid5_6: 0x%x\n",
113 			softs->max_aio_write_raid5_6);
114 		DBG_INIT("softs->max_aio_write_raid1_10_2drv: 0x%x\n",
115 			softs->max_aio_write_raid1_10_2drv);
116 		DBG_INIT("softs->max_aio_write_raid1_10_3drv: 0x%x\n",
117 			softs->max_aio_write_raid1_10_3drv);
118 		DBG_INIT("softs->max_aio_rw_xfer_crypto_nvme: 0x%x\n",
119 			softs->max_aio_rw_xfer_crypto_nvme);
120 		DBG_INIT("softs->max_aio_rw_xfer_crypto_sas_sata: 0x%x\n",
121 			softs->max_aio_rw_xfer_crypto_sas_sata);
122 
123 	} else {
124 		DBG_WARN("Problem getting AIO support subpage settings. "
125 			"Disabling advanced AIO writes.\n");
126 		softs->adv_aio_capable = false;
127 	}
128 
129 
130 	os_mem_free(softs, features, sizeof(*features));
131 	return ret;
132 free_err:
133 	os_mem_free(softs, features, sizeof(*features));
134 err:
135 	return PQI_STATUS_FAILURE;
136 }
137 
138 /*
139  * Initialize target ID pool for exposed physical devices .
140  */
141 void
142 pqisrc_init_bitmap(pqisrc_softstate_t *softs)
143 {
144 	memset(&softs->bit_map, SLOT_AVAILABLE, sizeof(softs->bit_map));
145 }
146 
147 void
148 pqisrc_remove_target_bit(pqisrc_softstate_t *softs, int target)
149 {
150 	if((target == PQI_CTLR_INDEX) || (target == INVALID_ELEM)) {
151 		DBG_ERR("Invalid target ID\n");
152 		return;
153 	}
154 	DBG_DISC("Giving back target %d\n", target);
155 	softs->bit_map.bit_vector[target] = SLOT_AVAILABLE;
156 }
157 
158 /* Use bit map to find availible targets */
159 int
160 pqisrc_find_avail_target(pqisrc_softstate_t *softs)
161 {
162 
163 	int avail_target;
164 	for(avail_target = 1; avail_target < MAX_TARGET_BIT; avail_target++) {
165 		if(softs->bit_map.bit_vector[avail_target] == SLOT_AVAILABLE){
166 			softs->bit_map.bit_vector[avail_target] = SLOT_TAKEN;
167 			DBG_DISC("Avail_target is %d\n", avail_target);
168 			return avail_target;
169 		}
170 	}
171 	DBG_ERR("No available targets\n");
172 	return INVALID_ELEM;
173 }
174 
175 /* Subroutine used to set Bus-Target-Lun for the requested device */
176 static inline void
177 pqisrc_set_btl(pqi_scsi_dev_t *device, int bus, int target, int lun)
178 {
179 	DBG_FUNC("IN\n");
180 
181 	device->bus = bus;
182 	device->target = target;
183 	device->lun = lun;
184 
185 	DBG_FUNC("OUT\n");
186 }
187 
188 /* Add all exposed physical devices, logical devices, controller devices, PT RAID
189 *  devices and multi-lun devices */
190 boolean_t
191 pqisrc_add_softs_entry(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device,
192 						uint8_t *scsi3addr)
193 {
194 	/* Add physical devices with targets that need
195 	*  targets */
196 	int j;
197 	int tid = 0;
198 	unsigned char addr1[8], addr2[8];
199 	pqi_scsi_dev_t *temp_device;
200 
201 	/* If controller device, add it to list because its lun/bus/target
202 	*  values are already set */
203 	if(pqisrc_is_hba_lunid(scsi3addr))
204 		goto add_device_to_dev_list;
205 
206 	/* If exposed physical device give it a target then add it
207 	*  to the dev list */
208 	if(!pqisrc_is_logical_device(device)) {
209 		tid = pqisrc_find_avail_target(softs);
210 		if(INVALID_ELEM != tid){
211 			pqisrc_set_btl(device, PQI_PHYSICAL_DEVICE_BUS, tid, 0);
212 			goto add_device_to_dev_list;
213 		}
214 	}
215 
216 	/* If external raid device , assign target from the target pool.
217 	 * If a non-zero lun device, search through the list & find the
218          * device which has same target (byte 2 of LUN address).
219          * Assign the same target for this new lun. */
220 	if (pqisrc_is_external_raid_device(device)) {
221 		memcpy(addr1, device->scsi3addr, 8);
222 		for(j = 0; j < PQI_MAX_DEVICES; j++) {
223 			if(softs->dev_list[j] == NULL)
224 				continue;
225 			temp_device = softs->dev_list[j];
226 			memcpy(addr2, temp_device->scsi3addr, 8);
227 			if (addr1[2] == addr2[2]) {
228 				pqisrc_set_btl(device, PQI_EXTERNAL_RAID_VOLUME_BUS,
229 					temp_device->target,device->scsi3addr[0]);
230 				goto add_device_to_dev_list;
231 			}
232 		}
233 		tid = pqisrc_find_avail_target(softs);
234 		if(INVALID_ELEM != tid){
235 			pqisrc_set_btl(device, PQI_EXTERNAL_RAID_VOLUME_BUS, tid, device->scsi3addr[0]);
236 			goto add_device_to_dev_list;
237 		}
238 	}
239 
240 	/* If logical device, add it to list because its lun/bus/target
241 	*  values are already set */
242 	if(pqisrc_is_logical_device(device) && !pqisrc_is_external_raid_device(device))
243 		goto add_device_to_dev_list;
244 
245 	/* This is a non-zero lun of a multi-lun device.
246 	*  Search through our list and find the device which
247 	*  has the same 8 byte LUN address, except with bytes 4 and 5.
248 	*  Assign the same bus and target for this new LUN.
249 	*  Use the logical unit number from the firmware. */
250 	memcpy(addr1, device->scsi3addr, 8);
251 	addr1[4] = 0;
252 	addr1[5] = 0;
253 	for(j = 0; j < PQI_MAX_DEVICES; j++) {
254 		if(softs->dev_list[j] == NULL)
255 			continue;
256 		temp_device = softs->dev_list[j];
257 		memcpy(addr2, temp_device->scsi3addr, 8);
258 		addr2[4] = 0;
259 		addr2[5] = 0;
260 		/* If addresses are the same, except for bytes 4 and 5
261 		*  then the passed-in device is an additional lun of a
262 		*  previously added multi-lun device. Use the same target
263 		*  id as that previous device. Otherwise, use the new
264 		*  target id */
265 		if(memcmp(addr1, addr2, 8) == 0) {
266 			pqisrc_set_btl(device, temp_device->bus,
267 				temp_device->target, temp_device->scsi3addr[4]);
268 			goto add_device_to_dev_list;
269 		}
270 	}
271 	DBG_ERR("The device is not a physical, lun or ptraid device"
272 		"B %d: T %d: L %d\n", device->bus, device->target,
273 		device->lun );
274 	return false;
275 
276 add_device_to_dev_list:
277 	/* Actually add the device to the driver list
278 	*  softs->dev_list */
279 	softs->num_devs++;
280 	for(j = 0; j < PQI_MAX_DEVICES; j++) {
281 		if(softs->dev_list[j])
282 			continue;
283 		softs->dev_list[j] = device;
284 		break;
285 	}
286 	DBG_NOTE("Added device [%d of %d]: B %d: T %d: L %d\n",
287 		j, softs->num_devs, device->bus, device->target,
288 		device->lun);
289 	return true;
290 }
291 
292 /* Return a given index for a specific bus, target, lun within the
293 *  softs dev_list (This function is specifically for freebsd)*/
294 int
295 pqisrc_find_btl_list_index(pqisrc_softstate_t *softs,
296 							int bus, int target, int lun)
297 {
298 
299 	int index;
300 	pqi_scsi_dev_t *temp_device;
301 	for(index = 0; index < PQI_MAX_DEVICES; index++) {
302 		if(softs->dev_list[index] == NULL)
303 			continue;
304 		temp_device = softs->dev_list[index];
305 		/* Match the devices then return the location
306 		*  of that device for further use*/
307 		if(bus == softs->bus_id &&
308 			target == temp_device->target &&
309 			lun == temp_device->lun){
310 			DBG_DISC("Returning device list index %d\n", index);
311 			return index;
312 
313 		}
314 		if ((temp_device->is_physical_device) && (target == temp_device->target)
315 					&& (temp_device->is_multi_lun)) {
316 			return index;
317 		}
318 	}
319 	return INVALID_ELEM;
320 }
321 
322 /* Return a given index for a specific device within the
323 *  softs dev_list */
324 int
325 pqisrc_find_device_list_index(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
326 {
327 
328 	int index;
329 	pqi_scsi_dev_t *temp_device;
330 	for(index = 0; index < PQI_MAX_DEVICES; index++) {
331 		if(softs->dev_list[index] == NULL)
332 			continue;
333 		temp_device = softs->dev_list[index];
334 		/* Match the devices then return the location
335 		*  of that device for further use*/
336 		if(device->bus == temp_device->bus &&
337 			device->target == temp_device->target
338 			&& device->lun == temp_device->lun){
339 			DBG_DISC("Returning device list index %d\n", index);
340 			return index;
341 
342 		}
343 	}
344 	return INVALID_ELEM;
345 }
346 
347 /* Delete a given device from the softs dev_list*/
348 int
349 pqisrc_delete_softs_entry(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
350 {
351 
352 	int index;
353 	index = pqisrc_find_device_list_index(softs, device);
354 	if (0 <= index && index < MAX_TARGET_BIT) {
355 		softs->dev_list[index] = NULL;
356 		softs->num_devs--;
357 		DBG_NOTE("Removing device : B %d: T %d: L %d positioned at %d\n",
358 				device->bus, device->target, device->lun, softs->num_devs);
359 		return PQI_STATUS_SUCCESS;
360 	}
361 	if (index == INVALID_ELEM) {
362 		DBG_NOTE("Invalid device, either it was already removed "
363 				"or never added\n");
364 		return PQI_STATUS_FAILURE;
365 	}
366 	DBG_ERR("This is a bogus device\n");
367 	return PQI_STATUS_FAILURE;
368 }
369 
370 int
371 pqisrc_simple_dma_alloc(pqisrc_softstate_t *softs, struct dma_mem *device_mem,
372 					    size_t datasize, sgt_t *sgd)
373 {
374 	int ret = PQI_STATUS_SUCCESS;
375 
376 	memset(device_mem, 0, sizeof(struct dma_mem));
377 
378 	/* for TUR datasize: 0 buff: NULL */
379 	if (datasize) {
380 
381 		os_strlcpy(device_mem->tag, "device_mem", sizeof(device_mem->tag));
382 		device_mem->size = datasize;
383 		device_mem->align = PQISRC_DEFAULT_DMA_ALIGN;
384 
385 		ret = os_dma_mem_alloc(softs, device_mem);
386 
387 		if (ret) {
388 			DBG_ERR("failed to allocate dma memory for device_mem return code %d\n", ret);
389 			return ret;
390 		}
391 
392 		ASSERT(device_mem->size == datasize);
393 
394 		sgd->addr = device_mem->dma_addr;
395 		sgd->len = datasize;
396 		sgd->flags = SG_FLAG_LAST;
397 
398 	}
399 
400 	return ret;
401 }
402 
403 /*
404  * Function used to build the internal raid request and analyze the response
405  */
406 static int
407 pqisrc_build_send_raid_request(pqisrc_softstate_t *softs, struct dma_mem device_mem,
408 							   pqisrc_raid_req_t *request, void *buff,
409 							   size_t datasize, uint8_t cmd, uint8_t *scsi3addr,
410 							   raid_path_error_info_elem_t *error_info)
411 {
412 
413 	uint32_t tag = 0;
414 	int ret = PQI_STATUS_SUCCESS;
415 
416 	ib_queue_t *ib_q = &softs->op_raid_ib_q[PQI_DEFAULT_IB_QUEUE];
417 	ob_queue_t *ob_q = &softs->op_ob_q[PQI_DEFAULT_IB_QUEUE];
418 
419 	rcb_t *rcb = NULL;
420 
421 	/* Build raid path request */
422 	request->header.iu_type = PQI_IU_TYPE_RAID_PATH_IO_REQUEST;
423 
424 	request->header.iu_length = LE_16(offsetof(pqisrc_raid_req_t,
425 							sg_descriptors[1]) - PQI_REQUEST_HEADER_LENGTH);
426 	request->buffer_length = LE_32(datasize);
427 	memcpy(request->lun_number, scsi3addr, sizeof(request->lun_number));
428 	request->task_attribute = SOP_TASK_ATTRIBUTE_SIMPLE;
429 	request->additional_cdb_bytes_usage = PQI_ADDITIONAL_CDB_BYTES_0;
430 
431 	tag = pqisrc_get_tag(&softs->taglist);
432 	if (INVALID_ELEM == tag) {
433 		DBG_ERR("Tag not available\n");
434 		ret = PQI_STATUS_FAILURE;
435 		goto err_notag;
436 	}
437 
438 	((pqisrc_raid_req_t *)request)->request_id = tag;
439 	((pqisrc_raid_req_t *)request)->error_index = ((pqisrc_raid_req_t *)request)->request_id;
440 	((pqisrc_raid_req_t *)request)->response_queue_id = ob_q->q_id;
441 	rcb = &softs->rcb[tag];
442 	rcb->success_cmp_callback = pqisrc_process_internal_raid_response_success;
443 	rcb->error_cmp_callback = pqisrc_process_internal_raid_response_error;
444 
445 	rcb->req_pending = true;
446 	rcb->tag = tag;
447 	/* Submit Command */
448 	ret = pqisrc_submit_cmnd(softs, ib_q, request);
449 
450 	if (ret != PQI_STATUS_SUCCESS) {
451 		DBG_ERR("Unable to submit command\n");
452 		goto err_out;
453 	}
454 
455 	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
456 	if (ret != PQI_STATUS_SUCCESS) {
457 		DBG_ERR("Internal RAID request timed out: cmd : 0x%c\n", cmd);
458 		goto err_out;
459 	}
460 
461 	if (datasize) {
462 		if (buff) {
463 			memcpy(buff, device_mem.virt_addr, datasize);
464 		}
465 		os_dma_mem_free(softs, &device_mem);
466 	}
467 
468 	ret = rcb->status;
469 	if (ret) {
470 		if(error_info) {
471 			memcpy(error_info,
472 			       rcb->error_info,
473 			       sizeof(*error_info));
474 
475 			if (error_info->data_out_result ==
476 			    PQI_RAID_DATA_IN_OUT_UNDERFLOW) {
477 				ret = PQI_STATUS_SUCCESS;
478 			}
479 			else{
480 				DBG_WARN("Bus=%u Target=%u, Cmd=0x%x,"
481 					"Ret=%d\n", BMIC_GET_LEVEL_2_BUS(scsi3addr),
482 					BMIC_GET_LEVEL_TWO_TARGET(scsi3addr),
483 					cmd, ret);
484 				ret = PQI_STATUS_FAILURE;
485 			}
486 		}
487 	} else {
488 		if(error_info) {
489 			ret = PQI_STATUS_SUCCESS;
490 			memset(error_info, 0, sizeof(*error_info));
491 		}
492 	}
493 
494 	os_reset_rcb(rcb);
495 	pqisrc_put_tag(&softs->taglist, ((pqisrc_raid_req_t *)request)->request_id);
496 	DBG_FUNC("OUT\n");
497 	return ret;
498 
499 err_out:
500 	DBG_ERR("Error!! Bus=%u Target=%u, Cmd=0x%x, Ret=%d\n",
501 		BMIC_GET_LEVEL_2_BUS(scsi3addr), BMIC_GET_LEVEL_TWO_TARGET(scsi3addr),
502 		cmd, ret);
503 	os_reset_rcb(rcb);
504 	pqisrc_put_tag(&softs->taglist, ((pqisrc_raid_req_t *)request)->request_id);
505 err_notag:
506 	if (datasize)
507 		os_dma_mem_free(softs, &device_mem);
508 	DBG_FUNC("FAILED \n");
509 	return ret;
510 }
511 
512 /* Use this if you need to specify specific target or if you want error info */
513 int
514 pqisrc_prepare_send_raid(pqisrc_softstate_t *softs, pqisrc_raid_req_t *request,
515     					 void *buff, size_t datasize, uint8_t *scsi3addr,
516 						 raid_path_error_info_elem_t *error_info)
517 {
518 	struct dma_mem device_mem;
519 	int ret = PQI_STATUS_SUCCESS;
520 	uint8_t cmd = IS_BMIC_OPCODE(request->cmd.cdb[0]) ? request->cmd.cdb[6] : request->cmd.cdb[0];
521 
522 	ret = pqisrc_simple_dma_alloc(softs, &device_mem, datasize, request->sg_descriptors);
523 	if (PQI_STATUS_SUCCESS != ret){
524 		DBG_ERR("failed to allocate dma memory for device_mem return code %d\n", ret);
525 		return ret;
526 	}
527 
528 	/* If we are sending out data, copy it over to dma buf */
529 	if (datasize && buff && request->data_direction == SOP_DATA_DIR_FROM_DEVICE)
530 		memcpy(device_mem.virt_addr, buff, datasize);
531 
532 	ret = pqisrc_build_send_raid_request(softs, device_mem, request, buff, datasize,
533 		cmd, scsi3addr, error_info);
534 
535 	return ret;
536 }
537 
538 /* Use this to target controller and don't care about error info */
539 int
540 pqisrc_prepare_send_ctrlr_request(pqisrc_softstate_t *softs, pqisrc_raid_req_t *request,
541     							  void *buff, size_t datasize)
542 {
543    raid_path_error_info_elem_t error_info; /* will be thrown away */
544    uint8_t *scsi3addr = RAID_CTLR_LUNID;
545 
546    return pqisrc_prepare_send_raid(softs, request, buff, datasize, scsi3addr, &error_info);
547 }
548 
549 /* common function used to send report physical and logical luns cmds */
550 static int
551 pqisrc_report_luns(pqisrc_softstate_t *softs, uint8_t cmd,
552 	void *buff, size_t buf_len)
553 {
554 	int ret;
555 	pqisrc_raid_req_t request;
556 
557 	DBG_FUNC("IN\n");
558 
559 	memset(&request, 0, sizeof(request));
560 
561 	request.data_direction = SOP_DATA_DIR_TO_DEVICE;
562 
563 	switch (cmd) {
564 	case SA_REPORT_LOG:
565 		request.cmd.cdb[0] = SA_REPORT_LOG;
566 		request.cmd.cdb[1] = SA_REPORT_LOG_EXTENDED;
567 		break;
568 	case SA_REPORT_PHYS:
569 		request.cmd.cdb[0] = SA_REPORT_PHYS;
570 		request.cmd.cdb[1] = SA_REPORT_PHYS_EXTENDED;
571 		break;
572    /* @todo: 0x56 does not exist, this is kludgy, need to pass in options */
573 	case PQI_LOG_EXT_QUEUE_ENABLE:
574 		request.cmd.cdb[0] = SA_REPORT_LOG;
575 		request.cmd.cdb[1] = (PQI_LOG_EXT_QUEUE_DEPTH_ENABLED | SA_REPORT_LOG_EXTENDED);
576 		break;
577 	}
578 
579 	request.cmd.cdb[8] = (uint8_t)((buf_len) >> 8);
580 	request.cmd.cdb[9] = (uint8_t)buf_len;
581 
582 	ret = pqisrc_prepare_send_ctrlr_request(softs, &request, buff, buf_len);
583 
584 	DBG_FUNC("OUT\n");
585 
586 	return ret;
587 }
588 
589 /* subroutine used to get physical and logical luns of the device */
590 int
591 pqisrc_get_physical_logical_luns(pqisrc_softstate_t *softs, uint8_t cmd,
592 		reportlun_data_ext_t **buff, size_t *data_length)
593 {
594 	int ret;
595 	size_t list_len;
596 	size_t data_len;
597 	size_t new_lun_list_length;
598 	reportlun_data_ext_t *lun_data;
599 	reportlun_header_t report_lun_header;
600 
601 	DBG_FUNC("IN\n");
602 
603 	ret = pqisrc_report_luns(softs, cmd, &report_lun_header,
604 		sizeof(report_lun_header));
605 
606 	if (ret) {
607 		DBG_ERR("failed return code: %d\n", ret);
608 		return ret;
609 	}
610 	list_len = BE_32(report_lun_header.list_length);
611 
612 retry:
613 	data_len = sizeof(reportlun_header_t) + list_len;
614 	*data_length = data_len;
615 
616 	lun_data = os_mem_alloc(softs, data_len);
617 
618 	if (!lun_data) {
619 		DBG_ERR("failed to allocate memory for lun_data\n");
620 		return PQI_STATUS_FAILURE;
621 	}
622 
623 	if (list_len == 0) {
624 		DBG_DISC("list_len is 0\n");
625 		memcpy(lun_data, &report_lun_header, sizeof(report_lun_header));
626 		goto out;
627 	}
628 
629 	ret = pqisrc_report_luns(softs, cmd, lun_data, data_len);
630 
631 	if (ret) {
632 		DBG_ERR("error\n");
633 		goto error;
634 	}
635 
636 	new_lun_list_length = BE_32(lun_data->header.list_length);
637 
638 	if (new_lun_list_length > list_len) {
639 		list_len = new_lun_list_length;
640 		os_mem_free(softs, (void *)lun_data, data_len);
641 		goto retry;
642 	}
643 
644 out:
645 	*buff = lun_data;
646 	DBG_FUNC("OUT\n");
647 	return 0;
648 
649 error:
650 	os_mem_free(softs, (void *)lun_data, data_len);
651 	DBG_ERR("FAILED\n");
652 	return ret;
653 }
654 
655 /*
656  * Function used to grab queue depth ext lun data for logical devices
657  */
658 static int
659 pqisrc_get_queue_lun_list(pqisrc_softstate_t *softs, uint8_t cmd,
660                 reportlun_queue_depth_data_t **buff, size_t *data_length)
661 {
662         int ret;
663         size_t list_len;
664         size_t data_len;
665         size_t new_lun_list_length;
666         reportlun_queue_depth_data_t *lun_data;
667         reportlun_header_t report_lun_header;
668 
669         DBG_FUNC("IN\n");
670 
671         ret = pqisrc_report_luns(softs, cmd, &report_lun_header,
672                 sizeof(report_lun_header));
673 
674         if (ret) {
675                 DBG_ERR("failed return code: %d\n", ret);
676                 return ret;
677         }
678         list_len = BE_32(report_lun_header.list_length);
679 retry:
680         data_len = sizeof(reportlun_header_t) + list_len;
681         *data_length = data_len;
682         lun_data = os_mem_alloc(softs, data_len);
683 
684 	if (!lun_data) {
685                 DBG_ERR("failed to allocate memory for lun_data\n");
686                 return PQI_STATUS_FAILURE;
687         }
688 
689         if (list_len == 0) {
690                 DBG_DISC("list_len is 0\n");
691                 memcpy(lun_data, &report_lun_header, sizeof(report_lun_header));
692                 goto out;
693         }
694         ret = pqisrc_report_luns(softs, cmd, lun_data, data_len);
695 
696         if (ret) {
697                 DBG_ERR("error\n");
698                 goto error;
699         }
700         new_lun_list_length = BE_32(lun_data->header.list_length);
701 
702         if (new_lun_list_length > list_len) {
703                 list_len = new_lun_list_length;
704                 os_mem_free(softs, (void *)lun_data, data_len);
705                 goto retry;
706         }
707 
708 out:
709         *buff = lun_data;
710         DBG_FUNC("OUT\n");
711         return 0;
712 
713 error:
714         os_mem_free(softs, (void *)lun_data, data_len);
715         DBG_ERR("FAILED\n");
716         return ret;
717 }
718 
719 /*
720  * Function used to get physical and logical device list
721  */
722 static int
723 pqisrc_get_phys_log_device_list(pqisrc_softstate_t *softs,
724 	reportlun_data_ext_t **physical_dev_list,
725 	reportlun_data_ext_t **logical_dev_list,
726 	reportlun_queue_depth_data_t **queue_dev_list,
727 	size_t *queue_data_length,
728 	size_t *phys_data_length,
729 	size_t *log_data_length)
730 {
731 	int ret = PQI_STATUS_SUCCESS;
732 	size_t logical_list_length;
733 	size_t logdev_data_length;
734 	size_t data_length;
735 	reportlun_data_ext_t *local_logdev_list;
736 	reportlun_data_ext_t *logdev_data;
737 	reportlun_header_t report_lun_header;
738 
739 	DBG_FUNC("IN\n");
740 
741 	ret = pqisrc_get_physical_logical_luns(softs, SA_REPORT_PHYS, physical_dev_list, phys_data_length);
742 	if (ret) {
743 		DBG_ERR("report physical LUNs failed");
744 		return ret;
745 	}
746 
747 	ret = pqisrc_get_physical_logical_luns(softs, SA_REPORT_LOG, logical_dev_list, log_data_length);
748 	if (ret) {
749 		DBG_ERR("report logical LUNs failed");
750 		return ret;
751 	}
752 
753 #ifdef PQI_NEED_RESCAN_TIMER_FOR_RBOD_HOTPLUG
754 	/* Save the report_log_dev buffer for deciding rescan requirement from OS driver*/
755 	if(softs->log_dev_data_length != *log_data_length) {
756 		if(softs->log_dev_list)
757 			os_mem_free(softs, softs->log_dev_list, softs->log_dev_data_length);
758 		softs->log_dev_list = os_mem_alloc(softs, *log_data_length);
759 	}
760 	memcpy(softs->log_dev_list, *logical_dev_list, *log_data_length);
761 	softs->log_dev_data_length = *log_data_length;
762 #endif
763 
764 	ret = pqisrc_get_queue_lun_list(softs, PQI_LOG_EXT_QUEUE_ENABLE, queue_dev_list, queue_data_length);
765 	if (ret) {
766 		DBG_ERR("report logical LUNs failed");
767 		return ret;
768 	}
769 
770 	logdev_data = *logical_dev_list;
771 
772 	if (logdev_data) {
773 		logical_list_length =
774 			BE_32(logdev_data->header.list_length);
775 	} else {
776 		memset(&report_lun_header, 0, sizeof(report_lun_header));
777 		logdev_data =
778 			(reportlun_data_ext_t *)&report_lun_header;
779 		logical_list_length = 0;
780 	}
781 
782 	logdev_data_length = sizeof(reportlun_header_t) +
783 		logical_list_length;
784 
785 	/* Adding LOGICAL device entry for controller */
786 	local_logdev_list = os_mem_alloc(softs,
787 					    logdev_data_length + sizeof(reportlun_ext_entry_t));
788 	if (!local_logdev_list) {
789 		data_length = *log_data_length;
790 		os_mem_free(softs, (char *)*logical_dev_list, data_length);
791 		*logical_dev_list = NULL;
792 		return PQI_STATUS_FAILURE;
793 	}
794 
795 	memcpy(local_logdev_list, logdev_data, logdev_data_length);
796 	memset((uint8_t *)local_logdev_list + logdev_data_length, 0,
797 		sizeof(reportlun_ext_entry_t));
798 	local_logdev_list->header.list_length = BE_32(logical_list_length +
799 							sizeof(reportlun_ext_entry_t));
800 	data_length = *log_data_length;
801 	os_mem_free(softs, (char *)*logical_dev_list, data_length);
802 	*log_data_length = logdev_data_length + sizeof(reportlun_ext_entry_t);
803 	*logical_dev_list = local_logdev_list;
804 
805 	DBG_FUNC("OUT\n");
806 
807 	return ret;
808 }
809 
810 inline boolean_t
811 pqisrc_is_external_raid_device(pqi_scsi_dev_t *device)
812 {
813 	return device->is_external_raid_device;
814 }
815 
816 static inline boolean_t
817 pqisrc_is_external_raid_addr(uint8_t *scsi3addr)
818 {
819 	return scsi3addr[2] != 0;
820 }
821 
822 /* Function used to assign Bus-Target-Lun for the requested device */
823 static void
824 pqisrc_assign_btl(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
825 {
826 	uint8_t *scsi3addr;
827 	uint32_t lunid;
828 	uint32_t bus;
829 	uint32_t target;
830 	uint32_t lun;
831 	DBG_FUNC("IN\n");
832 
833 	scsi3addr = device->scsi3addr;
834 	lunid = GET_LE32(scsi3addr);
835 
836 	if (pqisrc_is_hba_lunid(scsi3addr)) {
837 	/* The specified device is the controller. */
838 		pqisrc_set_btl(device, PQI_HBA_BUS, PQI_CTLR_INDEX, (lunid & 0x3fff));
839 		device->target_lun_valid = true;
840 		return;
841 	}
842 
843 	/* When the specified device is a logical volume,
844 	*  physicals will be given targets in pqisrc update
845 	*  device list in pqisrc scan devices. */
846 	if (pqisrc_is_logical_device(device)) {
847 			bus = PQI_RAID_VOLUME_BUS;
848 			lun = (lunid & 0x3fff) + 1;
849 			target = 0;
850 			pqisrc_set_btl(device, bus, target, lun);
851 			device->target_lun_valid = true;
852 			return;
853 	}
854 
855 	DBG_FUNC("OUT\n");
856 }
857 
858 /* Build and send the internal INQUIRY command to particular device */
859 int
860 pqisrc_send_scsi_inquiry(pqisrc_softstate_t *softs,
861 	uint8_t *scsi3addr, uint16_t vpd_page, uint8_t *buff, int buf_len)
862 {
863 	int ret = PQI_STATUS_SUCCESS;
864 	pqisrc_raid_req_t request;
865 	raid_path_error_info_elem_t error_info;
866 
867 	DBG_FUNC("IN\n");
868 
869 	memset(&request, 0, sizeof(request));
870 
871 	request.data_direction = SOP_DATA_DIR_TO_DEVICE;
872 	request.cmd.cdb[0] = SA_INQUIRY;
873 	if (vpd_page & VPD_PAGE) {
874 		request.cmd.cdb[1] = 0x1;
875 		request.cmd.cdb[2] = (uint8_t)vpd_page;
876 	}
877 	ASSERT(buf_len < 256);
878 	request.cmd.cdb[4] = (uint8_t)buf_len;
879 
880 	if (softs->timeout_in_passthrough) {
881 		request.timeout_in_sec = PQISRC_INQUIRY_TIMEOUT;
882 	}
883 
884 	pqisrc_prepare_send_raid(softs, &request, buff, buf_len, scsi3addr, &error_info);
885 
886 	DBG_FUNC("OUT\n");
887 	return ret;
888 }
889 
890 /* Determine logical volume status from vpd buffer.*/
891 static void pqisrc_get_dev_vol_status(pqisrc_softstate_t *softs,
892 	pqi_scsi_dev_t *device)
893 {
894 	int ret;
895 	uint8_t status = SA_LV_STATUS_VPD_UNSUPPORTED;
896 	uint8_t vpd_size = sizeof(vpd_volume_status);
897 	uint8_t offline = true;
898 	size_t page_length;
899 	vpd_volume_status *vpd;
900 
901 	DBG_FUNC("IN\n");
902 
903 	vpd = os_mem_alloc(softs, vpd_size);
904 	if (vpd == NULL)
905 		goto out;
906 
907 	/* Get the size of the VPD return buff. */
908 	ret = pqisrc_send_scsi_inquiry(softs, device->scsi3addr, VPD_PAGE | SA_VPD_LV_STATUS,
909 		(uint8_t *)vpd, vpd_size);
910 
911 	if (ret) {
912 		DBG_WARN("Inquiry returned failed status\n");
913 		goto out;
914 	}
915 
916 	if (vpd->page_code != SA_VPD_LV_STATUS) {
917 		DBG_WARN("Returned invalid buffer\n");
918 		goto out;
919 	}
920 
921 	page_length = offsetof(vpd_volume_status, volume_status) + vpd->page_length;
922 	if (page_length < vpd_size)
923 		goto out;
924 
925 	status = vpd->volume_status;
926 	offline = (vpd->flags & SA_LV_FLAGS_NO_HOST_IO)!=0;
927 
928 out:
929 	device->volume_offline = offline;
930 	device->volume_status = status;
931 
932 	os_mem_free(softs, (char *)vpd, vpd_size);
933 
934 	DBG_FUNC("OUT\n");
935 
936 	return;
937 }
938 
939 
940 /* Validate the RAID map parameters */
941 static int
942 pqisrc_raid_map_validation(pqisrc_softstate_t *softs,
943 	pqi_scsi_dev_t *device, pqisrc_raid_map_t *raid_map)
944 {
945 	char *error_msg;
946 	uint32_t raidmap_size;
947 	uint32_t r5or6_blocks_per_row;
948 /*	unsigned phys_dev_num; */
949 
950 	DBG_FUNC("IN\n");
951 
952 	raidmap_size = LE_32(raid_map->structure_size);
953 	if (raidmap_size < offsetof(pqisrc_raid_map_t, dev_data)) {
954 		error_msg = "RAID map too small\n";
955 		goto error;
956 	}
957 
958 #if 0
959 	phys_dev_num = LE_16(raid_map->layout_map_count) *
960 	(LE_16(raid_map->data_disks_per_row) +
961 	LE_16(raid_map->metadata_disks_per_row));
962 #endif
963 
964 	if (device->raid_level == SA_RAID_1) {
965 		if (LE_16(raid_map->layout_map_count) != 2) {
966 			error_msg = "invalid RAID-1 map\n";
967 			goto error;
968 		}
969 	} else if (device->raid_level == SA_RAID_ADM) {
970 		if (LE_16(raid_map->layout_map_count) != 3) {
971 			error_msg = "invalid RAID-1(triple) map\n";
972 			goto error;
973 		}
974 	} else if ((device->raid_level == SA_RAID_5 ||
975 		device->raid_level == SA_RAID_6) &&
976 		LE_16(raid_map->layout_map_count) > 1) {
977 		/* RAID 50/60 */
978 		r5or6_blocks_per_row =
979 			LE_16(raid_map->strip_size) *
980 			LE_16(raid_map->data_disks_per_row);
981 		if (r5or6_blocks_per_row == 0) {
982 			error_msg = "invalid RAID-5 or RAID-6 map\n";
983 			goto error;
984 		}
985 	}
986 
987 	DBG_FUNC("OUT\n");
988 
989 	return 0;
990 
991 error:
992 	DBG_NOTE("%s\n", error_msg);
993 	return PQI_STATUS_FAILURE;
994 }
995 
996 /* Get device raidmap for the requested device */
997 static int
998 pqisrc_get_device_raidmap(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
999 {
1000 	int ret = PQI_STATUS_SUCCESS;
1001 	int raidmap_alloc_size = sizeof(pqisrc_raid_map_t);
1002 	int raidmap_reported_size;
1003 	int structure_size;
1004 	int ii;
1005 	int *next_offload_to_mirror;
1006 
1007 	pqisrc_raid_req_t request;
1008 	pqisrc_raid_map_t *raid_map;
1009 
1010 	DBG_FUNC("IN\n");
1011 
1012 	for (ii = 0; ii < 2; ii++)
1013 	{
1014 		raid_map = os_mem_alloc(softs, raidmap_alloc_size);
1015 		if (!raid_map)
1016 			return PQI_STATUS_FAILURE;
1017 
1018 		memset(&request, 0, sizeof(request));
1019 		request.data_direction = SOP_DATA_DIR_TO_DEVICE;
1020 		request.cmd.cdb[0] = SA_CISS_READ;
1021 		request.cmd.cdb[1] = SA_GET_RAID_MAP;
1022 		request.cmd.cdb[8] = (uint8_t)((raidmap_alloc_size) >> 8);
1023 		request.cmd.cdb[9] = (uint8_t)(raidmap_alloc_size);
1024 
1025 		ret = pqisrc_prepare_send_raid(softs, &request, raid_map, raidmap_alloc_size, device->scsi3addr, NULL);
1026 
1027 		if (ret) {
1028 			DBG_ERR("error in build send raid req ret=%d\n", ret);
1029 			goto err_out;
1030 		}
1031 
1032 		raidmap_reported_size = LE_32(raid_map->structure_size);
1033 		if (raidmap_reported_size <= raidmap_alloc_size)
1034 			break;
1035 
1036 		DBG_NOTE("Raid map is larger than 1024 entries, request once again");
1037 		os_mem_free(softs, (char*)raid_map, raidmap_alloc_size);
1038 
1039 		raidmap_alloc_size = raidmap_reported_size;
1040 	}
1041 
1042 	ret = pqisrc_raid_map_validation(softs, device, raid_map);
1043 	if (ret) {
1044 		DBG_NOTE("error in raid map validation ret=%d\n", ret);
1045 		goto err_out;
1046 	}
1047 
1048 	structure_size = raid_map->data_disks_per_row * sizeof(*next_offload_to_mirror);
1049 	next_offload_to_mirror = os_mem_alloc(softs, structure_size);
1050 	if (!next_offload_to_mirror) {
1051 		ret = PQI_STATUS_FAILURE;
1052 		goto err_out;
1053 	}
1054 
1055 	device->raid_map = raid_map;
1056 	device->offload_to_mirror = next_offload_to_mirror;
1057 	DBG_FUNC("OUT\n");
1058 	return 0;
1059 
1060 err_out:
1061 	os_mem_free(softs, (char*)raid_map, sizeof(*raid_map));
1062 	DBG_FUNC("FAILED \n");
1063 	return ret;
1064 }
1065 
1066 /* Get device ioaccel_status to validate the type of device */
1067 static void
1068 pqisrc_get_dev_ioaccel_status(pqisrc_softstate_t *softs,
1069 	pqi_scsi_dev_t *device)
1070 {
1071 	int ret = PQI_STATUS_SUCCESS;
1072 	uint8_t *buff;
1073 	uint8_t ioaccel_status;
1074 
1075 	DBG_FUNC("IN\n");
1076 
1077 	buff = os_mem_alloc(softs, 64);
1078 	if (!buff)
1079 		return;
1080 
1081 	ret = pqisrc_send_scsi_inquiry(softs, device->scsi3addr,
1082 					VPD_PAGE | SA_VPD_LV_IOACCEL_STATUS, buff, 64);
1083 	if (ret) {
1084 		DBG_ERR("error in send scsi inquiry ret=%d\n", ret);
1085 		goto err_out;
1086 	}
1087 
1088 	ioaccel_status = buff[IOACCEL_STATUS_BYTE];
1089 	device->offload_config =
1090 		!!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
1091 
1092 	if (device->offload_config) {
1093 		device->offload_enabled_pending =
1094 			!!(ioaccel_status & OFFLOAD_ENABLED_BIT);
1095 		if (pqisrc_get_device_raidmap(softs, device))
1096 			device->offload_enabled_pending = false;
1097 	}
1098 
1099 	DBG_DISC("offload_config: 0x%x offload_enabled_pending: 0x%x \n",
1100 			device->offload_config, device->offload_enabled_pending);
1101 
1102 err_out:
1103 	os_mem_free(softs, (char*)buff, 64);
1104 	DBG_FUNC("OUT\n");
1105 }
1106 
1107 /* Get RAID level of requested device */
1108 static void
1109 pqisrc_get_dev_raid_level(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1110 {
1111 	uint8_t raid_level;
1112 	uint8_t *buff;
1113 
1114 	DBG_FUNC("IN\n");
1115 
1116 	raid_level = SA_RAID_UNKNOWN;
1117 
1118 	buff = os_mem_alloc(softs, 64);
1119 	if (buff) {
1120 		int ret;
1121 		ret = pqisrc_send_scsi_inquiry(softs, device->scsi3addr,
1122 			VPD_PAGE | SA_VPD_LV_DEVICE_GEOMETRY, buff, 64);
1123 		if (ret == 0) {
1124 			raid_level = buff[8];
1125 			if (raid_level > SA_RAID_MAX)
1126 				raid_level = SA_RAID_UNKNOWN;
1127 		}
1128 		os_mem_free(softs, (char*)buff, 64);
1129 	}
1130 
1131 	device->raid_level = raid_level;
1132 	DBG_DISC("RAID LEVEL: %x \n",  raid_level);
1133 	DBG_FUNC("OUT\n");
1134 }
1135 
1136 /* Parse the inquiry response and determine the type of device */
1137 static int
1138 pqisrc_get_dev_data(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1139 {
1140 	int ret = PQI_STATUS_SUCCESS;
1141 	uint8_t *inq_buff;
1142 	int retry = 3;
1143 
1144 	DBG_FUNC("IN\n");
1145 
1146 	inq_buff = os_mem_alloc(softs, OBDR_TAPE_INQ_SIZE);
1147 	if (!inq_buff)
1148 		return PQI_STATUS_FAILURE;
1149 
1150 	while(retry--) {
1151 		/* Send an inquiry to the device to see what it is. */
1152 		ret = pqisrc_send_scsi_inquiry(softs, device->scsi3addr, 0, inq_buff,
1153 			OBDR_TAPE_INQ_SIZE);
1154 		if (!ret)
1155 			break;
1156 		DBG_WARN("Retrying inquiry !!!\n");
1157 	}
1158 	if(retry <= 0)
1159 		goto err_out;
1160 	pqisrc_sanitize_inquiry_string(&inq_buff[8], 8);
1161 	pqisrc_sanitize_inquiry_string(&inq_buff[16], 16);
1162 
1163 	device->devtype = inq_buff[0] & 0x1f;
1164 	memcpy(device->vendor, &inq_buff[8],
1165 		sizeof(device->vendor));
1166 	memcpy(device->model, &inq_buff[16],
1167 		sizeof(device->model));
1168 	DBG_DISC("DEV_TYPE: %x VENDOR: %.8s MODEL: %.16s\n",  device->devtype, device->vendor, device->model);
1169 
1170 	if (pqisrc_is_logical_device(device) && device->devtype == DISK_DEVICE) {
1171 		if (pqisrc_is_external_raid_device(device)) {
1172 			device->raid_level = SA_RAID_UNKNOWN;
1173 			device->volume_status = SA_LV_OK;
1174 			device->volume_offline = false;
1175 		}
1176 		else {
1177 			pqisrc_get_dev_raid_level(softs, device);
1178 			pqisrc_get_dev_ioaccel_status(softs, device);
1179 			pqisrc_get_dev_vol_status(softs, device);
1180 		}
1181 	}
1182 
1183 	/*
1184 	 * Check if this is a One-Button-Disaster-Recovery device
1185 	 * by looking for "$DR-10" at offset 43 in the inquiry data.
1186 	 */
1187 	device->is_obdr_device = (device->devtype == ROM_DEVICE &&
1188 		memcmp(&inq_buff[OBDR_SIG_OFFSET], OBDR_TAPE_SIG,
1189 			OBDR_SIG_LEN) == 0);
1190 err_out:
1191 	os_mem_free(softs, (char*)inq_buff, OBDR_TAPE_INQ_SIZE);
1192 
1193 	DBG_FUNC("OUT\n");
1194 	return ret;
1195 }
1196 
1197 /*
1198  * BMIC (Basic Management And Interface Commands) command
1199  * to get the controller identify params
1200  */
1201 static int
1202 pqisrc_identify_ctrl(pqisrc_softstate_t *softs, bmic_ident_ctrl_t *buff)
1203 {
1204 	int ret = PQI_STATUS_SUCCESS;
1205 	pqisrc_raid_req_t request;
1206 
1207 	DBG_FUNC("IN\n");
1208 
1209 	memset(&request, 0, sizeof(request));
1210 
1211 	request.data_direction = SOP_DATA_DIR_TO_DEVICE;
1212 	request.cmd.bmic_cdb.op_code = BMIC_READ;
1213 	request.cmd.bmic_cdb.cmd = BMIC_IDENTIFY_CONTROLLER;
1214 	request.cmd.bmic_cdb.xfer_len = BE_16(sizeof(*buff));
1215 
1216 	ret = pqisrc_prepare_send_ctrlr_request(softs, &request, buff, sizeof(*buff));
1217 
1218 	DBG_FUNC("OUT\n");
1219 
1220 	return ret;
1221 }
1222 
1223 /* Get the adapter FW version using BMIC_IDENTIFY_CONTROLLER */
1224 int
1225 pqisrc_get_ctrl_fw_version(pqisrc_softstate_t *softs)
1226 {
1227 	int ret = PQI_STATUS_SUCCESS;
1228 	bmic_ident_ctrl_t *identify_ctrl;
1229 
1230 	DBG_FUNC("IN\n");
1231 
1232 	identify_ctrl = os_mem_alloc(softs, sizeof(*identify_ctrl));
1233 	if (!identify_ctrl) {
1234 		DBG_ERR("failed to allocate memory for identify_ctrl\n");
1235 		return PQI_STATUS_FAILURE;
1236 	}
1237 
1238 	memset(identify_ctrl, 0, sizeof(*identify_ctrl));
1239 
1240 	ret = pqisrc_identify_ctrl(softs, identify_ctrl);
1241 	if (ret)
1242 		goto out;
1243 
1244 	softs->fw_build_number = identify_ctrl->fw_build_number;
1245 	memcpy(softs->fw_version, identify_ctrl->fw_version,
1246 		sizeof(identify_ctrl->fw_version));
1247 	softs->fw_version[sizeof(identify_ctrl->fw_version)] = '\0';
1248 	snprintf(softs->fw_version +
1249 		strlen(softs->fw_version),
1250 		sizeof(softs->fw_version),
1251 		"-%u", identify_ctrl->fw_build_number);
1252 out:
1253 	os_mem_free(softs, (char *)identify_ctrl, sizeof(*identify_ctrl));
1254 	DBG_NOTE("Firmware version: %s Firmware build number: %d\n", softs->fw_version, softs->fw_build_number);
1255 	DBG_FUNC("OUT\n");
1256 	return ret;
1257 }
1258 
1259 /* BMIC command to determine scsi device identify params */
1260 static int
1261 pqisrc_identify_physical_disk(pqisrc_softstate_t *softs,
1262 	pqi_scsi_dev_t *device,
1263 	bmic_ident_physdev_t *buff,
1264 	int buf_len)
1265 {
1266 	int ret = PQI_STATUS_SUCCESS;
1267 	uint16_t bmic_device_index;
1268 	pqisrc_raid_req_t request;
1269 
1270 
1271 	DBG_FUNC("IN\n");
1272 
1273 	memset(&request, 0, sizeof(request));
1274 	bmic_device_index = BMIC_GET_DRIVE_NUMBER(device->scsi3addr);
1275 
1276 	request.data_direction = SOP_DATA_DIR_TO_DEVICE;
1277 	request.cmd.bmic_cdb.op_code = BMIC_READ;
1278 	request.cmd.bmic_cdb.cmd = BMIC_IDENTIFY_PHYSICAL_DEVICE;
1279 	request.cmd.bmic_cdb.xfer_len = BE_16(buf_len);
1280 	request.cmd.cdb[2] = (uint8_t)bmic_device_index;
1281 	request.cmd.cdb[9] = (uint8_t)(bmic_device_index >> 8);
1282 
1283 	ret = pqisrc_prepare_send_ctrlr_request(softs, &request, buff, buf_len);
1284 
1285 	DBG_FUNC("OUT\n");
1286 	return ret;
1287 }
1288 
1289 /*
1290  * Function used to get the scsi device information using one of BMIC
1291  * BMIC_IDENTIFY_PHYSICAL_DEVICE
1292  */
1293 static void
1294 pqisrc_get_physical_device_info(pqisrc_softstate_t *softs,
1295 	pqi_scsi_dev_t *device,
1296 	bmic_ident_physdev_t *id_phys)
1297 {
1298 	int ret = PQI_STATUS_SUCCESS;
1299 
1300 	DBG_FUNC("IN\n");
1301 	memset(id_phys, 0, sizeof(*id_phys));
1302 
1303 	ret= pqisrc_identify_physical_disk(softs, device,
1304 		id_phys, sizeof(*id_phys));
1305 	if (ret) {
1306 		device->queue_depth = PQI_PHYSICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1307 		return;
1308 	}
1309 
1310 	device->queue_depth =
1311 		LE_16(id_phys->current_queue_depth_limit);
1312 	device->device_type = id_phys->device_type;
1313 	device->active_path_index = id_phys->active_path_number;
1314 	device->path_map = id_phys->redundant_path_present_map;
1315 	memcpy(&device->box,
1316 		&id_phys->alternate_paths_phys_box_on_port,
1317 		sizeof(device->box));
1318 	memcpy(&device->phys_connector,
1319 		&id_phys->alternate_paths_phys_connector,
1320 		sizeof(device->phys_connector));
1321 	device->bay = id_phys->phys_bay_in_box;
1322 	if (id_phys->multi_lun_device_lun_count) {
1323 		device->is_multi_lun = true;
1324 	}
1325 
1326 	DBG_DISC("BMIC DEV_TYPE: %x QUEUE DEPTH: 0x%x \n",  device->device_type, device->queue_depth);
1327 	DBG_FUNC("OUT\n");
1328 }
1329 
1330 
1331 /* Function used to find the entry of the device in a list */
1332 static device_status_t
1333 pqisrc_scsi_find_entry(pqisrc_softstate_t *softs,
1334 	pqi_scsi_dev_t *device_to_find,	pqi_scsi_dev_t **same_device)
1335 {
1336 	pqi_scsi_dev_t *device;
1337 	int i;
1338 	DBG_FUNC("IN\n");
1339 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
1340 		device = softs->dev_list[i];
1341 		if(device == NULL)
1342 			continue;
1343 		if (pqisrc_scsi3addr_equal(device_to_find->scsi3addr,
1344 			device->scsi3addr)) {
1345 			*same_device = device;
1346 			if (device->in_remove == true)
1347 				return DEVICE_IN_REMOVE;
1348 			if (pqisrc_device_equal(device_to_find, device)) {
1349 				if (device_to_find->volume_offline)
1350 					return DEVICE_CHANGED;
1351 				return DEVICE_UNCHANGED;
1352 			}
1353 			return DEVICE_CHANGED;
1354 		}
1355 	}
1356 	DBG_FUNC("OUT\n");
1357 
1358 	return DEVICE_NOT_FOUND;
1359 }
1360 
1361 
1362 /* Update the newly added devices as existed device */
1363 static void
1364 pqisrc_exist_device_update(pqisrc_softstate_t *softs,
1365 	pqi_scsi_dev_t *device_exist, pqi_scsi_dev_t *new_device)
1366 {
1367 	DBG_FUNC("IN\n");
1368 	device_exist->expose_device = new_device->expose_device;
1369 	memcpy(device_exist->vendor, new_device->vendor,
1370 		sizeof(device_exist->vendor));
1371 	memcpy(device_exist->model, new_device->model,
1372 		sizeof(device_exist->model));
1373 	device_exist->is_physical_device = new_device->is_physical_device;
1374 	device_exist->is_external_raid_device =
1375 		new_device->is_external_raid_device;
1376 	/* Whenever a logical device expansion happens, reprobe of
1377 	 * all existing LDs will be triggered, which is resulting
1378 	 * in updating the size to the os. */
1379 	if ((softs->ld_rescan) && (pqisrc_is_logical_device(device_exist))) {
1380 		device_exist->scsi_rescan = true;
1381 	}
1382 
1383 	device_exist->sas_address = new_device->sas_address;
1384 	device_exist->raid_level = new_device->raid_level;
1385 	device_exist->queue_depth = new_device->queue_depth;
1386 	device_exist->ioaccel_handle = new_device->ioaccel_handle;
1387 	device_exist->volume_status = new_device->volume_status;
1388 	device_exist->active_path_index = new_device->active_path_index;
1389 	device_exist->path_map = new_device->path_map;
1390 	device_exist->bay = new_device->bay;
1391 	memcpy(device_exist->box, new_device->box,
1392 		sizeof(device_exist->box));
1393 	memcpy(device_exist->phys_connector, new_device->phys_connector,
1394 		sizeof(device_exist->phys_connector));
1395 	device_exist->offload_config = new_device->offload_config;
1396 	device_exist->offload_enabled_pending =
1397 		new_device->offload_enabled_pending;
1398 	if (device_exist->offload_to_mirror)
1399 		os_mem_free(softs,
1400 			(int *) device_exist->offload_to_mirror,
1401 			sizeof(*(device_exist->offload_to_mirror)));
1402 	device_exist->offload_to_mirror = new_device->offload_to_mirror;
1403 	if (device_exist->raid_map)
1404 		os_mem_free(softs,
1405 			(char *)device_exist->raid_map,
1406 			sizeof(*device_exist->raid_map));
1407 	device_exist->raid_map = new_device->raid_map;
1408 	/* To prevent these from being freed later. */
1409 	new_device->raid_map = NULL;
1410 	new_device->offload_to_mirror = NULL;
1411 	DBG_FUNC("OUT\n");
1412 }
1413 
1414 /* Function used to add a scsi device to OS scsi subsystem */
1415 static int
1416 pqisrc_add_device(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1417 {
1418 	DBG_FUNC("IN\n");
1419 	DBG_NOTE("vendor: %s model: %s bus:%d target:%d lun:%d is_physical_device:0x%x expose_device:0x%x volume_offline 0x%x volume_status 0x%x \n",
1420 		device->vendor, device->model, device->bus, device->target, device->lun, device->is_physical_device, device->expose_device, device->volume_offline, device->volume_status);
1421 
1422 	device->invalid = false;
1423 	device->schedule_rescan = false;
1424 	device->softs = softs;
1425 	device->in_remove = false;
1426 
1427 	if(device->expose_device) {
1428 		pqisrc_init_device_active_io(softs, device);
1429 		/* TBD: Call OS upper layer function to add the device entry */
1430 		os_add_device(softs,device);
1431 	}
1432 	DBG_FUNC("OUT\n");
1433 	return PQI_STATUS_SUCCESS;
1434 
1435 }
1436 
1437 /* Function used to remove a scsi device from OS scsi subsystem */
1438 void
1439 pqisrc_remove_device(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1440 {
1441 	DBG_FUNC("IN\n");
1442 	DBG_NOTE("vendor: %s model: %s bus:%d target:%d lun:%d is_physical_device:0x%x expose_device:0x%x volume_offline 0x%x volume_status 0x%x \n",
1443 		device->vendor, device->model, device->bus, device->target, device->lun, device->is_physical_device, device->expose_device, device->volume_offline, device->volume_status);
1444 	device->invalid = true;
1445 	if (device->expose_device == false) {
1446 		/*Masked physical devices are not been exposed to storage stack.
1447 		*Hence, free the masked device resources such as
1448 		*device memory, Target ID,etc., here.
1449 		*/
1450 		DBG_NOTE("Deallocated Masked Device Resources.\n");
1451 		/* softs->device_list[device->target][device->lun] = NULL; */
1452 		pqisrc_free_device(softs,device);
1453 		return;
1454 	}
1455 	/* Wait for device outstanding Io's */
1456 	pqisrc_wait_for_device_commands_to_complete(softs, device);
1457 	/* Call OS upper layer function to remove the exposed device entry */
1458 	os_remove_device(softs,device);
1459 	DBG_FUNC("OUT\n");
1460 }
1461 
1462 
1463 /*
1464  * When exposing new device to OS fails then adjst list according to the
1465  * mid scsi list
1466  */
1467 static void
1468 pqisrc_adjust_list(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1469 {
1470 	int i;
1471 	unsigned char addr1[8], addr2[8];
1472 	pqi_scsi_dev_t *temp_device;
1473 	DBG_FUNC("IN\n");
1474 
1475 	if (!device) {
1476 		DBG_ERR("softs = %p: device is NULL !!!\n", softs);
1477 		return;
1478 	}
1479 
1480 	OS_ACQUIRE_SPINLOCK(&softs->devlist_lock);
1481 	uint8_t *scsi3addr;
1482 	/*For external raid device, there can be multiple luns
1483 	 *with same target. So while freeing external raid device,
1484 	 *free target only after removing all luns with same target.*/
1485 	if (pqisrc_is_external_raid_device(device)) {
1486 		memcpy(addr1, device->scsi3addr, 8);
1487 		for(i = 0; i < PQI_MAX_DEVICES; i++) {
1488 			if(softs->dev_list[i] == NULL)
1489 				continue;
1490 			temp_device = softs->dev_list[i];
1491 			memcpy(addr2, temp_device->scsi3addr, 8);
1492 			if(memcmp(addr1, addr2, 8) == 0)  {
1493 				continue;
1494 			}
1495 			if (addr1[2] == addr2[2]) {
1496 				break;
1497 			}
1498                 }
1499 		if(i == PQI_MAX_DEVICES) {
1500 			pqisrc_remove_target_bit(softs, device->target);
1501 		}
1502 	}
1503 
1504 	if(pqisrc_delete_softs_entry(softs, device) == PQI_STATUS_SUCCESS){
1505 		scsi3addr = device->scsi3addr;
1506 		if (!pqisrc_is_logical_device(device) && !MASKED_DEVICE(scsi3addr)){
1507 			DBG_NOTE("About to remove target bit %d \n", device->target);
1508 			pqisrc_remove_target_bit(softs, device->target);
1509 		}
1510 	}
1511 	OS_RELEASE_SPINLOCK(&softs->devlist_lock);
1512 	pqisrc_device_mem_free(softs, device);
1513 
1514 	DBG_FUNC("OUT\n");
1515 }
1516 
1517 /* Debug routine used to display the RAID volume status of the device */
1518 static void
1519 pqisrc_display_volume_status(pqisrc_softstate_t *softs,	pqi_scsi_dev_t *device)
1520 {
1521 	char *status;
1522 
1523 	DBG_FUNC("IN\n");
1524 	switch (device->volume_status) {
1525 	case SA_LV_OK:
1526 		status = "Volume is online.";
1527 		break;
1528 	case SA_LV_UNDERGOING_ERASE:
1529 		status = "Volume is undergoing background erase process.";
1530 		break;
1531 	case SA_LV_NOT_AVAILABLE:
1532 		status = "Volume is waiting for transforming volume.";
1533 		break;
1534 	case SA_LV_UNDERGOING_RPI:
1535 		status = "Volume is undergoing rapid parity initialization process.";
1536 		break;
1537 	case SA_LV_PENDING_RPI:
1538 		status = "Volume is queued for rapid parity initialization process.";
1539 		break;
1540 	case SA_LV_ENCRYPTED_NO_KEY:
1541 		status = "Volume is encrypted and cannot be accessed because key is not present.";
1542 		break;
1543 	case SA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
1544 		status = "Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.";
1545 		break;
1546 	case SA_LV_UNDERGOING_ENCRYPTION:
1547 		status = "Volume is undergoing encryption process.";
1548 		break;
1549 	case SA_LV_UNDERGOING_ENCRYPTION_REKEYING:
1550 		status = "Volume is undergoing encryption re-keying process.";
1551 		break;
1552 	case SA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
1553 		status = "Volume is encrypted and cannot be accessed because controller does not have encryption enabled.";
1554 		break;
1555 	case SA_LV_PENDING_ENCRYPTION:
1556 		status = "Volume is pending migration to encrypted state, but process has not started.";
1557 		break;
1558 	case SA_LV_PENDING_ENCRYPTION_REKEYING:
1559 		status = "Volume is encrypted and is pending encryption rekeying.";
1560 		break;
1561 	case SA_LV_STATUS_VPD_UNSUPPORTED:
1562 		status = "Volume status is not available through vital product data pages.";
1563 		break;
1564 	case SA_LV_UNDERGOING_EXPANSION:
1565 		status = "Volume undergoing expansion";
1566 		break;
1567 	case SA_LV_QUEUED_FOR_EXPANSION:
1568 		status = "Volume queued for expansion";
1569 		break;
1570 	case SA_LV_EJECTED:
1571 		status = "Volume ejected";
1572 		break;
1573 	case SA_LV_WRONG_PHYSICAL_DRIVE_REPLACED:
1574 		status = "Volume has wrong physical drive replaced";
1575 		break;
1576 	case SA_LV_DISABLED_SCSI_ID_CONFLICT:
1577 		status = "Volume disabled scsi id conflict";
1578 		break;
1579 	case SA_LV_HARDWARE_HAS_OVERHEATED:
1580 		status = "Volume hardware has over heated";
1581 		break;
1582 	case SA_LV_HARDWARE_OVERHEATING:
1583 		status = "Volume hardware over heating";
1584 		break;
1585 	case SA_LV_PHYSICAL_DRIVE_CONNECTION_PROBLEM:
1586 		status = "Volume physical drive connection problem";
1587 		break;
1588 	default:
1589 		status = "Volume is in an unknown state.";
1590 		break;
1591 	}
1592 
1593 	DBG_NOTE("scsi BTL %d:%d:%d %s\n",
1594 		device->bus, device->target, device->lun, status);
1595 	DBG_FUNC("OUT\n");
1596 }
1597 
1598 void
1599 pqisrc_device_mem_free(pqisrc_softstate_t *softs, pqi_scsi_dev_t *device)
1600 {
1601 	DBG_FUNC("IN\n");
1602 	if (!device)
1603 		return;
1604 	if (device->raid_map) {
1605 			os_mem_free(softs, (char *)device->raid_map, sizeof(pqisrc_raid_map_t));
1606 	}
1607 	if (device->offload_to_mirror) {
1608 		os_mem_free(softs, (int *)device->offload_to_mirror, sizeof(*(device->offload_to_mirror)));
1609 	}
1610 	os_mem_free(softs, (char *)device,sizeof(*device));
1611 	DBG_FUNC("OUT\n");
1612 
1613 }
1614 
1615 /* OS should call this function to free the scsi device */
1616 void
1617 pqisrc_free_device(pqisrc_softstate_t * softs, pqi_scsi_dev_t *device)
1618 {
1619 	rcb_t *rcb;
1620 	uint8_t *scsi3addr;
1621 	int i, index;
1622 	pqi_scsi_dev_t *temp_device;
1623 	unsigned char addr1[8], addr2[8];
1624 	/* Clear the "device" field in the rcb.
1625 	 * Response coming after device removal shouldn't access this field
1626 	 */
1627 	for(i = 1; i <= softs->max_outstanding_io; i++)
1628 	{
1629 		rcb = &softs->rcb[i];
1630 		if(rcb->dvp == device) {
1631 			DBG_WARN("Pending requests for the removing device\n");
1632 			rcb->dvp = NULL;
1633 		}
1634 	}
1635 	/* Find the entry in device list for the freed device softs->dev_list[i]&
1636 	 *make it NULL before freeing the device memory
1637 	 */
1638 	index = pqisrc_find_device_list_index(softs, device);
1639 
1640 	OS_ACQUIRE_SPINLOCK(&softs->devlist_lock);
1641 	scsi3addr = device->scsi3addr;
1642 	if (!pqisrc_is_logical_device(device) && !MASKED_DEVICE(scsi3addr)) {
1643 		DBG_NOTE("Giving back target %i \n", device->target);
1644 		pqisrc_remove_target_bit(softs, device->target);
1645 	}
1646 	/*For external raid device, there can be multiple luns
1647 	 *with same target. So while freeing external raid device,
1648 	 *free target only after removing all luns with same target.*/
1649 	if (pqisrc_is_external_raid_device(device)) {
1650 		memcpy(addr1, device->scsi3addr, 8);
1651 		for(i = 0; i < PQI_MAX_DEVICES; i++) {
1652 			if(softs->dev_list[i] == NULL)
1653 				continue;
1654 			temp_device = softs->dev_list[i];
1655 			memcpy(addr2, temp_device->scsi3addr, 8);
1656 			if(memcmp(addr1, addr2, 8) == 0)  {
1657 				continue;
1658 			}
1659 			if (addr1[2] == addr2[2]) {
1660 				break;
1661 			}
1662 		}
1663 		if(i == PQI_MAX_DEVICES) {
1664 			 pqisrc_remove_target_bit(softs, device->target);
1665 		}
1666 	}
1667 
1668 	if (index >= 0 && index < PQI_MAX_DEVICES)
1669 		softs->dev_list[index] = NULL;
1670 	if (device->expose_device == true){
1671 		pqisrc_delete_softs_entry(softs, device);
1672 		DBG_NOTE("Removed memory for device : B %d: T %d: L %d\n",
1673 			device->bus, device->target, device->lun);
1674 		OS_RELEASE_SPINLOCK(&softs->devlist_lock);
1675 		pqisrc_device_mem_free(softs, device);
1676 	} else {
1677 		OS_RELEASE_SPINLOCK(&softs->devlist_lock);
1678 	}
1679 }
1680 
1681 
1682 /* Update the newly added devices to the device list */
1683 static void
1684 pqisrc_update_device_list(pqisrc_softstate_t *softs,
1685 	pqi_scsi_dev_t *new_device_list[], int num_new_devices)
1686 {
1687 	int ret;
1688 	int i;
1689 	device_status_t dev_status;
1690 	pqi_scsi_dev_t *device;
1691 	pqi_scsi_dev_t *same_device;
1692 	pqi_scsi_dev_t **added = NULL;
1693 	pqi_scsi_dev_t **removed = NULL;
1694 	int nadded = 0, nremoved = 0;
1695 	uint8_t *scsi3addr;
1696 
1697 	DBG_FUNC("IN\n");
1698 
1699 	added = os_mem_alloc(softs, sizeof(*added) * PQI_MAX_DEVICES);
1700 	removed = os_mem_alloc(softs, sizeof(*removed) * PQI_MAX_DEVICES);
1701 
1702 	if (!added || !removed) {
1703 		DBG_WARN("Out of memory \n");
1704 		goto free_and_out;
1705 	}
1706 
1707 	OS_ACQUIRE_SPINLOCK(&softs->devlist_lock);
1708 
1709 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
1710 		if(softs->dev_list[i] == NULL)
1711 			continue;
1712 		device = softs->dev_list[i];
1713 		device->device_gone = true;
1714 	}
1715 
1716 	/* TODO:Remove later */
1717 	DBG_IO("Device list used an array\n");
1718 	for (i = 0; i < num_new_devices; i++) {
1719 		device = new_device_list[i];
1720 
1721 		dev_status = pqisrc_scsi_find_entry(softs, device,
1722 			&same_device);
1723 
1724 		switch (dev_status) {
1725 		case DEVICE_UNCHANGED:
1726 			/* New Device present in existing device list  */
1727 			device->new_device = false;
1728 			same_device->device_gone = false;
1729 			pqisrc_exist_device_update(softs, same_device, device);
1730 			break;
1731 		case DEVICE_NOT_FOUND:
1732 			/* Device not found in existing list */
1733 			device->new_device = true;
1734 			break;
1735 		case DEVICE_CHANGED:
1736 			/* Actual device gone need to add device to list*/
1737 			device->new_device = true;
1738 			break;
1739 		case DEVICE_IN_REMOVE:
1740 			/*Older device with same target/lun is in removal stage*/
1741 			/*New device will be added/scanned when same target/lun
1742 			 * device_list[] gets removed from the OS target
1743 			 * free call*/
1744 			device->new_device = false;
1745 			same_device->schedule_rescan = true;
1746 			break;
1747 		default:
1748 			break;
1749 		}
1750 	}
1751 
1752 	/* Process all devices that have gone away. */
1753 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
1754 		device = softs->dev_list[i];
1755 		if(device == NULL)
1756 			continue;
1757 		if (device->device_gone) {
1758 			if(device->in_remove == true)
1759          {
1760 				continue;
1761 			}
1762 			device->in_remove = true;
1763 			removed[nremoved] = device;
1764 			softs->num_devs--;
1765 			nremoved++;
1766 		}
1767 	}
1768 
1769 	/* Process all new devices. */
1770 	for (i = 0, nadded = 0; i < num_new_devices; i++) {
1771 		device = new_device_list[i];
1772 		if (!device->new_device)
1773 			continue;
1774 		if (device->volume_offline)
1775 			continue;
1776 
1777 		/* Find out which devices to add to the driver list
1778 		*  in softs->dev_list */
1779 		scsi3addr = device->scsi3addr;
1780 		if (device->expose_device || !MASKED_DEVICE(scsi3addr)){
1781 			if(pqisrc_add_softs_entry(softs, device, scsi3addr)){
1782 				/* To prevent this entry from being freed later. */
1783 				new_device_list[i] = NULL;
1784 				added[nadded] = device;
1785 				nadded++;
1786 			}
1787 		}
1788 
1789 	}
1790 
1791 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
1792 		device = softs->dev_list[i];
1793 		if(device == NULL)
1794 			continue;
1795 		if (device->offload_enabled != device->offload_enabled_pending)
1796 		{
1797 			DBG_NOTE("[%d:%d:%d]Changing AIO to %d (was %d)\n",
1798 				device->bus, device->target, device->lun,
1799 				device->offload_enabled_pending,
1800 				device->offload_enabled);
1801 		}
1802 		device->offload_enabled = device->offload_enabled_pending;
1803 	}
1804 
1805 	OS_RELEASE_SPINLOCK(&softs->devlist_lock);
1806 
1807 	for(i = 0; i < nremoved; i++) {
1808 		device = removed[i];
1809 		if (device == NULL)
1810 			continue;
1811 		pqisrc_display_device_info(softs, "removed", device);
1812 		pqisrc_remove_device(softs, device);
1813 	}
1814 
1815 	OS_ACQUIRE_SPINLOCK(&softs->devlist_lock);
1816 
1817 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
1818 		if(softs->dev_list[i] == NULL)
1819 			continue;
1820 		device = softs->dev_list[i];
1821 		if (device->in_remove)
1822 			continue;
1823 		/*
1824 		* If firmware queue depth is corrupt or not working
1825 		* use the PQI_LOGICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH
1826 		* which is 0. That means there is no limit to the
1827 		* queue depth all the way up to the controller
1828 		* queue depth
1829 		*/
1830 		if (pqisrc_is_logical_device(device) &&
1831 				device->firmware_queue_depth_set == false)
1832 			device->queue_depth = PQI_LOGICAL_DISK_DEFAULT_MAX_QUEUE_DEPTH;
1833 
1834 		if (device->scsi_rescan) {
1835 			os_rescan_target(softs, device);
1836 		}
1837 	}
1838 	softs->ld_rescan = false;
1839 
1840 	OS_RELEASE_SPINLOCK(&softs->devlist_lock);
1841 
1842 	for(i = 0; i < nadded; i++) {
1843 		device = added[i];
1844 		if (device->expose_device) {
1845 			ret = pqisrc_add_device(softs, device);
1846 			if (ret) {
1847 				DBG_WARN("scsi %d:%d:%d addition failed, device not added\n",
1848 					device->bus, device->target, device->lun);
1849 				pqisrc_adjust_list(softs, device);
1850 				continue;
1851 			}
1852 		}
1853 
1854 		pqisrc_display_device_info(softs, "added", device);
1855 	}
1856 
1857 	/* Process all volumes that are offline. */
1858 	for (i = 0; i < num_new_devices; i++) {
1859 		device = new_device_list[i];
1860 		if (!device)
1861 			continue;
1862 		if (!device->new_device)
1863 			continue;
1864 		if (device->volume_offline) {
1865 			pqisrc_display_volume_status(softs, device);
1866 			pqisrc_display_device_info(softs, "offline", device);
1867 		}
1868 	}
1869 
1870 	for (i = 0; i < PQI_MAX_DEVICES; i++) {
1871 		device = softs->dev_list[i];
1872 		if(device == NULL)
1873 			continue;
1874 		DBG_DISC("Current device %d : B%d:T%d:L%d\n",
1875 			i, device->bus, device->target,
1876 			device->lun);
1877 	}
1878 
1879 free_and_out:
1880 	if (added)
1881 		os_mem_free(softs, (char *)added,
1882 			    sizeof(*added) * PQI_MAX_DEVICES);
1883 	if (removed)
1884 		os_mem_free(softs, (char *)removed,
1885 			    sizeof(*removed) * PQI_MAX_DEVICES);
1886 
1887 	DBG_FUNC("OUT\n");
1888 }
1889 
1890 /*
1891  * Let the Adapter know about driver version using one of BMIC
1892  * BMIC_WRITE_HOST_WELLNESS
1893  */
1894 int
1895 pqisrc_write_driver_version_to_host_wellness(pqisrc_softstate_t *softs)
1896 {
1897 	int rval = PQI_STATUS_SUCCESS;
1898 	struct bmic_host_wellness_driver_version *host_wellness_driver_ver;
1899 	size_t data_length;
1900 	pqisrc_raid_req_t request;
1901 
1902 	DBG_FUNC("IN\n");
1903 
1904 	memset(&request, 0, sizeof(request));
1905 	data_length = sizeof(*host_wellness_driver_ver);
1906 
1907 	host_wellness_driver_ver = os_mem_alloc(softs, data_length);
1908 	if (!host_wellness_driver_ver) {
1909 		DBG_ERR("failed to allocate memory for host wellness driver_version\n");
1910 		return PQI_STATUS_FAILURE;
1911 	}
1912 
1913 	host_wellness_driver_ver->start_tag[0] = '<';
1914 	host_wellness_driver_ver->start_tag[1] = 'H';
1915 	host_wellness_driver_ver->start_tag[2] = 'W';
1916 	host_wellness_driver_ver->start_tag[3] = '>';
1917 	host_wellness_driver_ver->driver_version_tag[0] = 'D';
1918 	host_wellness_driver_ver->driver_version_tag[1] = 'V';
1919 	host_wellness_driver_ver->driver_version_length = LE_16(sizeof(host_wellness_driver_ver->driver_version));
1920 	strncpy(host_wellness_driver_ver->driver_version, softs->os_name,
1921         sizeof(host_wellness_driver_ver->driver_version));
1922     if (strlen(softs->os_name) < sizeof(host_wellness_driver_ver->driver_version) ) {
1923         strncpy(host_wellness_driver_ver->driver_version + strlen(softs->os_name), PQISRC_DRIVER_VERSION,
1924 			sizeof(host_wellness_driver_ver->driver_version) -  strlen(softs->os_name));
1925     } else {
1926         DBG_DISC("OS name length(%u) is longer than buffer of driver_version\n",
1927             (unsigned int)strlen(softs->os_name));
1928 
1929     }
1930 	host_wellness_driver_ver->driver_version[sizeof(host_wellness_driver_ver->driver_version) - 1] = '\0';
1931 	host_wellness_driver_ver->end_tag[0] = 'Z';
1932 	host_wellness_driver_ver->end_tag[1] = 'Z';
1933 
1934 
1935 	request.data_direction = SOP_DATA_DIR_FROM_DEVICE;
1936 	request.cmd.bmic_cdb.op_code = BMIC_WRITE;
1937 	request.cmd.bmic_cdb.cmd = BMIC_WRITE_HOST_WELLNESS;
1938 	request.cmd.bmic_cdb.xfer_len = BE_16(data_length);
1939 
1940 	rval = pqisrc_prepare_send_ctrlr_request(softs, &request, host_wellness_driver_ver, data_length);
1941 
1942 	os_mem_free(softs, (char *)host_wellness_driver_ver, data_length);
1943 
1944 	DBG_FUNC("OUT");
1945 	return rval;
1946 }
1947 
1948 /*
1949  * Write current RTC time from host to the adapter using
1950  * BMIC_WRITE_HOST_WELLNESS
1951  */
1952 int
1953 pqisrc_write_current_time_to_host_wellness(pqisrc_softstate_t *softs)
1954 {
1955 	int rval = PQI_STATUS_SUCCESS;
1956 	struct bmic_host_wellness_time *host_wellness_time;
1957 	size_t data_length;
1958 	pqisrc_raid_req_t request;
1959 
1960 	DBG_FUNC("IN\n");
1961 
1962 	memset(&request, 0, sizeof(request));
1963 	data_length = sizeof(*host_wellness_time);
1964 
1965 	host_wellness_time = os_mem_alloc(softs, data_length);
1966 	if (!host_wellness_time) {
1967 		DBG_ERR("failed to allocate memory for host wellness time structure\n");
1968 		return PQI_STATUS_FAILURE;
1969 	}
1970 
1971 	host_wellness_time->start_tag[0] = '<';
1972 	host_wellness_time->start_tag[1] = 'H';
1973 	host_wellness_time->start_tag[2] = 'W';
1974 	host_wellness_time->start_tag[3] = '>';
1975 	host_wellness_time->time_tag[0] = 'T';
1976 	host_wellness_time->time_tag[1] = 'D';
1977 	host_wellness_time->time_length = LE_16(offsetof(struct bmic_host_wellness_time, time_length) -
1978 											offsetof(struct bmic_host_wellness_time, century));
1979 
1980 	os_get_time(host_wellness_time);
1981 
1982 	host_wellness_time->dont_write_tag[0] = 'D';
1983 	host_wellness_time->dont_write_tag[1] = 'W';
1984 	host_wellness_time->end_tag[0] = 'Z';
1985 	host_wellness_time->end_tag[1] = 'Z';
1986 
1987 
1988 	request.data_direction = SOP_DATA_DIR_FROM_DEVICE;
1989 	request.cmd.bmic_cdb.op_code = BMIC_WRITE;
1990 	request.cmd.bmic_cdb.cmd = BMIC_WRITE_HOST_WELLNESS;
1991 	request.cmd.bmic_cdb.xfer_len = BE_16(data_length);
1992 
1993 	rval = pqisrc_prepare_send_ctrlr_request(softs, &request, host_wellness_time, data_length);
1994 
1995 	os_mem_free(softs, (char *)host_wellness_time, data_length);
1996 
1997 	DBG_FUNC("OUT");
1998 	return rval;
1999 }
2000 static void
2001 pqisrc_get_device_vpd_info(pqisrc_softstate_t *softs,
2002 		bmic_ident_physdev_t *bmic_phy_info,pqi_scsi_dev_t *device)
2003 {
2004 	DBG_FUNC("IN\n");
2005         memcpy(&device->wwid, &bmic_phy_info->padding[79], sizeof(device->wwid));
2006         DBG_FUNC("OUT\n");
2007 }
2008 /*
2009  * Function used to perform a rescan of scsi devices
2010  * for any config change events
2011  */
2012 int
2013 pqisrc_scan_devices(pqisrc_softstate_t *softs)
2014 {
2015 	boolean_t is_physical_device;
2016 	int ret;
2017 	int i;
2018 	int new_dev_cnt;
2019 	int phy_log_dev_cnt;
2020 	size_t queue_log_data_length;
2021 	uint8_t *scsi3addr;
2022 	uint8_t multiplier;
2023 	uint16_t qdepth;
2024 	uint32_t physical_cnt;
2025 	uint32_t logical_cnt;
2026 	uint32_t logical_queue_cnt;
2027 	uint32_t ndev_allocated = 0;
2028 	size_t phys_data_length, log_data_length;
2029 	reportlun_data_ext_t *physical_dev_list = NULL;
2030 	reportlun_data_ext_t *logical_dev_list = NULL;
2031 	reportlun_ext_entry_t *lun_ext_entry = NULL;
2032 	reportlun_queue_depth_data_t *logical_queue_dev_list = NULL;
2033 	bmic_ident_physdev_t *bmic_phy_info = NULL;
2034 	pqi_scsi_dev_t **new_device_list = NULL;
2035 	pqi_scsi_dev_t *device = NULL;
2036 #ifdef PQI_NEED_RESCAN_TIMER_FOR_RBOD_HOTPLUG
2037 	int num_ext_raid_devices = 0;
2038 #endif
2039 
2040 	DBG_FUNC("IN\n");
2041 
2042 	ret = pqisrc_get_phys_log_device_list(softs, &physical_dev_list, &logical_dev_list,
2043 					&logical_queue_dev_list, &queue_log_data_length,
2044 					&phys_data_length, &log_data_length);
2045 
2046 	if (ret)
2047 		goto err_out;
2048 
2049 	physical_cnt = BE_32(physical_dev_list->header.list_length)
2050 		/ sizeof(physical_dev_list->lun_entries[0]);
2051 
2052 	logical_cnt = BE_32(logical_dev_list->header.list_length)
2053 		/ sizeof(logical_dev_list->lun_entries[0]);
2054 
2055 	logical_queue_cnt = BE_32(logical_queue_dev_list->header.list_length)
2056                 / sizeof(logical_queue_dev_list->lun_entries[0]);
2057 
2058 
2059 	DBG_DISC("physical_cnt %u logical_cnt %u queue_cnt %u\n", physical_cnt, logical_cnt, logical_queue_cnt);
2060 
2061 	if (physical_cnt) {
2062 		bmic_phy_info = os_mem_alloc(softs, sizeof(*bmic_phy_info));
2063 		if (bmic_phy_info == NULL) {
2064 			ret = PQI_STATUS_FAILURE;
2065 			DBG_ERR("failed to allocate memory for BMIC ID PHYS Device : %d\n", ret);
2066 			goto err_out;
2067 		}
2068 	}
2069 	phy_log_dev_cnt = physical_cnt + logical_cnt;
2070 	new_device_list = os_mem_alloc(softs,
2071 				sizeof(*new_device_list) * phy_log_dev_cnt);
2072 
2073 	if (new_device_list == NULL) {
2074 		ret = PQI_STATUS_FAILURE;
2075 		DBG_ERR("failed to allocate memory for device list : %d\n", ret);
2076 		goto err_out;
2077 	}
2078 
2079 	for (i = 0; i < phy_log_dev_cnt; i++) {
2080 		new_device_list[i] = os_mem_alloc(softs,
2081 						sizeof(*new_device_list[i]));
2082 		if (new_device_list[i] == NULL) {
2083 			ret = PQI_STATUS_FAILURE;
2084 			DBG_ERR("failed to allocate memory for device list : %d\n", ret);
2085 			ndev_allocated = i;
2086 			goto err_out;
2087 		}
2088 	}
2089 
2090 	ndev_allocated = phy_log_dev_cnt;
2091 	new_dev_cnt = 0;
2092 	for (i = 0; i < phy_log_dev_cnt; i++) {
2093 
2094 		if (i < physical_cnt) {
2095 			is_physical_device = true;
2096 			lun_ext_entry = &physical_dev_list->lun_entries[i];
2097 		} else {
2098 			is_physical_device = false;
2099 			lun_ext_entry =
2100 				&logical_dev_list->lun_entries[i - physical_cnt];
2101 		}
2102 
2103 		scsi3addr = lun_ext_entry->lunid;
2104 
2105 		/* Save the target sas adderess for external raid device */
2106 		if(lun_ext_entry->device_type == CONTROLLER_DEVICE) {
2107 #ifdef PQI_NEED_RESCAN_TIMER_FOR_RBOD_HOTPLUG
2108 			num_ext_raid_devices++;
2109 #endif
2110 			int target = lun_ext_entry->lunid[3] & 0x3f;
2111 			softs->target_sas_addr[target] = BE_64(lun_ext_entry->wwid);
2112 		}
2113 
2114 		/* Skip masked physical non-disk devices. */
2115 		if (MASKED_DEVICE(scsi3addr) && is_physical_device
2116 				&& (lun_ext_entry->ioaccel_handle == 0))
2117 			continue;
2118 
2119 		device = new_device_list[new_dev_cnt];
2120 		memset(device, 0, sizeof(*device));
2121 		memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
2122 		device->wwid = lun_ext_entry->wwid;
2123 		device->is_physical_device = is_physical_device;
2124 		if (!is_physical_device && logical_queue_cnt--) {
2125 			device->is_external_raid_device =
2126 				pqisrc_is_external_raid_addr(scsi3addr);
2127 			/* The multiplier is the value we multiply the queue
2128  			 * depth value with to get the actual queue depth.
2129  			 * If multiplier is 1 multiply by 256 if
2130  			 * multiplier 0 then multiply by 16 */
2131 			multiplier = logical_queue_dev_list->lun_entries[i - physical_cnt].multiplier;
2132 			qdepth = logical_queue_dev_list->lun_entries[i - physical_cnt].queue_depth;
2133 			if (multiplier) {
2134 				device->firmware_queue_depth_set = true;
2135 				device->queue_depth = qdepth*256;
2136 			} else {
2137 				device->firmware_queue_depth_set = true;
2138 				device->queue_depth = qdepth*16;
2139 			}
2140 			if (device->queue_depth > softs->adapterQDepth) {
2141 				device->firmware_queue_depth_set = true;
2142 				device->queue_depth = softs->adapterQDepth;
2143 			}
2144 			if ((multiplier == 1) &&
2145 				(qdepth >= MAX_RAW_M256_QDEPTH))
2146 				device->firmware_queue_depth_set = false;
2147 			if ((multiplier == 0) &&
2148 				(qdepth >= MAX_RAW_M16_QDEPTH))
2149 				device->firmware_queue_depth_set = false;
2150 
2151 		}
2152 
2153 
2154 		/* Get device type, vendor, model, device ID. */
2155 		ret = pqisrc_get_dev_data(softs, device);
2156 		if (ret) {
2157 			DBG_WARN("Inquiry failed, skipping device %016llx\n",
2158 				 (unsigned long long)BE_64(device->scsi3addr[0]));
2159 			DBG_DISC("INQUIRY FAILED \n");
2160 			continue;
2161 		}
2162 		/* Set controller queue depth to what
2163  		 * it was from the scsi midlayer */
2164 		if (device->devtype == RAID_DEVICE) {
2165 			device->firmware_queue_depth_set = true;
2166 			device->queue_depth = softs->adapterQDepth;
2167 		}
2168 		pqisrc_assign_btl(softs, device);
2169 
2170 		/*
2171 		 * Expose all devices except for physical devices that
2172 		 * are masked.
2173 		 */
2174 		if (device->is_physical_device &&
2175 			MASKED_DEVICE(scsi3addr))
2176 			device->expose_device = false;
2177 		else
2178 			device->expose_device = true;
2179 
2180 		if (device->is_physical_device &&
2181 		    (lun_ext_entry->device_flags &
2182 		     REPORT_LUN_DEV_FLAG_AIO_ENABLED) &&
2183 		     lun_ext_entry->ioaccel_handle) {
2184 			device->aio_enabled = true;
2185 		}
2186 		switch (device->devtype) {
2187 		case ROM_DEVICE:
2188 			/*
2189 			 * We don't *really* support actual CD-ROM devices,
2190 			 * but we do support the HP "One Button Disaster
2191 			 * Recovery" tape drive which temporarily pretends to
2192 			 * be a CD-ROM drive.
2193 			 */
2194 			if (device->is_obdr_device)
2195 				new_dev_cnt++;
2196 			break;
2197 		case DISK_DEVICE:
2198 		case ZBC_DEVICE:
2199 			if (device->is_physical_device) {
2200 				device->ioaccel_handle =
2201 					lun_ext_entry->ioaccel_handle;
2202 				pqisrc_get_physical_device_info(softs, device,
2203 					bmic_phy_info);
2204 				if ( (!softs->page83id_in_rpl) && (bmic_phy_info->device_type == BMIC_DEVICE_TYPE_SATA)) {
2205 					pqisrc_get_device_vpd_info(softs, bmic_phy_info, device);
2206 				}
2207 				device->sas_address = BE_64(device->wwid);
2208 			}
2209 			new_dev_cnt++;
2210 			break;
2211 		case ENCLOSURE_DEVICE:
2212 			if (device->is_physical_device) {
2213 				device->sas_address = BE_64(lun_ext_entry->wwid);
2214 			}
2215 			new_dev_cnt++;
2216 			break;
2217 		case TAPE_DEVICE:
2218 		case MEDIUM_CHANGER_DEVICE:
2219 			new_dev_cnt++;
2220 			break;
2221 		case RAID_DEVICE:
2222 			/*
2223 			 * Only present the HBA controller itself as a RAID
2224 			 * controller.  If it's a RAID controller other than
2225 			 * the HBA itself (an external RAID controller, MSA500
2226 			 * or similar), don't present it.
2227 			 */
2228 			if (pqisrc_is_hba_lunid(scsi3addr))
2229 				new_dev_cnt++;
2230 			break;
2231 		case SES_DEVICE:
2232 		case CONTROLLER_DEVICE:
2233 		default:
2234 			break;
2235 		}
2236 	}
2237 	DBG_DISC("new_dev_cnt %d\n", new_dev_cnt);
2238 #ifdef PQI_NEED_RESCAN_TIMER_FOR_RBOD_HOTPLUG
2239 	if(num_ext_raid_devices)
2240 		os_start_rescan_timer(softs);
2241 	else
2242 		 os_stop_rescan_timer(softs);
2243 #endif
2244 	pqisrc_update_device_list(softs, new_device_list, new_dev_cnt);
2245 
2246 err_out:
2247 	if (new_device_list) {
2248 		for (i = 0; i < ndev_allocated; i++) {
2249 			if (new_device_list[i]) {
2250 				if(new_device_list[i]->raid_map)
2251 					os_mem_free(softs, (char *)new_device_list[i]->raid_map,
2252 					    					sizeof(pqisrc_raid_map_t));
2253 				os_mem_free(softs, (char*)new_device_list[i],
2254 					    			sizeof(*new_device_list[i]));
2255 			}
2256 		}
2257 		os_mem_free(softs, (char *)new_device_list,
2258 			    		sizeof(*new_device_list) * ndev_allocated);
2259 	}
2260 	if(physical_dev_list)
2261 		os_mem_free(softs, (char *)physical_dev_list, phys_data_length);
2262     	if(logical_dev_list)
2263 		os_mem_free(softs, (char *)logical_dev_list, log_data_length);
2264 	if(logical_queue_dev_list)
2265 		os_mem_free(softs, (char*)logical_queue_dev_list,
2266 			queue_log_data_length);
2267 	if (bmic_phy_info)
2268 		os_mem_free(softs, (char *)bmic_phy_info, sizeof(*bmic_phy_info));
2269 
2270 	DBG_FUNC("OUT \n");
2271 
2272 	return ret;
2273 }
2274 
2275 /*
2276  * Clean up memory allocated for devices.
2277  */
2278 void
2279 pqisrc_cleanup_devices(pqisrc_softstate_t *softs)
2280 {
2281 	int i = 0;
2282 	pqi_scsi_dev_t *device = NULL;
2283 	DBG_FUNC("IN\n");
2284 	for(i = 0; i < PQI_MAX_DEVICES; i++) {
2285 		if(softs->dev_list[i] == NULL)
2286 			continue;
2287 		device = softs->dev_list[i];
2288 		pqisrc_device_mem_free(softs, device);
2289 	}
2290 
2291 	DBG_FUNC("OUT\n");
2292 }
2293