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