xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_os.c (revision 80d0624d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2023 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
15 static int mrioc_ids;
16 static int warn_non_secure_ctlr;
17 atomic64_t event_counter;
18 
19 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
20 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
21 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
22 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
23 
24 /* Module parameters*/
25 int prot_mask = -1;
26 module_param(prot_mask, int, 0);
27 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
28 
29 static int prot_guard_mask = 3;
30 module_param(prot_guard_mask, int, 0);
31 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
32 static int logging_level;
33 module_param(logging_level, int, 0);
34 MODULE_PARM_DESC(logging_level,
35 	" bits for enabling additional logging info (default=0)");
36 static int max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
37 module_param(max_sgl_entries, int, 0444);
38 MODULE_PARM_DESC(max_sgl_entries,
39 	"Preferred max number of SG entries to be used for a single I/O\n"
40 	"The actual value will be determined by the driver\n"
41 	"(Minimum=256, Maximum=2048, default=256)");
42 
43 /* Forward declarations*/
44 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
45 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
46 
47 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION	(0xFFFF)
48 
49 #define MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH	(0xFFFE)
50 
51 /**
52  * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
53  * @mrioc: Adapter instance reference
54  * @scmd: SCSI command reference
55  *
56  * Calculate the host tag based on block tag for a given scmd.
57  *
58  * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
59  */
mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)60 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
61 	struct scsi_cmnd *scmd)
62 {
63 	struct scmd_priv *priv = NULL;
64 	u32 unique_tag;
65 	u16 host_tag, hw_queue;
66 
67 	unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
68 
69 	hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
70 	if (hw_queue >= mrioc->num_op_reply_q)
71 		return MPI3MR_HOSTTAG_INVALID;
72 	host_tag = blk_mq_unique_tag_to_tag(unique_tag);
73 
74 	if (WARN_ON(host_tag >= mrioc->max_host_ios))
75 		return MPI3MR_HOSTTAG_INVALID;
76 
77 	priv = scsi_cmd_priv(scmd);
78 	/*host_tag 0 is invalid hence incrementing by 1*/
79 	priv->host_tag = host_tag + 1;
80 	priv->scmd = scmd;
81 	priv->in_lld_scope = 1;
82 	priv->req_q_idx = hw_queue;
83 	priv->meta_chain_idx = -1;
84 	priv->chain_idx = -1;
85 	priv->meta_sg_valid = 0;
86 	return priv->host_tag;
87 }
88 
89 /**
90  * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
91  * @mrioc: Adapter instance reference
92  * @host_tag: Host tag
93  * @qidx: Operational queue index
94  *
95  * Identify the block tag from the host tag and queue index and
96  * retrieve associated scsi command using scsi_host_find_tag().
97  *
98  * Return: SCSI command reference or NULL.
99  */
mpi3mr_scmd_from_host_tag(struct mpi3mr_ioc * mrioc,u16 host_tag,u16 qidx)100 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
101 	struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
102 {
103 	struct scsi_cmnd *scmd = NULL;
104 	struct scmd_priv *priv = NULL;
105 	u32 unique_tag = host_tag - 1;
106 
107 	if (WARN_ON(host_tag > mrioc->max_host_ios))
108 		goto out;
109 
110 	unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
111 
112 	scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
113 	if (scmd) {
114 		priv = scsi_cmd_priv(scmd);
115 		if (!priv->in_lld_scope)
116 			scmd = NULL;
117 	}
118 out:
119 	return scmd;
120 }
121 
122 /**
123  * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
124  * @mrioc: Adapter instance reference
125  * @scmd: SCSI command reference
126  *
127  * Invalidate the SCSI command private data to mark the command
128  * is not in LLD scope anymore.
129  *
130  * Return: Nothing.
131  */
mpi3mr_clear_scmd_priv(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)132 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
133 	struct scsi_cmnd *scmd)
134 {
135 	struct scmd_priv *priv = NULL;
136 
137 	priv = scsi_cmd_priv(scmd);
138 
139 	if (WARN_ON(priv->in_lld_scope == 0))
140 		return;
141 	priv->host_tag = MPI3MR_HOSTTAG_INVALID;
142 	priv->req_q_idx = 0xFFFF;
143 	priv->scmd = NULL;
144 	priv->in_lld_scope = 0;
145 	priv->meta_sg_valid = 0;
146 	if (priv->chain_idx >= 0) {
147 		clear_bit(priv->chain_idx, mrioc->chain_bitmap);
148 		priv->chain_idx = -1;
149 	}
150 	if (priv->meta_chain_idx >= 0) {
151 		clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
152 		priv->meta_chain_idx = -1;
153 	}
154 }
155 
156 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
157 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
158 static void mpi3mr_fwevt_worker(struct work_struct *work);
159 
160 /**
161  * mpi3mr_fwevt_free - firmware event memory dealloctor
162  * @r: k reference pointer of the firmware event
163  *
164  * Free firmware event memory when no reference.
165  */
mpi3mr_fwevt_free(struct kref * r)166 static void mpi3mr_fwevt_free(struct kref *r)
167 {
168 	kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
169 }
170 
171 /**
172  * mpi3mr_fwevt_get - k reference incrementor
173  * @fwevt: Firmware event reference
174  *
175  * Increment firmware event reference count.
176  */
mpi3mr_fwevt_get(struct mpi3mr_fwevt * fwevt)177 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
178 {
179 	kref_get(&fwevt->ref_count);
180 }
181 
182 /**
183  * mpi3mr_fwevt_put - k reference decrementor
184  * @fwevt: Firmware event reference
185  *
186  * decrement firmware event reference count.
187  */
mpi3mr_fwevt_put(struct mpi3mr_fwevt * fwevt)188 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
189 {
190 	kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
191 }
192 
193 /**
194  * mpi3mr_alloc_fwevt - Allocate firmware event
195  * @len: length of firmware event data to allocate
196  *
197  * Allocate firmware event with required length and initialize
198  * the reference counter.
199  *
200  * Return: firmware event reference.
201  */
mpi3mr_alloc_fwevt(int len)202 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
203 {
204 	struct mpi3mr_fwevt *fwevt;
205 
206 	fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
207 	if (!fwevt)
208 		return NULL;
209 
210 	kref_init(&fwevt->ref_count);
211 	return fwevt;
212 }
213 
214 /**
215  * mpi3mr_fwevt_add_to_list - Add firmware event to the list
216  * @mrioc: Adapter instance reference
217  * @fwevt: Firmware event reference
218  *
219  * Add the given firmware event to the firmware event list.
220  *
221  * Return: Nothing.
222  */
mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)223 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
224 	struct mpi3mr_fwevt *fwevt)
225 {
226 	unsigned long flags;
227 
228 	if (!mrioc->fwevt_worker_thread)
229 		return;
230 
231 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
232 	/* get fwevt reference count while adding it to fwevt_list */
233 	mpi3mr_fwevt_get(fwevt);
234 	INIT_LIST_HEAD(&fwevt->list);
235 	list_add_tail(&fwevt->list, &mrioc->fwevt_list);
236 	INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
237 	/* get fwevt reference count while enqueueing it to worker queue */
238 	mpi3mr_fwevt_get(fwevt);
239 	queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
240 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
241 }
242 
243 /**
244  * mpi3mr_fwevt_del_from_list - Delete firmware event from list
245  * @mrioc: Adapter instance reference
246  * @fwevt: Firmware event reference
247  *
248  * Delete the given firmware event from the firmware event list.
249  *
250  * Return: Nothing.
251  */
mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)252 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
253 	struct mpi3mr_fwevt *fwevt)
254 {
255 	unsigned long flags;
256 
257 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
258 	if (!list_empty(&fwevt->list)) {
259 		list_del_init(&fwevt->list);
260 		/*
261 		 * Put fwevt reference count after
262 		 * removing it from fwevt_list
263 		 */
264 		mpi3mr_fwevt_put(fwevt);
265 	}
266 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
267 }
268 
269 /**
270  * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
271  * @mrioc: Adapter instance reference
272  *
273  * Dequeue a firmware event from the firmware event list.
274  *
275  * Return: firmware event.
276  */
mpi3mr_dequeue_fwevt(struct mpi3mr_ioc * mrioc)277 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
278 	struct mpi3mr_ioc *mrioc)
279 {
280 	unsigned long flags;
281 	struct mpi3mr_fwevt *fwevt = NULL;
282 
283 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
284 	if (!list_empty(&mrioc->fwevt_list)) {
285 		fwevt = list_first_entry(&mrioc->fwevt_list,
286 		    struct mpi3mr_fwevt, list);
287 		list_del_init(&fwevt->list);
288 		/*
289 		 * Put fwevt reference count after
290 		 * removing it from fwevt_list
291 		 */
292 		mpi3mr_fwevt_put(fwevt);
293 	}
294 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
295 
296 	return fwevt;
297 }
298 
299 /**
300  * mpi3mr_cancel_work - cancel firmware event
301  * @fwevt: fwevt object which needs to be canceled
302  *
303  * Return: Nothing.
304  */
mpi3mr_cancel_work(struct mpi3mr_fwevt * fwevt)305 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
306 {
307 	/*
308 	 * Wait on the fwevt to complete. If this returns 1, then
309 	 * the event was never executed.
310 	 *
311 	 * If it did execute, we wait for it to finish, and the put will
312 	 * happen from mpi3mr_process_fwevt()
313 	 */
314 	if (cancel_work_sync(&fwevt->work)) {
315 		/*
316 		 * Put fwevt reference count after
317 		 * dequeuing it from worker queue
318 		 */
319 		mpi3mr_fwevt_put(fwevt);
320 		/*
321 		 * Put fwevt reference count to neutralize
322 		 * kref_init increment
323 		 */
324 		mpi3mr_fwevt_put(fwevt);
325 	}
326 }
327 
328 /**
329  * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
330  * @mrioc: Adapter instance reference
331  *
332  * Flush all pending firmware events from the firmware event
333  * list.
334  *
335  * Return: Nothing.
336  */
mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc * mrioc)337 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
338 {
339 	struct mpi3mr_fwevt *fwevt = NULL;
340 
341 	if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
342 	    !mrioc->fwevt_worker_thread)
343 		return;
344 
345 	while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
346 		mpi3mr_cancel_work(fwevt);
347 
348 	if (mrioc->current_event) {
349 		fwevt = mrioc->current_event;
350 		/*
351 		 * Don't call cancel_work_sync() API for the
352 		 * fwevt work if the controller reset is
353 		 * get called as part of processing the
354 		 * same fwevt work (or) when worker thread is
355 		 * waiting for device add/remove APIs to complete.
356 		 * Otherwise we will see deadlock.
357 		 */
358 		if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
359 			fwevt->discard = 1;
360 			return;
361 		}
362 
363 		mpi3mr_cancel_work(fwevt);
364 	}
365 }
366 
367 /**
368  * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
369  * @mrioc: Adapter instance reference
370  * @tg: Throttle group information pointer
371  *
372  * Accessor to queue on synthetically generated driver event to
373  * the event worker thread, the driver event will be used to
374  * reduce the QD of all VDs in the TG from the worker thread.
375  *
376  * Return: None.
377  */
mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg)378 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
379 	struct mpi3mr_throttle_group_info *tg)
380 {
381 	struct mpi3mr_fwevt *fwevt;
382 	u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
383 
384 	/*
385 	 * If the QD reduction event is already queued due to throttle and if
386 	 * the QD is not restored through device info change event
387 	 * then dont queue further reduction events
388 	 */
389 	if (tg->fw_qd != tg->modified_qd)
390 		return;
391 
392 	fwevt = mpi3mr_alloc_fwevt(sz);
393 	if (!fwevt) {
394 		ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
395 		return;
396 	}
397 	*(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
398 	fwevt->mrioc = mrioc;
399 	fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
400 	fwevt->send_ack = 0;
401 	fwevt->process_evt = 1;
402 	fwevt->evt_ctx = 0;
403 	fwevt->event_data_size = sz;
404 	tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
405 
406 	dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
407 	    tg->id);
408 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
409 }
410 
411 /**
412  * mpi3mr_invalidate_devhandles -Invalidate device handles
413  * @mrioc: Adapter instance reference
414  *
415  * Invalidate the device handles in the target device structures
416  * . Called post reset prior to reinitializing the controller.
417  *
418  * Return: Nothing.
419  */
mpi3mr_invalidate_devhandles(struct mpi3mr_ioc * mrioc)420 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
421 {
422 	struct mpi3mr_tgt_dev *tgtdev;
423 	struct mpi3mr_stgt_priv_data *tgt_priv;
424 
425 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
426 		tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
427 		if (tgtdev->starget && tgtdev->starget->hostdata) {
428 			tgt_priv = tgtdev->starget->hostdata;
429 			tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
430 			tgt_priv->io_throttle_enabled = 0;
431 			tgt_priv->io_divert = 0;
432 			tgt_priv->throttle_group = NULL;
433 			tgt_priv->wslen = 0;
434 			if (tgtdev->host_exposed)
435 				atomic_set(&tgt_priv->block_io, 1);
436 		}
437 	}
438 }
439 
440 /**
441  * mpi3mr_print_scmd - print individual SCSI command
442  * @rq: Block request
443  * @data: Adapter instance reference
444  *
445  * Print the SCSI command details if it is in LLD scope.
446  *
447  * Return: true always.
448  */
mpi3mr_print_scmd(struct request * rq,void * data)449 static bool mpi3mr_print_scmd(struct request *rq, void *data)
450 {
451 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
452 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
453 	struct scmd_priv *priv = NULL;
454 
455 	if (scmd) {
456 		priv = scsi_cmd_priv(scmd);
457 		if (!priv->in_lld_scope)
458 			goto out;
459 
460 		ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
461 		    __func__, priv->host_tag, priv->req_q_idx + 1);
462 		scsi_print_command(scmd);
463 	}
464 
465 out:
466 	return(true);
467 }
468 
469 /**
470  * mpi3mr_flush_scmd - Flush individual SCSI command
471  * @rq: Block request
472  * @data: Adapter instance reference
473  *
474  * Return the SCSI command to the upper layers if it is in LLD
475  * scope.
476  *
477  * Return: true always.
478  */
479 
mpi3mr_flush_scmd(struct request * rq,void * data)480 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
481 {
482 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
483 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
484 	struct scmd_priv *priv = NULL;
485 
486 	if (scmd) {
487 		priv = scsi_cmd_priv(scmd);
488 		if (!priv->in_lld_scope)
489 			goto out;
490 
491 		if (priv->meta_sg_valid)
492 			dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
493 			    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
494 		mpi3mr_clear_scmd_priv(mrioc, scmd);
495 		scsi_dma_unmap(scmd);
496 		scmd->result = DID_RESET << 16;
497 		scsi_print_command(scmd);
498 		scsi_done(scmd);
499 		mrioc->flush_io_count++;
500 	}
501 
502 out:
503 	return(true);
504 }
505 
506 /**
507  * mpi3mr_count_dev_pending - Count commands pending for a lun
508  * @rq: Block request
509  * @data: SCSI device reference
510  *
511  * This is an iterator function called for each SCSI command in
512  * a host and if the command is pending in the LLD for the
513  * specific device(lun) then device specific pending I/O counter
514  * is updated in the device structure.
515  *
516  * Return: true always.
517  */
518 
mpi3mr_count_dev_pending(struct request * rq,void * data)519 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
520 {
521 	struct scsi_device *sdev = (struct scsi_device *)data;
522 	struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
523 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
524 	struct scmd_priv *priv;
525 
526 	if (scmd) {
527 		priv = scsi_cmd_priv(scmd);
528 		if (!priv->in_lld_scope)
529 			goto out;
530 		if (scmd->device == sdev)
531 			sdev_priv_data->pend_count++;
532 	}
533 
534 out:
535 	return true;
536 }
537 
538 /**
539  * mpi3mr_count_tgt_pending - Count commands pending for target
540  * @rq: Block request
541  * @data: SCSI target reference
542  *
543  * This is an iterator function called for each SCSI command in
544  * a host and if the command is pending in the LLD for the
545  * specific target then target specific pending I/O counter is
546  * updated in the target structure.
547  *
548  * Return: true always.
549  */
550 
mpi3mr_count_tgt_pending(struct request * rq,void * data)551 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
552 {
553 	struct scsi_target *starget = (struct scsi_target *)data;
554 	struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
555 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
556 	struct scmd_priv *priv;
557 
558 	if (scmd) {
559 		priv = scsi_cmd_priv(scmd);
560 		if (!priv->in_lld_scope)
561 			goto out;
562 		if (scmd->device && (scsi_target(scmd->device) == starget))
563 			stgt_priv_data->pend_count++;
564 	}
565 
566 out:
567 	return true;
568 }
569 
570 /**
571  * mpi3mr_flush_host_io -  Flush host I/Os
572  * @mrioc: Adapter instance reference
573  *
574  * Flush all of the pending I/Os by calling
575  * blk_mq_tagset_busy_iter() for each possible tag. This is
576  * executed post controller reset
577  *
578  * Return: Nothing.
579  */
mpi3mr_flush_host_io(struct mpi3mr_ioc * mrioc)580 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
581 {
582 	struct Scsi_Host *shost = mrioc->shost;
583 
584 	mrioc->flush_io_count = 0;
585 	ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
586 	blk_mq_tagset_busy_iter(&shost->tag_set,
587 	    mpi3mr_flush_scmd, (void *)mrioc);
588 	ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
589 	    mrioc->flush_io_count);
590 }
591 
592 /**
593  * mpi3mr_flush_cmds_for_unrecovered_controller - Flush all pending cmds
594  * @mrioc: Adapter instance reference
595  *
596  * This function waits for currently running IO poll threads to
597  * exit and then flushes all host I/Os and any internal pending
598  * cmds. This is executed after controller is marked as
599  * unrecoverable.
600  *
601  * Return: Nothing.
602  */
mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc * mrioc)603 void mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc *mrioc)
604 {
605 	struct Scsi_Host *shost = mrioc->shost;
606 	int i;
607 
608 	if (!mrioc->unrecoverable)
609 		return;
610 
611 	if (mrioc->op_reply_qinfo) {
612 		for (i = 0; i < mrioc->num_queues; i++) {
613 			while (atomic_read(&mrioc->op_reply_qinfo[i].in_use))
614 				udelay(500);
615 			atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
616 		}
617 	}
618 	mrioc->flush_io_count = 0;
619 	blk_mq_tagset_busy_iter(&shost->tag_set,
620 	    mpi3mr_flush_scmd, (void *)mrioc);
621 	mpi3mr_flush_delayed_cmd_lists(mrioc);
622 	mpi3mr_flush_drv_cmds(mrioc);
623 }
624 
625 /**
626  * mpi3mr_alloc_tgtdev - target device allocator
627  *
628  * Allocate target device instance and initialize the reference
629  * count
630  *
631  * Return: target device instance.
632  */
mpi3mr_alloc_tgtdev(void)633 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
634 {
635 	struct mpi3mr_tgt_dev *tgtdev;
636 
637 	tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
638 	if (!tgtdev)
639 		return NULL;
640 	kref_init(&tgtdev->ref_count);
641 	return tgtdev;
642 }
643 
644 /**
645  * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
646  * @mrioc: Adapter instance reference
647  * @tgtdev: Target device
648  *
649  * Add the target device to the target device list
650  *
651  * Return: Nothing.
652  */
mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev)653 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
654 	struct mpi3mr_tgt_dev *tgtdev)
655 {
656 	unsigned long flags;
657 
658 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
659 	mpi3mr_tgtdev_get(tgtdev);
660 	INIT_LIST_HEAD(&tgtdev->list);
661 	list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
662 	tgtdev->state = MPI3MR_DEV_CREATED;
663 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
664 }
665 
666 /**
667  * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
668  * @mrioc: Adapter instance reference
669  * @tgtdev: Target device
670  * @must_delete: Must delete the target device from the list irrespective
671  * of the device state.
672  *
673  * Remove the target device from the target device list
674  *
675  * Return: Nothing.
676  */
mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev,bool must_delete)677 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
678 	struct mpi3mr_tgt_dev *tgtdev, bool must_delete)
679 {
680 	unsigned long flags;
681 
682 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
683 	if ((tgtdev->state == MPI3MR_DEV_REMOVE_HS_STARTED) || (must_delete == true)) {
684 		if (!list_empty(&tgtdev->list)) {
685 			list_del_init(&tgtdev->list);
686 			tgtdev->state = MPI3MR_DEV_DELETED;
687 			mpi3mr_tgtdev_put(tgtdev);
688 		}
689 	}
690 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
691 }
692 
693 /**
694  * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
695  * @mrioc: Adapter instance reference
696  * @handle: Device handle
697  *
698  * Accessor to retrieve target device from the device handle.
699  * Non Lock version
700  *
701  * Return: Target device reference.
702  */
__mpi3mr_get_tgtdev_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)703 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_handle(
704 	struct mpi3mr_ioc *mrioc, u16 handle)
705 {
706 	struct mpi3mr_tgt_dev *tgtdev;
707 
708 	assert_spin_locked(&mrioc->tgtdev_lock);
709 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
710 		if (tgtdev->dev_handle == handle)
711 			goto found_tgtdev;
712 	return NULL;
713 
714 found_tgtdev:
715 	mpi3mr_tgtdev_get(tgtdev);
716 	return tgtdev;
717 }
718 
719 /**
720  * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
721  * @mrioc: Adapter instance reference
722  * @handle: Device handle
723  *
724  * Accessor to retrieve target device from the device handle.
725  * Lock version
726  *
727  * Return: Target device reference.
728  */
mpi3mr_get_tgtdev_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)729 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
730 	struct mpi3mr_ioc *mrioc, u16 handle)
731 {
732 	struct mpi3mr_tgt_dev *tgtdev;
733 	unsigned long flags;
734 
735 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
736 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
737 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
738 	return tgtdev;
739 }
740 
741 /**
742  * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
743  * @mrioc: Adapter instance reference
744  * @persist_id: Persistent ID
745  *
746  * Accessor to retrieve target device from the Persistent ID.
747  * Non Lock version
748  *
749  * Return: Target device reference.
750  */
__mpi3mr_get_tgtdev_by_perst_id(struct mpi3mr_ioc * mrioc,u16 persist_id)751 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_perst_id(
752 	struct mpi3mr_ioc *mrioc, u16 persist_id)
753 {
754 	struct mpi3mr_tgt_dev *tgtdev;
755 
756 	assert_spin_locked(&mrioc->tgtdev_lock);
757 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
758 		if (tgtdev->perst_id == persist_id)
759 			goto found_tgtdev;
760 	return NULL;
761 
762 found_tgtdev:
763 	mpi3mr_tgtdev_get(tgtdev);
764 	return tgtdev;
765 }
766 
767 /**
768  * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
769  * @mrioc: Adapter instance reference
770  * @persist_id: Persistent ID
771  *
772  * Accessor to retrieve target device from the Persistent ID.
773  * Lock version
774  *
775  * Return: Target device reference.
776  */
mpi3mr_get_tgtdev_by_perst_id(struct mpi3mr_ioc * mrioc,u16 persist_id)777 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
778 	struct mpi3mr_ioc *mrioc, u16 persist_id)
779 {
780 	struct mpi3mr_tgt_dev *tgtdev;
781 	unsigned long flags;
782 
783 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
784 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
785 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
786 	return tgtdev;
787 }
788 
789 /**
790  * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
791  * @mrioc: Adapter instance reference
792  * @tgt_priv: Target private data
793  *
794  * Accessor to return target device from the target private
795  * data. Non Lock version
796  *
797  * Return: Target device reference.
798  */
__mpi3mr_get_tgtdev_from_tgtpriv(struct mpi3mr_ioc * mrioc,struct mpi3mr_stgt_priv_data * tgt_priv)799 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_from_tgtpriv(
800 	struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
801 {
802 	struct mpi3mr_tgt_dev *tgtdev;
803 
804 	assert_spin_locked(&mrioc->tgtdev_lock);
805 	tgtdev = tgt_priv->tgt_dev;
806 	if (tgtdev)
807 		mpi3mr_tgtdev_get(tgtdev);
808 	return tgtdev;
809 }
810 
811 /**
812  * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
813  * @mrioc: Adapter instance reference
814  * @tg: Throttle group information pointer
815  * @divert_value: 1 or 0
816  *
817  * Accessor to set io_divert flag for each device associated
818  * with the given throttle group with the given value.
819  *
820  * Return: None.
821  */
mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg,u8 divert_value)822 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
823 	struct mpi3mr_throttle_group_info *tg, u8 divert_value)
824 {
825 	unsigned long flags;
826 	struct mpi3mr_tgt_dev *tgtdev;
827 	struct mpi3mr_stgt_priv_data *tgt_priv;
828 
829 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
830 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
831 		if (tgtdev->starget && tgtdev->starget->hostdata) {
832 			tgt_priv = tgtdev->starget->hostdata;
833 			if (tgt_priv->throttle_group == tg)
834 				tgt_priv->io_divert = divert_value;
835 		}
836 	}
837 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
838 }
839 
840 /**
841  * mpi3mr_print_device_event_notice - print notice related to post processing of
842  *					device event after controller reset.
843  *
844  * @mrioc: Adapter instance reference
845  * @device_add: true for device add event and false for device removal event
846  *
847  * Return: None.
848  */
mpi3mr_print_device_event_notice(struct mpi3mr_ioc * mrioc,bool device_add)849 void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
850 	bool device_add)
851 {
852 	ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
853 	    (device_add ? "addition" : "removal"));
854 	ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
855 	ioc_notice(mrioc, "are matched with attached devices for correctness\n");
856 }
857 
858 /**
859  * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
860  * @mrioc: Adapter instance reference
861  * @tgtdev: Target device structure
862  *
863  * Checks whether the device is exposed to upper layers and if it
864  * is then remove the device from upper layers by calling
865  * scsi_remove_target().
866  *
867  * Return: 0 on success, non zero on failure.
868  */
mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev)869 void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
870 	struct mpi3mr_tgt_dev *tgtdev)
871 {
872 	struct mpi3mr_stgt_priv_data *tgt_priv;
873 
874 	ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
875 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
876 	if (tgtdev->starget && tgtdev->starget->hostdata) {
877 		tgt_priv = tgtdev->starget->hostdata;
878 		atomic_set(&tgt_priv->block_io, 0);
879 		tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
880 	}
881 
882 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
883 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl) {
884 		if (tgtdev->starget) {
885 			if (mrioc->current_event)
886 				mrioc->current_event->pending_at_sml = 1;
887 			scsi_remove_target(&tgtdev->starget->dev);
888 			tgtdev->host_exposed = 0;
889 			if (mrioc->current_event) {
890 				mrioc->current_event->pending_at_sml = 0;
891 				if (mrioc->current_event->discard) {
892 					mpi3mr_print_device_event_notice(mrioc,
893 					    false);
894 					return;
895 				}
896 			}
897 		}
898 	} else
899 		mpi3mr_remove_tgtdev_from_sas_transport(mrioc, tgtdev);
900 
901 	ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
902 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
903 }
904 
905 /**
906  * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
907  * @mrioc: Adapter instance reference
908  * @perst_id: Persistent ID of the device
909  *
910  * Checks whether the device can be exposed to upper layers and
911  * if it is not then expose the device to upper layers by
912  * calling scsi_scan_target().
913  *
914  * Return: 0 on success, non zero on failure.
915  */
mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc * mrioc,u16 perst_id)916 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
917 	u16 perst_id)
918 {
919 	int retval = 0;
920 	struct mpi3mr_tgt_dev *tgtdev;
921 
922 	if (mrioc->reset_in_progress)
923 		return -1;
924 
925 	tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
926 	if (!tgtdev) {
927 		retval = -1;
928 		goto out;
929 	}
930 	if (tgtdev->is_hidden || tgtdev->host_exposed) {
931 		retval = -1;
932 		goto out;
933 	}
934 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
935 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl){
936 		tgtdev->host_exposed = 1;
937 		if (mrioc->current_event)
938 			mrioc->current_event->pending_at_sml = 1;
939 		scsi_scan_target(&mrioc->shost->shost_gendev,
940 		    mrioc->scsi_device_channel, tgtdev->perst_id,
941 		    SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
942 		if (!tgtdev->starget)
943 			tgtdev->host_exposed = 0;
944 		if (mrioc->current_event) {
945 			mrioc->current_event->pending_at_sml = 0;
946 			if (mrioc->current_event->discard) {
947 				mpi3mr_print_device_event_notice(mrioc, true);
948 				goto out;
949 			}
950 		}
951 	} else
952 		mpi3mr_report_tgtdev_to_sas_transport(mrioc, tgtdev);
953 out:
954 	if (tgtdev)
955 		mpi3mr_tgtdev_put(tgtdev);
956 
957 	return retval;
958 }
959 
960 /**
961  * mpi3mr_change_queue_depth- Change QD callback handler
962  * @sdev: SCSI device reference
963  * @q_depth: Queue depth
964  *
965  * Validate and limit QD and call scsi_change_queue_depth.
966  *
967  * Return: return value of scsi_change_queue_depth
968  */
mpi3mr_change_queue_depth(struct scsi_device * sdev,int q_depth)969 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
970 	int q_depth)
971 {
972 	struct scsi_target *starget = scsi_target(sdev);
973 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
974 	int retval = 0;
975 
976 	if (!sdev->tagged_supported)
977 		q_depth = 1;
978 	if (q_depth > shost->can_queue)
979 		q_depth = shost->can_queue;
980 	else if (!q_depth)
981 		q_depth = MPI3MR_DEFAULT_SDEV_QD;
982 	retval = scsi_change_queue_depth(sdev, q_depth);
983 	sdev->max_queue_depth = sdev->queue_depth;
984 
985 	return retval;
986 }
987 
988 /**
989  * mpi3mr_update_sdev - Update SCSI device information
990  * @sdev: SCSI device reference
991  * @data: target device reference
992  *
993  * This is an iterator function called for each SCSI device in a
994  * target to update the target specific information into each
995  * SCSI device.
996  *
997  * Return: Nothing.
998  */
999 static void
mpi3mr_update_sdev(struct scsi_device * sdev,void * data)1000 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
1001 {
1002 	struct mpi3mr_tgt_dev *tgtdev;
1003 
1004 	tgtdev = (struct mpi3mr_tgt_dev *)data;
1005 	if (!tgtdev)
1006 		return;
1007 
1008 	mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
1009 	switch (tgtdev->dev_type) {
1010 	case MPI3_DEVICE_DEVFORM_PCIE:
1011 		/*The block layer hw sector size = 512*/
1012 		if ((tgtdev->dev_spec.pcie_inf.dev_info &
1013 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
1014 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
1015 			blk_queue_max_hw_sectors(sdev->request_queue,
1016 			    tgtdev->dev_spec.pcie_inf.mdts / 512);
1017 			if (tgtdev->dev_spec.pcie_inf.pgsz == 0)
1018 				blk_queue_virt_boundary(sdev->request_queue,
1019 				    ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
1020 			else
1021 				blk_queue_virt_boundary(sdev->request_queue,
1022 				    ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
1023 		}
1024 		break;
1025 	default:
1026 		break;
1027 	}
1028 }
1029 
1030 /**
1031  * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
1032  * @mrioc: Adapter instance reference
1033  *
1034  * This is executed post controller reset to identify any
1035  * missing devices during reset and remove from the upper layers
1036  * or expose any newly detected device to the upper layers.
1037  *
1038  * Return: Nothing.
1039  */
1040 
mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc * mrioc)1041 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
1042 {
1043 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
1044 	struct mpi3mr_stgt_priv_data *tgt_priv;
1045 
1046 	dprint_reset(mrioc, "refresh target devices: check for removals\n");
1047 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1048 	    list) {
1049 		if ((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) &&
1050 		     tgtdev->is_hidden &&
1051 		     tgtdev->host_exposed && tgtdev->starget &&
1052 		     tgtdev->starget->hostdata) {
1053 			tgt_priv = tgtdev->starget->hostdata;
1054 			tgt_priv->dev_removed = 1;
1055 			atomic_set(&tgt_priv->block_io, 0);
1056 		}
1057 	}
1058 
1059 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1060 	    list) {
1061 		if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1062 			dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1063 			    tgtdev->perst_id);
1064 			if (tgtdev->host_exposed)
1065 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1066 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1067 			mpi3mr_tgtdev_put(tgtdev);
1068 		} else if (tgtdev->is_hidden & tgtdev->host_exposed) {
1069 			dprint_reset(mrioc, "hiding target device with perst_id(%d)\n",
1070 				     tgtdev->perst_id);
1071 			mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1072 		}
1073 	}
1074 
1075 	tgtdev = NULL;
1076 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1077 		if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1078 		    !tgtdev->is_hidden) {
1079 			if (!tgtdev->host_exposed)
1080 				mpi3mr_report_tgtdev_to_host(mrioc,
1081 							     tgtdev->perst_id);
1082 			else if (tgtdev->starget)
1083 				starget_for_each_device(tgtdev->starget,
1084 							(void *)tgtdev, mpi3mr_update_sdev);
1085 	}
1086 	}
1087 }
1088 
1089 /**
1090  * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1091  * @mrioc: Adapter instance reference
1092  * @tgtdev: Target device internal structure
1093  * @dev_pg0: New device page0
1094  * @is_added: Flag to indicate the device is just added
1095  *
1096  * Update the information from the device page0 into the driver
1097  * cached target device structure.
1098  *
1099  * Return: Nothing.
1100  */
mpi3mr_update_tgtdev(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev,struct mpi3_device_page0 * dev_pg0,bool is_added)1101 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1102 	struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1103 	bool is_added)
1104 {
1105 	u16 flags = 0;
1106 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1107 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1108 	u8 prot_mask = 0;
1109 
1110 	tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1111 	tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1112 	tgtdev->dev_type = dev_pg0->device_form;
1113 	tgtdev->io_unit_port = dev_pg0->io_unit_port;
1114 	tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1115 	tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1116 	tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1117 	tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1118 	tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1119 	tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1120 
1121 	if (tgtdev->encl_handle)
1122 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1123 		    tgtdev->encl_handle);
1124 	if (enclosure_dev)
1125 		tgtdev->enclosure_logical_id = le64_to_cpu(
1126 		    enclosure_dev->pg0.enclosure_logical_id);
1127 
1128 	flags = tgtdev->devpg0_flag;
1129 
1130 	tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1131 
1132 	if (is_added == true)
1133 		tgtdev->io_throttle_enabled =
1134 		    (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1135 
1136 	switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1137 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1138 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1139 		break;
1140 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1141 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1142 		break;
1143 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1144 	default:
1145 		tgtdev->wslen = 0;
1146 		break;
1147 	}
1148 
1149 	if (tgtdev->starget && tgtdev->starget->hostdata) {
1150 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1151 		    tgtdev->starget->hostdata;
1152 		scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1153 		scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1154 		scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1155 		scsi_tgt_priv_data->io_throttle_enabled =
1156 		    tgtdev->io_throttle_enabled;
1157 		if (is_added == true)
1158 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
1159 		scsi_tgt_priv_data->wslen = tgtdev->wslen;
1160 	}
1161 
1162 	switch (dev_pg0->access_status) {
1163 	case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1164 	case MPI3_DEVICE0_ASTATUS_PREPARE:
1165 	case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1166 	case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1167 		break;
1168 	default:
1169 		tgtdev->is_hidden = 1;
1170 		break;
1171 	}
1172 
1173 	switch (tgtdev->dev_type) {
1174 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
1175 	{
1176 		struct mpi3_device0_sas_sata_format *sasinf =
1177 		    &dev_pg0->device_specific.sas_sata_format;
1178 		u16 dev_info = le16_to_cpu(sasinf->device_info);
1179 
1180 		tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1181 		tgtdev->dev_spec.sas_sata_inf.sas_address =
1182 		    le64_to_cpu(sasinf->sas_address);
1183 		tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1184 		tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1185 		    sasinf->attached_phy_identifier;
1186 		if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1187 		    MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1188 			tgtdev->is_hidden = 1;
1189 		else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1190 		    MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1191 			tgtdev->is_hidden = 1;
1192 
1193 		if (((tgtdev->devpg0_flag &
1194 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1195 		    && (tgtdev->devpg0_flag &
1196 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1197 		    (tgtdev->parent_handle == 0xFFFF))
1198 			tgtdev->non_stl = 1;
1199 		if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1200 			tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1201 			    dev_pg0->io_unit_port;
1202 		break;
1203 	}
1204 	case MPI3_DEVICE_DEVFORM_PCIE:
1205 	{
1206 		struct mpi3_device0_pcie_format *pcieinf =
1207 		    &dev_pg0->device_specific.pcie_format;
1208 		u16 dev_info = le16_to_cpu(pcieinf->device_info);
1209 
1210 		tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1211 		tgtdev->dev_spec.pcie_inf.capb =
1212 		    le32_to_cpu(pcieinf->capabilities);
1213 		tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1214 		/* 2^12 = 4096 */
1215 		tgtdev->dev_spec.pcie_inf.pgsz = 12;
1216 		if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1217 			tgtdev->dev_spec.pcie_inf.mdts =
1218 			    le32_to_cpu(pcieinf->maximum_data_transfer_size);
1219 			tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1220 			tgtdev->dev_spec.pcie_inf.reset_to =
1221 			    max_t(u8, pcieinf->controller_reset_to,
1222 			     MPI3MR_INTADMCMD_TIMEOUT);
1223 			tgtdev->dev_spec.pcie_inf.abort_to =
1224 			    max_t(u8, pcieinf->nvme_abort_to,
1225 			    MPI3MR_INTADMCMD_TIMEOUT);
1226 		}
1227 		if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1228 			tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1229 		if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1230 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1231 		    ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1232 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1233 			tgtdev->is_hidden = 1;
1234 		tgtdev->non_stl = 1;
1235 		if (!mrioc->shost)
1236 			break;
1237 		prot_mask = scsi_host_get_prot(mrioc->shost);
1238 		if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1239 			scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1240 			ioc_info(mrioc,
1241 			    "%s : Disabling DIX0 prot capability\n", __func__);
1242 			ioc_info(mrioc,
1243 			    "because HBA does not support DIX0 operation on NVME drives\n");
1244 		}
1245 		break;
1246 	}
1247 	case MPI3_DEVICE_DEVFORM_VD:
1248 	{
1249 		struct mpi3_device0_vd_format *vdinf =
1250 		    &dev_pg0->device_specific.vd_format;
1251 		struct mpi3mr_throttle_group_info *tg = NULL;
1252 		u16 vdinf_io_throttle_group =
1253 		    le16_to_cpu(vdinf->io_throttle_group);
1254 
1255 		tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1256 		if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1257 			tgtdev->is_hidden = 1;
1258 		tgtdev->non_stl = 1;
1259 		tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1260 		tgtdev->dev_spec.vd_inf.tg_high =
1261 		    le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1262 		tgtdev->dev_spec.vd_inf.tg_low =
1263 		    le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1264 		if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1265 			tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1266 			tg->id = vdinf_io_throttle_group;
1267 			tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1268 			tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1269 			tg->qd_reduction =
1270 			    tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1271 			if (is_added == true)
1272 				tg->fw_qd = tgtdev->q_depth;
1273 			tg->modified_qd = tgtdev->q_depth;
1274 		}
1275 		tgtdev->dev_spec.vd_inf.tg = tg;
1276 		if (scsi_tgt_priv_data)
1277 			scsi_tgt_priv_data->throttle_group = tg;
1278 		break;
1279 	}
1280 	default:
1281 		break;
1282 	}
1283 }
1284 
1285 /**
1286  * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1287  * @mrioc: Adapter instance reference
1288  * @fwevt: Firmware event information.
1289  *
1290  * Process Device status Change event and based on device's new
1291  * information, either expose the device to the upper layers, or
1292  * remove the device from upper layers.
1293  *
1294  * Return: Nothing.
1295  */
mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1296 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1297 	struct mpi3mr_fwevt *fwevt)
1298 {
1299 	u16 dev_handle = 0;
1300 	u8 uhide = 0, delete = 0, cleanup = 0;
1301 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1302 	struct mpi3_event_data_device_status_change *evtdata =
1303 	    (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1304 
1305 	dev_handle = le16_to_cpu(evtdata->dev_handle);
1306 	ioc_info(mrioc,
1307 	    "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1308 	    __func__, dev_handle, evtdata->reason_code);
1309 	switch (evtdata->reason_code) {
1310 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1311 		delete = 1;
1312 		break;
1313 	case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1314 		uhide = 1;
1315 		break;
1316 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1317 		delete = 1;
1318 		cleanup = 1;
1319 		break;
1320 	default:
1321 		ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1322 		    evtdata->reason_code);
1323 		break;
1324 	}
1325 
1326 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1327 	if (!tgtdev)
1328 		goto out;
1329 	if (uhide) {
1330 		tgtdev->is_hidden = 0;
1331 		if (!tgtdev->host_exposed)
1332 			mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1333 	}
1334 
1335 	if (delete)
1336 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1337 
1338 	if (cleanup) {
1339 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1340 		mpi3mr_tgtdev_put(tgtdev);
1341 	}
1342 
1343 out:
1344 	if (tgtdev)
1345 		mpi3mr_tgtdev_put(tgtdev);
1346 }
1347 
1348 /**
1349  * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1350  * @mrioc: Adapter instance reference
1351  * @dev_pg0: New device page0
1352  *
1353  * Process Device Info Change event and based on device's new
1354  * information, either expose the device to the upper layers, or
1355  * remove the device from upper layers or update the details of
1356  * the device.
1357  *
1358  * Return: Nothing.
1359  */
mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3_device_page0 * dev_pg0)1360 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1361 	struct mpi3_device_page0 *dev_pg0)
1362 {
1363 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1364 	u16 dev_handle = 0, perst_id = 0;
1365 
1366 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
1367 	dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1368 	ioc_info(mrioc,
1369 	    "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1370 	    __func__, dev_handle, perst_id);
1371 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1372 	if (!tgtdev)
1373 		goto out;
1374 	mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1375 	if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1376 		mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1377 	if (tgtdev->is_hidden && tgtdev->host_exposed)
1378 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1379 	if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1380 		starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1381 		    mpi3mr_update_sdev);
1382 out:
1383 	if (tgtdev)
1384 		mpi3mr_tgtdev_put(tgtdev);
1385 }
1386 
1387 /**
1388  * mpi3mr_free_enclosure_list - release enclosures
1389  * @mrioc: Adapter instance reference
1390  *
1391  * Free memory allocated during encloure add.
1392  *
1393  * Return nothing.
1394  */
mpi3mr_free_enclosure_list(struct mpi3mr_ioc * mrioc)1395 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1396 {
1397 	struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1398 
1399 	list_for_each_entry_safe(enclosure_dev,
1400 	    enclosure_dev_next, &mrioc->enclosure_list, list) {
1401 		list_del(&enclosure_dev->list);
1402 		kfree(enclosure_dev);
1403 	}
1404 }
1405 
1406 /**
1407  * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1408  * @mrioc: Adapter instance reference
1409  * @handle: Firmware device handle of the enclosure
1410  *
1411  * This searches for enclosure device based on handle, then returns the
1412  * enclosure object.
1413  *
1414  * Return: Enclosure object reference or NULL
1415  */
mpi3mr_enclosure_find_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)1416 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1417 	struct mpi3mr_ioc *mrioc, u16 handle)
1418 {
1419 	struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1420 
1421 	list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1422 		if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1423 			continue;
1424 		r = enclosure_dev;
1425 		goto out;
1426 	}
1427 out:
1428 	return r;
1429 }
1430 
1431 /**
1432  * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1433  * @mrioc: Adapter instance reference
1434  * @encl_pg0: Enclosure page 0.
1435  * @is_added: Added event or not
1436  *
1437  * Return nothing.
1438  */
mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_enclosure_page0 * encl_pg0,u8 is_added)1439 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1440 	struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1441 {
1442 	char *reason_str = NULL;
1443 
1444 	if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1445 		return;
1446 
1447 	if (is_added)
1448 		reason_str = "enclosure added";
1449 	else
1450 		reason_str = "enclosure dev status changed";
1451 
1452 	ioc_info(mrioc,
1453 	    "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1454 	    reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1455 	    (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1456 	ioc_info(mrioc,
1457 	    "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1458 	    le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1459 	    le16_to_cpu(encl_pg0->flags),
1460 	    ((le16_to_cpu(encl_pg0->flags) &
1461 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1462 }
1463 
1464 /**
1465  * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1466  * @mrioc: Adapter instance reference
1467  * @fwevt: Firmware event reference
1468  *
1469  * Prints information about the Enclosure device status or
1470  * Enclosure add events if logging is enabled and add or remove
1471  * the enclosure from the controller's internal list of
1472  * enclosures.
1473  *
1474  * Return: Nothing.
1475  */
mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1476 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1477 	struct mpi3mr_fwevt *fwevt)
1478 {
1479 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1480 	struct mpi3_enclosure_page0 *encl_pg0;
1481 	u16 encl_handle;
1482 	u8 added, present;
1483 
1484 	encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1485 	added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1486 	mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1487 
1488 
1489 	encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1490 	present = ((le16_to_cpu(encl_pg0->flags) &
1491 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1492 
1493 	if (encl_handle)
1494 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1495 		    encl_handle);
1496 	if (!enclosure_dev && present) {
1497 		enclosure_dev =
1498 			kzalloc(sizeof(struct mpi3mr_enclosure_node),
1499 			    GFP_KERNEL);
1500 		if (!enclosure_dev)
1501 			return;
1502 		list_add_tail(&enclosure_dev->list,
1503 		    &mrioc->enclosure_list);
1504 	}
1505 	if (enclosure_dev) {
1506 		if (!present) {
1507 			list_del(&enclosure_dev->list);
1508 			kfree(enclosure_dev);
1509 		} else
1510 			memcpy(&enclosure_dev->pg0, encl_pg0,
1511 			    sizeof(enclosure_dev->pg0));
1512 
1513 	}
1514 }
1515 
1516 /**
1517  * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1518  * @mrioc: Adapter instance reference
1519  * @event_data: SAS topology change list event data
1520  *
1521  * Prints information about the SAS topology change event.
1522  *
1523  * Return: Nothing.
1524  */
1525 static void
mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_event_data_sas_topology_change_list * event_data)1526 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1527 	struct mpi3_event_data_sas_topology_change_list *event_data)
1528 {
1529 	int i;
1530 	u16 handle;
1531 	u8 reason_code, phy_number;
1532 	char *status_str = NULL;
1533 	u8 link_rate, prev_link_rate;
1534 
1535 	switch (event_data->exp_status) {
1536 	case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1537 		status_str = "remove";
1538 		break;
1539 	case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1540 		status_str =  "responding";
1541 		break;
1542 	case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1543 		status_str = "remove delay";
1544 		break;
1545 	case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1546 		status_str = "direct attached";
1547 		break;
1548 	default:
1549 		status_str = "unknown status";
1550 		break;
1551 	}
1552 	ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1553 	    __func__, status_str);
1554 	ioc_info(mrioc,
1555 	    "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1556 	    __func__, le16_to_cpu(event_data->expander_dev_handle),
1557 	    event_data->io_unit_port,
1558 	    le16_to_cpu(event_data->enclosure_handle),
1559 	    event_data->start_phy_num, event_data->num_entries);
1560 	for (i = 0; i < event_data->num_entries; i++) {
1561 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1562 		if (!handle)
1563 			continue;
1564 		phy_number = event_data->start_phy_num + i;
1565 		reason_code = event_data->phy_entry[i].status &
1566 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1567 		switch (reason_code) {
1568 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1569 			status_str = "target remove";
1570 			break;
1571 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1572 			status_str = "delay target remove";
1573 			break;
1574 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1575 			status_str = "link status change";
1576 			break;
1577 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1578 			status_str = "link status no change";
1579 			break;
1580 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1581 			status_str = "target responding";
1582 			break;
1583 		default:
1584 			status_str = "unknown";
1585 			break;
1586 		}
1587 		link_rate = event_data->phy_entry[i].link_rate >> 4;
1588 		prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1589 		ioc_info(mrioc,
1590 		    "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1591 		    __func__, phy_number, handle, status_str, link_rate,
1592 		    prev_link_rate);
1593 	}
1594 }
1595 
1596 /**
1597  * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1598  * @mrioc: Adapter instance reference
1599  * @fwevt: Firmware event reference
1600  *
1601  * Prints information about the SAS topology change event and
1602  * for "not responding" event code, removes the device from the
1603  * upper layers.
1604  *
1605  * Return: Nothing.
1606  */
mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1607 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1608 	struct mpi3mr_fwevt *fwevt)
1609 {
1610 	struct mpi3_event_data_sas_topology_change_list *event_data =
1611 	    (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1612 	int i;
1613 	u16 handle;
1614 	u8 reason_code;
1615 	u64 exp_sas_address = 0, parent_sas_address = 0;
1616 	struct mpi3mr_hba_port *hba_port = NULL;
1617 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1618 	struct mpi3mr_sas_node *sas_expander = NULL;
1619 	unsigned long flags;
1620 	u8 link_rate, prev_link_rate, parent_phy_number;
1621 
1622 	mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1623 	if (mrioc->sas_transport_enabled) {
1624 		hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1625 		    event_data->io_unit_port);
1626 		if (le16_to_cpu(event_data->expander_dev_handle)) {
1627 			spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1628 			sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1629 			    le16_to_cpu(event_data->expander_dev_handle));
1630 			if (sas_expander) {
1631 				exp_sas_address = sas_expander->sas_address;
1632 				hba_port = sas_expander->hba_port;
1633 			}
1634 			spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1635 			parent_sas_address = exp_sas_address;
1636 		} else
1637 			parent_sas_address = mrioc->sas_hba.sas_address;
1638 	}
1639 
1640 	for (i = 0; i < event_data->num_entries; i++) {
1641 		if (fwevt->discard)
1642 			return;
1643 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1644 		if (!handle)
1645 			continue;
1646 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1647 		if (!tgtdev)
1648 			continue;
1649 
1650 		reason_code = event_data->phy_entry[i].status &
1651 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1652 
1653 		switch (reason_code) {
1654 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1655 			if (tgtdev->host_exposed)
1656 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1657 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1658 			mpi3mr_tgtdev_put(tgtdev);
1659 			break;
1660 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1661 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1662 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1663 		{
1664 			if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1665 			    || tgtdev->is_hidden)
1666 				break;
1667 			link_rate = event_data->phy_entry[i].link_rate >> 4;
1668 			prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1669 			if (link_rate == prev_link_rate)
1670 				break;
1671 			if (!parent_sas_address)
1672 				break;
1673 			parent_phy_number = event_data->start_phy_num + i;
1674 			mpi3mr_update_links(mrioc, parent_sas_address, handle,
1675 			    parent_phy_number, link_rate, hba_port);
1676 			break;
1677 		}
1678 		default:
1679 			break;
1680 		}
1681 		if (tgtdev)
1682 			mpi3mr_tgtdev_put(tgtdev);
1683 	}
1684 
1685 	if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1686 	    MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1687 		if (sas_expander)
1688 			mpi3mr_expander_remove(mrioc, exp_sas_address,
1689 			    hba_port);
1690 	}
1691 }
1692 
1693 /**
1694  * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1695  * @mrioc: Adapter instance reference
1696  * @event_data: PCIe topology change list event data
1697  *
1698  * Prints information about the PCIe topology change event.
1699  *
1700  * Return: Nothing.
1701  */
1702 static void
mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_event_data_pcie_topology_change_list * event_data)1703 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1704 	struct mpi3_event_data_pcie_topology_change_list *event_data)
1705 {
1706 	int i;
1707 	u16 handle;
1708 	u16 reason_code;
1709 	u8 port_number;
1710 	char *status_str = NULL;
1711 	u8 link_rate, prev_link_rate;
1712 
1713 	switch (event_data->switch_status) {
1714 	case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1715 		status_str = "remove";
1716 		break;
1717 	case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1718 		status_str =  "responding";
1719 		break;
1720 	case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1721 		status_str = "remove delay";
1722 		break;
1723 	case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1724 		status_str = "direct attached";
1725 		break;
1726 	default:
1727 		status_str = "unknown status";
1728 		break;
1729 	}
1730 	ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1731 	    __func__, status_str);
1732 	ioc_info(mrioc,
1733 	    "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1734 	    __func__, le16_to_cpu(event_data->switch_dev_handle),
1735 	    le16_to_cpu(event_data->enclosure_handle),
1736 	    event_data->start_port_num, event_data->num_entries);
1737 	for (i = 0; i < event_data->num_entries; i++) {
1738 		handle =
1739 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1740 		if (!handle)
1741 			continue;
1742 		port_number = event_data->start_port_num + i;
1743 		reason_code = event_data->port_entry[i].port_status;
1744 		switch (reason_code) {
1745 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1746 			status_str = "target remove";
1747 			break;
1748 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1749 			status_str = "delay target remove";
1750 			break;
1751 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1752 			status_str = "link status change";
1753 			break;
1754 		case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1755 			status_str = "link status no change";
1756 			break;
1757 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1758 			status_str = "target responding";
1759 			break;
1760 		default:
1761 			status_str = "unknown";
1762 			break;
1763 		}
1764 		link_rate = event_data->port_entry[i].current_port_info &
1765 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1766 		prev_link_rate = event_data->port_entry[i].previous_port_info &
1767 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1768 		ioc_info(mrioc,
1769 		    "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1770 		    __func__, port_number, handle, status_str, link_rate,
1771 		    prev_link_rate);
1772 	}
1773 }
1774 
1775 /**
1776  * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1777  * @mrioc: Adapter instance reference
1778  * @fwevt: Firmware event reference
1779  *
1780  * Prints information about the PCIe topology change event and
1781  * for "not responding" event code, removes the device from the
1782  * upper layers.
1783  *
1784  * Return: Nothing.
1785  */
mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1786 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1787 	struct mpi3mr_fwevt *fwevt)
1788 {
1789 	struct mpi3_event_data_pcie_topology_change_list *event_data =
1790 	    (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1791 	int i;
1792 	u16 handle;
1793 	u8 reason_code;
1794 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1795 
1796 	mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1797 
1798 	for (i = 0; i < event_data->num_entries; i++) {
1799 		if (fwevt->discard)
1800 			return;
1801 		handle =
1802 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1803 		if (!handle)
1804 			continue;
1805 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1806 		if (!tgtdev)
1807 			continue;
1808 
1809 		reason_code = event_data->port_entry[i].port_status;
1810 
1811 		switch (reason_code) {
1812 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1813 			if (tgtdev->host_exposed)
1814 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1815 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1816 			mpi3mr_tgtdev_put(tgtdev);
1817 			break;
1818 		default:
1819 			break;
1820 		}
1821 		if (tgtdev)
1822 			mpi3mr_tgtdev_put(tgtdev);
1823 	}
1824 }
1825 
1826 /**
1827  * mpi3mr_logdata_evt_bh -  Log data event bottomhalf
1828  * @mrioc: Adapter instance reference
1829  * @fwevt: Firmware event reference
1830  *
1831  * Extracts the event data and calls application interfacing
1832  * function to process the event further.
1833  *
1834  * Return: Nothing.
1835  */
mpi3mr_logdata_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1836 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1837 	struct mpi3mr_fwevt *fwevt)
1838 {
1839 	mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1840 	    fwevt->event_data_size);
1841 }
1842 
1843 /**
1844  * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1845  * @sdev: SCSI device reference
1846  * @data: Queue depth reference
1847  *
1848  * This is an iterator function called for each SCSI device in a
1849  * target to update the QD of each SCSI device.
1850  *
1851  * Return: Nothing.
1852  */
mpi3mr_update_sdev_qd(struct scsi_device * sdev,void * data)1853 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1854 {
1855 	u16 *q_depth = (u16 *)data;
1856 
1857 	scsi_change_queue_depth(sdev, (int)*q_depth);
1858 	sdev->max_queue_depth = sdev->queue_depth;
1859 }
1860 
1861 /**
1862  * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1863  * @mrioc: Adapter instance reference
1864  * @tg: Throttle group information pointer
1865  *
1866  * Accessor to reduce QD for each device associated with the
1867  * given throttle group.
1868  *
1869  * Return: None.
1870  */
mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg)1871 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1872 	struct mpi3mr_throttle_group_info *tg)
1873 {
1874 	unsigned long flags;
1875 	struct mpi3mr_tgt_dev *tgtdev;
1876 	struct mpi3mr_stgt_priv_data *tgt_priv;
1877 
1878 
1879 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1880 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1881 		if (tgtdev->starget && tgtdev->starget->hostdata) {
1882 			tgt_priv = tgtdev->starget->hostdata;
1883 			if (tgt_priv->throttle_group == tg) {
1884 				dprint_event_bh(mrioc,
1885 				    "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1886 				    tgt_priv->perst_id, tgtdev->q_depth,
1887 				    tg->modified_qd);
1888 				starget_for_each_device(tgtdev->starget,
1889 				    (void *)&tg->modified_qd,
1890 				    mpi3mr_update_sdev_qd);
1891 			}
1892 		}
1893 	}
1894 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1895 }
1896 
1897 /**
1898  * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1899  * @mrioc: Adapter instance reference
1900  * @fwevt: Firmware event reference
1901  *
1902  * Identifies the firmware event and calls corresponding bottomg
1903  * half handler and sends event acknowledgment if required.
1904  *
1905  * Return: Nothing.
1906  */
mpi3mr_fwevt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1907 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1908 	struct mpi3mr_fwevt *fwevt)
1909 {
1910 	struct mpi3_device_page0 *dev_pg0 = NULL;
1911 	u16 perst_id, handle, dev_info;
1912 	struct mpi3_device0_sas_sata_format *sasinf = NULL;
1913 
1914 	mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1915 	mrioc->current_event = fwevt;
1916 
1917 	if (mrioc->stop_drv_processing)
1918 		goto out;
1919 
1920 	if (mrioc->unrecoverable) {
1921 		dprint_event_bh(mrioc,
1922 		    "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
1923 		    fwevt->event_id);
1924 		goto out;
1925 	}
1926 
1927 	if (!fwevt->process_evt)
1928 		goto evt_ack;
1929 
1930 	switch (fwevt->event_id) {
1931 	case MPI3_EVENT_DEVICE_ADDED:
1932 	{
1933 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1934 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
1935 		handle = le16_to_cpu(dev_pg0->dev_handle);
1936 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1937 			mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1938 		else if (mrioc->sas_transport_enabled &&
1939 		    (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
1940 			sasinf = &dev_pg0->device_specific.sas_sata_format;
1941 			dev_info = le16_to_cpu(sasinf->device_info);
1942 			if (!mrioc->sas_hba.num_phys)
1943 				mpi3mr_sas_host_add(mrioc);
1944 			else
1945 				mpi3mr_sas_host_refresh(mrioc);
1946 
1947 			if (mpi3mr_is_expander_device(dev_info))
1948 				mpi3mr_expander_add(mrioc, handle);
1949 		}
1950 		break;
1951 	}
1952 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
1953 	{
1954 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1955 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
1956 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1957 			mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
1958 		break;
1959 	}
1960 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1961 	{
1962 		mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1963 		break;
1964 	}
1965 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
1966 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
1967 	{
1968 		mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
1969 		break;
1970 	}
1971 
1972 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1973 	{
1974 		mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1975 		break;
1976 	}
1977 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1978 	{
1979 		mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1980 		break;
1981 	}
1982 	case MPI3_EVENT_LOG_DATA:
1983 	{
1984 		mpi3mr_logdata_evt_bh(mrioc, fwevt);
1985 		break;
1986 	}
1987 	case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
1988 	{
1989 		struct mpi3mr_throttle_group_info *tg;
1990 
1991 		tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
1992 		dprint_event_bh(mrioc,
1993 		    "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
1994 		    tg->id, tg->need_qd_reduction);
1995 		if (tg->need_qd_reduction) {
1996 			mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
1997 			tg->need_qd_reduction = 0;
1998 		}
1999 		break;
2000 	}
2001 	case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
2002 	{
2003 		while (mrioc->device_refresh_on)
2004 			msleep(500);
2005 
2006 		dprint_event_bh(mrioc,
2007 		    "scan for non responding and newly added devices after soft reset started\n");
2008 		if (mrioc->sas_transport_enabled) {
2009 			mpi3mr_refresh_sas_ports(mrioc);
2010 			mpi3mr_refresh_expanders(mrioc);
2011 		}
2012 		mpi3mr_rfresh_tgtdevs(mrioc);
2013 		ioc_info(mrioc,
2014 		    "scan for non responding and newly added devices after soft reset completed\n");
2015 		break;
2016 	}
2017 	default:
2018 		break;
2019 	}
2020 
2021 evt_ack:
2022 	if (fwevt->send_ack)
2023 		mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2024 		    fwevt->evt_ctx);
2025 out:
2026 	/* Put fwevt reference count to neutralize kref_init increment */
2027 	mpi3mr_fwevt_put(fwevt);
2028 	mrioc->current_event = NULL;
2029 }
2030 
2031 /**
2032  * mpi3mr_fwevt_worker - Firmware event worker
2033  * @work: Work struct containing firmware event
2034  *
2035  * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2036  *
2037  * Return: Nothing.
2038  */
mpi3mr_fwevt_worker(struct work_struct * work)2039 static void mpi3mr_fwevt_worker(struct work_struct *work)
2040 {
2041 	struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2042 	    work);
2043 	mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2044 	/*
2045 	 * Put fwevt reference count after
2046 	 * dequeuing it from worker queue
2047 	 */
2048 	mpi3mr_fwevt_put(fwevt);
2049 }
2050 
2051 /**
2052  * mpi3mr_create_tgtdev - Create and add a target device
2053  * @mrioc: Adapter instance reference
2054  * @dev_pg0: Device Page 0 data
2055  *
2056  * If the device specified by the device page 0 data is not
2057  * present in the driver's internal list, allocate the memory
2058  * for the device, populate the data and add to the list, else
2059  * update the device data.  The key is persistent ID.
2060  *
2061  * Return: 0 on success, -ENOMEM on memory allocation failure
2062  */
mpi3mr_create_tgtdev(struct mpi3mr_ioc * mrioc,struct mpi3_device_page0 * dev_pg0)2063 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2064 	struct mpi3_device_page0 *dev_pg0)
2065 {
2066 	int retval = 0;
2067 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2068 	u16 perst_id = 0;
2069 	unsigned long flags;
2070 
2071 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
2072 	if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2073 		return retval;
2074 
2075 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2076 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2077 	if (tgtdev)
2078 		tgtdev->state = MPI3MR_DEV_CREATED;
2079 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2080 
2081 	if (tgtdev) {
2082 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2083 		mpi3mr_tgtdev_put(tgtdev);
2084 	} else {
2085 		tgtdev = mpi3mr_alloc_tgtdev();
2086 		if (!tgtdev)
2087 			return -ENOMEM;
2088 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2089 		mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2090 	}
2091 
2092 	return retval;
2093 }
2094 
2095 /**
2096  * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2097  * @mrioc: Adapter instance reference
2098  *
2099  * Flush pending commands in the delayed lists due to a
2100  * controller reset or driver removal as a cleanup.
2101  *
2102  * Return: Nothing
2103  */
mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc * mrioc)2104 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2105 {
2106 	struct delayed_dev_rmhs_node *_rmhs_node;
2107 	struct delayed_evt_ack_node *_evtack_node;
2108 
2109 	dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2110 	while (!list_empty(&mrioc->delayed_rmhs_list)) {
2111 		_rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2112 		    struct delayed_dev_rmhs_node, list);
2113 		list_del(&_rmhs_node->list);
2114 		kfree(_rmhs_node);
2115 	}
2116 	dprint_reset(mrioc, "flushing delayed event ack commands\n");
2117 	while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2118 		_evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2119 		    struct delayed_evt_ack_node, list);
2120 		list_del(&_evtack_node->list);
2121 		kfree(_evtack_node);
2122 	}
2123 }
2124 
2125 /**
2126  * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2127  * @mrioc: Adapter instance reference
2128  * @drv_cmd: Internal command tracker
2129  *
2130  * Issues a target reset TM to the firmware from the device
2131  * removal TM pend list or retry the removal handshake sequence
2132  * based on the IOU control request IOC status.
2133  *
2134  * Return: Nothing
2135  */
mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2136 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2137 	struct mpi3mr_drv_cmd *drv_cmd)
2138 {
2139 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2140 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2141 
2142 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2143 		goto clear_drv_cmd;
2144 
2145 	ioc_info(mrioc,
2146 	    "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2147 	    __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2148 	    drv_cmd->ioc_loginfo);
2149 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2150 		if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2151 			drv_cmd->retry_count++;
2152 			ioc_info(mrioc,
2153 			    "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2154 			    __func__, drv_cmd->dev_handle,
2155 			    drv_cmd->retry_count);
2156 			mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2157 			    drv_cmd, drv_cmd->iou_rc);
2158 			return;
2159 		}
2160 		ioc_err(mrioc,
2161 		    "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2162 		    __func__, drv_cmd->dev_handle);
2163 	} else {
2164 		ioc_info(mrioc,
2165 		    "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2166 		    __func__, drv_cmd->dev_handle);
2167 		clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2168 	}
2169 
2170 	if (!list_empty(&mrioc->delayed_rmhs_list)) {
2171 		delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2172 		    struct delayed_dev_rmhs_node, list);
2173 		drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2174 		drv_cmd->retry_count = 0;
2175 		drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2176 		ioc_info(mrioc,
2177 		    "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2178 		    __func__, drv_cmd->dev_handle);
2179 		mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2180 		    drv_cmd->iou_rc);
2181 		list_del(&delayed_dev_rmhs->list);
2182 		kfree(delayed_dev_rmhs);
2183 		return;
2184 	}
2185 
2186 clear_drv_cmd:
2187 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2188 	drv_cmd->callback = NULL;
2189 	drv_cmd->retry_count = 0;
2190 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2191 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2192 }
2193 
2194 /**
2195  * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2196  * @mrioc: Adapter instance reference
2197  * @drv_cmd: Internal command tracker
2198  *
2199  * Issues a target reset TM to the firmware from the device
2200  * removal TM pend list or issue IO unit control request as
2201  * part of device removal or hidden acknowledgment handshake.
2202  *
2203  * Return: Nothing
2204  */
mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2205 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2206 	struct mpi3mr_drv_cmd *drv_cmd)
2207 {
2208 	struct mpi3_iounit_control_request iou_ctrl;
2209 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2210 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2211 	int retval;
2212 
2213 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2214 		goto clear_drv_cmd;
2215 
2216 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2217 		tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2218 
2219 	if (tm_reply)
2220 		pr_info(IOCNAME
2221 		    "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2222 		    mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2223 		    drv_cmd->ioc_loginfo,
2224 		    le32_to_cpu(tm_reply->termination_count));
2225 
2226 	pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2227 	    mrioc->name, drv_cmd->dev_handle, cmd_idx);
2228 
2229 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2230 
2231 	drv_cmd->state = MPI3MR_CMD_PENDING;
2232 	drv_cmd->is_waiting = 0;
2233 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2234 	iou_ctrl.operation = drv_cmd->iou_rc;
2235 	iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2236 	iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2237 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2238 
2239 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2240 	    1);
2241 	if (retval) {
2242 		pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2243 		    mrioc->name);
2244 		goto clear_drv_cmd;
2245 	}
2246 
2247 	return;
2248 clear_drv_cmd:
2249 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2250 	drv_cmd->callback = NULL;
2251 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2252 	drv_cmd->retry_count = 0;
2253 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2254 }
2255 
2256 /**
2257  * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2258  * @mrioc: Adapter instance reference
2259  * @handle: Device handle
2260  * @cmdparam: Internal command tracker
2261  * @iou_rc: IO unit reason code
2262  *
2263  * Issues a target reset TM to the firmware or add it to a pend
2264  * list as part of device removal or hidden acknowledgment
2265  * handshake.
2266  *
2267  * Return: Nothing
2268  */
mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc * mrioc,u16 handle,struct mpi3mr_drv_cmd * cmdparam,u8 iou_rc)2269 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2270 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2271 {
2272 	struct mpi3_scsi_task_mgmt_request tm_req;
2273 	int retval = 0;
2274 	u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2275 	u8 retrycount = 5;
2276 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2277 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2278 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2279 	unsigned long flags;
2280 
2281 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2282 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2283 	if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2284 		tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2285 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2286 
2287 	if (drv_cmd)
2288 		goto issue_cmd;
2289 	do {
2290 		cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2291 		    MPI3MR_NUM_DEVRMCMD);
2292 		if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2293 			if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2294 				break;
2295 			cmd_idx = MPI3MR_NUM_DEVRMCMD;
2296 		}
2297 	} while (retrycount--);
2298 
2299 	if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2300 		delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
2301 		    GFP_ATOMIC);
2302 		if (!delayed_dev_rmhs)
2303 			return;
2304 		INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2305 		delayed_dev_rmhs->handle = handle;
2306 		delayed_dev_rmhs->iou_rc = iou_rc;
2307 		list_add_tail(&delayed_dev_rmhs->list,
2308 		    &mrioc->delayed_rmhs_list);
2309 		ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2310 		    __func__, handle);
2311 		return;
2312 	}
2313 	drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2314 
2315 issue_cmd:
2316 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2317 	ioc_info(mrioc,
2318 	    "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2319 	    __func__, handle, cmd_idx);
2320 
2321 	memset(&tm_req, 0, sizeof(tm_req));
2322 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2323 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2324 		goto out;
2325 	}
2326 	drv_cmd->state = MPI3MR_CMD_PENDING;
2327 	drv_cmd->is_waiting = 0;
2328 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2329 	drv_cmd->dev_handle = handle;
2330 	drv_cmd->iou_rc = iou_rc;
2331 	tm_req.dev_handle = cpu_to_le16(handle);
2332 	tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2333 	tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2334 	tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2335 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2336 
2337 	set_bit(handle, mrioc->removepend_bitmap);
2338 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2339 	if (retval) {
2340 		ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2341 		    __func__);
2342 		goto out_failed;
2343 	}
2344 out:
2345 	return;
2346 out_failed:
2347 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2348 	drv_cmd->callback = NULL;
2349 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2350 	drv_cmd->retry_count = 0;
2351 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2352 }
2353 
2354 /**
2355  * mpi3mr_complete_evt_ack - event ack request completion
2356  * @mrioc: Adapter instance reference
2357  * @drv_cmd: Internal command tracker
2358  *
2359  * This is the completion handler for non blocking event
2360  * acknowledgment sent to the firmware and this will issue any
2361  * pending event acknowledgment request.
2362  *
2363  * Return: Nothing
2364  */
mpi3mr_complete_evt_ack(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2365 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2366 	struct mpi3mr_drv_cmd *drv_cmd)
2367 {
2368 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2369 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2370 
2371 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2372 		goto clear_drv_cmd;
2373 
2374 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2375 		dprint_event_th(mrioc,
2376 		    "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2377 		    (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2378 		    drv_cmd->ioc_loginfo);
2379 	}
2380 
2381 	if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2382 		delayed_evtack =
2383 			list_entry(mrioc->delayed_evtack_cmds_list.next,
2384 			    struct delayed_evt_ack_node, list);
2385 		mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2386 		    delayed_evtack->event_ctx);
2387 		list_del(&delayed_evtack->list);
2388 		kfree(delayed_evtack);
2389 		return;
2390 	}
2391 clear_drv_cmd:
2392 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2393 	drv_cmd->callback = NULL;
2394 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2395 }
2396 
2397 /**
2398  * mpi3mr_send_event_ack - Issue event acknwoledgment request
2399  * @mrioc: Adapter instance reference
2400  * @event: MPI3 event id
2401  * @cmdparam: Internal command tracker
2402  * @event_ctx: event context
2403  *
2404  * Issues event acknowledgment request to the firmware if there
2405  * is a free command to send the event ack else it to a pend
2406  * list so that it will be processed on a completion of a prior
2407  * event acknowledgment .
2408  *
2409  * Return: Nothing
2410  */
mpi3mr_send_event_ack(struct mpi3mr_ioc * mrioc,u8 event,struct mpi3mr_drv_cmd * cmdparam,u32 event_ctx)2411 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2412 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2413 {
2414 	struct mpi3_event_ack_request evtack_req;
2415 	int retval = 0;
2416 	u8 retrycount = 5;
2417 	u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2418 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2419 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2420 
2421 	if (drv_cmd) {
2422 		dprint_event_th(mrioc,
2423 		    "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2424 		    event, event_ctx);
2425 		goto issue_cmd;
2426 	}
2427 	dprint_event_th(mrioc,
2428 	    "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2429 	    event, event_ctx);
2430 	do {
2431 		cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2432 		    MPI3MR_NUM_EVTACKCMD);
2433 		if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2434 			if (!test_and_set_bit(cmd_idx,
2435 			    mrioc->evtack_cmds_bitmap))
2436 				break;
2437 			cmd_idx = MPI3MR_NUM_EVTACKCMD;
2438 		}
2439 	} while (retrycount--);
2440 
2441 	if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2442 		delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2443 		    GFP_ATOMIC);
2444 		if (!delayed_evtack)
2445 			return;
2446 		INIT_LIST_HEAD(&delayed_evtack->list);
2447 		delayed_evtack->event = event;
2448 		delayed_evtack->event_ctx = event_ctx;
2449 		list_add_tail(&delayed_evtack->list,
2450 		    &mrioc->delayed_evtack_cmds_list);
2451 		dprint_event_th(mrioc,
2452 		    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2453 		    event, event_ctx);
2454 		return;
2455 	}
2456 	drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2457 
2458 issue_cmd:
2459 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2460 
2461 	memset(&evtack_req, 0, sizeof(evtack_req));
2462 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2463 		dprint_event_th(mrioc,
2464 		    "sending event ack failed due to command in use\n");
2465 		goto out;
2466 	}
2467 	drv_cmd->state = MPI3MR_CMD_PENDING;
2468 	drv_cmd->is_waiting = 0;
2469 	drv_cmd->callback = mpi3mr_complete_evt_ack;
2470 	evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2471 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2472 	evtack_req.event = event;
2473 	evtack_req.event_context = cpu_to_le32(event_ctx);
2474 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2475 	    sizeof(evtack_req), 1);
2476 	if (retval) {
2477 		dprint_event_th(mrioc,
2478 		    "posting event ack request is failed\n");
2479 		goto out_failed;
2480 	}
2481 
2482 	dprint_event_th(mrioc,
2483 	    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2484 	    event, event_ctx);
2485 out:
2486 	return;
2487 out_failed:
2488 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2489 	drv_cmd->callback = NULL;
2490 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2491 }
2492 
2493 /**
2494  * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2495  * @mrioc: Adapter instance reference
2496  * @event_reply: event data
2497  *
2498  * Checks for the reason code and based on that either block I/O
2499  * to device, or unblock I/O to the device, or start the device
2500  * removal handshake with reason as remove with the firmware for
2501  * PCIe devices.
2502  *
2503  * Return: Nothing
2504  */
mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2505 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2506 	struct mpi3_event_notification_reply *event_reply)
2507 {
2508 	struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2509 	    (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2510 	int i;
2511 	u16 handle;
2512 	u8 reason_code;
2513 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2514 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2515 
2516 	for (i = 0; i < topo_evt->num_entries; i++) {
2517 		handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2518 		if (!handle)
2519 			continue;
2520 		reason_code = topo_evt->port_entry[i].port_status;
2521 		scsi_tgt_priv_data =  NULL;
2522 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2523 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2524 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2525 			    tgtdev->starget->hostdata;
2526 		switch (reason_code) {
2527 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2528 			if (scsi_tgt_priv_data) {
2529 				scsi_tgt_priv_data->dev_removed = 1;
2530 				scsi_tgt_priv_data->dev_removedelay = 0;
2531 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2532 			}
2533 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2534 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2535 			break;
2536 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2537 			if (scsi_tgt_priv_data) {
2538 				scsi_tgt_priv_data->dev_removedelay = 1;
2539 				atomic_inc(&scsi_tgt_priv_data->block_io);
2540 			}
2541 			break;
2542 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2543 			if (scsi_tgt_priv_data &&
2544 			    scsi_tgt_priv_data->dev_removedelay) {
2545 				scsi_tgt_priv_data->dev_removedelay = 0;
2546 				atomic_dec_if_positive
2547 				    (&scsi_tgt_priv_data->block_io);
2548 			}
2549 			break;
2550 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2551 		default:
2552 			break;
2553 		}
2554 		if (tgtdev)
2555 			mpi3mr_tgtdev_put(tgtdev);
2556 	}
2557 }
2558 
2559 /**
2560  * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2561  * @mrioc: Adapter instance reference
2562  * @event_reply: event data
2563  *
2564  * Checks for the reason code and based on that either block I/O
2565  * to device, or unblock I/O to the device, or start the device
2566  * removal handshake with reason as remove with the firmware for
2567  * SAS/SATA devices.
2568  *
2569  * Return: Nothing
2570  */
mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2571 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2572 	struct mpi3_event_notification_reply *event_reply)
2573 {
2574 	struct mpi3_event_data_sas_topology_change_list *topo_evt =
2575 	    (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2576 	int i;
2577 	u16 handle;
2578 	u8 reason_code;
2579 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2580 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2581 
2582 	for (i = 0; i < topo_evt->num_entries; i++) {
2583 		handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2584 		if (!handle)
2585 			continue;
2586 		reason_code = topo_evt->phy_entry[i].status &
2587 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2588 		scsi_tgt_priv_data =  NULL;
2589 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2590 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2591 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2592 			    tgtdev->starget->hostdata;
2593 		switch (reason_code) {
2594 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2595 			if (scsi_tgt_priv_data) {
2596 				scsi_tgt_priv_data->dev_removed = 1;
2597 				scsi_tgt_priv_data->dev_removedelay = 0;
2598 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2599 			}
2600 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2601 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2602 			break;
2603 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2604 			if (scsi_tgt_priv_data) {
2605 				scsi_tgt_priv_data->dev_removedelay = 1;
2606 				atomic_inc(&scsi_tgt_priv_data->block_io);
2607 			}
2608 			break;
2609 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2610 			if (scsi_tgt_priv_data &&
2611 			    scsi_tgt_priv_data->dev_removedelay) {
2612 				scsi_tgt_priv_data->dev_removedelay = 0;
2613 				atomic_dec_if_positive
2614 				    (&scsi_tgt_priv_data->block_io);
2615 			}
2616 			break;
2617 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2618 		default:
2619 			break;
2620 		}
2621 		if (tgtdev)
2622 			mpi3mr_tgtdev_put(tgtdev);
2623 	}
2624 }
2625 
2626 /**
2627  * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2628  * @mrioc: Adapter instance reference
2629  * @event_reply: event data
2630  *
2631  * Checks for the reason code and based on that either block I/O
2632  * to device, or unblock I/O to the device, or start the device
2633  * removal handshake with reason as remove/hide acknowledgment
2634  * with the firmware.
2635  *
2636  * Return: Nothing
2637  */
mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2638 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2639 	struct mpi3_event_notification_reply *event_reply)
2640 {
2641 	u16 dev_handle = 0;
2642 	u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2643 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2644 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2645 	struct mpi3_event_data_device_status_change *evtdata =
2646 	    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2647 
2648 	if (mrioc->stop_drv_processing)
2649 		goto out;
2650 
2651 	dev_handle = le16_to_cpu(evtdata->dev_handle);
2652 
2653 	switch (evtdata->reason_code) {
2654 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2655 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2656 		block = 1;
2657 		break;
2658 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2659 		delete = 1;
2660 		hide = 1;
2661 		break;
2662 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2663 		delete = 1;
2664 		remove = 1;
2665 		break;
2666 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2667 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2668 		ublock = 1;
2669 		break;
2670 	default:
2671 		break;
2672 	}
2673 
2674 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2675 	if (!tgtdev)
2676 		goto out;
2677 	if (hide)
2678 		tgtdev->is_hidden = hide;
2679 	if (tgtdev->starget && tgtdev->starget->hostdata) {
2680 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2681 		    tgtdev->starget->hostdata;
2682 		if (block)
2683 			atomic_inc(&scsi_tgt_priv_data->block_io);
2684 		if (delete)
2685 			scsi_tgt_priv_data->dev_removed = 1;
2686 		if (ublock)
2687 			atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2688 	}
2689 	if (remove)
2690 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2691 		    MPI3_CTRL_OP_REMOVE_DEVICE);
2692 	if (hide)
2693 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2694 		    MPI3_CTRL_OP_HIDDEN_ACK);
2695 
2696 out:
2697 	if (tgtdev)
2698 		mpi3mr_tgtdev_put(tgtdev);
2699 }
2700 
2701 /**
2702  * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2703  * @mrioc: Adapter instance reference
2704  * @event_reply: event data
2705  *
2706  * Blocks and unblocks host level I/O based on the reason code
2707  *
2708  * Return: Nothing
2709  */
mpi3mr_preparereset_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2710 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2711 	struct mpi3_event_notification_reply *event_reply)
2712 {
2713 	struct mpi3_event_data_prepare_for_reset *evtdata =
2714 	    (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2715 
2716 	if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2717 		dprint_event_th(mrioc,
2718 		    "prepare for reset event top half with rc=start\n");
2719 		if (mrioc->prepare_for_reset)
2720 			return;
2721 		mrioc->prepare_for_reset = 1;
2722 		mrioc->prepare_for_reset_timeout_counter = 0;
2723 	} else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2724 		dprint_event_th(mrioc,
2725 		    "prepare for reset top half with rc=abort\n");
2726 		mrioc->prepare_for_reset = 0;
2727 		mrioc->prepare_for_reset_timeout_counter = 0;
2728 	}
2729 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2730 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2731 		mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2732 		    le32_to_cpu(event_reply->event_context));
2733 }
2734 
2735 /**
2736  * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2737  * @mrioc: Adapter instance reference
2738  * @event_reply: event data
2739  *
2740  * Identifies the new shutdown timeout value and update.
2741  *
2742  * Return: Nothing
2743  */
mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2744 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2745 	struct mpi3_event_notification_reply *event_reply)
2746 {
2747 	struct mpi3_event_data_energy_pack_change *evtdata =
2748 	    (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2749 	u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2750 
2751 	if (shutdown_timeout <= 0) {
2752 		ioc_warn(mrioc,
2753 		    "%s :Invalid Shutdown Timeout received = %d\n",
2754 		    __func__, shutdown_timeout);
2755 		return;
2756 	}
2757 
2758 	ioc_info(mrioc,
2759 	    "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2760 	    __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2761 	mrioc->facts.shutdown_timeout = shutdown_timeout;
2762 }
2763 
2764 /**
2765  * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2766  * @mrioc: Adapter instance reference
2767  * @event_reply: event data
2768  *
2769  * Displays Cable manegemt event details.
2770  *
2771  * Return: Nothing
2772  */
mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2773 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2774 	struct mpi3_event_notification_reply *event_reply)
2775 {
2776 	struct mpi3_event_data_cable_management *evtdata =
2777 	    (struct mpi3_event_data_cable_management *)event_reply->event_data;
2778 
2779 	switch (evtdata->status) {
2780 	case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2781 	{
2782 		ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2783 		    "Devices connected to this cable are not detected.\n"
2784 		    "This cable requires %d mW of power.\n",
2785 		    evtdata->receptacle_id,
2786 		    le32_to_cpu(evtdata->active_cable_power_requirement));
2787 		break;
2788 	}
2789 	case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2790 	{
2791 		ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2792 		    evtdata->receptacle_id);
2793 		break;
2794 	}
2795 	default:
2796 		break;
2797 	}
2798 }
2799 
2800 /**
2801  * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
2802  * @mrioc: Adapter instance reference
2803  *
2804  * Add driver specific event to make sure that the driver won't process the
2805  * events until all the devices are refreshed during soft reset.
2806  *
2807  * Return: Nothing
2808  */
mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc * mrioc)2809 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
2810 {
2811 	struct mpi3mr_fwevt *fwevt = NULL;
2812 
2813 	fwevt = mpi3mr_alloc_fwevt(0);
2814 	if (!fwevt) {
2815 		dprint_event_th(mrioc,
2816 		    "failed to schedule bottom half handler for event(0x%02x)\n",
2817 		    MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
2818 		return;
2819 	}
2820 	fwevt->mrioc = mrioc;
2821 	fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
2822 	fwevt->send_ack = 0;
2823 	fwevt->process_evt = 1;
2824 	fwevt->evt_ctx = 0;
2825 	fwevt->event_data_size = 0;
2826 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2827 }
2828 
2829 /**
2830  * mpi3mr_os_handle_events - Firmware event handler
2831  * @mrioc: Adapter instance reference
2832  * @event_reply: event data
2833  *
2834  * Identify whteher the event has to handled and acknowledged
2835  * and either process the event in the tophalf and/or schedule a
2836  * bottom half through mpi3mr_fwevt_worker.
2837  *
2838  * Return: Nothing
2839  */
mpi3mr_os_handle_events(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2840 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2841 	struct mpi3_event_notification_reply *event_reply)
2842 {
2843 	u16 evt_type, sz;
2844 	struct mpi3mr_fwevt *fwevt = NULL;
2845 	bool ack_req = 0, process_evt_bh = 0;
2846 
2847 	if (mrioc->stop_drv_processing)
2848 		return;
2849 
2850 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2851 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2852 		ack_req = 1;
2853 
2854 	evt_type = event_reply->event;
2855 
2856 	switch (evt_type) {
2857 	case MPI3_EVENT_DEVICE_ADDED:
2858 	{
2859 		struct mpi3_device_page0 *dev_pg0 =
2860 		    (struct mpi3_device_page0 *)event_reply->event_data;
2861 		if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2862 			ioc_err(mrioc,
2863 			    "%s :Failed to add device in the device add event\n",
2864 			    __func__);
2865 		else
2866 			process_evt_bh = 1;
2867 		break;
2868 	}
2869 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2870 	{
2871 		process_evt_bh = 1;
2872 		mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2873 		break;
2874 	}
2875 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2876 	{
2877 		process_evt_bh = 1;
2878 		mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2879 		break;
2880 	}
2881 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2882 	{
2883 		process_evt_bh = 1;
2884 		mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2885 		break;
2886 	}
2887 	case MPI3_EVENT_PREPARE_FOR_RESET:
2888 	{
2889 		mpi3mr_preparereset_evt_th(mrioc, event_reply);
2890 		ack_req = 0;
2891 		break;
2892 	}
2893 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2894 	case MPI3_EVENT_LOG_DATA:
2895 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2896 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
2897 	{
2898 		process_evt_bh = 1;
2899 		break;
2900 	}
2901 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
2902 	{
2903 		mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2904 		break;
2905 	}
2906 	case MPI3_EVENT_CABLE_MGMT:
2907 	{
2908 		mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2909 		break;
2910 	}
2911 	case MPI3_EVENT_SAS_DISCOVERY:
2912 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2913 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2914 	case MPI3_EVENT_PCIE_ENUMERATION:
2915 		break;
2916 	default:
2917 		ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2918 		    __func__, evt_type);
2919 		break;
2920 	}
2921 	if (process_evt_bh || ack_req) {
2922 		sz = event_reply->event_data_length * 4;
2923 		fwevt = mpi3mr_alloc_fwevt(sz);
2924 		if (!fwevt) {
2925 			ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2926 			    __func__, __FILE__, __LINE__, __func__);
2927 			return;
2928 		}
2929 
2930 		memcpy(fwevt->event_data, event_reply->event_data, sz);
2931 		fwevt->mrioc = mrioc;
2932 		fwevt->event_id = evt_type;
2933 		fwevt->send_ack = ack_req;
2934 		fwevt->process_evt = process_evt_bh;
2935 		fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2936 		mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2937 	}
2938 }
2939 
2940 /**
2941  * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2942  * @mrioc: Adapter instance reference
2943  * @scmd: SCSI command reference
2944  * @scsiio_req: MPI3 SCSI IO request
2945  *
2946  * Identifies the protection information flags from the SCSI
2947  * command and set appropriate flags in the MPI3 SCSI IO
2948  * request.
2949  *
2950  * Return: Nothing
2951  */
mpi3mr_setup_eedp(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)2952 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2953 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2954 {
2955 	u16 eedp_flags = 0;
2956 	unsigned char prot_op = scsi_get_prot_op(scmd);
2957 
2958 	switch (prot_op) {
2959 	case SCSI_PROT_NORMAL:
2960 		return;
2961 	case SCSI_PROT_READ_STRIP:
2962 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2963 		break;
2964 	case SCSI_PROT_WRITE_INSERT:
2965 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2966 		break;
2967 	case SCSI_PROT_READ_INSERT:
2968 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2969 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2970 		break;
2971 	case SCSI_PROT_WRITE_STRIP:
2972 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2973 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2974 		break;
2975 	case SCSI_PROT_READ_PASS:
2976 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2977 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2978 		break;
2979 	case SCSI_PROT_WRITE_PASS:
2980 		if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2981 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2982 			scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2983 			    0xffff;
2984 		} else
2985 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2986 
2987 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2988 		break;
2989 	default:
2990 		return;
2991 	}
2992 
2993 	if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2994 		eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2995 
2996 	if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2997 		eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2998 
2999 	if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
3000 		eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
3001 			MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3002 		scsiio_req->cdb.eedp32.primary_reference_tag =
3003 			cpu_to_be32(scsi_prot_ref_tag(scmd));
3004 	}
3005 
3006 	if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
3007 		eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3008 
3009 	eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3010 
3011 	switch (scsi_prot_interval(scmd)) {
3012 	case 512:
3013 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3014 		break;
3015 	case 520:
3016 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3017 		break;
3018 	case 4080:
3019 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3020 		break;
3021 	case 4088:
3022 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3023 		break;
3024 	case 4096:
3025 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3026 		break;
3027 	case 4104:
3028 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3029 		break;
3030 	case 4160:
3031 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3032 		break;
3033 	default:
3034 		break;
3035 	}
3036 
3037 	scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3038 	scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3039 }
3040 
3041 /**
3042  * mpi3mr_build_sense_buffer - Map sense information
3043  * @desc: Sense type
3044  * @buf: Sense buffer to populate
3045  * @key: Sense key
3046  * @asc: Additional sense code
3047  * @ascq: Additional sense code qualifier
3048  *
3049  * Maps the given sense information into either descriptor or
3050  * fixed format sense data.
3051  *
3052  * Return: Nothing
3053  */
mpi3mr_build_sense_buffer(int desc,u8 * buf,u8 key,u8 asc,u8 ascq)3054 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3055 	u8 asc, u8 ascq)
3056 {
3057 	if (desc) {
3058 		buf[0] = 0x72;	/* descriptor, current */
3059 		buf[1] = key;
3060 		buf[2] = asc;
3061 		buf[3] = ascq;
3062 		buf[7] = 0;
3063 	} else {
3064 		buf[0] = 0x70;	/* fixed, current */
3065 		buf[2] = key;
3066 		buf[7] = 0xa;
3067 		buf[12] = asc;
3068 		buf[13] = ascq;
3069 	}
3070 }
3071 
3072 /**
3073  * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3074  * @scmd: SCSI command reference
3075  * @ioc_status: status of MPI3 request
3076  *
3077  * Maps the EEDP error status of the SCSI IO request to sense
3078  * data.
3079  *
3080  * Return: Nothing
3081  */
mpi3mr_map_eedp_error(struct scsi_cmnd * scmd,u16 ioc_status)3082 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3083 	u16 ioc_status)
3084 {
3085 	u8 ascq = 0;
3086 
3087 	switch (ioc_status) {
3088 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3089 		ascq = 0x01;
3090 		break;
3091 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3092 		ascq = 0x02;
3093 		break;
3094 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3095 		ascq = 0x03;
3096 		break;
3097 	default:
3098 		ascq = 0x00;
3099 		break;
3100 	}
3101 
3102 	mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3103 	    0x10, ascq);
3104 	scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3105 }
3106 
3107 /**
3108  * mpi3mr_process_op_reply_desc - reply descriptor handler
3109  * @mrioc: Adapter instance reference
3110  * @reply_desc: Operational reply descriptor
3111  * @reply_dma: place holder for reply DMA address
3112  * @qidx: Operational queue index
3113  *
3114  * Process the operational reply descriptor and identifies the
3115  * descriptor type. Based on the descriptor map the MPI3 request
3116  * status to a SCSI command status and calls scsi_done call
3117  * back.
3118  *
3119  * Return: Nothing
3120  */
mpi3mr_process_op_reply_desc(struct mpi3mr_ioc * mrioc,struct mpi3_default_reply_descriptor * reply_desc,u64 * reply_dma,u16 qidx)3121 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3122 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3123 {
3124 	u16 reply_desc_type, host_tag = 0;
3125 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3126 	u32 ioc_loginfo = 0;
3127 	struct mpi3_status_reply_descriptor *status_desc = NULL;
3128 	struct mpi3_address_reply_descriptor *addr_desc = NULL;
3129 	struct mpi3_success_reply_descriptor *success_desc = NULL;
3130 	struct mpi3_scsi_io_reply *scsi_reply = NULL;
3131 	struct scsi_cmnd *scmd = NULL;
3132 	struct scmd_priv *priv = NULL;
3133 	u8 *sense_buf = NULL;
3134 	u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3135 	u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3136 	u16 dev_handle = 0xFFFF;
3137 	struct scsi_sense_hdr sshdr;
3138 	struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3139 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3140 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3141 	struct mpi3mr_throttle_group_info *tg = NULL;
3142 	u8 throttle_enabled_dev = 0;
3143 
3144 	*reply_dma = 0;
3145 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3146 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3147 	switch (reply_desc_type) {
3148 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3149 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3150 		host_tag = le16_to_cpu(status_desc->host_tag);
3151 		ioc_status = le16_to_cpu(status_desc->ioc_status);
3152 		if (ioc_status &
3153 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3154 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3155 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3156 		break;
3157 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3158 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3159 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3160 		scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3161 		    *reply_dma);
3162 		if (!scsi_reply) {
3163 			panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3164 			    mrioc->name);
3165 			goto out;
3166 		}
3167 		host_tag = le16_to_cpu(scsi_reply->host_tag);
3168 		ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3169 		scsi_status = scsi_reply->scsi_status;
3170 		scsi_state = scsi_reply->scsi_state;
3171 		dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3172 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3173 		xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3174 		sense_count = le32_to_cpu(scsi_reply->sense_count);
3175 		resp_data = le32_to_cpu(scsi_reply->response_data);
3176 		sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3177 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3178 		if (ioc_status &
3179 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3180 			ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3181 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3182 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3183 			panic("%s: Ran out of sense buffers\n", mrioc->name);
3184 		break;
3185 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3186 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3187 		host_tag = le16_to_cpu(success_desc->host_tag);
3188 		break;
3189 	default:
3190 		break;
3191 	}
3192 	scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3193 	if (!scmd) {
3194 		panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
3195 		    mrioc->name, host_tag);
3196 		goto out;
3197 	}
3198 	priv = scsi_cmd_priv(scmd);
3199 
3200 	data_len_blks = scsi_bufflen(scmd) >> 9;
3201 	sdev_priv_data = scmd->device->hostdata;
3202 	if (sdev_priv_data) {
3203 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
3204 		if (stgt_priv_data) {
3205 			tg = stgt_priv_data->throttle_group;
3206 			throttle_enabled_dev =
3207 			    stgt_priv_data->io_throttle_enabled;
3208 		}
3209 	}
3210 	if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3211 	    throttle_enabled_dev)) {
3212 		ioc_pend_data_len = atomic_sub_return(data_len_blks,
3213 		    &mrioc->pend_large_data_sz);
3214 		if (tg) {
3215 			tg_pend_data_len = atomic_sub_return(data_len_blks,
3216 			    &tg->pend_large_data_sz);
3217 			if (tg->io_divert  && ((ioc_pend_data_len <=
3218 			    mrioc->io_throttle_low) &&
3219 			    (tg_pend_data_len <= tg->low))) {
3220 				tg->io_divert = 0;
3221 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3222 				    mrioc, tg, 0);
3223 			}
3224 		} else {
3225 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3226 				stgt_priv_data->io_divert = 0;
3227 		}
3228 	} else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3229 		ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3230 		if (!tg) {
3231 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3232 				stgt_priv_data->io_divert = 0;
3233 
3234 		} else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3235 			tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3236 			if (tg->io_divert  && (tg_pend_data_len <= tg->low)) {
3237 				tg->io_divert = 0;
3238 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3239 				    mrioc, tg, 0);
3240 			}
3241 		}
3242 	}
3243 
3244 	if (success_desc) {
3245 		scmd->result = DID_OK << 16;
3246 		goto out_success;
3247 	}
3248 
3249 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3250 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3251 	    xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3252 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3253 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3254 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
3255 
3256 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3257 	    sense_buf) {
3258 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3259 
3260 		memcpy(scmd->sense_buffer, sense_buf, sz);
3261 	}
3262 
3263 	switch (ioc_status) {
3264 	case MPI3_IOCSTATUS_BUSY:
3265 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3266 		scmd->result = SAM_STAT_BUSY;
3267 		break;
3268 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3269 		scmd->result = DID_NO_CONNECT << 16;
3270 		break;
3271 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3272 		scmd->result = DID_SOFT_ERROR << 16;
3273 		break;
3274 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3275 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3276 		scmd->result = DID_RESET << 16;
3277 		break;
3278 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3279 		if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3280 			scmd->result = DID_SOFT_ERROR << 16;
3281 		else
3282 			scmd->result = (DID_OK << 16) | scsi_status;
3283 		break;
3284 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3285 		scmd->result = (DID_OK << 16) | scsi_status;
3286 		if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3287 			break;
3288 		if (xfer_count < scmd->underflow) {
3289 			if (scsi_status == SAM_STAT_BUSY)
3290 				scmd->result = SAM_STAT_BUSY;
3291 			else
3292 				scmd->result = DID_SOFT_ERROR << 16;
3293 		} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3294 		    (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3295 			scmd->result = DID_SOFT_ERROR << 16;
3296 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3297 			scmd->result = DID_RESET << 16;
3298 		break;
3299 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3300 		scsi_set_resid(scmd, 0);
3301 		fallthrough;
3302 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3303 	case MPI3_IOCSTATUS_SUCCESS:
3304 		scmd->result = (DID_OK << 16) | scsi_status;
3305 		if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3306 		    (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3307 			(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3308 			scmd->result = DID_SOFT_ERROR << 16;
3309 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3310 			scmd->result = DID_RESET << 16;
3311 		break;
3312 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3313 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3314 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3315 		mpi3mr_map_eedp_error(scmd, ioc_status);
3316 		break;
3317 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3318 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
3319 	case MPI3_IOCSTATUS_INVALID_SGL:
3320 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
3321 	case MPI3_IOCSTATUS_INVALID_FIELD:
3322 	case MPI3_IOCSTATUS_INVALID_STATE:
3323 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3324 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3325 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3326 	default:
3327 		scmd->result = DID_SOFT_ERROR << 16;
3328 		break;
3329 	}
3330 
3331 	if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3332 	    (scmd->cmnd[0] != ATA_16) &&
3333 	    mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3334 		ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3335 		    scmd->result);
3336 		scsi_print_command(scmd);
3337 		ioc_info(mrioc,
3338 		    "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3339 		    __func__, dev_handle, ioc_status, ioc_loginfo,
3340 		    priv->req_q_idx + 1);
3341 		ioc_info(mrioc,
3342 		    " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3343 		    host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3344 		if (sense_buf) {
3345 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3346 			ioc_info(mrioc,
3347 			    "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3348 			    __func__, sense_count, sshdr.sense_key,
3349 			    sshdr.asc, sshdr.ascq);
3350 		}
3351 	}
3352 out_success:
3353 	if (priv->meta_sg_valid) {
3354 		dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3355 		    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3356 	}
3357 	mpi3mr_clear_scmd_priv(mrioc, scmd);
3358 	scsi_dma_unmap(scmd);
3359 	scsi_done(scmd);
3360 out:
3361 	if (sense_buf)
3362 		mpi3mr_repost_sense_buf(mrioc,
3363 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3364 }
3365 
3366 /**
3367  * mpi3mr_get_chain_idx - get free chain buffer index
3368  * @mrioc: Adapter instance reference
3369  *
3370  * Try to get a free chain buffer index from the free pool.
3371  *
3372  * Return: -1 on failure or the free chain buffer index
3373  */
mpi3mr_get_chain_idx(struct mpi3mr_ioc * mrioc)3374 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3375 {
3376 	u8 retry_count = 5;
3377 	int cmd_idx = -1;
3378 	unsigned long flags;
3379 
3380 	spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3381 	do {
3382 		cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3383 		    mrioc->chain_buf_count);
3384 		if (cmd_idx < mrioc->chain_buf_count) {
3385 			set_bit(cmd_idx, mrioc->chain_bitmap);
3386 			break;
3387 		}
3388 		cmd_idx = -1;
3389 	} while (retry_count--);
3390 	spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3391 	return cmd_idx;
3392 }
3393 
3394 /**
3395  * mpi3mr_prepare_sg_scmd - build scatter gather list
3396  * @mrioc: Adapter instance reference
3397  * @scmd: SCSI command reference
3398  * @scsiio_req: MPI3 SCSI IO request
3399  *
3400  * This function maps SCSI command's data and protection SGEs to
3401  * MPI request SGEs. If required additional 4K chain buffer is
3402  * used to send the SGEs.
3403  *
3404  * Return: 0 on success, -ENOMEM on dma_map_sg failure
3405  */
mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)3406 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3407 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3408 {
3409 	dma_addr_t chain_dma;
3410 	struct scatterlist *sg_scmd;
3411 	void *sg_local, *chain;
3412 	u32 chain_length;
3413 	int sges_left, chain_idx;
3414 	u32 sges_in_segment;
3415 	u8 simple_sgl_flags;
3416 	u8 simple_sgl_flags_last;
3417 	u8 last_chain_sgl_flags;
3418 	struct chain_element *chain_req;
3419 	struct scmd_priv *priv = NULL;
3420 	u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3421 	    MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3422 
3423 	priv = scsi_cmd_priv(scmd);
3424 
3425 	simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3426 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3427 	simple_sgl_flags_last = simple_sgl_flags |
3428 	    MPI3_SGE_FLAGS_END_OF_LIST;
3429 	last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3430 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3431 
3432 	if (meta_sg)
3433 		sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3434 	else
3435 		sg_local = &scsiio_req->sgl;
3436 
3437 	if (!scsiio_req->data_length && !meta_sg) {
3438 		mpi3mr_build_zero_len_sge(sg_local);
3439 		return 0;
3440 	}
3441 
3442 	if (meta_sg) {
3443 		sg_scmd = scsi_prot_sglist(scmd);
3444 		sges_left = dma_map_sg(&mrioc->pdev->dev,
3445 		    scsi_prot_sglist(scmd),
3446 		    scsi_prot_sg_count(scmd),
3447 		    scmd->sc_data_direction);
3448 		priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3449 	} else {
3450 		/*
3451 		 * Some firmware versions byte-swap the REPORT ZONES command
3452 		 * reply from ATA-ZAC devices by directly accessing in the host
3453 		 * buffer. This does not respect the default command DMA
3454 		 * direction and causes IOMMU page faults on some architectures
3455 		 * with an IOMMU enforcing write mappings (e.g. AMD hosts).
3456 		 * Avoid such issue by making the REPORT ZONES buffer mapping
3457 		 * bi-directional.
3458 		 */
3459 		if (scmd->cmnd[0] == ZBC_IN && scmd->cmnd[1] == ZI_REPORT_ZONES)
3460 			scmd->sc_data_direction = DMA_BIDIRECTIONAL;
3461 		sg_scmd = scsi_sglist(scmd);
3462 		sges_left = scsi_dma_map(scmd);
3463 	}
3464 
3465 	if (sges_left < 0) {
3466 		sdev_printk(KERN_ERR, scmd->device,
3467 		    "scsi_dma_map failed: request for %d bytes!\n",
3468 		    scsi_bufflen(scmd));
3469 		return -ENOMEM;
3470 	}
3471 	if (sges_left > mrioc->max_sgl_entries) {
3472 		sdev_printk(KERN_ERR, scmd->device,
3473 		    "scsi_dma_map returned unsupported sge count %d!\n",
3474 		    sges_left);
3475 		return -ENOMEM;
3476 	}
3477 
3478 	sges_in_segment = (mrioc->facts.op_req_sz -
3479 	    offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3480 
3481 	if (scsiio_req->sgl[0].eedp.flags ==
3482 	    MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3483 		sg_local += sizeof(struct mpi3_sge_common);
3484 		sges_in_segment--;
3485 		/* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3486 	}
3487 
3488 	if (scsiio_req->msg_flags ==
3489 	    MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3490 		sges_in_segment--;
3491 		/* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3492 	}
3493 
3494 	if (meta_sg)
3495 		sges_in_segment = 1;
3496 
3497 	if (sges_left <= sges_in_segment)
3498 		goto fill_in_last_segment;
3499 
3500 	/* fill in main message segment when there is a chain following */
3501 	while (sges_in_segment > 1) {
3502 		mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3503 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3504 		sg_scmd = sg_next(sg_scmd);
3505 		sg_local += sizeof(struct mpi3_sge_common);
3506 		sges_left--;
3507 		sges_in_segment--;
3508 	}
3509 
3510 	chain_idx = mpi3mr_get_chain_idx(mrioc);
3511 	if (chain_idx < 0)
3512 		return -1;
3513 	chain_req = &mrioc->chain_sgl_list[chain_idx];
3514 	if (meta_sg)
3515 		priv->meta_chain_idx = chain_idx;
3516 	else
3517 		priv->chain_idx = chain_idx;
3518 
3519 	chain = chain_req->addr;
3520 	chain_dma = chain_req->dma_addr;
3521 	sges_in_segment = sges_left;
3522 	chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3523 
3524 	mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3525 	    chain_length, chain_dma);
3526 
3527 	sg_local = chain;
3528 
3529 fill_in_last_segment:
3530 	while (sges_left > 0) {
3531 		if (sges_left == 1)
3532 			mpi3mr_add_sg_single(sg_local,
3533 			    simple_sgl_flags_last, sg_dma_len(sg_scmd),
3534 			    sg_dma_address(sg_scmd));
3535 		else
3536 			mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3537 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3538 		sg_scmd = sg_next(sg_scmd);
3539 		sg_local += sizeof(struct mpi3_sge_common);
3540 		sges_left--;
3541 	}
3542 
3543 	return 0;
3544 }
3545 
3546 /**
3547  * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3548  * @mrioc: Adapter instance reference
3549  * @scmd: SCSI command reference
3550  * @scsiio_req: MPI3 SCSI IO request
3551  *
3552  * This function calls mpi3mr_prepare_sg_scmd for constructing
3553  * both data SGEs and protection information SGEs in the MPI
3554  * format from the SCSI Command as appropriate .
3555  *
3556  * Return: return value of mpi3mr_prepare_sg_scmd.
3557  */
mpi3mr_build_sg_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)3558 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3559 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3560 {
3561 	int ret;
3562 
3563 	ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3564 	if (ret)
3565 		return ret;
3566 
3567 	if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3568 		/* There is a valid meta sg */
3569 		scsiio_req->flags |=
3570 		    cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3571 		ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3572 	}
3573 
3574 	return ret;
3575 }
3576 
3577 /**
3578  * mpi3mr_tm_response_name -  get TM response as a string
3579  * @resp_code: TM response code
3580  *
3581  * Convert known task management response code as a readable
3582  * string.
3583  *
3584  * Return: response code string.
3585  */
mpi3mr_tm_response_name(u8 resp_code)3586 static const char *mpi3mr_tm_response_name(u8 resp_code)
3587 {
3588 	char *desc;
3589 
3590 	switch (resp_code) {
3591 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3592 		desc = "task management request completed";
3593 		break;
3594 	case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3595 		desc = "invalid frame";
3596 		break;
3597 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3598 		desc = "task management request not supported";
3599 		break;
3600 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3601 		desc = "task management request failed";
3602 		break;
3603 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3604 		desc = "task management request succeeded";
3605 		break;
3606 	case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3607 		desc = "invalid LUN";
3608 		break;
3609 	case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3610 		desc = "overlapped tag attempted";
3611 		break;
3612 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3613 		desc = "task queued, however not sent to target";
3614 		break;
3615 	case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3616 		desc = "task management request denied by NVMe device";
3617 		break;
3618 	default:
3619 		desc = "unknown";
3620 		break;
3621 	}
3622 
3623 	return desc;
3624 }
3625 
mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc * mrioc)3626 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3627 {
3628 	int i;
3629 	int num_of_reply_queues =
3630 	    mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3631 
3632 	for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3633 		mpi3mr_process_op_reply_q(mrioc,
3634 		    mrioc->intr_info[i].op_reply_q);
3635 }
3636 
3637 /**
3638  * mpi3mr_issue_tm - Issue Task Management request
3639  * @mrioc: Adapter instance reference
3640  * @tm_type: Task Management type
3641  * @handle: Device handle
3642  * @lun: lun ID
3643  * @htag: Host tag of the TM request
3644  * @timeout: TM timeout value
3645  * @drv_cmd: Internal command tracker
3646  * @resp_code: Response code place holder
3647  * @scmd: SCSI command
3648  *
3649  * Issues a Task Management Request to the controller for a
3650  * specified target, lun and command and wait for its completion
3651  * and check TM response. Recover the TM if it timed out by
3652  * issuing controller reset.
3653  *
3654  * Return: 0 on success, non-zero on errors
3655  */
mpi3mr_issue_tm(struct mpi3mr_ioc * mrioc,u8 tm_type,u16 handle,uint lun,u16 htag,ulong timeout,struct mpi3mr_drv_cmd * drv_cmd,u8 * resp_code,struct scsi_cmnd * scmd)3656 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3657 	u16 handle, uint lun, u16 htag, ulong timeout,
3658 	struct mpi3mr_drv_cmd *drv_cmd,
3659 	u8 *resp_code, struct scsi_cmnd *scmd)
3660 {
3661 	struct mpi3_scsi_task_mgmt_request tm_req;
3662 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3663 	int retval = 0;
3664 	struct mpi3mr_tgt_dev *tgtdev = NULL;
3665 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3666 	struct scmd_priv *cmd_priv = NULL;
3667 	struct scsi_device *sdev = NULL;
3668 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3669 
3670 	ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3671 	     __func__, tm_type, handle);
3672 	if (mrioc->unrecoverable) {
3673 		retval = -1;
3674 		ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3675 		    __func__);
3676 		goto out;
3677 	}
3678 
3679 	memset(&tm_req, 0, sizeof(tm_req));
3680 	mutex_lock(&drv_cmd->mutex);
3681 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3682 		retval = -1;
3683 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3684 		mutex_unlock(&drv_cmd->mutex);
3685 		goto out;
3686 	}
3687 	if (mrioc->reset_in_progress) {
3688 		retval = -1;
3689 		ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3690 		mutex_unlock(&drv_cmd->mutex);
3691 		goto out;
3692 	}
3693 
3694 	drv_cmd->state = MPI3MR_CMD_PENDING;
3695 	drv_cmd->is_waiting = 1;
3696 	drv_cmd->callback = NULL;
3697 	tm_req.dev_handle = cpu_to_le16(handle);
3698 	tm_req.task_type = tm_type;
3699 	tm_req.host_tag = cpu_to_le16(htag);
3700 
3701 	int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3702 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3703 
3704 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3705 
3706 	if (scmd) {
3707 		sdev = scmd->device;
3708 		sdev_priv_data = sdev->hostdata;
3709 		scsi_tgt_priv_data = ((sdev_priv_data) ?
3710 		    sdev_priv_data->tgt_priv_data : NULL);
3711 	} else {
3712 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3713 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3714 			    tgtdev->starget->hostdata;
3715 	}
3716 
3717 	if (scsi_tgt_priv_data)
3718 		atomic_inc(&scsi_tgt_priv_data->block_io);
3719 
3720 	if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3721 		if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3722 			timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3723 		else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3724 			timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3725 	}
3726 
3727 	init_completion(&drv_cmd->done);
3728 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3729 	if (retval) {
3730 		ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3731 		goto out_unlock;
3732 	}
3733 	wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3734 
3735 	if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3736 		drv_cmd->is_waiting = 0;
3737 		retval = -1;
3738 		if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3739 			dprint_tm(mrioc,
3740 			    "task management request timed out after %ld seconds\n",
3741 			    timeout);
3742 			if (mrioc->logging_level & MPI3_DEBUG_TM)
3743 				dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3744 			mpi3mr_soft_reset_handler(mrioc,
3745 			    MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3746 		}
3747 		goto out_unlock;
3748 	}
3749 
3750 	if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3751 		dprint_tm(mrioc, "invalid task management reply message\n");
3752 		retval = -1;
3753 		goto out_unlock;
3754 	}
3755 
3756 	tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3757 
3758 	switch (drv_cmd->ioc_status) {
3759 	case MPI3_IOCSTATUS_SUCCESS:
3760 		*resp_code = le32_to_cpu(tm_reply->response_data) &
3761 			MPI3MR_RI_MASK_RESPCODE;
3762 		break;
3763 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3764 		*resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3765 		break;
3766 	default:
3767 		dprint_tm(mrioc,
3768 		    "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3769 		    handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3770 		retval = -1;
3771 		goto out_unlock;
3772 	}
3773 
3774 	switch (*resp_code) {
3775 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3776 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3777 		break;
3778 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3779 		if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3780 			retval = -1;
3781 		break;
3782 	default:
3783 		retval = -1;
3784 		break;
3785 	}
3786 
3787 	dprint_tm(mrioc,
3788 	    "task management request type(%d) completed for handle(0x%04x) with ioc_status(0x%04x), log_info(0x%08x), termination_count(%d), response:%s(0x%x)\n",
3789 	    tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3790 	    le32_to_cpu(tm_reply->termination_count),
3791 	    mpi3mr_tm_response_name(*resp_code), *resp_code);
3792 
3793 	if (!retval) {
3794 		mpi3mr_ioc_disable_intr(mrioc);
3795 		mpi3mr_poll_pend_io_completions(mrioc);
3796 		mpi3mr_ioc_enable_intr(mrioc);
3797 		mpi3mr_poll_pend_io_completions(mrioc);
3798 		mpi3mr_process_admin_reply_q(mrioc);
3799 	}
3800 	switch (tm_type) {
3801 	case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3802 		if (!scsi_tgt_priv_data)
3803 			break;
3804 		scsi_tgt_priv_data->pend_count = 0;
3805 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3806 		    mpi3mr_count_tgt_pending,
3807 		    (void *)scsi_tgt_priv_data->starget);
3808 		break;
3809 	case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3810 		if (!sdev_priv_data)
3811 			break;
3812 		sdev_priv_data->pend_count = 0;
3813 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3814 		    mpi3mr_count_dev_pending, (void *)sdev);
3815 		break;
3816 	default:
3817 		break;
3818 	}
3819 
3820 out_unlock:
3821 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3822 	mutex_unlock(&drv_cmd->mutex);
3823 	if (scsi_tgt_priv_data)
3824 		atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3825 	if (tgtdev)
3826 		mpi3mr_tgtdev_put(tgtdev);
3827 out:
3828 	return retval;
3829 }
3830 
3831 /**
3832  * mpi3mr_bios_param - BIOS param callback
3833  * @sdev: SCSI device reference
3834  * @bdev: Block device reference
3835  * @capacity: Capacity in logical sectors
3836  * @params: Parameter array
3837  *
3838  * Just the parameters with heads/secots/cylinders.
3839  *
3840  * Return: 0 always
3841  */
mpi3mr_bios_param(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int params[])3842 static int mpi3mr_bios_param(struct scsi_device *sdev,
3843 	struct block_device *bdev, sector_t capacity, int params[])
3844 {
3845 	int heads;
3846 	int sectors;
3847 	sector_t cylinders;
3848 	ulong dummy;
3849 
3850 	heads = 64;
3851 	sectors = 32;
3852 
3853 	dummy = heads * sectors;
3854 	cylinders = capacity;
3855 	sector_div(cylinders, dummy);
3856 
3857 	if ((ulong)capacity >= 0x200000) {
3858 		heads = 255;
3859 		sectors = 63;
3860 		dummy = heads * sectors;
3861 		cylinders = capacity;
3862 		sector_div(cylinders, dummy);
3863 	}
3864 
3865 	params[0] = heads;
3866 	params[1] = sectors;
3867 	params[2] = cylinders;
3868 	return 0;
3869 }
3870 
3871 /**
3872  * mpi3mr_map_queues - Map queues callback handler
3873  * @shost: SCSI host reference
3874  *
3875  * Maps default and poll queues.
3876  *
3877  * Return: return zero.
3878  */
mpi3mr_map_queues(struct Scsi_Host * shost)3879 static void mpi3mr_map_queues(struct Scsi_Host *shost)
3880 {
3881 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3882 	int i, qoff, offset;
3883 	struct blk_mq_queue_map *map = NULL;
3884 
3885 	offset = mrioc->op_reply_q_offset;
3886 
3887 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3888 		map = &shost->tag_set.map[i];
3889 
3890 		map->nr_queues  = 0;
3891 
3892 		if (i == HCTX_TYPE_DEFAULT)
3893 			map->nr_queues = mrioc->default_qcount;
3894 		else if (i == HCTX_TYPE_POLL)
3895 			map->nr_queues = mrioc->active_poll_qcount;
3896 
3897 		if (!map->nr_queues) {
3898 			BUG_ON(i == HCTX_TYPE_DEFAULT);
3899 			continue;
3900 		}
3901 
3902 		/*
3903 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3904 		 * affinity), so use the regular blk-mq cpu mapping
3905 		 */
3906 		map->queue_offset = qoff;
3907 		if (i != HCTX_TYPE_POLL)
3908 			blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3909 		else
3910 			blk_mq_map_queues(map);
3911 
3912 		qoff += map->nr_queues;
3913 		offset += map->nr_queues;
3914 	}
3915 }
3916 
3917 /**
3918  * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3919  * @mrioc: Adapter instance reference
3920  *
3921  * Calculate the pending I/Os for the controller and return.
3922  *
3923  * Return: Number of pending I/Os
3924  */
mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc * mrioc)3925 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3926 {
3927 	u16 i;
3928 	uint pend_ios = 0;
3929 
3930 	for (i = 0; i < mrioc->num_op_reply_q; i++)
3931 		pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3932 	return pend_ios;
3933 }
3934 
3935 /**
3936  * mpi3mr_print_pending_host_io - print pending I/Os
3937  * @mrioc: Adapter instance reference
3938  *
3939  * Print number of pending I/Os and each I/O details prior to
3940  * reset for debug purpose.
3941  *
3942  * Return: Nothing
3943  */
mpi3mr_print_pending_host_io(struct mpi3mr_ioc * mrioc)3944 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3945 {
3946 	struct Scsi_Host *shost = mrioc->shost;
3947 
3948 	ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3949 	    __func__, mpi3mr_get_fw_pending_ios(mrioc));
3950 	blk_mq_tagset_busy_iter(&shost->tag_set,
3951 	    mpi3mr_print_scmd, (void *)mrioc);
3952 }
3953 
3954 /**
3955  * mpi3mr_wait_for_host_io - block for I/Os to complete
3956  * @mrioc: Adapter instance reference
3957  * @timeout: time out in seconds
3958  * Waits for pending I/Os for the given adapter to complete or
3959  * to hit the timeout.
3960  *
3961  * Return: Nothing
3962  */
mpi3mr_wait_for_host_io(struct mpi3mr_ioc * mrioc,u32 timeout)3963 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3964 {
3965 	enum mpi3mr_iocstate iocstate;
3966 	int i = 0;
3967 
3968 	iocstate = mpi3mr_get_iocstate(mrioc);
3969 	if (iocstate != MRIOC_STATE_READY)
3970 		return;
3971 
3972 	if (!mpi3mr_get_fw_pending_ios(mrioc))
3973 		return;
3974 	ioc_info(mrioc,
3975 	    "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3976 	    __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3977 
3978 	for (i = 0; i < timeout; i++) {
3979 		if (!mpi3mr_get_fw_pending_ios(mrioc))
3980 			break;
3981 		iocstate = mpi3mr_get_iocstate(mrioc);
3982 		if (iocstate != MRIOC_STATE_READY)
3983 			break;
3984 		msleep(1000);
3985 	}
3986 
3987 	ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3988 	    mpi3mr_get_fw_pending_ios(mrioc));
3989 }
3990 
3991 /**
3992  * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
3993  * @mrioc: Adapter instance reference
3994  * @scmd: SCSI command reference
3995  * @scsiio_req: MPI3 SCSI IO request
3996  * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
3997  * @wslen: write same max length
3998  *
3999  * Gets values of unmap, ndob and number of blocks from write
4000  * same scsi io and based on these values it sets divert IO flag
4001  * and reason for diverting IO to firmware.
4002  *
4003  * Return: Nothing
4004  */
mpi3mr_setup_divert_ws(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req,u32 * scsiio_flags,u16 wslen)4005 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
4006 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
4007 	u32 *scsiio_flags, u16 wslen)
4008 {
4009 	u8 unmap = 0, ndob = 0;
4010 	u8 opcode = scmd->cmnd[0];
4011 	u32 num_blocks = 0;
4012 	u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
4013 
4014 	if (opcode == WRITE_SAME_16) {
4015 		unmap = scmd->cmnd[1] & 0x08;
4016 		ndob = scmd->cmnd[1] & 0x01;
4017 		num_blocks = get_unaligned_be32(scmd->cmnd + 10);
4018 	} else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
4019 		unmap = scmd->cmnd[10] & 0x08;
4020 		ndob = scmd->cmnd[10] & 0x01;
4021 		num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4022 	} else
4023 		return;
4024 
4025 	if ((unmap) && (ndob) && (num_blocks > wslen)) {
4026 		scsiio_req->msg_flags |=
4027 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4028 		*scsiio_flags |=
4029 			MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4030 	}
4031 }
4032 
4033 /**
4034  * mpi3mr_eh_host_reset - Host reset error handling callback
4035  * @scmd: SCSI command reference
4036  *
4037  * Issue controller reset if the scmd is for a Physical Device,
4038  * if the scmd is for RAID volume, then wait for
4039  * MPI3MR_RAID_ERRREC_RESET_TIMEOUT and checke whether any
4040  * pending I/Os prior to issuing reset to the controller.
4041  *
4042  * Return: SUCCESS of successful reset else FAILED
4043  */
mpi3mr_eh_host_reset(struct scsi_cmnd * scmd)4044 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4045 {
4046 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4047 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4048 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4049 	u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4050 	int retval = FAILED, ret;
4051 
4052 	sdev_priv_data = scmd->device->hostdata;
4053 	if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4054 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
4055 		dev_type = stgt_priv_data->dev_type;
4056 	}
4057 
4058 	if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4059 		mpi3mr_wait_for_host_io(mrioc,
4060 		    MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4061 		if (!mpi3mr_get_fw_pending_ios(mrioc)) {
4062 			retval = SUCCESS;
4063 			goto out;
4064 		}
4065 	}
4066 
4067 	mpi3mr_print_pending_host_io(mrioc);
4068 	ret = mpi3mr_soft_reset_handler(mrioc,
4069 	    MPI3MR_RESET_FROM_EH_HOS, 1);
4070 	if (ret)
4071 		goto out;
4072 
4073 	retval = SUCCESS;
4074 out:
4075 	sdev_printk(KERN_INFO, scmd->device,
4076 	    "Host reset is %s for scmd(%p)\n",
4077 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4078 
4079 	return retval;
4080 }
4081 
4082 /**
4083  * mpi3mr_eh_target_reset - Target reset error handling callback
4084  * @scmd: SCSI command reference
4085  *
4086  * Issue Target reset Task Management and verify the scmd is
4087  * terminated successfully and return status accordingly.
4088  *
4089  * Return: SUCCESS of successful termination of the scmd else
4090  *         FAILED
4091  */
mpi3mr_eh_target_reset(struct scsi_cmnd * scmd)4092 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4093 {
4094 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4095 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4096 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4097 	u16 dev_handle;
4098 	u8 resp_code = 0;
4099 	int retval = FAILED, ret = 0;
4100 
4101 	sdev_printk(KERN_INFO, scmd->device,
4102 	    "Attempting Target Reset! scmd(%p)\n", scmd);
4103 	scsi_print_command(scmd);
4104 
4105 	sdev_priv_data = scmd->device->hostdata;
4106 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4107 		sdev_printk(KERN_INFO, scmd->device,
4108 		    "SCSI device is not available\n");
4109 		retval = SUCCESS;
4110 		goto out;
4111 	}
4112 
4113 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4114 	dev_handle = stgt_priv_data->dev_handle;
4115 	if (stgt_priv_data->dev_removed) {
4116 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4117 		sdev_printk(KERN_INFO, scmd->device,
4118 		    "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4119 		    mrioc->name, dev_handle);
4120 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4121 			retval = SUCCESS;
4122 		else
4123 			retval = FAILED;
4124 		goto out;
4125 	}
4126 	sdev_printk(KERN_INFO, scmd->device,
4127 	    "Target Reset is issued to handle(0x%04x)\n",
4128 	    dev_handle);
4129 
4130 	ret = mpi3mr_issue_tm(mrioc,
4131 	    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4132 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4133 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4134 
4135 	if (ret)
4136 		goto out;
4137 
4138 	if (stgt_priv_data->pend_count) {
4139 		sdev_printk(KERN_INFO, scmd->device,
4140 		    "%s: target has %d pending commands, target reset is failed\n",
4141 		    mrioc->name, stgt_priv_data->pend_count);
4142 		goto out;
4143 	}
4144 
4145 	retval = SUCCESS;
4146 out:
4147 	sdev_printk(KERN_INFO, scmd->device,
4148 	    "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4149 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4150 
4151 	return retval;
4152 }
4153 
4154 /**
4155  * mpi3mr_eh_dev_reset- Device reset error handling callback
4156  * @scmd: SCSI command reference
4157  *
4158  * Issue lun reset Task Management and verify the scmd is
4159  * terminated successfully and return status accordingly.
4160  *
4161  * Return: SUCCESS of successful termination of the scmd else
4162  *         FAILED
4163  */
mpi3mr_eh_dev_reset(struct scsi_cmnd * scmd)4164 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4165 {
4166 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4167 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4168 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4169 	u16 dev_handle;
4170 	u8 resp_code = 0;
4171 	int retval = FAILED, ret = 0;
4172 
4173 	sdev_printk(KERN_INFO, scmd->device,
4174 	    "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4175 	scsi_print_command(scmd);
4176 
4177 	sdev_priv_data = scmd->device->hostdata;
4178 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4179 		sdev_printk(KERN_INFO, scmd->device,
4180 		    "SCSI device is not available\n");
4181 		retval = SUCCESS;
4182 		goto out;
4183 	}
4184 
4185 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4186 	dev_handle = stgt_priv_data->dev_handle;
4187 	if (stgt_priv_data->dev_removed) {
4188 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4189 		sdev_printk(KERN_INFO, scmd->device,
4190 		    "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4191 		    mrioc->name, dev_handle);
4192 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4193 			retval = SUCCESS;
4194 		else
4195 			retval = FAILED;
4196 		goto out;
4197 	}
4198 	sdev_printk(KERN_INFO, scmd->device,
4199 	    "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4200 
4201 	ret = mpi3mr_issue_tm(mrioc,
4202 	    MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4203 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4204 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4205 
4206 	if (ret)
4207 		goto out;
4208 
4209 	if (sdev_priv_data->pend_count) {
4210 		sdev_printk(KERN_INFO, scmd->device,
4211 		    "%s: device has %d pending commands, device(LUN) reset is failed\n",
4212 		    mrioc->name, sdev_priv_data->pend_count);
4213 		goto out;
4214 	}
4215 	retval = SUCCESS;
4216 out:
4217 	sdev_printk(KERN_INFO, scmd->device,
4218 	    "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4219 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4220 
4221 	return retval;
4222 }
4223 
4224 /**
4225  * mpi3mr_scan_start - Scan start callback handler
4226  * @shost: SCSI host reference
4227  *
4228  * Issue port enable request asynchronously.
4229  *
4230  * Return: Nothing
4231  */
mpi3mr_scan_start(struct Scsi_Host * shost)4232 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4233 {
4234 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4235 
4236 	mrioc->scan_started = 1;
4237 	ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4238 	if (mpi3mr_issue_port_enable(mrioc, 1)) {
4239 		ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4240 		mrioc->scan_started = 0;
4241 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4242 	}
4243 }
4244 
4245 /**
4246  * mpi3mr_scan_finished - Scan finished callback handler
4247  * @shost: SCSI host reference
4248  * @time: Jiffies from the scan start
4249  *
4250  * Checks whether the port enable is completed or timedout or
4251  * failed and set the scan status accordingly after taking any
4252  * recovery if required.
4253  *
4254  * Return: 1 on scan finished or timed out, 0 for in progress
4255  */
mpi3mr_scan_finished(struct Scsi_Host * shost,unsigned long time)4256 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4257 	unsigned long time)
4258 {
4259 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4260 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4261 	u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4262 
4263 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4264 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4265 		ioc_err(mrioc, "port enable failed due to fault or reset\n");
4266 		mpi3mr_print_fault_info(mrioc);
4267 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4268 		mrioc->scan_started = 0;
4269 		mrioc->init_cmds.is_waiting = 0;
4270 		mrioc->init_cmds.callback = NULL;
4271 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4272 	}
4273 
4274 	if (time >= (pe_timeout * HZ)) {
4275 		ioc_err(mrioc, "port enable failed due to time out\n");
4276 		mpi3mr_check_rh_fault_ioc(mrioc,
4277 		    MPI3MR_RESET_FROM_PE_TIMEOUT);
4278 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4279 		mrioc->scan_started = 0;
4280 		mrioc->init_cmds.is_waiting = 0;
4281 		mrioc->init_cmds.callback = NULL;
4282 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4283 	}
4284 
4285 	if (mrioc->scan_started)
4286 		return 0;
4287 
4288 	if (mrioc->scan_failed) {
4289 		ioc_err(mrioc,
4290 		    "port enable failed with status=0x%04x\n",
4291 		    mrioc->scan_failed);
4292 	} else
4293 		ioc_info(mrioc, "port enable is successfully completed\n");
4294 
4295 	mpi3mr_start_watchdog(mrioc);
4296 	mrioc->is_driver_loading = 0;
4297 	mrioc->stop_bsgs = 0;
4298 	return 1;
4299 }
4300 
4301 /**
4302  * mpi3mr_slave_destroy - Slave destroy callback handler
4303  * @sdev: SCSI device reference
4304  *
4305  * Cleanup and free per device(lun) private data.
4306  *
4307  * Return: Nothing.
4308  */
mpi3mr_slave_destroy(struct scsi_device * sdev)4309 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
4310 {
4311 	struct Scsi_Host *shost;
4312 	struct mpi3mr_ioc *mrioc;
4313 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4314 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4315 	unsigned long flags;
4316 	struct scsi_target *starget;
4317 	struct sas_rphy *rphy = NULL;
4318 
4319 	if (!sdev->hostdata)
4320 		return;
4321 
4322 	starget = scsi_target(sdev);
4323 	shost = dev_to_shost(&starget->dev);
4324 	mrioc = shost_priv(shost);
4325 	scsi_tgt_priv_data = starget->hostdata;
4326 
4327 	scsi_tgt_priv_data->num_luns--;
4328 
4329 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4330 	if (starget->channel == mrioc->scsi_device_channel)
4331 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4332 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4333 		rphy = dev_to_rphy(starget->dev.parent);
4334 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4335 		    rphy->identify.sas_address, rphy);
4336 	}
4337 
4338 	if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4339 		tgt_dev->starget = NULL;
4340 	if (tgt_dev)
4341 		mpi3mr_tgtdev_put(tgt_dev);
4342 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4343 
4344 	kfree(sdev->hostdata);
4345 	sdev->hostdata = NULL;
4346 }
4347 
4348 /**
4349  * mpi3mr_target_destroy - Target destroy callback handler
4350  * @starget: SCSI target reference
4351  *
4352  * Cleanup and free per target private data.
4353  *
4354  * Return: Nothing.
4355  */
mpi3mr_target_destroy(struct scsi_target * starget)4356 static void mpi3mr_target_destroy(struct scsi_target *starget)
4357 {
4358 	struct Scsi_Host *shost;
4359 	struct mpi3mr_ioc *mrioc;
4360 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4361 	struct mpi3mr_tgt_dev *tgt_dev;
4362 	unsigned long flags;
4363 
4364 	if (!starget->hostdata)
4365 		return;
4366 
4367 	shost = dev_to_shost(&starget->dev);
4368 	mrioc = shost_priv(shost);
4369 	scsi_tgt_priv_data = starget->hostdata;
4370 
4371 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4372 	tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4373 	if (tgt_dev && (tgt_dev->starget == starget) &&
4374 	    (tgt_dev->perst_id == starget->id))
4375 		tgt_dev->starget = NULL;
4376 	if (tgt_dev) {
4377 		scsi_tgt_priv_data->tgt_dev = NULL;
4378 		scsi_tgt_priv_data->perst_id = 0;
4379 		mpi3mr_tgtdev_put(tgt_dev);
4380 		mpi3mr_tgtdev_put(tgt_dev);
4381 	}
4382 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4383 
4384 	kfree(starget->hostdata);
4385 	starget->hostdata = NULL;
4386 }
4387 
4388 /**
4389  * mpi3mr_slave_configure - Slave configure callback handler
4390  * @sdev: SCSI device reference
4391  *
4392  * Configure queue depth, max hardware sectors and virt boundary
4393  * as required
4394  *
4395  * Return: 0 always.
4396  */
mpi3mr_slave_configure(struct scsi_device * sdev)4397 static int mpi3mr_slave_configure(struct scsi_device *sdev)
4398 {
4399 	struct scsi_target *starget;
4400 	struct Scsi_Host *shost;
4401 	struct mpi3mr_ioc *mrioc;
4402 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4403 	unsigned long flags;
4404 	int retval = 0;
4405 	struct sas_rphy *rphy = NULL;
4406 
4407 	starget = scsi_target(sdev);
4408 	shost = dev_to_shost(&starget->dev);
4409 	mrioc = shost_priv(shost);
4410 
4411 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4412 	if (starget->channel == mrioc->scsi_device_channel)
4413 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4414 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4415 		rphy = dev_to_rphy(starget->dev.parent);
4416 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4417 		    rphy->identify.sas_address, rphy);
4418 	}
4419 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4420 	if (!tgt_dev)
4421 		return -ENXIO;
4422 
4423 	mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4424 
4425 	sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4426 	blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4427 
4428 	switch (tgt_dev->dev_type) {
4429 	case MPI3_DEVICE_DEVFORM_PCIE:
4430 		/*The block layer hw sector size = 512*/
4431 		if ((tgt_dev->dev_spec.pcie_inf.dev_info &
4432 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4433 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
4434 			blk_queue_max_hw_sectors(sdev->request_queue,
4435 			    tgt_dev->dev_spec.pcie_inf.mdts / 512);
4436 			if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
4437 				blk_queue_virt_boundary(sdev->request_queue,
4438 				    ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
4439 			else
4440 				blk_queue_virt_boundary(sdev->request_queue,
4441 				    ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
4442 		}
4443 		break;
4444 	default:
4445 		break;
4446 	}
4447 
4448 	mpi3mr_tgtdev_put(tgt_dev);
4449 
4450 	return retval;
4451 }
4452 
4453 /**
4454  * mpi3mr_slave_alloc -Slave alloc callback handler
4455  * @sdev: SCSI device reference
4456  *
4457  * Allocate per device(lun) private data and initialize it.
4458  *
4459  * Return: 0 on success -ENOMEM on memory allocation failure.
4460  */
mpi3mr_slave_alloc(struct scsi_device * sdev)4461 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
4462 {
4463 	struct Scsi_Host *shost;
4464 	struct mpi3mr_ioc *mrioc;
4465 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4466 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4467 	struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4468 	unsigned long flags;
4469 	struct scsi_target *starget;
4470 	int retval = 0;
4471 	struct sas_rphy *rphy = NULL;
4472 
4473 	starget = scsi_target(sdev);
4474 	shost = dev_to_shost(&starget->dev);
4475 	mrioc = shost_priv(shost);
4476 	scsi_tgt_priv_data = starget->hostdata;
4477 
4478 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4479 
4480 	if (starget->channel == mrioc->scsi_device_channel)
4481 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4482 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4483 		rphy = dev_to_rphy(starget->dev.parent);
4484 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4485 		    rphy->identify.sas_address, rphy);
4486 	}
4487 
4488 	if (tgt_dev) {
4489 		if (tgt_dev->starget == NULL)
4490 			tgt_dev->starget = starget;
4491 		mpi3mr_tgtdev_put(tgt_dev);
4492 		retval = 0;
4493 	} else {
4494 		spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4495 		return -ENXIO;
4496 	}
4497 
4498 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4499 
4500 	scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4501 	if (!scsi_dev_priv_data)
4502 		return -ENOMEM;
4503 
4504 	scsi_dev_priv_data->lun_id = sdev->lun;
4505 	scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4506 	sdev->hostdata = scsi_dev_priv_data;
4507 
4508 	scsi_tgt_priv_data->num_luns++;
4509 
4510 	return retval;
4511 }
4512 
4513 /**
4514  * mpi3mr_target_alloc - Target alloc callback handler
4515  * @starget: SCSI target reference
4516  *
4517  * Allocate per target private data and initialize it.
4518  *
4519  * Return: 0 on success -ENOMEM on memory allocation failure.
4520  */
mpi3mr_target_alloc(struct scsi_target * starget)4521 static int mpi3mr_target_alloc(struct scsi_target *starget)
4522 {
4523 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4524 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4525 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4526 	struct mpi3mr_tgt_dev *tgt_dev;
4527 	unsigned long flags;
4528 	int retval = 0;
4529 	struct sas_rphy *rphy = NULL;
4530 
4531 	scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4532 	if (!scsi_tgt_priv_data)
4533 		return -ENOMEM;
4534 
4535 	starget->hostdata = scsi_tgt_priv_data;
4536 
4537 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4538 	if (starget->channel == mrioc->scsi_device_channel) {
4539 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4540 		if (tgt_dev && !tgt_dev->is_hidden) {
4541 			scsi_tgt_priv_data->starget = starget;
4542 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4543 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4544 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4545 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4546 			tgt_dev->starget = starget;
4547 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4548 			retval = 0;
4549 			if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4550 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4551 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4552 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4553 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4554 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4555 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4556 				scsi_tgt_priv_data->dev_nvme_dif = 1;
4557 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4558 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4559 			if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4560 				scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4561 		} else
4562 			retval = -ENXIO;
4563 	} else if (mrioc->sas_transport_enabled && !starget->channel) {
4564 		rphy = dev_to_rphy(starget->dev.parent);
4565 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4566 		    rphy->identify.sas_address, rphy);
4567 		if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4568 		    (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4569 			scsi_tgt_priv_data->starget = starget;
4570 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4571 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4572 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4573 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4574 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4575 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4576 			tgt_dev->starget = starget;
4577 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4578 			retval = 0;
4579 		} else
4580 			retval = -ENXIO;
4581 	}
4582 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4583 
4584 	return retval;
4585 }
4586 
4587 /**
4588  * mpi3mr_check_return_unmap - Whether an unmap is allowed
4589  * @mrioc: Adapter instance reference
4590  * @scmd: SCSI Command reference
4591  *
4592  * The controller hardware cannot handle certain unmap commands
4593  * for NVMe drives, this routine checks those and return true
4594  * and completes the SCSI command with proper status and sense
4595  * data.
4596  *
4597  * Return: TRUE for not  allowed unmap, FALSE otherwise.
4598  */
mpi3mr_check_return_unmap(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)4599 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4600 	struct scsi_cmnd *scmd)
4601 {
4602 	unsigned char *buf;
4603 	u16 param_len, desc_len, trunc_param_len;
4604 
4605 	trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4606 
4607 	if (mrioc->pdev->revision) {
4608 		if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4609 			trunc_param_len -= (param_len - 8) & 0xF;
4610 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4611 			dprint_scsi_err(mrioc,
4612 			    "truncating param_len from (%d) to (%d)\n",
4613 			    param_len, trunc_param_len);
4614 			put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4615 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4616 		}
4617 		return false;
4618 	}
4619 
4620 	if (!param_len) {
4621 		ioc_warn(mrioc,
4622 		    "%s: cdb received with zero parameter length\n",
4623 		    __func__);
4624 		scsi_print_command(scmd);
4625 		scmd->result = DID_OK << 16;
4626 		scsi_done(scmd);
4627 		return true;
4628 	}
4629 
4630 	if (param_len < 24) {
4631 		ioc_warn(mrioc,
4632 		    "%s: cdb received with invalid param_len: %d\n",
4633 		    __func__, param_len);
4634 		scsi_print_command(scmd);
4635 		scmd->result = SAM_STAT_CHECK_CONDITION;
4636 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4637 		    0x1A, 0);
4638 		scsi_done(scmd);
4639 		return true;
4640 	}
4641 	if (param_len != scsi_bufflen(scmd)) {
4642 		ioc_warn(mrioc,
4643 		    "%s: cdb received with param_len: %d bufflen: %d\n",
4644 		    __func__, param_len, scsi_bufflen(scmd));
4645 		scsi_print_command(scmd);
4646 		scmd->result = SAM_STAT_CHECK_CONDITION;
4647 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4648 		    0x1A, 0);
4649 		scsi_done(scmd);
4650 		return true;
4651 	}
4652 	buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4653 	if (!buf) {
4654 		scsi_print_command(scmd);
4655 		scmd->result = SAM_STAT_CHECK_CONDITION;
4656 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4657 		    0x55, 0x03);
4658 		scsi_done(scmd);
4659 		return true;
4660 	}
4661 	scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4662 	desc_len = get_unaligned_be16(&buf[2]);
4663 
4664 	if (desc_len < 16) {
4665 		ioc_warn(mrioc,
4666 		    "%s: Invalid descriptor length in param list: %d\n",
4667 		    __func__, desc_len);
4668 		scsi_print_command(scmd);
4669 		scmd->result = SAM_STAT_CHECK_CONDITION;
4670 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4671 		    0x26, 0);
4672 		scsi_done(scmd);
4673 		kfree(buf);
4674 		return true;
4675 	}
4676 
4677 	if (param_len > (desc_len + 8)) {
4678 		trunc_param_len = desc_len + 8;
4679 		scsi_print_command(scmd);
4680 		dprint_scsi_err(mrioc,
4681 		    "truncating param_len(%d) to desc_len+8(%d)\n",
4682 		    param_len, trunc_param_len);
4683 		put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4684 		scsi_print_command(scmd);
4685 	}
4686 
4687 	kfree(buf);
4688 	return false;
4689 }
4690 
4691 /**
4692  * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4693  * @scmd: SCSI Command reference
4694  *
4695  * Checks whether a cdb is allowed during shutdown or not.
4696  *
4697  * Return: TRUE for allowed commands, FALSE otherwise.
4698  */
4699 
mpi3mr_allow_scmd_to_fw(struct scsi_cmnd * scmd)4700 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4701 {
4702 	switch (scmd->cmnd[0]) {
4703 	case SYNCHRONIZE_CACHE:
4704 	case START_STOP:
4705 		return true;
4706 	default:
4707 		return false;
4708 	}
4709 }
4710 
4711 /**
4712  * mpi3mr_qcmd - I/O request despatcher
4713  * @shost: SCSI Host reference
4714  * @scmd: SCSI Command reference
4715  *
4716  * Issues the SCSI Command as an MPI3 request.
4717  *
4718  * Return: 0 on successful queueing of the request or if the
4719  *         request is completed with failure.
4720  *         SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4721  *         SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4722  */
mpi3mr_qcmd(struct Scsi_Host * shost,struct scsi_cmnd * scmd)4723 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4724 	struct scsi_cmnd *scmd)
4725 {
4726 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4727 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4728 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4729 	struct scmd_priv *scmd_priv_data = NULL;
4730 	struct mpi3_scsi_io_request *scsiio_req = NULL;
4731 	struct op_req_qinfo *op_req_q = NULL;
4732 	int retval = 0;
4733 	u16 dev_handle;
4734 	u16 host_tag;
4735 	u32 scsiio_flags = 0, data_len_blks = 0;
4736 	struct request *rq = scsi_cmd_to_rq(scmd);
4737 	int iprio_class;
4738 	u8 is_pcie_dev = 0;
4739 	u32 tracked_io_sz = 0;
4740 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4741 	struct mpi3mr_throttle_group_info *tg = NULL;
4742 
4743 	if (mrioc->unrecoverable) {
4744 		scmd->result = DID_ERROR << 16;
4745 		scsi_done(scmd);
4746 		goto out;
4747 	}
4748 
4749 	sdev_priv_data = scmd->device->hostdata;
4750 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4751 		scmd->result = DID_NO_CONNECT << 16;
4752 		scsi_done(scmd);
4753 		goto out;
4754 	}
4755 
4756 	if (mrioc->stop_drv_processing &&
4757 	    !(mpi3mr_allow_scmd_to_fw(scmd))) {
4758 		scmd->result = DID_NO_CONNECT << 16;
4759 		scsi_done(scmd);
4760 		goto out;
4761 	}
4762 
4763 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4764 	dev_handle = stgt_priv_data->dev_handle;
4765 
4766 	/* Avoid error handling escalation when device is removed or blocked */
4767 
4768 	if (scmd->device->host->shost_state == SHOST_RECOVERY &&
4769 		scmd->cmnd[0] == TEST_UNIT_READY &&
4770 		(stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
4771 		scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
4772 		scsi_done(scmd);
4773 		goto out;
4774 	}
4775 
4776 	if (mrioc->reset_in_progress) {
4777 		retval = SCSI_MLQUEUE_HOST_BUSY;
4778 		goto out;
4779 	}
4780 
4781 	if (atomic_read(&stgt_priv_data->block_io)) {
4782 		if (mrioc->stop_drv_processing) {
4783 			scmd->result = DID_NO_CONNECT << 16;
4784 			scsi_done(scmd);
4785 			goto out;
4786 		}
4787 		retval = SCSI_MLQUEUE_DEVICE_BUSY;
4788 		goto out;
4789 	}
4790 
4791 	if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4792 		scmd->result = DID_NO_CONNECT << 16;
4793 		scsi_done(scmd);
4794 		goto out;
4795 	}
4796 	if (stgt_priv_data->dev_removed) {
4797 		scmd->result = DID_NO_CONNECT << 16;
4798 		scsi_done(scmd);
4799 		goto out;
4800 	}
4801 
4802 	if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4803 		is_pcie_dev = 1;
4804 	if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4805 	    (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4806 	    mpi3mr_check_return_unmap(mrioc, scmd))
4807 		goto out;
4808 
4809 	host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4810 	if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4811 		scmd->result = DID_ERROR << 16;
4812 		scsi_done(scmd);
4813 		goto out;
4814 	}
4815 
4816 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4817 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4818 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4819 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4820 	else
4821 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4822 
4823 	scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4824 
4825 	if (sdev_priv_data->ncq_prio_enable) {
4826 		iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4827 		if (iprio_class == IOPRIO_CLASS_RT)
4828 			scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4829 	}
4830 
4831 	if (scmd->cmd_len > 16)
4832 		scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4833 
4834 	scmd_priv_data = scsi_cmd_priv(scmd);
4835 	memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4836 	scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4837 	scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4838 	scsiio_req->host_tag = cpu_to_le16(host_tag);
4839 
4840 	mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4841 
4842 	if (stgt_priv_data->wslen)
4843 		mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
4844 		    stgt_priv_data->wslen);
4845 
4846 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4847 	scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4848 	scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4849 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
4850 	int_to_scsilun(sdev_priv_data->lun_id,
4851 	    (struct scsi_lun *)scsiio_req->lun);
4852 
4853 	if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4854 		mpi3mr_clear_scmd_priv(mrioc, scmd);
4855 		retval = SCSI_MLQUEUE_HOST_BUSY;
4856 		goto out;
4857 	}
4858 	op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4859 	data_len_blks = scsi_bufflen(scmd) >> 9;
4860 	if ((data_len_blks >= mrioc->io_throttle_data_length) &&
4861 	    stgt_priv_data->io_throttle_enabled) {
4862 		tracked_io_sz = data_len_blks;
4863 		tg = stgt_priv_data->throttle_group;
4864 		if (tg) {
4865 			ioc_pend_data_len = atomic_add_return(data_len_blks,
4866 			    &mrioc->pend_large_data_sz);
4867 			tg_pend_data_len = atomic_add_return(data_len_blks,
4868 			    &tg->pend_large_data_sz);
4869 			if (!tg->io_divert  && ((ioc_pend_data_len >=
4870 			    mrioc->io_throttle_high) ||
4871 			    (tg_pend_data_len >= tg->high))) {
4872 				tg->io_divert = 1;
4873 				tg->need_qd_reduction = 1;
4874 				mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
4875 				    tg, 1);
4876 				mpi3mr_queue_qd_reduction_event(mrioc, tg);
4877 			}
4878 		} else {
4879 			ioc_pend_data_len = atomic_add_return(data_len_blks,
4880 			    &mrioc->pend_large_data_sz);
4881 			if (ioc_pend_data_len >= mrioc->io_throttle_high)
4882 				stgt_priv_data->io_divert = 1;
4883 		}
4884 	}
4885 
4886 	if (stgt_priv_data->io_divert) {
4887 		scsiio_req->msg_flags |=
4888 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4889 		scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
4890 	}
4891 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
4892 
4893 	if (mpi3mr_op_request_post(mrioc, op_req_q,
4894 	    scmd_priv_data->mpi3mr_scsiio_req)) {
4895 		mpi3mr_clear_scmd_priv(mrioc, scmd);
4896 		retval = SCSI_MLQUEUE_HOST_BUSY;
4897 		if (tracked_io_sz) {
4898 			atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
4899 			if (tg)
4900 				atomic_sub(tracked_io_sz,
4901 				    &tg->pend_large_data_sz);
4902 		}
4903 		goto out;
4904 	}
4905 
4906 out:
4907 	return retval;
4908 }
4909 
4910 static const struct scsi_host_template mpi3mr_driver_template = {
4911 	.module				= THIS_MODULE,
4912 	.name				= "MPI3 Storage Controller",
4913 	.proc_name			= MPI3MR_DRIVER_NAME,
4914 	.queuecommand			= mpi3mr_qcmd,
4915 	.target_alloc			= mpi3mr_target_alloc,
4916 	.slave_alloc			= mpi3mr_slave_alloc,
4917 	.slave_configure		= mpi3mr_slave_configure,
4918 	.target_destroy			= mpi3mr_target_destroy,
4919 	.slave_destroy			= mpi3mr_slave_destroy,
4920 	.scan_finished			= mpi3mr_scan_finished,
4921 	.scan_start			= mpi3mr_scan_start,
4922 	.change_queue_depth		= mpi3mr_change_queue_depth,
4923 	.eh_device_reset_handler	= mpi3mr_eh_dev_reset,
4924 	.eh_target_reset_handler	= mpi3mr_eh_target_reset,
4925 	.eh_host_reset_handler		= mpi3mr_eh_host_reset,
4926 	.bios_param			= mpi3mr_bios_param,
4927 	.map_queues			= mpi3mr_map_queues,
4928 	.mq_poll                        = mpi3mr_blk_mq_poll,
4929 	.no_write_same			= 1,
4930 	.can_queue			= 1,
4931 	.this_id			= -1,
4932 	.sg_tablesize			= MPI3MR_DEFAULT_SGL_ENTRIES,
4933 	/* max xfer supported is 1M (2K in 512 byte sized sectors)
4934 	 */
4935 	.max_sectors			= (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
4936 	.cmd_per_lun			= MPI3MR_MAX_CMDS_LUN,
4937 	.max_segment_size		= 0xffffffff,
4938 	.track_queue_depth		= 1,
4939 	.cmd_size			= sizeof(struct scmd_priv),
4940 	.shost_groups			= mpi3mr_host_groups,
4941 	.sdev_groups			= mpi3mr_dev_groups,
4942 };
4943 
4944 /**
4945  * mpi3mr_init_drv_cmd - Initialize internal command tracker
4946  * @cmdptr: Internal command tracker
4947  * @host_tag: Host tag used for the specific command
4948  *
4949  * Initialize the internal command tracker structure with
4950  * specified host tag.
4951  *
4952  * Return: Nothing.
4953  */
mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd * cmdptr,u16 host_tag)4954 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
4955 	u16 host_tag)
4956 {
4957 	mutex_init(&cmdptr->mutex);
4958 	cmdptr->reply = NULL;
4959 	cmdptr->state = MPI3MR_CMD_NOTUSED;
4960 	cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
4961 	cmdptr->host_tag = host_tag;
4962 }
4963 
4964 /**
4965  * osintfc_mrioc_security_status -Check controller secure status
4966  * @pdev: PCI device instance
4967  *
4968  * Read the Device Serial Number capability from PCI config
4969  * space and decide whether the controller is secure or not.
4970  *
4971  * Return: 0 on success, non-zero on failure.
4972  */
4973 static int
osintfc_mrioc_security_status(struct pci_dev * pdev)4974 osintfc_mrioc_security_status(struct pci_dev *pdev)
4975 {
4976 	u32 cap_data;
4977 	int base;
4978 	u32 ctlr_status;
4979 	u32 debug_status;
4980 	int retval = 0;
4981 
4982 	base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
4983 	if (!base) {
4984 		dev_err(&pdev->dev,
4985 		    "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
4986 		return -1;
4987 	}
4988 
4989 	pci_read_config_dword(pdev, base + 4, &cap_data);
4990 
4991 	debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
4992 	ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
4993 
4994 	switch (ctlr_status) {
4995 	case MPI3MR_INVALID_DEVICE:
4996 		dev_err(&pdev->dev,
4997 		    "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4998 		    __func__, pdev->device, pdev->subsystem_vendor,
4999 		    pdev->subsystem_device);
5000 		retval = -1;
5001 		break;
5002 	case MPI3MR_CONFIG_SECURE_DEVICE:
5003 		if (!debug_status)
5004 			dev_info(&pdev->dev,
5005 			    "%s: Config secure ctlr is detected\n",
5006 			    __func__);
5007 		break;
5008 	case MPI3MR_HARD_SECURE_DEVICE:
5009 		break;
5010 	case MPI3MR_TAMPERED_DEVICE:
5011 		dev_err(&pdev->dev,
5012 		    "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5013 		    __func__, pdev->device, pdev->subsystem_vendor,
5014 		    pdev->subsystem_device);
5015 		retval = -1;
5016 		break;
5017 	default:
5018 		retval = -1;
5019 			break;
5020 	}
5021 
5022 	if (!retval && debug_status) {
5023 		dev_err(&pdev->dev,
5024 		    "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5025 		    __func__, pdev->device, pdev->subsystem_vendor,
5026 		    pdev->subsystem_device);
5027 		retval = -1;
5028 	}
5029 
5030 	return retval;
5031 }
5032 
5033 /**
5034  * mpi3mr_probe - PCI probe callback
5035  * @pdev: PCI device instance
5036  * @id: PCI device ID details
5037  *
5038  * controller initialization routine. Checks the security status
5039  * of the controller and if it is invalid or tampered return the
5040  * probe without initializing the controller. Otherwise,
5041  * allocate per adapter instance through shost_priv and
5042  * initialize controller specific data structures, initializae
5043  * the controller hardware, add shost to the SCSI subsystem.
5044  *
5045  * Return: 0 on success, non-zero on failure.
5046  */
5047 
5048 static int
mpi3mr_probe(struct pci_dev * pdev,const struct pci_device_id * id)5049 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5050 {
5051 	struct mpi3mr_ioc *mrioc = NULL;
5052 	struct Scsi_Host *shost = NULL;
5053 	int retval = 0, i;
5054 
5055 	if (osintfc_mrioc_security_status(pdev)) {
5056 		warn_non_secure_ctlr = 1;
5057 		return 1; /* For Invalid and Tampered device */
5058 	}
5059 
5060 	shost = scsi_host_alloc(&mpi3mr_driver_template,
5061 	    sizeof(struct mpi3mr_ioc));
5062 	if (!shost) {
5063 		retval = -ENODEV;
5064 		goto shost_failed;
5065 	}
5066 
5067 	mrioc = shost_priv(shost);
5068 	mrioc->id = mrioc_ids++;
5069 	sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
5070 	sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
5071 	INIT_LIST_HEAD(&mrioc->list);
5072 	spin_lock(&mrioc_list_lock);
5073 	list_add_tail(&mrioc->list, &mrioc_list);
5074 	spin_unlock(&mrioc_list_lock);
5075 
5076 	spin_lock_init(&mrioc->admin_req_lock);
5077 	spin_lock_init(&mrioc->reply_free_queue_lock);
5078 	spin_lock_init(&mrioc->sbq_lock);
5079 	spin_lock_init(&mrioc->fwevt_lock);
5080 	spin_lock_init(&mrioc->tgtdev_lock);
5081 	spin_lock_init(&mrioc->watchdog_lock);
5082 	spin_lock_init(&mrioc->chain_buf_lock);
5083 	spin_lock_init(&mrioc->sas_node_lock);
5084 
5085 	INIT_LIST_HEAD(&mrioc->fwevt_list);
5086 	INIT_LIST_HEAD(&mrioc->tgtdev_list);
5087 	INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5088 	INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5089 	INIT_LIST_HEAD(&mrioc->sas_expander_list);
5090 	INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5091 	INIT_LIST_HEAD(&mrioc->enclosure_list);
5092 
5093 	mutex_init(&mrioc->reset_mutex);
5094 	mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5095 	mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5096 	mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5097 	mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5098 	mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5099 	    MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5100 
5101 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5102 		mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5103 		    MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5104 
5105 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5106 		mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5107 				    MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5108 
5109 	if ((pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5110 		!pdev->revision)
5111 		mrioc->enable_segqueue = false;
5112 	else
5113 		mrioc->enable_segqueue = true;
5114 
5115 	init_waitqueue_head(&mrioc->reset_waitq);
5116 	mrioc->logging_level = logging_level;
5117 	mrioc->shost = shost;
5118 	mrioc->pdev = pdev;
5119 	mrioc->stop_bsgs = 1;
5120 
5121 	mrioc->max_sgl_entries = max_sgl_entries;
5122 	if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5123 		mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5124 	else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5125 		mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5126 	else {
5127 		mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5128 		mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5129 	}
5130 
5131 	/* init shost parameters */
5132 	shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5133 	shost->max_lun = -1;
5134 	shost->unique_id = mrioc->id;
5135 
5136 	shost->max_channel = 0;
5137 	shost->max_id = 0xFFFFFFFF;
5138 
5139 	shost->host_tagset = 1;
5140 
5141 	if (prot_mask >= 0)
5142 		scsi_host_set_prot(shost, prot_mask);
5143 	else {
5144 		prot_mask = SHOST_DIF_TYPE1_PROTECTION
5145 		    | SHOST_DIF_TYPE2_PROTECTION
5146 		    | SHOST_DIF_TYPE3_PROTECTION;
5147 		scsi_host_set_prot(shost, prot_mask);
5148 	}
5149 
5150 	ioc_info(mrioc,
5151 	    "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5152 	    __func__,
5153 	    (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5154 	    (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5155 	    (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5156 	    (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5157 	    (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5158 	    (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5159 	    (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5160 
5161 	if (prot_guard_mask)
5162 		scsi_host_set_guard(shost, (prot_guard_mask & 3));
5163 	else
5164 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5165 
5166 	snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
5167 	    "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
5168 	mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5169 	    mrioc->fwevt_worker_name, 0);
5170 	if (!mrioc->fwevt_worker_thread) {
5171 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5172 		    __FILE__, __LINE__, __func__);
5173 		retval = -ENODEV;
5174 		goto fwevtthread_failed;
5175 	}
5176 
5177 	mrioc->is_driver_loading = 1;
5178 	mrioc->cpu_count = num_online_cpus();
5179 	if (mpi3mr_setup_resources(mrioc)) {
5180 		ioc_err(mrioc, "setup resources failed\n");
5181 		retval = -ENODEV;
5182 		goto resource_alloc_failed;
5183 	}
5184 	if (mpi3mr_init_ioc(mrioc)) {
5185 		ioc_err(mrioc, "initializing IOC failed\n");
5186 		retval = -ENODEV;
5187 		goto init_ioc_failed;
5188 	}
5189 
5190 	shost->nr_hw_queues = mrioc->num_op_reply_q;
5191 	if (mrioc->active_poll_qcount)
5192 		shost->nr_maps = 3;
5193 
5194 	shost->can_queue = mrioc->max_host_ios;
5195 	shost->sg_tablesize = mrioc->max_sgl_entries;
5196 	shost->max_id = mrioc->facts.max_perids + 1;
5197 
5198 	retval = scsi_add_host(shost, &pdev->dev);
5199 	if (retval) {
5200 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5201 		    __FILE__, __LINE__, __func__);
5202 		goto addhost_failed;
5203 	}
5204 
5205 	scsi_scan_host(shost);
5206 	mpi3mr_bsg_init(mrioc);
5207 	return retval;
5208 
5209 addhost_failed:
5210 	mpi3mr_stop_watchdog(mrioc);
5211 	mpi3mr_cleanup_ioc(mrioc);
5212 init_ioc_failed:
5213 	mpi3mr_free_mem(mrioc);
5214 	mpi3mr_cleanup_resources(mrioc);
5215 resource_alloc_failed:
5216 	destroy_workqueue(mrioc->fwevt_worker_thread);
5217 fwevtthread_failed:
5218 	spin_lock(&mrioc_list_lock);
5219 	list_del(&mrioc->list);
5220 	spin_unlock(&mrioc_list_lock);
5221 	scsi_host_put(shost);
5222 shost_failed:
5223 	return retval;
5224 }
5225 
5226 /**
5227  * mpi3mr_remove - PCI remove callback
5228  * @pdev: PCI device instance
5229  *
5230  * Cleanup the IOC by issuing MUR and shutdown notification.
5231  * Free up all memory and resources associated with the
5232  * controllerand target devices, unregister the shost.
5233  *
5234  * Return: Nothing.
5235  */
mpi3mr_remove(struct pci_dev * pdev)5236 static void mpi3mr_remove(struct pci_dev *pdev)
5237 {
5238 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5239 	struct mpi3mr_ioc *mrioc;
5240 	struct workqueue_struct	*wq;
5241 	unsigned long flags;
5242 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5243 	struct mpi3mr_hba_port *port, *hba_port_next;
5244 	struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5245 
5246 	if (!shost)
5247 		return;
5248 
5249 	mrioc = shost_priv(shost);
5250 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5251 		ssleep(1);
5252 
5253 	if (!pci_device_is_present(mrioc->pdev)) {
5254 		mrioc->unrecoverable = 1;
5255 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5256 	}
5257 
5258 	mpi3mr_bsg_exit(mrioc);
5259 	mrioc->stop_drv_processing = 1;
5260 	mpi3mr_cleanup_fwevt_list(mrioc);
5261 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5262 	wq = mrioc->fwevt_worker_thread;
5263 	mrioc->fwevt_worker_thread = NULL;
5264 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5265 	if (wq)
5266 		destroy_workqueue(wq);
5267 
5268 	if (mrioc->sas_transport_enabled)
5269 		sas_remove_host(shost);
5270 	else
5271 		scsi_remove_host(shost);
5272 
5273 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5274 	    list) {
5275 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5276 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5277 		mpi3mr_tgtdev_put(tgtdev);
5278 	}
5279 	mpi3mr_stop_watchdog(mrioc);
5280 	mpi3mr_cleanup_ioc(mrioc);
5281 	mpi3mr_free_mem(mrioc);
5282 	mpi3mr_cleanup_resources(mrioc);
5283 
5284 	spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5285 	list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5286 	    &mrioc->sas_expander_list, list) {
5287 		spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5288 		mpi3mr_expander_node_remove(mrioc, sas_expander);
5289 		spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5290 	}
5291 	list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5292 		ioc_info(mrioc,
5293 		    "removing hba_port entry: %p port: %d from hba_port list\n",
5294 		    port, port->port_id);
5295 		list_del(&port->list);
5296 		kfree(port);
5297 	}
5298 	spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5299 
5300 	if (mrioc->sas_hba.num_phys) {
5301 		kfree(mrioc->sas_hba.phy);
5302 		mrioc->sas_hba.phy = NULL;
5303 		mrioc->sas_hba.num_phys = 0;
5304 	}
5305 
5306 	spin_lock(&mrioc_list_lock);
5307 	list_del(&mrioc->list);
5308 	spin_unlock(&mrioc_list_lock);
5309 
5310 	scsi_host_put(shost);
5311 }
5312 
5313 /**
5314  * mpi3mr_shutdown - PCI shutdown callback
5315  * @pdev: PCI device instance
5316  *
5317  * Free up all memory and resources associated with the
5318  * controller
5319  *
5320  * Return: Nothing.
5321  */
mpi3mr_shutdown(struct pci_dev * pdev)5322 static void mpi3mr_shutdown(struct pci_dev *pdev)
5323 {
5324 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5325 	struct mpi3mr_ioc *mrioc;
5326 	struct workqueue_struct	*wq;
5327 	unsigned long flags;
5328 
5329 	if (!shost)
5330 		return;
5331 
5332 	mrioc = shost_priv(shost);
5333 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5334 		ssleep(1);
5335 
5336 	mrioc->stop_drv_processing = 1;
5337 	mpi3mr_cleanup_fwevt_list(mrioc);
5338 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5339 	wq = mrioc->fwevt_worker_thread;
5340 	mrioc->fwevt_worker_thread = NULL;
5341 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5342 	if (wq)
5343 		destroy_workqueue(wq);
5344 
5345 	mpi3mr_stop_watchdog(mrioc);
5346 	mpi3mr_cleanup_ioc(mrioc);
5347 	mpi3mr_cleanup_resources(mrioc);
5348 }
5349 
5350 /**
5351  * mpi3mr_suspend - PCI power management suspend callback
5352  * @dev: Device struct
5353  *
5354  * Change the power state to the given value and cleanup the IOC
5355  * by issuing MUR and shutdown notification
5356  *
5357  * Return: 0 always.
5358  */
5359 static int __maybe_unused
mpi3mr_suspend(struct device * dev)5360 mpi3mr_suspend(struct device *dev)
5361 {
5362 	struct pci_dev *pdev = to_pci_dev(dev);
5363 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5364 	struct mpi3mr_ioc *mrioc;
5365 
5366 	if (!shost)
5367 		return 0;
5368 
5369 	mrioc = shost_priv(shost);
5370 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5371 		ssleep(1);
5372 	mrioc->stop_drv_processing = 1;
5373 	mpi3mr_cleanup_fwevt_list(mrioc);
5374 	scsi_block_requests(shost);
5375 	mpi3mr_stop_watchdog(mrioc);
5376 	mpi3mr_cleanup_ioc(mrioc);
5377 
5378 	ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5379 	    pdev, pci_name(pdev));
5380 	mpi3mr_cleanup_resources(mrioc);
5381 
5382 	return 0;
5383 }
5384 
5385 /**
5386  * mpi3mr_resume - PCI power management resume callback
5387  * @dev: Device struct
5388  *
5389  * Restore the power state to D0 and reinitialize the controller
5390  * and resume I/O operations to the target devices
5391  *
5392  * Return: 0 on success, non-zero on failure
5393  */
5394 static int __maybe_unused
mpi3mr_resume(struct device * dev)5395 mpi3mr_resume(struct device *dev)
5396 {
5397 	struct pci_dev *pdev = to_pci_dev(dev);
5398 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5399 	struct mpi3mr_ioc *mrioc;
5400 	pci_power_t device_state = pdev->current_state;
5401 	int r;
5402 
5403 	if (!shost)
5404 		return 0;
5405 
5406 	mrioc = shost_priv(shost);
5407 
5408 	ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5409 	    pdev, pci_name(pdev), device_state);
5410 	mrioc->pdev = pdev;
5411 	mrioc->cpu_count = num_online_cpus();
5412 	r = mpi3mr_setup_resources(mrioc);
5413 	if (r) {
5414 		ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5415 		    __func__, r);
5416 		return r;
5417 	}
5418 
5419 	mrioc->stop_drv_processing = 0;
5420 	mpi3mr_invalidate_devhandles(mrioc);
5421 	mpi3mr_free_enclosure_list(mrioc);
5422 	mpi3mr_memset_buffers(mrioc);
5423 	r = mpi3mr_reinit_ioc(mrioc, 1);
5424 	if (r) {
5425 		ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5426 		return r;
5427 	}
5428 	ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5429 	scsi_unblock_requests(shost);
5430 	mrioc->device_refresh_on = 0;
5431 	mpi3mr_start_watchdog(mrioc);
5432 
5433 	return 0;
5434 }
5435 
5436 static const struct pci_device_id mpi3mr_pci_id_table[] = {
5437 	{
5438 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5439 		    MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
5440 	},
5441 	{
5442 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5443 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI, PCI_ANY_ID, PCI_ANY_ID)
5444 	},
5445 	{
5446 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5447 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI_MGMT, PCI_ANY_ID, PCI_ANY_ID)
5448 	},
5449 	{ 0 }
5450 };
5451 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
5452 
5453 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
5454 
5455 static struct pci_driver mpi3mr_pci_driver = {
5456 	.name = MPI3MR_DRIVER_NAME,
5457 	.id_table = mpi3mr_pci_id_table,
5458 	.probe = mpi3mr_probe,
5459 	.remove = mpi3mr_remove,
5460 	.shutdown = mpi3mr_shutdown,
5461 	.driver.pm = &mpi3mr_pm_ops,
5462 };
5463 
event_counter_show(struct device_driver * dd,char * buf)5464 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
5465 {
5466 	return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
5467 }
5468 static DRIVER_ATTR_RO(event_counter);
5469 
mpi3mr_init(void)5470 static int __init mpi3mr_init(void)
5471 {
5472 	int ret_val;
5473 
5474 	pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
5475 	    MPI3MR_DRIVER_VERSION);
5476 
5477 	mpi3mr_transport_template =
5478 	    sas_attach_transport(&mpi3mr_transport_functions);
5479 	if (!mpi3mr_transport_template) {
5480 		pr_err("%s failed to load due to sas transport attach failure\n",
5481 		    MPI3MR_DRIVER_NAME);
5482 		return -ENODEV;
5483 	}
5484 
5485 	ret_val = pci_register_driver(&mpi3mr_pci_driver);
5486 	if (ret_val) {
5487 		pr_err("%s failed to load due to pci register driver failure\n",
5488 		    MPI3MR_DRIVER_NAME);
5489 		goto err_pci_reg_fail;
5490 	}
5491 
5492 	ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
5493 				     &driver_attr_event_counter);
5494 	if (ret_val)
5495 		goto err_event_counter;
5496 
5497 	return ret_val;
5498 
5499 err_event_counter:
5500 	pci_unregister_driver(&mpi3mr_pci_driver);
5501 
5502 err_pci_reg_fail:
5503 	sas_release_transport(mpi3mr_transport_template);
5504 	return ret_val;
5505 }
5506 
mpi3mr_exit(void)5507 static void __exit mpi3mr_exit(void)
5508 {
5509 	if (warn_non_secure_ctlr)
5510 		pr_warn(
5511 		    "Unloading %s version %s while managing a non secure controller\n",
5512 		    MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
5513 	else
5514 		pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
5515 		    MPI3MR_DRIVER_VERSION);
5516 
5517 	driver_remove_file(&mpi3mr_pci_driver.driver,
5518 			   &driver_attr_event_counter);
5519 	pci_unregister_driver(&mpi3mr_pci_driver);
5520 	sas_release_transport(mpi3mr_transport_template);
5521 }
5522 
5523 module_init(mpi3mr_init);
5524 module_exit(mpi3mr_exit);
5525