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