]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/scsi/mpt3sas/mpt3sas_scsih.c
scsi: megaraid_sas: handle dma_addr_t right on 32-bit
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
1 /*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5 * Copyright (C) 2012-2014 LSI Corporation
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/errno.h>
49 #include <linux/blkdev.h>
50 #include <linux/sched.h>
51 #include <linux/workqueue.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
54 #include <linux/interrupt.h>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57 #include <asm/unaligned.h>
58
59 #include "mpt3sas_base.h"
60
61 #define RAID_CHANNEL 1
62 /* forward proto's */
63 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
64 struct _sas_node *sas_expander);
65 static void _firmware_event_work(struct work_struct *work);
66
67 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
68 struct _sas_device *sas_device);
69 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
70 u8 retry_count, u8 is_pd);
71
72 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
73
74 /* global parameters */
75 LIST_HEAD(mpt3sas_ioc_list);
76 /* global ioc lock for list operations */
77 DEFINE_SPINLOCK(gioc_lock);
78
79 MODULE_AUTHOR(MPT3SAS_AUTHOR);
80 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
83 MODULE_ALIAS("mpt2sas");
84
85 /* local parameters */
86 static u8 scsi_io_cb_idx = -1;
87 static u8 tm_cb_idx = -1;
88 static u8 ctl_cb_idx = -1;
89 static u8 base_cb_idx = -1;
90 static u8 port_enable_cb_idx = -1;
91 static u8 transport_cb_idx = -1;
92 static u8 scsih_cb_idx = -1;
93 static u8 config_cb_idx = -1;
94 static int mpt2_ids;
95 static int mpt3_ids;
96
97 static u8 tm_tr_cb_idx = -1 ;
98 static u8 tm_tr_volume_cb_idx = -1 ;
99 static u8 tm_sas_control_cb_idx = -1;
100
101 /* command line options */
102 static u32 logging_level;
103 MODULE_PARM_DESC(logging_level,
104 " bits for enabling additional logging info (default=0)");
105
106
107 static ushort max_sectors = 0xFFFF;
108 module_param(max_sectors, ushort, 0);
109 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
110
111
112 static int missing_delay[2] = {-1, -1};
113 module_param_array(missing_delay, int, NULL, 0);
114 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
115
116 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
117 #define MPT3SAS_MAX_LUN (16895)
118 static u64 max_lun = MPT3SAS_MAX_LUN;
119 module_param(max_lun, ullong, 0);
120 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
121
122 static ushort hbas_to_enumerate;
123 module_param(hbas_to_enumerate, ushort, 0);
124 MODULE_PARM_DESC(hbas_to_enumerate,
125 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
126 1 - enumerates only SAS 2.0 generation HBAs\n \
127 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
128
129 /* diag_buffer_enable is bitwise
130 * bit 0 set = TRACE
131 * bit 1 set = SNAPSHOT
132 * bit 2 set = EXTENDED
133 *
134 * Either bit can be set, or both
135 */
136 static int diag_buffer_enable = -1;
137 module_param(diag_buffer_enable, int, 0);
138 MODULE_PARM_DESC(diag_buffer_enable,
139 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
140 static int disable_discovery = -1;
141 module_param(disable_discovery, int, 0);
142 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
143
144
145 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
146 static int prot_mask = -1;
147 module_param(prot_mask, int, 0);
148 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
149
150
151 /* raid transport support */
152 struct raid_template *mpt3sas_raid_template;
153 struct raid_template *mpt2sas_raid_template;
154
155
156 /**
157 * struct sense_info - common structure for obtaining sense keys
158 * @skey: sense key
159 * @asc: additional sense code
160 * @ascq: additional sense code qualifier
161 */
162 struct sense_info {
163 u8 skey;
164 u8 asc;
165 u8 ascq;
166 };
167
168 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
169 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
170 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
171 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
172 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
173 /**
174 * struct fw_event_work - firmware event struct
175 * @list: link list framework
176 * @work: work object (ioc->fault_reset_work_q)
177 * @ioc: per adapter object
178 * @device_handle: device handle
179 * @VF_ID: virtual function id
180 * @VP_ID: virtual port id
181 * @ignore: flag meaning this event has been marked to ignore
182 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
183 * @refcount: kref for this event
184 * @event_data: reply event data payload follows
185 *
186 * This object stored on ioc->fw_event_list.
187 */
188 struct fw_event_work {
189 struct list_head list;
190 struct work_struct work;
191
192 struct MPT3SAS_ADAPTER *ioc;
193 u16 device_handle;
194 u8 VF_ID;
195 u8 VP_ID;
196 u8 ignore;
197 u16 event;
198 struct kref refcount;
199 char event_data[0] __aligned(4);
200 };
201
202 static void fw_event_work_free(struct kref *r)
203 {
204 kfree(container_of(r, struct fw_event_work, refcount));
205 }
206
207 static void fw_event_work_get(struct fw_event_work *fw_work)
208 {
209 kref_get(&fw_work->refcount);
210 }
211
212 static void fw_event_work_put(struct fw_event_work *fw_work)
213 {
214 kref_put(&fw_work->refcount, fw_event_work_free);
215 }
216
217 static struct fw_event_work *alloc_fw_event_work(int len)
218 {
219 struct fw_event_work *fw_event;
220
221 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
222 if (!fw_event)
223 return NULL;
224
225 kref_init(&fw_event->refcount);
226 return fw_event;
227 }
228
229 /**
230 * struct _scsi_io_transfer - scsi io transfer
231 * @handle: sas device handle (assigned by firmware)
232 * @is_raid: flag set for hidden raid components
233 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
234 * @data_length: data transfer length
235 * @data_dma: dma pointer to data
236 * @sense: sense data
237 * @lun: lun number
238 * @cdb_length: cdb length
239 * @cdb: cdb contents
240 * @timeout: timeout for this command
241 * @VF_ID: virtual function id
242 * @VP_ID: virtual port id
243 * @valid_reply: flag set for reply message
244 * @sense_length: sense length
245 * @ioc_status: ioc status
246 * @scsi_state: scsi state
247 * @scsi_status: scsi staus
248 * @log_info: log information
249 * @transfer_length: data length transfer when there is a reply message
250 *
251 * Used for sending internal scsi commands to devices within this module.
252 * Refer to _scsi_send_scsi_io().
253 */
254 struct _scsi_io_transfer {
255 u16 handle;
256 u8 is_raid;
257 enum dma_data_direction dir;
258 u32 data_length;
259 dma_addr_t data_dma;
260 u8 sense[SCSI_SENSE_BUFFERSIZE];
261 u32 lun;
262 u8 cdb_length;
263 u8 cdb[32];
264 u8 timeout;
265 u8 VF_ID;
266 u8 VP_ID;
267 u8 valid_reply;
268 /* the following bits are only valid when 'valid_reply = 1' */
269 u32 sense_length;
270 u16 ioc_status;
271 u8 scsi_state;
272 u8 scsi_status;
273 u32 log_info;
274 u32 transfer_length;
275 };
276
277 /**
278 * _scsih_set_debug_level - global setting of ioc->logging_level.
279 *
280 * Note: The logging levels are defined in mpt3sas_debug.h.
281 */
282 static int
283 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
284 {
285 int ret = param_set_int(val, kp);
286 struct MPT3SAS_ADAPTER *ioc;
287
288 if (ret)
289 return ret;
290
291 pr_info("setting logging_level(0x%08x)\n", logging_level);
292 spin_lock(&gioc_lock);
293 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
294 ioc->logging_level = logging_level;
295 spin_unlock(&gioc_lock);
296 return 0;
297 }
298 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
299 &logging_level, 0644);
300
301 /**
302 * _scsih_srch_boot_sas_address - search based on sas_address
303 * @sas_address: sas address
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308 static inline int
309 _scsih_srch_boot_sas_address(u64 sas_address,
310 Mpi2BootDeviceSasWwid_t *boot_device)
311 {
312 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
313 }
314
315 /**
316 * _scsih_srch_boot_device_name - search based on device name
317 * @device_name: device name specified in INDENTIFY fram
318 * @boot_device: boot device object from bios page 2
319 *
320 * Returns 1 when there's a match, 0 means no match.
321 */
322 static inline int
323 _scsih_srch_boot_device_name(u64 device_name,
324 Mpi2BootDeviceDeviceName_t *boot_device)
325 {
326 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
327 }
328
329 /**
330 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
331 * @enclosure_logical_id: enclosure logical id
332 * @slot_number: slot number
333 * @boot_device: boot device object from bios page 2
334 *
335 * Returns 1 when there's a match, 0 means no match.
336 */
337 static inline int
338 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
339 Mpi2BootDeviceEnclosureSlot_t *boot_device)
340 {
341 return (enclosure_logical_id == le64_to_cpu(boot_device->
342 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
343 SlotNumber)) ? 1 : 0;
344 }
345
346 /**
347 * _scsih_is_boot_device - search for matching boot device.
348 * @sas_address: sas address
349 * @device_name: device name specified in INDENTIFY fram
350 * @enclosure_logical_id: enclosure logical id
351 * @slot_number: slot number
352 * @form: specifies boot device form
353 * @boot_device: boot device object from bios page 2
354 *
355 * Returns 1 when there's a match, 0 means no match.
356 */
357 static int
358 _scsih_is_boot_device(u64 sas_address, u64 device_name,
359 u64 enclosure_logical_id, u16 slot, u8 form,
360 Mpi2BiosPage2BootDevice_t *boot_device)
361 {
362 int rc = 0;
363
364 switch (form) {
365 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
366 if (!sas_address)
367 break;
368 rc = _scsih_srch_boot_sas_address(
369 sas_address, &boot_device->SasWwid);
370 break;
371 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
372 if (!enclosure_logical_id)
373 break;
374 rc = _scsih_srch_boot_encl_slot(
375 enclosure_logical_id,
376 slot, &boot_device->EnclosureSlot);
377 break;
378 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
379 if (!device_name)
380 break;
381 rc = _scsih_srch_boot_device_name(
382 device_name, &boot_device->DeviceName);
383 break;
384 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
385 break;
386 }
387
388 return rc;
389 }
390
391 /**
392 * _scsih_get_sas_address - set the sas_address for given device handle
393 * @handle: device handle
394 * @sas_address: sas address
395 *
396 * Returns 0 success, non-zero when failure
397 */
398 static int
399 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
400 u64 *sas_address)
401 {
402 Mpi2SasDevicePage0_t sas_device_pg0;
403 Mpi2ConfigReply_t mpi_reply;
404 u32 ioc_status;
405
406 *sas_address = 0;
407
408 if (handle <= ioc->sas_hba.num_phys) {
409 *sas_address = ioc->sas_hba.sas_address;
410 return 0;
411 }
412
413 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
414 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
415 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
416 __FILE__, __LINE__, __func__);
417 return -ENXIO;
418 }
419
420 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
421 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
422 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
423 return 0;
424 }
425
426 /* we hit this because the given parent handle doesn't exist */
427 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
428 return -ENXIO;
429
430 /* else error case */
431 pr_err(MPT3SAS_FMT
432 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
433 ioc->name, handle, ioc_status,
434 __FILE__, __LINE__, __func__);
435 return -EIO;
436 }
437
438 /**
439 * _scsih_determine_boot_device - determine boot device.
440 * @ioc: per adapter object
441 * @device: either sas_device or raid_device object
442 * @is_raid: [flag] 1 = raid object, 0 = sas object
443 *
444 * Determines whether this device should be first reported device to
445 * to scsi-ml or sas transport, this purpose is for persistent boot device.
446 * There are primary, alternate, and current entries in bios page 2. The order
447 * priority is primary, alternate, then current. This routine saves
448 * the corresponding device object and is_raid flag in the ioc object.
449 * The saved data to be used later in _scsih_probe_boot_devices().
450 */
451 static void
452 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
453 void *device, u8 is_raid)
454 {
455 struct _sas_device *sas_device;
456 struct _raid_device *raid_device;
457 u64 sas_address;
458 u64 device_name;
459 u64 enclosure_logical_id;
460 u16 slot;
461
462 /* only process this function when driver loads */
463 if (!ioc->is_driver_loading)
464 return;
465
466 /* no Bios, return immediately */
467 if (!ioc->bios_pg3.BiosVersion)
468 return;
469
470 if (!is_raid) {
471 sas_device = device;
472 sas_address = sas_device->sas_address;
473 device_name = sas_device->device_name;
474 enclosure_logical_id = sas_device->enclosure_logical_id;
475 slot = sas_device->slot;
476 } else {
477 raid_device = device;
478 sas_address = raid_device->wwid;
479 device_name = 0;
480 enclosure_logical_id = 0;
481 slot = 0;
482 }
483
484 if (!ioc->req_boot_device.device) {
485 if (_scsih_is_boot_device(sas_address, device_name,
486 enclosure_logical_id, slot,
487 (ioc->bios_pg2.ReqBootDeviceForm &
488 MPI2_BIOSPAGE2_FORM_MASK),
489 &ioc->bios_pg2.RequestedBootDevice)) {
490 dinitprintk(ioc, pr_info(MPT3SAS_FMT
491 "%s: req_boot_device(0x%016llx)\n",
492 ioc->name, __func__,
493 (unsigned long long)sas_address));
494 ioc->req_boot_device.device = device;
495 ioc->req_boot_device.is_raid = is_raid;
496 }
497 }
498
499 if (!ioc->req_alt_boot_device.device) {
500 if (_scsih_is_boot_device(sas_address, device_name,
501 enclosure_logical_id, slot,
502 (ioc->bios_pg2.ReqAltBootDeviceForm &
503 MPI2_BIOSPAGE2_FORM_MASK),
504 &ioc->bios_pg2.RequestedAltBootDevice)) {
505 dinitprintk(ioc, pr_info(MPT3SAS_FMT
506 "%s: req_alt_boot_device(0x%016llx)\n",
507 ioc->name, __func__,
508 (unsigned long long)sas_address));
509 ioc->req_alt_boot_device.device = device;
510 ioc->req_alt_boot_device.is_raid = is_raid;
511 }
512 }
513
514 if (!ioc->current_boot_device.device) {
515 if (_scsih_is_boot_device(sas_address, device_name,
516 enclosure_logical_id, slot,
517 (ioc->bios_pg2.CurrentBootDeviceForm &
518 MPI2_BIOSPAGE2_FORM_MASK),
519 &ioc->bios_pg2.CurrentBootDevice)) {
520 dinitprintk(ioc, pr_info(MPT3SAS_FMT
521 "%s: current_boot_device(0x%016llx)\n",
522 ioc->name, __func__,
523 (unsigned long long)sas_address));
524 ioc->current_boot_device.device = device;
525 ioc->current_boot_device.is_raid = is_raid;
526 }
527 }
528 }
529
530 static struct _sas_device *
531 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
532 struct MPT3SAS_TARGET *tgt_priv)
533 {
534 struct _sas_device *ret;
535
536 assert_spin_locked(&ioc->sas_device_lock);
537
538 ret = tgt_priv->sdev;
539 if (ret)
540 sas_device_get(ret);
541
542 return ret;
543 }
544
545 static struct _sas_device *
546 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
547 struct MPT3SAS_TARGET *tgt_priv)
548 {
549 struct _sas_device *ret;
550 unsigned long flags;
551
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
555
556 return ret;
557 }
558
559
560 struct _sas_device *
561 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
562 u64 sas_address)
563 {
564 struct _sas_device *sas_device;
565
566 assert_spin_locked(&ioc->sas_device_lock);
567
568 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
569 if (sas_device->sas_address == sas_address)
570 goto found_device;
571
572 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
573 if (sas_device->sas_address == sas_address)
574 goto found_device;
575
576 return NULL;
577
578 found_device:
579 sas_device_get(sas_device);
580 return sas_device;
581 }
582
583 /**
584 * mpt3sas_get_sdev_by_addr - sas device search
585 * @ioc: per adapter object
586 * @sas_address: sas address
587 * Context: Calling function should acquire ioc->sas_device_lock
588 *
589 * This searches for sas_device based on sas_address, then return sas_device
590 * object.
591 */
592 struct _sas_device *
593 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
594 u64 sas_address)
595 {
596 struct _sas_device *sas_device;
597 unsigned long flags;
598
599 spin_lock_irqsave(&ioc->sas_device_lock, flags);
600 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
601 sas_address);
602 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
603
604 return sas_device;
605 }
606
607 static struct _sas_device *
608 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
609 {
610 struct _sas_device *sas_device;
611
612 assert_spin_locked(&ioc->sas_device_lock);
613
614 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
615 if (sas_device->handle == handle)
616 goto found_device;
617
618 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
619 if (sas_device->handle == handle)
620 goto found_device;
621
622 return NULL;
623
624 found_device:
625 sas_device_get(sas_device);
626 return sas_device;
627 }
628
629 /**
630 * mpt3sas_get_sdev_by_handle - sas device search
631 * @ioc: per adapter object
632 * @handle: sas device handle (assigned by firmware)
633 * Context: Calling function should acquire ioc->sas_device_lock
634 *
635 * This searches for sas_device based on sas_address, then return sas_device
636 * object.
637 */
638 static struct _sas_device *
639 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
640 {
641 struct _sas_device *sas_device;
642 unsigned long flags;
643
644 spin_lock_irqsave(&ioc->sas_device_lock, flags);
645 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
646 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
647
648 return sas_device;
649 }
650
651 /**
652 * _scsih_sas_device_remove - remove sas_device from list.
653 * @ioc: per adapter object
654 * @sas_device: the sas_device object
655 * Context: This function will acquire ioc->sas_device_lock.
656 *
657 * If sas_device is on the list, remove it and decrement its reference count.
658 */
659 static void
660 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
661 struct _sas_device *sas_device)
662 {
663 unsigned long flags;
664
665 if (!sas_device)
666 return;
667 pr_info(MPT3SAS_FMT
668 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
669 ioc->name, sas_device->handle,
670 (unsigned long long) sas_device->sas_address);
671
672 if (sas_device->enclosure_handle != 0)
673 pr_info(MPT3SAS_FMT
674 "removing enclosure logical id(0x%016llx), slot(%d)\n",
675 ioc->name, (unsigned long long)
676 sas_device->enclosure_logical_id, sas_device->slot);
677
678 if (sas_device->connector_name[0] != '\0')
679 pr_info(MPT3SAS_FMT
680 "removing enclosure level(0x%04x), connector name( %s)\n",
681 ioc->name, sas_device->enclosure_level,
682 sas_device->connector_name);
683
684 /*
685 * The lock serializes access to the list, but we still need to verify
686 * that nobody removed the entry while we were waiting on the lock.
687 */
688 spin_lock_irqsave(&ioc->sas_device_lock, flags);
689 if (!list_empty(&sas_device->list)) {
690 list_del_init(&sas_device->list);
691 sas_device_put(sas_device);
692 }
693 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
694 }
695
696 /**
697 * _scsih_device_remove_by_handle - removing device object by handle
698 * @ioc: per adapter object
699 * @handle: device handle
700 *
701 * Return nothing.
702 */
703 static void
704 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
705 {
706 struct _sas_device *sas_device;
707 unsigned long flags;
708
709 if (ioc->shost_recovery)
710 return;
711
712 spin_lock_irqsave(&ioc->sas_device_lock, flags);
713 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
714 if (sas_device) {
715 list_del_init(&sas_device->list);
716 sas_device_put(sas_device);
717 }
718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
719 if (sas_device) {
720 _scsih_remove_device(ioc, sas_device);
721 sas_device_put(sas_device);
722 }
723 }
724
725 /**
726 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
727 * @ioc: per adapter object
728 * @sas_address: device sas_address
729 *
730 * Return nothing.
731 */
732 void
733 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
734 u64 sas_address)
735 {
736 struct _sas_device *sas_device;
737 unsigned long flags;
738
739 if (ioc->shost_recovery)
740 return;
741
742 spin_lock_irqsave(&ioc->sas_device_lock, flags);
743 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
744 if (sas_device) {
745 list_del_init(&sas_device->list);
746 sas_device_put(sas_device);
747 }
748 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
749 if (sas_device) {
750 _scsih_remove_device(ioc, sas_device);
751 sas_device_put(sas_device);
752 }
753 }
754
755 /**
756 * _scsih_sas_device_add - insert sas_device to the list.
757 * @ioc: per adapter object
758 * @sas_device: the sas_device object
759 * Context: This function will acquire ioc->sas_device_lock.
760 *
761 * Adding new object to the ioc->sas_device_list.
762 */
763 static void
764 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
765 struct _sas_device *sas_device)
766 {
767 unsigned long flags;
768
769 dewtprintk(ioc, pr_info(MPT3SAS_FMT
770 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
771 ioc->name, __func__, sas_device->handle,
772 (unsigned long long)sas_device->sas_address));
773
774 if (sas_device->enclosure_handle != 0)
775 dewtprintk(ioc, pr_info(MPT3SAS_FMT
776 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
777 ioc->name, __func__, (unsigned long long)
778 sas_device->enclosure_logical_id, sas_device->slot));
779
780 if (sas_device->connector_name[0] != '\0')
781 dewtprintk(ioc, pr_info(MPT3SAS_FMT
782 "%s: enclosure level(0x%04x), connector name( %s)\n",
783 ioc->name, __func__,
784 sas_device->enclosure_level, sas_device->connector_name));
785
786 spin_lock_irqsave(&ioc->sas_device_lock, flags);
787 sas_device_get(sas_device);
788 list_add_tail(&sas_device->list, &ioc->sas_device_list);
789 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
790
791 if (ioc->hide_drives) {
792 clear_bit(sas_device->handle, ioc->pend_os_device_add);
793 return;
794 }
795
796 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
797 sas_device->sas_address_parent)) {
798 _scsih_sas_device_remove(ioc, sas_device);
799 } else if (!sas_device->starget) {
800 /*
801 * When asyn scanning is enabled, its not possible to remove
802 * devices while scanning is turned on due to an oops in
803 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
804 */
805 if (!ioc->is_driver_loading) {
806 mpt3sas_transport_port_remove(ioc,
807 sas_device->sas_address,
808 sas_device->sas_address_parent);
809 _scsih_sas_device_remove(ioc, sas_device);
810 }
811 } else
812 clear_bit(sas_device->handle, ioc->pend_os_device_add);
813 }
814
815 /**
816 * _scsih_sas_device_init_add - insert sas_device to the list.
817 * @ioc: per adapter object
818 * @sas_device: the sas_device object
819 * Context: This function will acquire ioc->sas_device_lock.
820 *
821 * Adding new object at driver load time to the ioc->sas_device_init_list.
822 */
823 static void
824 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
825 struct _sas_device *sas_device)
826 {
827 unsigned long flags;
828
829 dewtprintk(ioc, pr_info(MPT3SAS_FMT
830 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
831 __func__, sas_device->handle,
832 (unsigned long long)sas_device->sas_address));
833
834 if (sas_device->enclosure_handle != 0)
835 dewtprintk(ioc, pr_info(MPT3SAS_FMT
836 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
837 ioc->name, __func__, (unsigned long long)
838 sas_device->enclosure_logical_id, sas_device->slot));
839
840 if (sas_device->connector_name[0] != '\0')
841 dewtprintk(ioc, pr_info(MPT3SAS_FMT
842 "%s: enclosure level(0x%04x), connector name( %s)\n",
843 ioc->name, __func__, sas_device->enclosure_level,
844 sas_device->connector_name));
845
846 spin_lock_irqsave(&ioc->sas_device_lock, flags);
847 sas_device_get(sas_device);
848 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
849 _scsih_determine_boot_device(ioc, sas_device, 0);
850 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
851 }
852
853 /**
854 * _scsih_raid_device_find_by_id - raid device search
855 * @ioc: per adapter object
856 * @id: sas device target id
857 * @channel: sas device channel
858 * Context: Calling function should acquire ioc->raid_device_lock
859 *
860 * This searches for raid_device based on target id, then return raid_device
861 * object.
862 */
863 static struct _raid_device *
864 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
865 {
866 struct _raid_device *raid_device, *r;
867
868 r = NULL;
869 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
870 if (raid_device->id == id && raid_device->channel == channel) {
871 r = raid_device;
872 goto out;
873 }
874 }
875
876 out:
877 return r;
878 }
879
880 /**
881 * mpt3sas_raid_device_find_by_handle - raid device search
882 * @ioc: per adapter object
883 * @handle: sas device handle (assigned by firmware)
884 * Context: Calling function should acquire ioc->raid_device_lock
885 *
886 * This searches for raid_device based on handle, then return raid_device
887 * object.
888 */
889 struct _raid_device *
890 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
891 {
892 struct _raid_device *raid_device, *r;
893
894 r = NULL;
895 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
896 if (raid_device->handle != handle)
897 continue;
898 r = raid_device;
899 goto out;
900 }
901
902 out:
903 return r;
904 }
905
906 /**
907 * _scsih_raid_device_find_by_wwid - raid device search
908 * @ioc: per adapter object
909 * @handle: sas device handle (assigned by firmware)
910 * Context: Calling function should acquire ioc->raid_device_lock
911 *
912 * This searches for raid_device based on wwid, then return raid_device
913 * object.
914 */
915 static struct _raid_device *
916 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
917 {
918 struct _raid_device *raid_device, *r;
919
920 r = NULL;
921 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
922 if (raid_device->wwid != wwid)
923 continue;
924 r = raid_device;
925 goto out;
926 }
927
928 out:
929 return r;
930 }
931
932 /**
933 * _scsih_raid_device_add - add raid_device object
934 * @ioc: per adapter object
935 * @raid_device: raid_device object
936 *
937 * This is added to the raid_device_list link list.
938 */
939 static void
940 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
941 struct _raid_device *raid_device)
942 {
943 unsigned long flags;
944
945 dewtprintk(ioc, pr_info(MPT3SAS_FMT
946 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
947 raid_device->handle, (unsigned long long)raid_device->wwid));
948
949 spin_lock_irqsave(&ioc->raid_device_lock, flags);
950 list_add_tail(&raid_device->list, &ioc->raid_device_list);
951 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
952 }
953
954 /**
955 * _scsih_raid_device_remove - delete raid_device object
956 * @ioc: per adapter object
957 * @raid_device: raid_device object
958 *
959 */
960 static void
961 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
962 struct _raid_device *raid_device)
963 {
964 unsigned long flags;
965
966 spin_lock_irqsave(&ioc->raid_device_lock, flags);
967 list_del(&raid_device->list);
968 kfree(raid_device);
969 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
970 }
971
972 /**
973 * mpt3sas_scsih_expander_find_by_handle - expander device search
974 * @ioc: per adapter object
975 * @handle: expander handle (assigned by firmware)
976 * Context: Calling function should acquire ioc->sas_device_lock
977 *
978 * This searches for expander device based on handle, then returns the
979 * sas_node object.
980 */
981 struct _sas_node *
982 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
983 {
984 struct _sas_node *sas_expander, *r;
985
986 r = NULL;
987 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
988 if (sas_expander->handle != handle)
989 continue;
990 r = sas_expander;
991 goto out;
992 }
993 out:
994 return r;
995 }
996
997 /**
998 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
999 * @ioc: per adapter object
1000 * @sas_address: sas address
1001 * Context: Calling function should acquire ioc->sas_node_lock.
1002 *
1003 * This searches for expander device based on sas_address, then returns the
1004 * sas_node object.
1005 */
1006 struct _sas_node *
1007 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1008 u64 sas_address)
1009 {
1010 struct _sas_node *sas_expander, *r;
1011
1012 r = NULL;
1013 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1014 if (sas_expander->sas_address != sas_address)
1015 continue;
1016 r = sas_expander;
1017 goto out;
1018 }
1019 out:
1020 return r;
1021 }
1022
1023 /**
1024 * _scsih_expander_node_add - insert expander device to the list.
1025 * @ioc: per adapter object
1026 * @sas_expander: the sas_device object
1027 * Context: This function will acquire ioc->sas_node_lock.
1028 *
1029 * Adding new object to the ioc->sas_expander_list.
1030 *
1031 * Return nothing.
1032 */
1033 static void
1034 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1035 struct _sas_node *sas_expander)
1036 {
1037 unsigned long flags;
1038
1039 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1040 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1041 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1042 }
1043
1044 /**
1045 * _scsih_is_end_device - determines if device is an end device
1046 * @device_info: bitfield providing information about the device.
1047 * Context: none
1048 *
1049 * Returns 1 if end device.
1050 */
1051 static int
1052 _scsih_is_end_device(u32 device_info)
1053 {
1054 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1055 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1056 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1057 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1058 return 1;
1059 else
1060 return 0;
1061 }
1062
1063 /**
1064 * _scsih_scsi_lookup_get - returns scmd entry
1065 * @ioc: per adapter object
1066 * @smid: system request message index
1067 *
1068 * Returns the smid stored scmd pointer.
1069 */
1070 static struct scsi_cmnd *
1071 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1072 {
1073 return ioc->scsi_lookup[smid - 1].scmd;
1074 }
1075
1076 /**
1077 * __scsih_scsi_lookup_get_clear - returns scmd entry without
1078 * holding any lock.
1079 * @ioc: per adapter object
1080 * @smid: system request message index
1081 *
1082 * Returns the smid stored scmd pointer.
1083 * Then will dereference the stored scmd pointer.
1084 */
1085 static inline struct scsi_cmnd *
1086 __scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc,
1087 u16 smid)
1088 {
1089 struct scsi_cmnd *scmd = NULL;
1090
1091 swap(scmd, ioc->scsi_lookup[smid - 1].scmd);
1092
1093 return scmd;
1094 }
1095
1096 /**
1097 * _scsih_scsi_lookup_get_clear - returns scmd entry
1098 * @ioc: per adapter object
1099 * @smid: system request message index
1100 *
1101 * Returns the smid stored scmd pointer.
1102 * Then will derefrence the stored scmd pointer.
1103 */
1104 static inline struct scsi_cmnd *
1105 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1106 {
1107 unsigned long flags;
1108 struct scsi_cmnd *scmd;
1109
1110 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1111 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
1112 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1113
1114 return scmd;
1115 }
1116
1117 /**
1118 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1119 * @ioc: per adapter object
1120 * @smid: system request message index
1121 * @scmd: pointer to scsi command object
1122 * Context: This function will acquire ioc->scsi_lookup_lock.
1123 *
1124 * This will search for a scmd pointer in the scsi_lookup array,
1125 * returning the revelent smid. A returned value of zero means invalid.
1126 */
1127 static u16
1128 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1129 *scmd)
1130 {
1131 u16 smid;
1132 unsigned long flags;
1133 int i;
1134
1135 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1136 smid = 0;
1137 for (i = 0; i < ioc->scsiio_depth; i++) {
1138 if (ioc->scsi_lookup[i].scmd == scmd) {
1139 smid = ioc->scsi_lookup[i].smid;
1140 goto out;
1141 }
1142 }
1143 out:
1144 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1145 return smid;
1146 }
1147
1148 /**
1149 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1150 * @ioc: per adapter object
1151 * @id: target id
1152 * @channel: channel
1153 * Context: This function will acquire ioc->scsi_lookup_lock.
1154 *
1155 * This will search for a matching channel:id in the scsi_lookup array,
1156 * returning 1 if found.
1157 */
1158 static u8
1159 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1160 int channel)
1161 {
1162 u8 found;
1163 unsigned long flags;
1164 int i;
1165
1166 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1167 found = 0;
1168 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1169 if (ioc->scsi_lookup[i].scmd &&
1170 (ioc->scsi_lookup[i].scmd->device->id == id &&
1171 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1172 found = 1;
1173 goto out;
1174 }
1175 }
1176 out:
1177 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1178 return found;
1179 }
1180
1181 /**
1182 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1183 * @ioc: per adapter object
1184 * @id: target id
1185 * @lun: lun number
1186 * @channel: channel
1187 * Context: This function will acquire ioc->scsi_lookup_lock.
1188 *
1189 * This will search for a matching channel:id:lun in the scsi_lookup array,
1190 * returning 1 if found.
1191 */
1192 static u8
1193 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1194 unsigned int lun, int channel)
1195 {
1196 u8 found;
1197 unsigned long flags;
1198 int i;
1199
1200 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1201 found = 0;
1202 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1203 if (ioc->scsi_lookup[i].scmd &&
1204 (ioc->scsi_lookup[i].scmd->device->id == id &&
1205 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1206 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1207 found = 1;
1208 goto out;
1209 }
1210 }
1211 out:
1212 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1213 return found;
1214 }
1215
1216 /**
1217 * scsih_change_queue_depth - setting device queue depth
1218 * @sdev: scsi device struct
1219 * @qdepth: requested queue depth
1220 *
1221 * Returns queue depth.
1222 */
1223 static int
1224 scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1225 {
1226 struct Scsi_Host *shost = sdev->host;
1227 int max_depth;
1228 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1229 struct MPT3SAS_DEVICE *sas_device_priv_data;
1230 struct MPT3SAS_TARGET *sas_target_priv_data;
1231 struct _sas_device *sas_device;
1232 unsigned long flags;
1233
1234 max_depth = shost->can_queue;
1235
1236 /* limit max device queue for SATA to 32 */
1237 sas_device_priv_data = sdev->hostdata;
1238 if (!sas_device_priv_data)
1239 goto not_sata;
1240 sas_target_priv_data = sas_device_priv_data->sas_target;
1241 if (!sas_target_priv_data)
1242 goto not_sata;
1243 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1244 goto not_sata;
1245
1246 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1247 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1248 if (sas_device) {
1249 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1250 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1251
1252 sas_device_put(sas_device);
1253 }
1254 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1255
1256 not_sata:
1257
1258 if (!sdev->tagged_supported)
1259 max_depth = 1;
1260 if (qdepth > max_depth)
1261 qdepth = max_depth;
1262 return scsi_change_queue_depth(sdev, qdepth);
1263 }
1264
1265 /**
1266 * scsih_target_alloc - target add routine
1267 * @starget: scsi target struct
1268 *
1269 * Returns 0 if ok. Any other return is assumed to be an error and
1270 * the device is ignored.
1271 */
1272 static int
1273 scsih_target_alloc(struct scsi_target *starget)
1274 {
1275 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1276 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1277 struct MPT3SAS_TARGET *sas_target_priv_data;
1278 struct _sas_device *sas_device;
1279 struct _raid_device *raid_device;
1280 unsigned long flags;
1281 struct sas_rphy *rphy;
1282
1283 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1284 GFP_KERNEL);
1285 if (!sas_target_priv_data)
1286 return -ENOMEM;
1287
1288 starget->hostdata = sas_target_priv_data;
1289 sas_target_priv_data->starget = starget;
1290 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1291
1292 /* RAID volumes */
1293 if (starget->channel == RAID_CHANNEL) {
1294 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1295 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1296 starget->channel);
1297 if (raid_device) {
1298 sas_target_priv_data->handle = raid_device->handle;
1299 sas_target_priv_data->sas_address = raid_device->wwid;
1300 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1301 if (ioc->is_warpdrive)
1302 sas_target_priv_data->raid_device = raid_device;
1303 raid_device->starget = starget;
1304 }
1305 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1306 return 0;
1307 }
1308
1309 /* sas/sata devices */
1310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1311 rphy = dev_to_rphy(starget->dev.parent);
1312 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1313 rphy->identify.sas_address);
1314
1315 if (sas_device) {
1316 sas_target_priv_data->handle = sas_device->handle;
1317 sas_target_priv_data->sas_address = sas_device->sas_address;
1318 sas_target_priv_data->sdev = sas_device;
1319 sas_device->starget = starget;
1320 sas_device->id = starget->id;
1321 sas_device->channel = starget->channel;
1322 if (test_bit(sas_device->handle, ioc->pd_handles))
1323 sas_target_priv_data->flags |=
1324 MPT_TARGET_FLAGS_RAID_COMPONENT;
1325 if (sas_device->fast_path)
1326 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1327 }
1328 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1329
1330 return 0;
1331 }
1332
1333 /**
1334 * scsih_target_destroy - target destroy routine
1335 * @starget: scsi target struct
1336 *
1337 * Returns nothing.
1338 */
1339 static void
1340 scsih_target_destroy(struct scsi_target *starget)
1341 {
1342 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1343 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1344 struct MPT3SAS_TARGET *sas_target_priv_data;
1345 struct _sas_device *sas_device;
1346 struct _raid_device *raid_device;
1347 unsigned long flags;
1348
1349 sas_target_priv_data = starget->hostdata;
1350 if (!sas_target_priv_data)
1351 return;
1352
1353 if (starget->channel == RAID_CHANNEL) {
1354 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1355 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1356 starget->channel);
1357 if (raid_device) {
1358 raid_device->starget = NULL;
1359 raid_device->sdev = NULL;
1360 }
1361 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1362 goto out;
1363 }
1364
1365 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1366 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1367 if (sas_device && (sas_device->starget == starget) &&
1368 (sas_device->id == starget->id) &&
1369 (sas_device->channel == starget->channel))
1370 sas_device->starget = NULL;
1371
1372 if (sas_device) {
1373 /*
1374 * Corresponding get() is in _scsih_target_alloc()
1375 */
1376 sas_target_priv_data->sdev = NULL;
1377 sas_device_put(sas_device);
1378
1379 sas_device_put(sas_device);
1380 }
1381 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1382
1383 out:
1384 kfree(sas_target_priv_data);
1385 starget->hostdata = NULL;
1386 }
1387
1388 /**
1389 * scsih_slave_alloc - device add routine
1390 * @sdev: scsi device struct
1391 *
1392 * Returns 0 if ok. Any other return is assumed to be an error and
1393 * the device is ignored.
1394 */
1395 static int
1396 scsih_slave_alloc(struct scsi_device *sdev)
1397 {
1398 struct Scsi_Host *shost;
1399 struct MPT3SAS_ADAPTER *ioc;
1400 struct MPT3SAS_TARGET *sas_target_priv_data;
1401 struct MPT3SAS_DEVICE *sas_device_priv_data;
1402 struct scsi_target *starget;
1403 struct _raid_device *raid_device;
1404 struct _sas_device *sas_device;
1405 unsigned long flags;
1406
1407 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1408 GFP_KERNEL);
1409 if (!sas_device_priv_data)
1410 return -ENOMEM;
1411
1412 sas_device_priv_data->lun = sdev->lun;
1413 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1414
1415 starget = scsi_target(sdev);
1416 sas_target_priv_data = starget->hostdata;
1417 sas_target_priv_data->num_luns++;
1418 sas_device_priv_data->sas_target = sas_target_priv_data;
1419 sdev->hostdata = sas_device_priv_data;
1420 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1421 sdev->no_uld_attach = 1;
1422
1423 shost = dev_to_shost(&starget->dev);
1424 ioc = shost_priv(shost);
1425 if (starget->channel == RAID_CHANNEL) {
1426 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1427 raid_device = _scsih_raid_device_find_by_id(ioc,
1428 starget->id, starget->channel);
1429 if (raid_device)
1430 raid_device->sdev = sdev; /* raid is single lun */
1431 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1432 }
1433
1434 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1435 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1436 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1437 sas_target_priv_data->sas_address);
1438 if (sas_device && (sas_device->starget == NULL)) {
1439 sdev_printk(KERN_INFO, sdev,
1440 "%s : sas_device->starget set to starget @ %d\n",
1441 __func__, __LINE__);
1442 sas_device->starget = starget;
1443 }
1444
1445 if (sas_device)
1446 sas_device_put(sas_device);
1447
1448 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1449 }
1450
1451 return 0;
1452 }
1453
1454 /**
1455 * scsih_slave_destroy - device destroy routine
1456 * @sdev: scsi device struct
1457 *
1458 * Returns nothing.
1459 */
1460 static void
1461 scsih_slave_destroy(struct scsi_device *sdev)
1462 {
1463 struct MPT3SAS_TARGET *sas_target_priv_data;
1464 struct scsi_target *starget;
1465 struct Scsi_Host *shost;
1466 struct MPT3SAS_ADAPTER *ioc;
1467 struct _sas_device *sas_device;
1468 unsigned long flags;
1469
1470 if (!sdev->hostdata)
1471 return;
1472
1473 starget = scsi_target(sdev);
1474 sas_target_priv_data = starget->hostdata;
1475 sas_target_priv_data->num_luns--;
1476
1477 shost = dev_to_shost(&starget->dev);
1478 ioc = shost_priv(shost);
1479
1480 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1481 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1482 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1483 sas_target_priv_data);
1484 if (sas_device && !sas_target_priv_data->num_luns)
1485 sas_device->starget = NULL;
1486
1487 if (sas_device)
1488 sas_device_put(sas_device);
1489 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1490 }
1491
1492 kfree(sdev->hostdata);
1493 sdev->hostdata = NULL;
1494 }
1495
1496 /**
1497 * _scsih_display_sata_capabilities - sata capabilities
1498 * @ioc: per adapter object
1499 * @handle: device handle
1500 * @sdev: scsi device struct
1501 */
1502 static void
1503 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1504 u16 handle, struct scsi_device *sdev)
1505 {
1506 Mpi2ConfigReply_t mpi_reply;
1507 Mpi2SasDevicePage0_t sas_device_pg0;
1508 u32 ioc_status;
1509 u16 flags;
1510 u32 device_info;
1511
1512 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1513 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1514 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1515 ioc->name, __FILE__, __LINE__, __func__);
1516 return;
1517 }
1518
1519 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1520 MPI2_IOCSTATUS_MASK;
1521 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1522 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1523 ioc->name, __FILE__, __LINE__, __func__);
1524 return;
1525 }
1526
1527 flags = le16_to_cpu(sas_device_pg0.Flags);
1528 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1529
1530 sdev_printk(KERN_INFO, sdev,
1531 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1532 "sw_preserve(%s)\n",
1533 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1534 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1535 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1536 "n",
1537 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1538 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1539 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1540 }
1541
1542 /*
1543 * raid transport support -
1544 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1545 * unloading the driver followed by a load - I believe that the subroutine
1546 * raid_class_release() is not cleaning up properly.
1547 */
1548
1549 /**
1550 * scsih_is_raid - return boolean indicating device is raid volume
1551 * @dev the device struct object
1552 */
1553 static int
1554 scsih_is_raid(struct device *dev)
1555 {
1556 struct scsi_device *sdev = to_scsi_device(dev);
1557 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1558
1559 if (ioc->is_warpdrive)
1560 return 0;
1561 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1562 }
1563
1564 /**
1565 * scsih_get_resync - get raid volume resync percent complete
1566 * @dev the device struct object
1567 */
1568 static void
1569 scsih_get_resync(struct device *dev)
1570 {
1571 struct scsi_device *sdev = to_scsi_device(dev);
1572 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1573 static struct _raid_device *raid_device;
1574 unsigned long flags;
1575 Mpi2RaidVolPage0_t vol_pg0;
1576 Mpi2ConfigReply_t mpi_reply;
1577 u32 volume_status_flags;
1578 u8 percent_complete;
1579 u16 handle;
1580
1581 percent_complete = 0;
1582 handle = 0;
1583 if (ioc->is_warpdrive)
1584 goto out;
1585
1586 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1587 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1588 sdev->channel);
1589 if (raid_device) {
1590 handle = raid_device->handle;
1591 percent_complete = raid_device->percent_complete;
1592 }
1593 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1594
1595 if (!handle)
1596 goto out;
1597
1598 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1599 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1600 sizeof(Mpi2RaidVolPage0_t))) {
1601 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1602 ioc->name, __FILE__, __LINE__, __func__);
1603 percent_complete = 0;
1604 goto out;
1605 }
1606
1607 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1608 if (!(volume_status_flags &
1609 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1610 percent_complete = 0;
1611
1612 out:
1613
1614 switch (ioc->hba_mpi_version_belonged) {
1615 case MPI2_VERSION:
1616 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1617 break;
1618 case MPI25_VERSION:
1619 case MPI26_VERSION:
1620 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1621 break;
1622 }
1623 }
1624
1625 /**
1626 * scsih_get_state - get raid volume level
1627 * @dev the device struct object
1628 */
1629 static void
1630 scsih_get_state(struct device *dev)
1631 {
1632 struct scsi_device *sdev = to_scsi_device(dev);
1633 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1634 static struct _raid_device *raid_device;
1635 unsigned long flags;
1636 Mpi2RaidVolPage0_t vol_pg0;
1637 Mpi2ConfigReply_t mpi_reply;
1638 u32 volstate;
1639 enum raid_state state = RAID_STATE_UNKNOWN;
1640 u16 handle = 0;
1641
1642 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1643 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1644 sdev->channel);
1645 if (raid_device)
1646 handle = raid_device->handle;
1647 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1648
1649 if (!raid_device)
1650 goto out;
1651
1652 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1653 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1654 sizeof(Mpi2RaidVolPage0_t))) {
1655 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1656 ioc->name, __FILE__, __LINE__, __func__);
1657 goto out;
1658 }
1659
1660 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1661 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1662 state = RAID_STATE_RESYNCING;
1663 goto out;
1664 }
1665
1666 switch (vol_pg0.VolumeState) {
1667 case MPI2_RAID_VOL_STATE_OPTIMAL:
1668 case MPI2_RAID_VOL_STATE_ONLINE:
1669 state = RAID_STATE_ACTIVE;
1670 break;
1671 case MPI2_RAID_VOL_STATE_DEGRADED:
1672 state = RAID_STATE_DEGRADED;
1673 break;
1674 case MPI2_RAID_VOL_STATE_FAILED:
1675 case MPI2_RAID_VOL_STATE_MISSING:
1676 state = RAID_STATE_OFFLINE;
1677 break;
1678 }
1679 out:
1680 switch (ioc->hba_mpi_version_belonged) {
1681 case MPI2_VERSION:
1682 raid_set_state(mpt2sas_raid_template, dev, state);
1683 break;
1684 case MPI25_VERSION:
1685 case MPI26_VERSION:
1686 raid_set_state(mpt3sas_raid_template, dev, state);
1687 break;
1688 }
1689 }
1690
1691 /**
1692 * _scsih_set_level - set raid level
1693 * @sdev: scsi device struct
1694 * @volume_type: volume type
1695 */
1696 static void
1697 _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1698 struct scsi_device *sdev, u8 volume_type)
1699 {
1700 enum raid_level level = RAID_LEVEL_UNKNOWN;
1701
1702 switch (volume_type) {
1703 case MPI2_RAID_VOL_TYPE_RAID0:
1704 level = RAID_LEVEL_0;
1705 break;
1706 case MPI2_RAID_VOL_TYPE_RAID10:
1707 level = RAID_LEVEL_10;
1708 break;
1709 case MPI2_RAID_VOL_TYPE_RAID1E:
1710 level = RAID_LEVEL_1E;
1711 break;
1712 case MPI2_RAID_VOL_TYPE_RAID1:
1713 level = RAID_LEVEL_1;
1714 break;
1715 }
1716
1717 switch (ioc->hba_mpi_version_belonged) {
1718 case MPI2_VERSION:
1719 raid_set_level(mpt2sas_raid_template,
1720 &sdev->sdev_gendev, level);
1721 break;
1722 case MPI25_VERSION:
1723 case MPI26_VERSION:
1724 raid_set_level(mpt3sas_raid_template,
1725 &sdev->sdev_gendev, level);
1726 break;
1727 }
1728 }
1729
1730
1731 /**
1732 * _scsih_get_volume_capabilities - volume capabilities
1733 * @ioc: per adapter object
1734 * @sas_device: the raid_device object
1735 *
1736 * Returns 0 for success, else 1
1737 */
1738 static int
1739 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1740 struct _raid_device *raid_device)
1741 {
1742 Mpi2RaidVolPage0_t *vol_pg0;
1743 Mpi2RaidPhysDiskPage0_t pd_pg0;
1744 Mpi2SasDevicePage0_t sas_device_pg0;
1745 Mpi2ConfigReply_t mpi_reply;
1746 u16 sz;
1747 u8 num_pds;
1748
1749 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1750 &num_pds)) || !num_pds) {
1751 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1752 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1753 __func__));
1754 return 1;
1755 }
1756
1757 raid_device->num_pds = num_pds;
1758 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1759 sizeof(Mpi2RaidVol0PhysDisk_t));
1760 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1761 if (!vol_pg0) {
1762 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1763 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1764 __func__));
1765 return 1;
1766 }
1767
1768 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1769 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1770 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1771 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1772 __func__));
1773 kfree(vol_pg0);
1774 return 1;
1775 }
1776
1777 raid_device->volume_type = vol_pg0->VolumeType;
1778
1779 /* figure out what the underlying devices are by
1780 * obtaining the device_info bits for the 1st device
1781 */
1782 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1783 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1784 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1785 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1786 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1787 le16_to_cpu(pd_pg0.DevHandle)))) {
1788 raid_device->device_info =
1789 le32_to_cpu(sas_device_pg0.DeviceInfo);
1790 }
1791 }
1792
1793 kfree(vol_pg0);
1794 return 0;
1795 }
1796
1797 /**
1798 * _scsih_enable_tlr - setting TLR flags
1799 * @ioc: per adapter object
1800 * @sdev: scsi device struct
1801 *
1802 * Enabling Transaction Layer Retries for tape devices when
1803 * vpd page 0x90 is present
1804 *
1805 */
1806 static void
1807 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1808 {
1809
1810 /* only for TAPE */
1811 if (sdev->type != TYPE_TAPE)
1812 return;
1813
1814 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1815 return;
1816
1817 sas_enable_tlr(sdev);
1818 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1819 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1820 return;
1821
1822 }
1823
1824 /**
1825 * scsih_slave_configure - device configure routine.
1826 * @sdev: scsi device struct
1827 *
1828 * Returns 0 if ok. Any other return is assumed to be an error and
1829 * the device is ignored.
1830 */
1831 static int
1832 scsih_slave_configure(struct scsi_device *sdev)
1833 {
1834 struct Scsi_Host *shost = sdev->host;
1835 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1836 struct MPT3SAS_DEVICE *sas_device_priv_data;
1837 struct MPT3SAS_TARGET *sas_target_priv_data;
1838 struct _sas_device *sas_device;
1839 struct _raid_device *raid_device;
1840 unsigned long flags;
1841 int qdepth;
1842 u8 ssp_target = 0;
1843 char *ds = "";
1844 char *r_level = "";
1845 u16 handle, volume_handle = 0;
1846 u64 volume_wwid = 0;
1847
1848 qdepth = 1;
1849 sas_device_priv_data = sdev->hostdata;
1850 sas_device_priv_data->configured_lun = 1;
1851 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1852 sas_target_priv_data = sas_device_priv_data->sas_target;
1853 handle = sas_target_priv_data->handle;
1854
1855 /* raid volume handling */
1856 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1857
1858 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1859 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
1860 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1861 if (!raid_device) {
1862 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1863 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1864 __LINE__, __func__));
1865 return 1;
1866 }
1867
1868 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1869 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1870 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1871 __LINE__, __func__));
1872 return 1;
1873 }
1874
1875 /*
1876 * WARPDRIVE: Initialize the required data for Direct IO
1877 */
1878 mpt3sas_init_warpdrive_properties(ioc, raid_device);
1879
1880 /* RAID Queue Depth Support
1881 * IS volume = underlying qdepth of drive type, either
1882 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1883 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1884 */
1885 if (raid_device->device_info &
1886 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1887 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1888 ds = "SSP";
1889 } else {
1890 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1891 if (raid_device->device_info &
1892 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1893 ds = "SATA";
1894 else
1895 ds = "STP";
1896 }
1897
1898 switch (raid_device->volume_type) {
1899 case MPI2_RAID_VOL_TYPE_RAID0:
1900 r_level = "RAID0";
1901 break;
1902 case MPI2_RAID_VOL_TYPE_RAID1E:
1903 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1904 if (ioc->manu_pg10.OEMIdentifier &&
1905 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1906 MFG10_GF0_R10_DISPLAY) &&
1907 !(raid_device->num_pds % 2))
1908 r_level = "RAID10";
1909 else
1910 r_level = "RAID1E";
1911 break;
1912 case MPI2_RAID_VOL_TYPE_RAID1:
1913 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1914 r_level = "RAID1";
1915 break;
1916 case MPI2_RAID_VOL_TYPE_RAID10:
1917 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1918 r_level = "RAID10";
1919 break;
1920 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1921 default:
1922 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1923 r_level = "RAIDX";
1924 break;
1925 }
1926
1927 if (!ioc->hide_ir_msg)
1928 sdev_printk(KERN_INFO, sdev,
1929 "%s: handle(0x%04x), wwid(0x%016llx),"
1930 " pd_count(%d), type(%s)\n",
1931 r_level, raid_device->handle,
1932 (unsigned long long)raid_device->wwid,
1933 raid_device->num_pds, ds);
1934
1935 if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
1936 blk_queue_max_hw_sectors(sdev->request_queue,
1937 MPT3SAS_RAID_MAX_SECTORS);
1938 sdev_printk(KERN_INFO, sdev,
1939 "Set queue's max_sector to: %u\n",
1940 MPT3SAS_RAID_MAX_SECTORS);
1941 }
1942
1943 scsih_change_queue_depth(sdev, qdepth);
1944
1945 /* raid transport support */
1946 if (!ioc->is_warpdrive)
1947 _scsih_set_level(ioc, sdev, raid_device->volume_type);
1948 return 0;
1949 }
1950
1951 /* non-raid handling */
1952 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1953 if (mpt3sas_config_get_volume_handle(ioc, handle,
1954 &volume_handle)) {
1955 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1956 "failure at %s:%d/%s()!\n", ioc->name,
1957 __FILE__, __LINE__, __func__));
1958 return 1;
1959 }
1960 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1961 volume_handle, &volume_wwid)) {
1962 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1963 "failure at %s:%d/%s()!\n", ioc->name,
1964 __FILE__, __LINE__, __func__));
1965 return 1;
1966 }
1967 }
1968
1969 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1970 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1971 sas_device_priv_data->sas_target->sas_address);
1972 if (!sas_device) {
1973 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1974 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1975 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1976 __func__));
1977 return 1;
1978 }
1979
1980 sas_device->volume_handle = volume_handle;
1981 sas_device->volume_wwid = volume_wwid;
1982 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1983 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1984 ssp_target = 1;
1985 if (sas_device->device_info &
1986 MPI2_SAS_DEVICE_INFO_SEP) {
1987 sdev_printk(KERN_WARNING, sdev,
1988 "set ignore_delay_remove for handle(0x%04x)\n",
1989 sas_device_priv_data->sas_target->handle);
1990 sas_device_priv_data->ignore_delay_remove = 1;
1991 ds = "SES";
1992 } else
1993 ds = "SSP";
1994 } else {
1995 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1996 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1997 ds = "STP";
1998 else if (sas_device->device_info &
1999 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2000 ds = "SATA";
2001 }
2002
2003 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
2004 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2005 ds, handle, (unsigned long long)sas_device->sas_address,
2006 sas_device->phy, (unsigned long long)sas_device->device_name);
2007 if (sas_device->enclosure_handle != 0)
2008 sdev_printk(KERN_INFO, sdev,
2009 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
2010 ds, (unsigned long long)
2011 sas_device->enclosure_logical_id, sas_device->slot);
2012 if (sas_device->connector_name[0] != '\0')
2013 sdev_printk(KERN_INFO, sdev,
2014 "%s: enclosure level(0x%04x), connector name( %s)\n",
2015 ds, sas_device->enclosure_level,
2016 sas_device->connector_name);
2017
2018 sas_device_put(sas_device);
2019 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2020
2021 if (!ssp_target)
2022 _scsih_display_sata_capabilities(ioc, handle, sdev);
2023
2024
2025 scsih_change_queue_depth(sdev, qdepth);
2026
2027 if (ssp_target) {
2028 sas_read_port_mode_page(sdev);
2029 _scsih_enable_tlr(ioc, sdev);
2030 }
2031
2032 return 0;
2033 }
2034
2035 /**
2036 * scsih_bios_param - fetch head, sector, cylinder info for a disk
2037 * @sdev: scsi device struct
2038 * @bdev: pointer to block device context
2039 * @capacity: device size (in 512 byte sectors)
2040 * @params: three element array to place output:
2041 * params[0] number of heads (max 255)
2042 * params[1] number of sectors (max 63)
2043 * params[2] number of cylinders
2044 *
2045 * Return nothing.
2046 */
2047 static int
2048 scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2049 sector_t capacity, int params[])
2050 {
2051 int heads;
2052 int sectors;
2053 sector_t cylinders;
2054 ulong dummy;
2055
2056 heads = 64;
2057 sectors = 32;
2058
2059 dummy = heads * sectors;
2060 cylinders = capacity;
2061 sector_div(cylinders, dummy);
2062
2063 /*
2064 * Handle extended translation size for logical drives
2065 * > 1Gb
2066 */
2067 if ((ulong)capacity >= 0x200000) {
2068 heads = 255;
2069 sectors = 63;
2070 dummy = heads * sectors;
2071 cylinders = capacity;
2072 sector_div(cylinders, dummy);
2073 }
2074
2075 /* return result */
2076 params[0] = heads;
2077 params[1] = sectors;
2078 params[2] = cylinders;
2079
2080 return 0;
2081 }
2082
2083 /**
2084 * _scsih_response_code - translation of device response code
2085 * @ioc: per adapter object
2086 * @response_code: response code returned by the device
2087 *
2088 * Return nothing.
2089 */
2090 static void
2091 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2092 {
2093 char *desc;
2094
2095 switch (response_code) {
2096 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2097 desc = "task management request completed";
2098 break;
2099 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2100 desc = "invalid frame";
2101 break;
2102 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2103 desc = "task management request not supported";
2104 break;
2105 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2106 desc = "task management request failed";
2107 break;
2108 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2109 desc = "task management request succeeded";
2110 break;
2111 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2112 desc = "invalid lun";
2113 break;
2114 case 0xA:
2115 desc = "overlapped tag attempted";
2116 break;
2117 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2118 desc = "task queued, however not sent to target";
2119 break;
2120 default:
2121 desc = "unknown";
2122 break;
2123 }
2124 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2125 ioc->name, response_code, desc);
2126 }
2127
2128 /**
2129 * _scsih_tm_done - tm completion routine
2130 * @ioc: per adapter object
2131 * @smid: system request message index
2132 * @msix_index: MSIX table index supplied by the OS
2133 * @reply: reply message frame(lower 32bit addr)
2134 * Context: none.
2135 *
2136 * The callback handler when using scsih_issue_tm.
2137 *
2138 * Return 1 meaning mf should be freed from _base_interrupt
2139 * 0 means the mf is freed from this function.
2140 */
2141 static u8
2142 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2143 {
2144 MPI2DefaultReply_t *mpi_reply;
2145
2146 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2147 return 1;
2148 if (ioc->tm_cmds.smid != smid)
2149 return 1;
2150 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2151 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2152 if (mpi_reply) {
2153 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2154 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2155 }
2156 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2157 complete(&ioc->tm_cmds.done);
2158 return 1;
2159 }
2160
2161 /**
2162 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2163 * @ioc: per adapter object
2164 * @handle: device handle
2165 *
2166 * During taskmangement request, we need to freeze the device queue.
2167 */
2168 void
2169 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2170 {
2171 struct MPT3SAS_DEVICE *sas_device_priv_data;
2172 struct scsi_device *sdev;
2173 u8 skip = 0;
2174
2175 shost_for_each_device(sdev, ioc->shost) {
2176 if (skip)
2177 continue;
2178 sas_device_priv_data = sdev->hostdata;
2179 if (!sas_device_priv_data)
2180 continue;
2181 if (sas_device_priv_data->sas_target->handle == handle) {
2182 sas_device_priv_data->sas_target->tm_busy = 1;
2183 skip = 1;
2184 ioc->ignore_loginfos = 1;
2185 }
2186 }
2187 }
2188
2189 /**
2190 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2191 * @ioc: per adapter object
2192 * @handle: device handle
2193 *
2194 * During taskmangement request, we need to freeze the device queue.
2195 */
2196 void
2197 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2198 {
2199 struct MPT3SAS_DEVICE *sas_device_priv_data;
2200 struct scsi_device *sdev;
2201 u8 skip = 0;
2202
2203 shost_for_each_device(sdev, ioc->shost) {
2204 if (skip)
2205 continue;
2206 sas_device_priv_data = sdev->hostdata;
2207 if (!sas_device_priv_data)
2208 continue;
2209 if (sas_device_priv_data->sas_target->handle == handle) {
2210 sas_device_priv_data->sas_target->tm_busy = 0;
2211 skip = 1;
2212 ioc->ignore_loginfos = 0;
2213 }
2214 }
2215 }
2216
2217 /**
2218 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2219 * @ioc: per adapter struct
2220 * @device_handle: device handle
2221 * @channel: the channel assigned by the OS
2222 * @id: the id assigned by the OS
2223 * @lun: lun number
2224 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2225 * @smid_task: smid assigned to the task
2226 * @timeout: timeout in seconds
2227 * Context: user
2228 *
2229 * A generic API for sending task management requests to firmware.
2230 *
2231 * The callback index is set inside `ioc->tm_cb_idx`.
2232 *
2233 * Return SUCCESS or FAILED.
2234 */
2235 int
2236 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2237 uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2238 {
2239 Mpi2SCSITaskManagementRequest_t *mpi_request;
2240 Mpi2SCSITaskManagementReply_t *mpi_reply;
2241 u16 smid = 0;
2242 u32 ioc_state;
2243 struct scsiio_tracker *scsi_lookup = NULL;
2244 int rc;
2245 u16 msix_task = 0;
2246
2247 lockdep_assert_held(&ioc->tm_cmds.mutex);
2248
2249 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2250 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2251 __func__, ioc->name);
2252 return FAILED;
2253 }
2254
2255 if (ioc->shost_recovery || ioc->remove_host ||
2256 ioc->pci_error_recovery) {
2257 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2258 __func__, ioc->name);
2259 return FAILED;
2260 }
2261
2262 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2263 if (ioc_state & MPI2_DOORBELL_USED) {
2264 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2265 "unexpected doorbell active!\n", ioc->name));
2266 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2267 return (!rc) ? SUCCESS : FAILED;
2268 }
2269
2270 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2271 mpt3sas_base_fault_info(ioc, ioc_state &
2272 MPI2_DOORBELL_DATA_MASK);
2273 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2274 return (!rc) ? SUCCESS : FAILED;
2275 }
2276
2277 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2278 if (!smid) {
2279 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2280 ioc->name, __func__);
2281 return FAILED;
2282 }
2283
2284 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2285 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2286
2287 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2288 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2289 ioc->name, handle, type, smid_task));
2290 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2291 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2292 ioc->tm_cmds.smid = smid;
2293 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2294 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2295 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2296 mpi_request->DevHandle = cpu_to_le16(handle);
2297 mpi_request->TaskType = type;
2298 mpi_request->TaskMID = cpu_to_le16(smid_task);
2299 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2300 mpt3sas_scsih_set_tm_flag(ioc, handle);
2301 init_completion(&ioc->tm_cmds.done);
2302 if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2303 (scsi_lookup->msix_io < ioc->reply_queue_count))
2304 msix_task = scsi_lookup->msix_io;
2305 else
2306 msix_task = 0;
2307 ioc->put_smid_hi_priority(ioc, smid, msix_task);
2308 wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2309 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2310 pr_err(MPT3SAS_FMT "%s: timeout\n",
2311 ioc->name, __func__);
2312 _debug_dump_mf(mpi_request,
2313 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2314 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2315 rc = mpt3sas_base_hard_reset_handler(ioc,
2316 FORCE_BIG_HAMMER);
2317 rc = (!rc) ? SUCCESS : FAILED;
2318 goto out;
2319 }
2320 }
2321
2322 /* sync IRQs in case those were busy during flush. */
2323 mpt3sas_base_sync_reply_irqs(ioc);
2324
2325 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2326 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2327 mpi_reply = ioc->tm_cmds.reply;
2328 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2329 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2330 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2331 le32_to_cpu(mpi_reply->IOCLogInfo),
2332 le32_to_cpu(mpi_reply->TerminationCount)));
2333 if (ioc->logging_level & MPT_DEBUG_TM) {
2334 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2335 if (mpi_reply->IOCStatus)
2336 _debug_dump_mf(mpi_request,
2337 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2338 }
2339 }
2340
2341 switch (type) {
2342 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2343 rc = SUCCESS;
2344 if (scsi_lookup->scmd == NULL)
2345 break;
2346 rc = FAILED;
2347 break;
2348
2349 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2350 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2351 rc = FAILED;
2352 else
2353 rc = SUCCESS;
2354 break;
2355 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2356 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2357 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2358 rc = FAILED;
2359 else
2360 rc = SUCCESS;
2361 break;
2362 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2363 rc = SUCCESS;
2364 break;
2365 default:
2366 rc = FAILED;
2367 break;
2368 }
2369
2370 out:
2371 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2372 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2373 return rc;
2374 }
2375
2376 int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2377 uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2378 {
2379 int ret;
2380
2381 mutex_lock(&ioc->tm_cmds.mutex);
2382 ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
2383 smid_task, timeout);
2384 mutex_unlock(&ioc->tm_cmds.mutex);
2385
2386 return ret;
2387 }
2388
2389 /**
2390 * _scsih_tm_display_info - displays info about the device
2391 * @ioc: per adapter struct
2392 * @scmd: pointer to scsi command object
2393 *
2394 * Called by task management callback handlers.
2395 */
2396 static void
2397 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2398 {
2399 struct scsi_target *starget = scmd->device->sdev_target;
2400 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2401 struct _sas_device *sas_device = NULL;
2402 unsigned long flags;
2403 char *device_str = NULL;
2404
2405 if (!priv_target)
2406 return;
2407 if (ioc->hide_ir_msg)
2408 device_str = "WarpDrive";
2409 else
2410 device_str = "volume";
2411
2412 scsi_print_command(scmd);
2413 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2414 starget_printk(KERN_INFO, starget,
2415 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2416 device_str, priv_target->handle,
2417 device_str, (unsigned long long)priv_target->sas_address);
2418 } else {
2419 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2420 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2421 if (sas_device) {
2422 if (priv_target->flags &
2423 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2424 starget_printk(KERN_INFO, starget,
2425 "volume handle(0x%04x), "
2426 "volume wwid(0x%016llx)\n",
2427 sas_device->volume_handle,
2428 (unsigned long long)sas_device->volume_wwid);
2429 }
2430 starget_printk(KERN_INFO, starget,
2431 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2432 sas_device->handle,
2433 (unsigned long long)sas_device->sas_address,
2434 sas_device->phy);
2435 if (sas_device->enclosure_handle != 0)
2436 starget_printk(KERN_INFO, starget,
2437 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2438 (unsigned long long)
2439 sas_device->enclosure_logical_id,
2440 sas_device->slot);
2441 if (sas_device->connector_name[0] != '\0')
2442 starget_printk(KERN_INFO, starget,
2443 "enclosure level(0x%04x),connector name(%s)\n",
2444 sas_device->enclosure_level,
2445 sas_device->connector_name);
2446
2447 sas_device_put(sas_device);
2448 }
2449 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2450 }
2451 }
2452
2453 /**
2454 * scsih_abort - eh threads main abort routine
2455 * @scmd: pointer to scsi command object
2456 *
2457 * Returns SUCCESS if command aborted else FAILED
2458 */
2459 static int
2460 scsih_abort(struct scsi_cmnd *scmd)
2461 {
2462 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2463 struct MPT3SAS_DEVICE *sas_device_priv_data;
2464 u16 smid;
2465 u16 handle;
2466 int r;
2467
2468 sdev_printk(KERN_INFO, scmd->device,
2469 "attempting task abort! scmd(%p)\n", scmd);
2470 _scsih_tm_display_info(ioc, scmd);
2471
2472 sas_device_priv_data = scmd->device->hostdata;
2473 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2474 sdev_printk(KERN_INFO, scmd->device,
2475 "device been deleted! scmd(%p)\n", scmd);
2476 scmd->result = DID_NO_CONNECT << 16;
2477 scmd->scsi_done(scmd);
2478 r = SUCCESS;
2479 goto out;
2480 }
2481
2482 /* search for the command */
2483 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2484 if (!smid) {
2485 scmd->result = DID_RESET << 16;
2486 r = SUCCESS;
2487 goto out;
2488 }
2489
2490 /* for hidden raid components and volumes this is not supported */
2491 if (sas_device_priv_data->sas_target->flags &
2492 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2493 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2494 scmd->result = DID_RESET << 16;
2495 r = FAILED;
2496 goto out;
2497 }
2498
2499 mpt3sas_halt_firmware(ioc);
2500
2501 handle = sas_device_priv_data->sas_target->handle;
2502 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2503 scmd->device->id, scmd->device->lun,
2504 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
2505
2506 out:
2507 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2508 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2509 return r;
2510 }
2511
2512 /**
2513 * scsih_dev_reset - eh threads main device reset routine
2514 * @scmd: pointer to scsi command object
2515 *
2516 * Returns SUCCESS if command aborted else FAILED
2517 */
2518 static int
2519 scsih_dev_reset(struct scsi_cmnd *scmd)
2520 {
2521 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2522 struct MPT3SAS_DEVICE *sas_device_priv_data;
2523 struct _sas_device *sas_device = NULL;
2524 u16 handle;
2525 int r;
2526
2527 struct scsi_target *starget = scmd->device->sdev_target;
2528 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2529
2530 sdev_printk(KERN_INFO, scmd->device,
2531 "attempting device reset! scmd(%p)\n", scmd);
2532 _scsih_tm_display_info(ioc, scmd);
2533
2534 sas_device_priv_data = scmd->device->hostdata;
2535 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2536 sdev_printk(KERN_INFO, scmd->device,
2537 "device been deleted! scmd(%p)\n", scmd);
2538 scmd->result = DID_NO_CONNECT << 16;
2539 scmd->scsi_done(scmd);
2540 r = SUCCESS;
2541 goto out;
2542 }
2543
2544 /* for hidden raid components obtain the volume_handle */
2545 handle = 0;
2546 if (sas_device_priv_data->sas_target->flags &
2547 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2548 sas_device = mpt3sas_get_sdev_from_target(ioc,
2549 target_priv_data);
2550 if (sas_device)
2551 handle = sas_device->volume_handle;
2552 } else
2553 handle = sas_device_priv_data->sas_target->handle;
2554
2555 if (!handle) {
2556 scmd->result = DID_RESET << 16;
2557 r = FAILED;
2558 goto out;
2559 }
2560
2561 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2562 scmd->device->id, scmd->device->lun,
2563 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
2564
2565 out:
2566 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2567 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2568
2569 if (sas_device)
2570 sas_device_put(sas_device);
2571
2572 return r;
2573 }
2574
2575 /**
2576 * scsih_target_reset - eh threads main target reset routine
2577 * @scmd: pointer to scsi command object
2578 *
2579 * Returns SUCCESS if command aborted else FAILED
2580 */
2581 static int
2582 scsih_target_reset(struct scsi_cmnd *scmd)
2583 {
2584 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2585 struct MPT3SAS_DEVICE *sas_device_priv_data;
2586 struct _sas_device *sas_device = NULL;
2587 u16 handle;
2588 int r;
2589 struct scsi_target *starget = scmd->device->sdev_target;
2590 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2591
2592 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2593 scmd);
2594 _scsih_tm_display_info(ioc, scmd);
2595
2596 sas_device_priv_data = scmd->device->hostdata;
2597 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2598 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2599 scmd);
2600 scmd->result = DID_NO_CONNECT << 16;
2601 scmd->scsi_done(scmd);
2602 r = SUCCESS;
2603 goto out;
2604 }
2605
2606 /* for hidden raid components obtain the volume_handle */
2607 handle = 0;
2608 if (sas_device_priv_data->sas_target->flags &
2609 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2610 sas_device = mpt3sas_get_sdev_from_target(ioc,
2611 target_priv_data);
2612 if (sas_device)
2613 handle = sas_device->volume_handle;
2614 } else
2615 handle = sas_device_priv_data->sas_target->handle;
2616
2617 if (!handle) {
2618 scmd->result = DID_RESET << 16;
2619 r = FAILED;
2620 goto out;
2621 }
2622
2623 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
2624 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2625 30);
2626
2627 out:
2628 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2629 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2630
2631 if (sas_device)
2632 sas_device_put(sas_device);
2633
2634 return r;
2635 }
2636
2637
2638 /**
2639 * scsih_host_reset - eh threads main host reset routine
2640 * @scmd: pointer to scsi command object
2641 *
2642 * Returns SUCCESS if command aborted else FAILED
2643 */
2644 static int
2645 scsih_host_reset(struct scsi_cmnd *scmd)
2646 {
2647 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2648 int r, retval;
2649
2650 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2651 ioc->name, scmd);
2652 scsi_print_command(scmd);
2653
2654 if (ioc->is_driver_loading) {
2655 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2656 ioc->name);
2657 r = FAILED;
2658 goto out;
2659 }
2660
2661 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2662 r = (retval < 0) ? FAILED : SUCCESS;
2663 out:
2664 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2665 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2666
2667 return r;
2668 }
2669
2670 /**
2671 * _scsih_fw_event_add - insert and queue up fw_event
2672 * @ioc: per adapter object
2673 * @fw_event: object describing the event
2674 * Context: This function will acquire ioc->fw_event_lock.
2675 *
2676 * This adds the firmware event object into link list, then queues it up to
2677 * be processed from user context.
2678 *
2679 * Return nothing.
2680 */
2681 static void
2682 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2683 {
2684 unsigned long flags;
2685
2686 if (ioc->firmware_event_thread == NULL)
2687 return;
2688
2689 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2690 fw_event_work_get(fw_event);
2691 INIT_LIST_HEAD(&fw_event->list);
2692 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2693 INIT_WORK(&fw_event->work, _firmware_event_work);
2694 fw_event_work_get(fw_event);
2695 queue_work(ioc->firmware_event_thread, &fw_event->work);
2696 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2697 }
2698
2699 /**
2700 * _scsih_fw_event_del_from_list - delete fw_event from the list
2701 * @ioc: per adapter object
2702 * @fw_event: object describing the event
2703 * Context: This function will acquire ioc->fw_event_lock.
2704 *
2705 * If the fw_event is on the fw_event_list, remove it and do a put.
2706 *
2707 * Return nothing.
2708 */
2709 static void
2710 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2711 *fw_event)
2712 {
2713 unsigned long flags;
2714
2715 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2716 if (!list_empty(&fw_event->list)) {
2717 list_del_init(&fw_event->list);
2718 fw_event_work_put(fw_event);
2719 }
2720 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2721 }
2722
2723
2724 /**
2725 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2726 * @ioc: per adapter object
2727 * @event_data: trigger event data
2728 *
2729 * Return nothing.
2730 */
2731 void
2732 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2733 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2734 {
2735 struct fw_event_work *fw_event;
2736 u16 sz;
2737
2738 if (ioc->is_driver_loading)
2739 return;
2740 sz = sizeof(*event_data);
2741 fw_event = alloc_fw_event_work(sz);
2742 if (!fw_event)
2743 return;
2744 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2745 fw_event->ioc = ioc;
2746 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2747 _scsih_fw_event_add(ioc, fw_event);
2748 fw_event_work_put(fw_event);
2749 }
2750
2751 /**
2752 * _scsih_error_recovery_delete_devices - remove devices not responding
2753 * @ioc: per adapter object
2754 *
2755 * Return nothing.
2756 */
2757 static void
2758 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2759 {
2760 struct fw_event_work *fw_event;
2761
2762 if (ioc->is_driver_loading)
2763 return;
2764 fw_event = alloc_fw_event_work(0);
2765 if (!fw_event)
2766 return;
2767 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2768 fw_event->ioc = ioc;
2769 _scsih_fw_event_add(ioc, fw_event);
2770 fw_event_work_put(fw_event);
2771 }
2772
2773 /**
2774 * mpt3sas_port_enable_complete - port enable completed (fake event)
2775 * @ioc: per adapter object
2776 *
2777 * Return nothing.
2778 */
2779 void
2780 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2781 {
2782 struct fw_event_work *fw_event;
2783
2784 fw_event = alloc_fw_event_work(0);
2785 if (!fw_event)
2786 return;
2787 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2788 fw_event->ioc = ioc;
2789 _scsih_fw_event_add(ioc, fw_event);
2790 fw_event_work_put(fw_event);
2791 }
2792
2793 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2794 {
2795 unsigned long flags;
2796 struct fw_event_work *fw_event = NULL;
2797
2798 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2799 if (!list_empty(&ioc->fw_event_list)) {
2800 fw_event = list_first_entry(&ioc->fw_event_list,
2801 struct fw_event_work, list);
2802 list_del_init(&fw_event->list);
2803 }
2804 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2805
2806 return fw_event;
2807 }
2808
2809 /**
2810 * _scsih_fw_event_cleanup_queue - cleanup event queue
2811 * @ioc: per adapter object
2812 *
2813 * Walk the firmware event queue, either killing timers, or waiting
2814 * for outstanding events to complete
2815 *
2816 * Return nothing.
2817 */
2818 static void
2819 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2820 {
2821 struct fw_event_work *fw_event;
2822
2823 if (list_empty(&ioc->fw_event_list) ||
2824 !ioc->firmware_event_thread || in_interrupt())
2825 return;
2826
2827 while ((fw_event = dequeue_next_fw_event(ioc))) {
2828 /*
2829 * Wait on the fw_event to complete. If this returns 1, then
2830 * the event was never executed, and we need a put for the
2831 * reference the work had on the fw_event.
2832 *
2833 * If it did execute, we wait for it to finish, and the put will
2834 * happen from _firmware_event_work()
2835 */
2836 if (cancel_work_sync(&fw_event->work))
2837 fw_event_work_put(fw_event);
2838
2839 fw_event_work_put(fw_event);
2840 }
2841 }
2842
2843 /**
2844 * _scsih_internal_device_block - block the sdev device
2845 * @sdev: per device object
2846 * @sas_device_priv_data : per device driver private data
2847 *
2848 * make sure device is blocked without error, if not
2849 * print an error
2850 */
2851 static void
2852 _scsih_internal_device_block(struct scsi_device *sdev,
2853 struct MPT3SAS_DEVICE *sas_device_priv_data)
2854 {
2855 int r = 0;
2856
2857 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2858 sas_device_priv_data->sas_target->handle);
2859 sas_device_priv_data->block = 1;
2860
2861 r = scsi_internal_device_block(sdev);
2862 if (r == -EINVAL)
2863 sdev_printk(KERN_WARNING, sdev,
2864 "device_block failed with return(%d) for handle(0x%04x)\n",
2865 r, sas_device_priv_data->sas_target->handle);
2866 }
2867
2868 /**
2869 * _scsih_internal_device_unblock - unblock the sdev device
2870 * @sdev: per device object
2871 * @sas_device_priv_data : per device driver private data
2872 * make sure device is unblocked without error, if not retry
2873 * by blocking and then unblocking
2874 */
2875
2876 static void
2877 _scsih_internal_device_unblock(struct scsi_device *sdev,
2878 struct MPT3SAS_DEVICE *sas_device_priv_data)
2879 {
2880 int r = 0;
2881
2882 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2883 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2884 sas_device_priv_data->block = 0;
2885 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2886 if (r == -EINVAL) {
2887 /* The device has been set to SDEV_RUNNING by SD layer during
2888 * device addition but the request queue is still stopped by
2889 * our earlier block call. We need to perform a block again
2890 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2891
2892 sdev_printk(KERN_WARNING, sdev,
2893 "device_unblock failed with return(%d) for handle(0x%04x) "
2894 "performing a block followed by an unblock\n",
2895 r, sas_device_priv_data->sas_target->handle);
2896 sas_device_priv_data->block = 1;
2897 r = scsi_internal_device_block(sdev);
2898 if (r)
2899 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2900 "failed with return(%d) for handle(0x%04x)\n",
2901 r, sas_device_priv_data->sas_target->handle);
2902
2903 sas_device_priv_data->block = 0;
2904 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2905 if (r)
2906 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2907 " failed with return(%d) for handle(0x%04x)\n",
2908 r, sas_device_priv_data->sas_target->handle);
2909 }
2910 }
2911
2912 /**
2913 * _scsih_ublock_io_all_device - unblock every device
2914 * @ioc: per adapter object
2915 *
2916 * change the device state from block to running
2917 */
2918 static void
2919 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2920 {
2921 struct MPT3SAS_DEVICE *sas_device_priv_data;
2922 struct scsi_device *sdev;
2923
2924 shost_for_each_device(sdev, ioc->shost) {
2925 sas_device_priv_data = sdev->hostdata;
2926 if (!sas_device_priv_data)
2927 continue;
2928 if (!sas_device_priv_data->block)
2929 continue;
2930
2931 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2932 "device_running, handle(0x%04x)\n",
2933 sas_device_priv_data->sas_target->handle));
2934 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2935 }
2936 }
2937
2938
2939 /**
2940 * _scsih_ublock_io_device - prepare device to be deleted
2941 * @ioc: per adapter object
2942 * @sas_addr: sas address
2943 *
2944 * unblock then put device in offline state
2945 */
2946 static void
2947 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2948 {
2949 struct MPT3SAS_DEVICE *sas_device_priv_data;
2950 struct scsi_device *sdev;
2951
2952 shost_for_each_device(sdev, ioc->shost) {
2953 sas_device_priv_data = sdev->hostdata;
2954 if (!sas_device_priv_data)
2955 continue;
2956 if (sas_device_priv_data->sas_target->sas_address
2957 != sas_address)
2958 continue;
2959 if (sas_device_priv_data->block)
2960 _scsih_internal_device_unblock(sdev,
2961 sas_device_priv_data);
2962 }
2963 }
2964
2965 /**
2966 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2967 * @ioc: per adapter object
2968 * @handle: device handle
2969 *
2970 * During device pull we need to appropriately set the sdev state.
2971 */
2972 static void
2973 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2974 {
2975 struct MPT3SAS_DEVICE *sas_device_priv_data;
2976 struct scsi_device *sdev;
2977
2978 shost_for_each_device(sdev, ioc->shost) {
2979 sas_device_priv_data = sdev->hostdata;
2980 if (!sas_device_priv_data)
2981 continue;
2982 if (sas_device_priv_data->block)
2983 continue;
2984 if (sas_device_priv_data->ignore_delay_remove) {
2985 sdev_printk(KERN_INFO, sdev,
2986 "%s skip device_block for SES handle(0x%04x)\n",
2987 __func__, sas_device_priv_data->sas_target->handle);
2988 continue;
2989 }
2990 _scsih_internal_device_block(sdev, sas_device_priv_data);
2991 }
2992 }
2993
2994 /**
2995 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2996 * @ioc: per adapter object
2997 * @handle: device handle
2998 *
2999 * During device pull we need to appropriately set the sdev state.
3000 */
3001 static void
3002 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3003 {
3004 struct MPT3SAS_DEVICE *sas_device_priv_data;
3005 struct scsi_device *sdev;
3006 struct _sas_device *sas_device;
3007
3008 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
3009 if (!sas_device)
3010 return;
3011
3012 shost_for_each_device(sdev, ioc->shost) {
3013 sas_device_priv_data = sdev->hostdata;
3014 if (!sas_device_priv_data)
3015 continue;
3016 if (sas_device_priv_data->sas_target->handle != handle)
3017 continue;
3018 if (sas_device_priv_data->block)
3019 continue;
3020 if (sas_device->pend_sas_rphy_add)
3021 continue;
3022 if (sas_device_priv_data->ignore_delay_remove) {
3023 sdev_printk(KERN_INFO, sdev,
3024 "%s skip device_block for SES handle(0x%04x)\n",
3025 __func__, sas_device_priv_data->sas_target->handle);
3026 continue;
3027 }
3028 _scsih_internal_device_block(sdev, sas_device_priv_data);
3029 }
3030
3031 sas_device_put(sas_device);
3032 }
3033
3034 /**
3035 * _scsih_block_io_to_children_attached_to_ex
3036 * @ioc: per adapter object
3037 * @sas_expander: the sas_device object
3038 *
3039 * This routine set sdev state to SDEV_BLOCK for all devices
3040 * attached to this expander. This function called when expander is
3041 * pulled.
3042 */
3043 static void
3044 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3045 struct _sas_node *sas_expander)
3046 {
3047 struct _sas_port *mpt3sas_port;
3048 struct _sas_device *sas_device;
3049 struct _sas_node *expander_sibling;
3050 unsigned long flags;
3051
3052 if (!sas_expander)
3053 return;
3054
3055 list_for_each_entry(mpt3sas_port,
3056 &sas_expander->sas_port_list, port_list) {
3057 if (mpt3sas_port->remote_identify.device_type ==
3058 SAS_END_DEVICE) {
3059 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3060 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3061 mpt3sas_port->remote_identify.sas_address);
3062 if (sas_device) {
3063 set_bit(sas_device->handle,
3064 ioc->blocking_handles);
3065 sas_device_put(sas_device);
3066 }
3067 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3068 }
3069 }
3070
3071 list_for_each_entry(mpt3sas_port,
3072 &sas_expander->sas_port_list, port_list) {
3073
3074 if (mpt3sas_port->remote_identify.device_type ==
3075 SAS_EDGE_EXPANDER_DEVICE ||
3076 mpt3sas_port->remote_identify.device_type ==
3077 SAS_FANOUT_EXPANDER_DEVICE) {
3078 expander_sibling =
3079 mpt3sas_scsih_expander_find_by_sas_address(
3080 ioc, mpt3sas_port->remote_identify.sas_address);
3081 _scsih_block_io_to_children_attached_to_ex(ioc,
3082 expander_sibling);
3083 }
3084 }
3085 }
3086
3087 /**
3088 * _scsih_block_io_to_children_attached_directly
3089 * @ioc: per adapter object
3090 * @event_data: topology change event data
3091 *
3092 * This routine set sdev state to SDEV_BLOCK for all devices
3093 * direct attached during device pull.
3094 */
3095 static void
3096 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3097 Mpi2EventDataSasTopologyChangeList_t *event_data)
3098 {
3099 int i;
3100 u16 handle;
3101 u16 reason_code;
3102
3103 for (i = 0; i < event_data->NumEntries; i++) {
3104 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3105 if (!handle)
3106 continue;
3107 reason_code = event_data->PHY[i].PhyStatus &
3108 MPI2_EVENT_SAS_TOPO_RC_MASK;
3109 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3110 _scsih_block_io_device(ioc, handle);
3111 }
3112 }
3113
3114 /**
3115 * _scsih_tm_tr_send - send task management request
3116 * @ioc: per adapter object
3117 * @handle: device handle
3118 * Context: interrupt time.
3119 *
3120 * This code is to initiate the device removal handshake protocol
3121 * with controller firmware. This function will issue target reset
3122 * using high priority request queue. It will send a sas iounit
3123 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3124 *
3125 * This is designed to send muliple task management request at the same
3126 * time to the fifo. If the fifo is full, we will append the request,
3127 * and process it in a future completion.
3128 */
3129 static void
3130 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3131 {
3132 Mpi2SCSITaskManagementRequest_t *mpi_request;
3133 u16 smid;
3134 struct _sas_device *sas_device = NULL;
3135 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3136 u64 sas_address = 0;
3137 unsigned long flags;
3138 struct _tr_list *delayed_tr;
3139 u32 ioc_state;
3140
3141 if (ioc->remove_host) {
3142 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3143 "%s: host has been removed: handle(0x%04x)\n",
3144 __func__, ioc->name, handle));
3145 return;
3146 } else if (ioc->pci_error_recovery) {
3147 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3148 "%s: host in pci error recovery: handle(0x%04x)\n",
3149 __func__, ioc->name,
3150 handle));
3151 return;
3152 }
3153 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3154 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3155 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3156 "%s: host is not operational: handle(0x%04x)\n",
3157 __func__, ioc->name,
3158 handle));
3159 return;
3160 }
3161
3162 /* if PD, then return */
3163 if (test_bit(handle, ioc->pd_handles))
3164 return;
3165
3166 clear_bit(handle, ioc->pend_os_device_add);
3167
3168 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3169 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3170 if (sas_device && sas_device->starget &&
3171 sas_device->starget->hostdata) {
3172 sas_target_priv_data = sas_device->starget->hostdata;
3173 sas_target_priv_data->deleted = 1;
3174 sas_address = sas_device->sas_address;
3175 }
3176 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3177
3178 if (sas_target_priv_data) {
3179 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3180 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3181 ioc->name, handle,
3182 (unsigned long long)sas_address));
3183 if (sas_device->enclosure_handle != 0)
3184 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3185 "setting delete flag:enclosure logical id(0x%016llx),"
3186 " slot(%d)\n", ioc->name, (unsigned long long)
3187 sas_device->enclosure_logical_id,
3188 sas_device->slot));
3189 if (sas_device->connector_name[0] != '\0')
3190 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3191 "setting delete flag: enclosure level(0x%04x),"
3192 " connector name( %s)\n", ioc->name,
3193 sas_device->enclosure_level,
3194 sas_device->connector_name));
3195 _scsih_ublock_io_device(ioc, sas_address);
3196 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3197 }
3198
3199 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3200 if (!smid) {
3201 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3202 if (!delayed_tr)
3203 goto out;
3204 INIT_LIST_HEAD(&delayed_tr->list);
3205 delayed_tr->handle = handle;
3206 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3207 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3208 "DELAYED:tr:handle(0x%04x), (open)\n",
3209 ioc->name, handle));
3210 goto out;
3211 }
3212
3213 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3214 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3215 ioc->name, handle, smid,
3216 ioc->tm_tr_cb_idx));
3217 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3218 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3219 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3220 mpi_request->DevHandle = cpu_to_le16(handle);
3221 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3222 set_bit(handle, ioc->device_remove_in_progress);
3223 ioc->put_smid_hi_priority(ioc, smid, 0);
3224 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3225
3226 out:
3227 if (sas_device)
3228 sas_device_put(sas_device);
3229 }
3230
3231 /**
3232 * _scsih_tm_tr_complete -
3233 * @ioc: per adapter object
3234 * @smid: system request message index
3235 * @msix_index: MSIX table index supplied by the OS
3236 * @reply: reply message frame(lower 32bit addr)
3237 * Context: interrupt time.
3238 *
3239 * This is the target reset completion routine.
3240 * This code is part of the code to initiate the device removal
3241 * handshake protocol with controller firmware.
3242 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3243 *
3244 * Return 1 meaning mf should be freed from _base_interrupt
3245 * 0 means the mf is freed from this function.
3246 */
3247 static u8
3248 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3249 u32 reply)
3250 {
3251 u16 handle;
3252 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3253 Mpi2SCSITaskManagementReply_t *mpi_reply =
3254 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3255 Mpi2SasIoUnitControlRequest_t *mpi_request;
3256 u16 smid_sas_ctrl;
3257 u32 ioc_state;
3258 struct _sc_list *delayed_sc;
3259
3260 if (ioc->remove_host) {
3261 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3262 "%s: host has been removed\n", __func__, ioc->name));
3263 return 1;
3264 } else if (ioc->pci_error_recovery) {
3265 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3266 "%s: host in pci error recovery\n", __func__,
3267 ioc->name));
3268 return 1;
3269 }
3270 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3271 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3272 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3273 "%s: host is not operational\n", __func__, ioc->name));
3274 return 1;
3275 }
3276 if (unlikely(!mpi_reply)) {
3277 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3278 ioc->name, __FILE__, __LINE__, __func__);
3279 return 1;
3280 }
3281 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3282 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3283 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3284 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3285 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3286 ioc->name, handle,
3287 le16_to_cpu(mpi_reply->DevHandle), smid));
3288 return 0;
3289 }
3290
3291 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3292 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3293 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3294 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3295 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3296 le32_to_cpu(mpi_reply->IOCLogInfo),
3297 le32_to_cpu(mpi_reply->TerminationCount)));
3298
3299 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3300 if (!smid_sas_ctrl) {
3301 delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3302 if (!delayed_sc)
3303 return _scsih_check_for_pending_tm(ioc, smid);
3304 INIT_LIST_HEAD(&delayed_sc->list);
3305 delayed_sc->handle = mpi_request_tm->DevHandle;
3306 list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3307 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3308 "DELAYED:sc:handle(0x%04x), (open)\n",
3309 ioc->name, handle));
3310 return _scsih_check_for_pending_tm(ioc, smid);
3311 }
3312
3313 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3314 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3315 ioc->name, handle, smid_sas_ctrl,
3316 ioc->tm_sas_control_cb_idx));
3317 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3318 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3319 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3320 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3321 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3322 ioc->put_smid_default(ioc, smid_sas_ctrl);
3323
3324 return _scsih_check_for_pending_tm(ioc, smid);
3325 }
3326
3327
3328 /**
3329 * _scsih_sas_control_complete - completion routine
3330 * @ioc: per adapter object
3331 * @smid: system request message index
3332 * @msix_index: MSIX table index supplied by the OS
3333 * @reply: reply message frame(lower 32bit addr)
3334 * Context: interrupt time.
3335 *
3336 * This is the sas iounit control completion routine.
3337 * This code is part of the code to initiate the device removal
3338 * handshake protocol with controller firmware.
3339 *
3340 * Return 1 meaning mf should be freed from _base_interrupt
3341 * 0 means the mf is freed from this function.
3342 */
3343 static u8
3344 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3345 u8 msix_index, u32 reply)
3346 {
3347 Mpi2SasIoUnitControlReply_t *mpi_reply =
3348 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3349
3350 if (likely(mpi_reply)) {
3351 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3352 "sc_complete:handle(0x%04x), (open) "
3353 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3354 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3355 le16_to_cpu(mpi_reply->IOCStatus),
3356 le32_to_cpu(mpi_reply->IOCLogInfo)));
3357 if (le16_to_cpu(mpi_reply->IOCStatus) ==
3358 MPI2_IOCSTATUS_SUCCESS) {
3359 clear_bit(le16_to_cpu(mpi_reply->DevHandle),
3360 ioc->device_remove_in_progress);
3361 }
3362 } else {
3363 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3364 ioc->name, __FILE__, __LINE__, __func__);
3365 }
3366 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
3367 }
3368
3369 /**
3370 * _scsih_tm_tr_volume_send - send target reset request for volumes
3371 * @ioc: per adapter object
3372 * @handle: device handle
3373 * Context: interrupt time.
3374 *
3375 * This is designed to send muliple task management request at the same
3376 * time to the fifo. If the fifo is full, we will append the request,
3377 * and process it in a future completion.
3378 */
3379 static void
3380 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3381 {
3382 Mpi2SCSITaskManagementRequest_t *mpi_request;
3383 u16 smid;
3384 struct _tr_list *delayed_tr;
3385
3386 if (ioc->shost_recovery || ioc->remove_host ||
3387 ioc->pci_error_recovery) {
3388 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3389 "%s: host reset in progress!\n",
3390 __func__, ioc->name));
3391 return;
3392 }
3393
3394 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3395 if (!smid) {
3396 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3397 if (!delayed_tr)
3398 return;
3399 INIT_LIST_HEAD(&delayed_tr->list);
3400 delayed_tr->handle = handle;
3401 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3402 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3403 "DELAYED:tr:handle(0x%04x), (open)\n",
3404 ioc->name, handle));
3405 return;
3406 }
3407
3408 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3409 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3410 ioc->name, handle, smid,
3411 ioc->tm_tr_volume_cb_idx));
3412 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3413 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3414 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3415 mpi_request->DevHandle = cpu_to_le16(handle);
3416 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3417 ioc->put_smid_hi_priority(ioc, smid, 0);
3418 }
3419
3420 /**
3421 * _scsih_tm_volume_tr_complete - target reset completion
3422 * @ioc: per adapter object
3423 * @smid: system request message index
3424 * @msix_index: MSIX table index supplied by the OS
3425 * @reply: reply message frame(lower 32bit addr)
3426 * Context: interrupt time.
3427 *
3428 * Return 1 meaning mf should be freed from _base_interrupt
3429 * 0 means the mf is freed from this function.
3430 */
3431 static u8
3432 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3433 u8 msix_index, u32 reply)
3434 {
3435 u16 handle;
3436 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3437 Mpi2SCSITaskManagementReply_t *mpi_reply =
3438 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3439
3440 if (ioc->shost_recovery || ioc->remove_host ||
3441 ioc->pci_error_recovery) {
3442 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3443 "%s: host reset in progress!\n",
3444 __func__, ioc->name));
3445 return 1;
3446 }
3447 if (unlikely(!mpi_reply)) {
3448 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3449 ioc->name, __FILE__, __LINE__, __func__);
3450 return 1;
3451 }
3452
3453 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3454 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3455 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3456 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3457 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3458 ioc->name, handle,
3459 le16_to_cpu(mpi_reply->DevHandle), smid));
3460 return 0;
3461 }
3462
3463 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3464 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3465 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3466 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3467 le32_to_cpu(mpi_reply->IOCLogInfo),
3468 le32_to_cpu(mpi_reply->TerminationCount)));
3469
3470 return _scsih_check_for_pending_tm(ioc, smid);
3471 }
3472
3473 /**
3474 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3475 * @ioc: per adapter object
3476 * @smid: system request message index
3477 * @event: Event ID
3478 * @event_context: used to track events uniquely
3479 *
3480 * Context - processed in interrupt context.
3481 */
3482 static void
3483 _scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3484 u32 event_context)
3485 {
3486 Mpi2EventAckRequest_t *ack_request;
3487 int i = smid - ioc->internal_smid;
3488 unsigned long flags;
3489
3490 /* Without releasing the smid just update the
3491 * call back index and reuse the same smid for
3492 * processing this delayed request
3493 */
3494 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3495 ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3496 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3497
3498 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3499 "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3500 ioc->name, le16_to_cpu(event), smid,
3501 ioc->base_cb_idx));
3502 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3503 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3504 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3505 ack_request->Event = event;
3506 ack_request->EventContext = event_context;
3507 ack_request->VF_ID = 0; /* TODO */
3508 ack_request->VP_ID = 0;
3509 ioc->put_smid_default(ioc, smid);
3510 }
3511
3512 /**
3513 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3514 * sas_io_unit_ctrl messages
3515 * @ioc: per adapter object
3516 * @smid: system request message index
3517 * @handle: device handle
3518 *
3519 * Context - processed in interrupt context.
3520 */
3521 static void
3522 _scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3523 u16 smid, u16 handle)
3524 {
3525 Mpi2SasIoUnitControlRequest_t *mpi_request;
3526 u32 ioc_state;
3527 int i = smid - ioc->internal_smid;
3528 unsigned long flags;
3529
3530 if (ioc->remove_host) {
3531 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3532 "%s: host has been removed\n",
3533 __func__, ioc->name));
3534 return;
3535 } else if (ioc->pci_error_recovery) {
3536 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3537 "%s: host in pci error recovery\n",
3538 __func__, ioc->name));
3539 return;
3540 }
3541 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3542 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3543 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3544 "%s: host is not operational\n",
3545 __func__, ioc->name));
3546 return;
3547 }
3548
3549 /* Without releasing the smid just update the
3550 * call back index and reuse the same smid for
3551 * processing this delayed request
3552 */
3553 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3554 ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3555 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3556
3557 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3558 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3559 ioc->name, le16_to_cpu(handle), smid,
3560 ioc->tm_sas_control_cb_idx));
3561 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3562 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3563 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3564 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3565 mpi_request->DevHandle = handle;
3566 ioc->put_smid_default(ioc, smid);
3567 }
3568
3569 /**
3570 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3571 * @ioc: per adapter object
3572 * @smid: system request message index
3573 *
3574 * Context: Executed in interrupt context
3575 *
3576 * This will check delayed internal messages list, and process the
3577 * next request.
3578 *
3579 * Return 1 meaning mf should be freed from _base_interrupt
3580 * 0 means the mf is freed from this function.
3581 */
3582 u8
3583 mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3584 {
3585 struct _sc_list *delayed_sc;
3586 struct _event_ack_list *delayed_event_ack;
3587
3588 if (!list_empty(&ioc->delayed_event_ack_list)) {
3589 delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3590 struct _event_ack_list, list);
3591 _scsih_issue_delayed_event_ack(ioc, smid,
3592 delayed_event_ack->Event, delayed_event_ack->EventContext);
3593 list_del(&delayed_event_ack->list);
3594 kfree(delayed_event_ack);
3595 return 0;
3596 }
3597
3598 if (!list_empty(&ioc->delayed_sc_list)) {
3599 delayed_sc = list_entry(ioc->delayed_sc_list.next,
3600 struct _sc_list, list);
3601 _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3602 delayed_sc->handle);
3603 list_del(&delayed_sc->list);
3604 kfree(delayed_sc);
3605 return 0;
3606 }
3607 return 1;
3608 }
3609
3610 /**
3611 * _scsih_check_for_pending_tm - check for pending task management
3612 * @ioc: per adapter object
3613 * @smid: system request message index
3614 *
3615 * This will check delayed target reset list, and feed the
3616 * next reqeust.
3617 *
3618 * Return 1 meaning mf should be freed from _base_interrupt
3619 * 0 means the mf is freed from this function.
3620 */
3621 static u8
3622 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3623 {
3624 struct _tr_list *delayed_tr;
3625
3626 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3627 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3628 struct _tr_list, list);
3629 mpt3sas_base_free_smid(ioc, smid);
3630 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3631 list_del(&delayed_tr->list);
3632 kfree(delayed_tr);
3633 return 0;
3634 }
3635
3636 if (!list_empty(&ioc->delayed_tr_list)) {
3637 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3638 struct _tr_list, list);
3639 mpt3sas_base_free_smid(ioc, smid);
3640 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3641 list_del(&delayed_tr->list);
3642 kfree(delayed_tr);
3643 return 0;
3644 }
3645
3646 return 1;
3647 }
3648
3649 /**
3650 * _scsih_check_topo_delete_events - sanity check on topo events
3651 * @ioc: per adapter object
3652 * @event_data: the event data payload
3653 *
3654 * This routine added to better handle cable breaker.
3655 *
3656 * This handles the case where driver receives multiple expander
3657 * add and delete events in a single shot. When there is a delete event
3658 * the routine will void any pending add events waiting in the event queue.
3659 *
3660 * Return nothing.
3661 */
3662 static void
3663 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3664 Mpi2EventDataSasTopologyChangeList_t *event_data)
3665 {
3666 struct fw_event_work *fw_event;
3667 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3668 u16 expander_handle;
3669 struct _sas_node *sas_expander;
3670 unsigned long flags;
3671 int i, reason_code;
3672 u16 handle;
3673
3674 for (i = 0 ; i < event_data->NumEntries; i++) {
3675 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3676 if (!handle)
3677 continue;
3678 reason_code = event_data->PHY[i].PhyStatus &
3679 MPI2_EVENT_SAS_TOPO_RC_MASK;
3680 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3681 _scsih_tm_tr_send(ioc, handle);
3682 }
3683
3684 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3685 if (expander_handle < ioc->sas_hba.num_phys) {
3686 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3687 return;
3688 }
3689 if (event_data->ExpStatus ==
3690 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3691 /* put expander attached devices into blocking state */
3692 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3693 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3694 expander_handle);
3695 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3696 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3697 do {
3698 handle = find_first_bit(ioc->blocking_handles,
3699 ioc->facts.MaxDevHandle);
3700 if (handle < ioc->facts.MaxDevHandle)
3701 _scsih_block_io_device(ioc, handle);
3702 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3703 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3704 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3705
3706 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3707 return;
3708
3709 /* mark ignore flag for pending events */
3710 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3711 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3712 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3713 fw_event->ignore)
3714 continue;
3715 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3716 fw_event->event_data;
3717 if (local_event_data->ExpStatus ==
3718 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3719 local_event_data->ExpStatus ==
3720 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3721 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3722 expander_handle) {
3723 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3724 "setting ignoring flag\n", ioc->name));
3725 fw_event->ignore = 1;
3726 }
3727 }
3728 }
3729 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3730 }
3731
3732 /**
3733 * _scsih_set_volume_delete_flag - setting volume delete flag
3734 * @ioc: per adapter object
3735 * @handle: device handle
3736 *
3737 * This returns nothing.
3738 */
3739 static void
3740 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3741 {
3742 struct _raid_device *raid_device;
3743 struct MPT3SAS_TARGET *sas_target_priv_data;
3744 unsigned long flags;
3745
3746 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3747 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
3748 if (raid_device && raid_device->starget &&
3749 raid_device->starget->hostdata) {
3750 sas_target_priv_data =
3751 raid_device->starget->hostdata;
3752 sas_target_priv_data->deleted = 1;
3753 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3754 "setting delete flag: handle(0x%04x), "
3755 "wwid(0x%016llx)\n", ioc->name, handle,
3756 (unsigned long long) raid_device->wwid));
3757 }
3758 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3759 }
3760
3761 /**
3762 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3763 * @handle: input handle
3764 * @a: handle for volume a
3765 * @b: handle for volume b
3766 *
3767 * IR firmware only supports two raid volumes. The purpose of this
3768 * routine is to set the volume handle in either a or b. When the given
3769 * input handle is non-zero, or when a and b have not been set before.
3770 */
3771 static void
3772 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3773 {
3774 if (!handle || handle == *a || handle == *b)
3775 return;
3776 if (!*a)
3777 *a = handle;
3778 else if (!*b)
3779 *b = handle;
3780 }
3781
3782 /**
3783 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3784 * @ioc: per adapter object
3785 * @event_data: the event data payload
3786 * Context: interrupt time.
3787 *
3788 * This routine will send target reset to volume, followed by target
3789 * resets to the PDs. This is called when a PD has been removed, or
3790 * volume has been deleted or removed. When the target reset is sent
3791 * to volume, the PD target resets need to be queued to start upon
3792 * completion of the volume target reset.
3793 *
3794 * Return nothing.
3795 */
3796 static void
3797 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3798 Mpi2EventDataIrConfigChangeList_t *event_data)
3799 {
3800 Mpi2EventIrConfigElement_t *element;
3801 int i;
3802 u16 handle, volume_handle, a, b;
3803 struct _tr_list *delayed_tr;
3804
3805 a = 0;
3806 b = 0;
3807
3808 if (ioc->is_warpdrive)
3809 return;
3810
3811 /* Volume Resets for Deleted or Removed */
3812 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3813 for (i = 0; i < event_data->NumElements; i++, element++) {
3814 if (le32_to_cpu(event_data->Flags) &
3815 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3816 continue;
3817 if (element->ReasonCode ==
3818 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3819 element->ReasonCode ==
3820 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3821 volume_handle = le16_to_cpu(element->VolDevHandle);
3822 _scsih_set_volume_delete_flag(ioc, volume_handle);
3823 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3824 }
3825 }
3826
3827 /* Volume Resets for UNHIDE events */
3828 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3829 for (i = 0; i < event_data->NumElements; i++, element++) {
3830 if (le32_to_cpu(event_data->Flags) &
3831 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3832 continue;
3833 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3834 volume_handle = le16_to_cpu(element->VolDevHandle);
3835 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3836 }
3837 }
3838
3839 if (a)
3840 _scsih_tm_tr_volume_send(ioc, a);
3841 if (b)
3842 _scsih_tm_tr_volume_send(ioc, b);
3843
3844 /* PD target resets */
3845 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3846 for (i = 0; i < event_data->NumElements; i++, element++) {
3847 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3848 continue;
3849 handle = le16_to_cpu(element->PhysDiskDevHandle);
3850 volume_handle = le16_to_cpu(element->VolDevHandle);
3851 clear_bit(handle, ioc->pd_handles);
3852 if (!volume_handle)
3853 _scsih_tm_tr_send(ioc, handle);
3854 else if (volume_handle == a || volume_handle == b) {
3855 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3856 BUG_ON(!delayed_tr);
3857 INIT_LIST_HEAD(&delayed_tr->list);
3858 delayed_tr->handle = handle;
3859 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3860 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3861 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3862 handle));
3863 } else
3864 _scsih_tm_tr_send(ioc, handle);
3865 }
3866 }
3867
3868
3869 /**
3870 * _scsih_check_volume_delete_events - set delete flag for volumes
3871 * @ioc: per adapter object
3872 * @event_data: the event data payload
3873 * Context: interrupt time.
3874 *
3875 * This will handle the case when the cable connected to entire volume is
3876 * pulled. We will take care of setting the deleted flag so normal IO will
3877 * not be sent.
3878 *
3879 * Return nothing.
3880 */
3881 static void
3882 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3883 Mpi2EventDataIrVolume_t *event_data)
3884 {
3885 u32 state;
3886
3887 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3888 return;
3889 state = le32_to_cpu(event_data->NewValue);
3890 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3891 MPI2_RAID_VOL_STATE_FAILED)
3892 _scsih_set_volume_delete_flag(ioc,
3893 le16_to_cpu(event_data->VolDevHandle));
3894 }
3895
3896 /**
3897 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3898 * @ioc: per adapter object
3899 * @event_data: the temp threshold event data
3900 * Context: interrupt time.
3901 *
3902 * Return nothing.
3903 */
3904 static void
3905 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3906 Mpi2EventDataTemperature_t *event_data)
3907 {
3908 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3909 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3910 " exceeded for Sensor: %d !!!\n", ioc->name,
3911 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3912 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3913 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3914 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3915 event_data->SensorNum);
3916 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3917 ioc->name, event_data->CurrentTemperature);
3918 }
3919 }
3920
3921 static inline bool ata_12_16_cmd(struct scsi_cmnd *scmd)
3922 {
3923 return (scmd->cmnd[0] == ATA_12 || scmd->cmnd[0] == ATA_16);
3924 }
3925
3926 /**
3927 * _scsih_flush_running_cmds - completing outstanding commands.
3928 * @ioc: per adapter object
3929 *
3930 * The flushing out of all pending scmd commands following host reset,
3931 * where all IO is dropped to the floor.
3932 *
3933 * Return nothing.
3934 */
3935 static void
3936 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3937 {
3938 struct scsi_cmnd *scmd;
3939 u16 smid;
3940 u16 count = 0;
3941
3942 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3943 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3944 if (!scmd)
3945 continue;
3946 count++;
3947 if (ata_12_16_cmd(scmd))
3948 scsi_internal_device_unblock(scmd->device,
3949 SDEV_RUNNING);
3950 mpt3sas_base_free_smid(ioc, smid);
3951 scsi_dma_unmap(scmd);
3952 if (ioc->pci_error_recovery)
3953 scmd->result = DID_NO_CONNECT << 16;
3954 else
3955 scmd->result = DID_RESET << 16;
3956 scmd->scsi_done(scmd);
3957 }
3958 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3959 ioc->name, count));
3960 }
3961
3962 /**
3963 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3964 * @ioc: per adapter object
3965 * @scmd: pointer to scsi command object
3966 * @mpi_request: pointer to the SCSI_IO request message frame
3967 *
3968 * Supporting protection 1 and 3.
3969 *
3970 * Returns nothing
3971 */
3972 static void
3973 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3974 Mpi2SCSIIORequest_t *mpi_request)
3975 {
3976 u16 eedp_flags;
3977 unsigned char prot_op = scsi_get_prot_op(scmd);
3978 unsigned char prot_type = scsi_get_prot_type(scmd);
3979 Mpi25SCSIIORequest_t *mpi_request_3v =
3980 (Mpi25SCSIIORequest_t *)mpi_request;
3981
3982 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3983 return;
3984
3985 if (prot_op == SCSI_PROT_READ_STRIP)
3986 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3987 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3988 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3989 else
3990 return;
3991
3992 switch (prot_type) {
3993 case SCSI_PROT_DIF_TYPE1:
3994 case SCSI_PROT_DIF_TYPE2:
3995
3996 /*
3997 * enable ref/guard checking
3998 * auto increment ref tag
3999 */
4000 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
4001 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
4002 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4003 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
4004 cpu_to_be32(scsi_prot_ref_tag(scmd));
4005 break;
4006
4007 case SCSI_PROT_DIF_TYPE3:
4008
4009 /*
4010 * enable guard checking
4011 */
4012 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4013
4014 break;
4015 }
4016
4017 mpi_request_3v->EEDPBlockSize =
4018 cpu_to_le16(scmd->device->sector_size);
4019
4020 if (ioc->is_gen35_ioc)
4021 eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
4022 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
4023 }
4024
4025 /**
4026 * _scsih_eedp_error_handling - return sense code for EEDP errors
4027 * @scmd: pointer to scsi command object
4028 * @ioc_status: ioc status
4029 *
4030 * Returns nothing
4031 */
4032 static void
4033 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
4034 {
4035 u8 ascq;
4036
4037 switch (ioc_status) {
4038 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4039 ascq = 0x01;
4040 break;
4041 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4042 ascq = 0x02;
4043 break;
4044 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4045 ascq = 0x03;
4046 break;
4047 default:
4048 ascq = 0x00;
4049 break;
4050 }
4051 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4052 ascq);
4053 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4054 SAM_STAT_CHECK_CONDITION;
4055 }
4056
4057 /**
4058 * scsih_qcmd - main scsi request entry point
4059 * @scmd: pointer to scsi command object
4060 * @done: function pointer to be invoked on completion
4061 *
4062 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4063 *
4064 * Returns 0 on success. If there's a failure, return either:
4065 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4066 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4067 */
4068 static int
4069 scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
4070 {
4071 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
4072 struct MPT3SAS_DEVICE *sas_device_priv_data;
4073 struct MPT3SAS_TARGET *sas_target_priv_data;
4074 struct _raid_device *raid_device;
4075 struct request *rq = scmd->request;
4076 int class;
4077 Mpi2SCSIIORequest_t *mpi_request;
4078 u32 mpi_control;
4079 u16 smid;
4080 u16 handle;
4081
4082 if (ioc->logging_level & MPT_DEBUG_SCSI)
4083 scsi_print_command(scmd);
4084
4085 /*
4086 * Lock the device for any subsequent command until command is
4087 * done.
4088 */
4089 if (ata_12_16_cmd(scmd))
4090 scsi_internal_device_block(scmd->device);
4091
4092 sas_device_priv_data = scmd->device->hostdata;
4093 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4094 scmd->result = DID_NO_CONNECT << 16;
4095 scmd->scsi_done(scmd);
4096 return 0;
4097 }
4098
4099 if (ioc->pci_error_recovery || ioc->remove_host) {
4100 scmd->result = DID_NO_CONNECT << 16;
4101 scmd->scsi_done(scmd);
4102 return 0;
4103 }
4104
4105 sas_target_priv_data = sas_device_priv_data->sas_target;
4106
4107 /* invalid device handle */
4108 handle = sas_target_priv_data->handle;
4109 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4110 scmd->result = DID_NO_CONNECT << 16;
4111 scmd->scsi_done(scmd);
4112 return 0;
4113 }
4114
4115
4116 /* host recovery or link resets sent via IOCTLs */
4117 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4118 return SCSI_MLQUEUE_HOST_BUSY;
4119
4120 /* device has been deleted */
4121 else if (sas_target_priv_data->deleted) {
4122 scmd->result = DID_NO_CONNECT << 16;
4123 scmd->scsi_done(scmd);
4124 return 0;
4125 /* device busy with task management */
4126 } else if (sas_target_priv_data->tm_busy ||
4127 sas_device_priv_data->block)
4128 return SCSI_MLQUEUE_DEVICE_BUSY;
4129
4130 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4131 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4132 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4133 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4134 else
4135 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4136
4137 /* set tags */
4138 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4139 /* NCQ Prio supported, make sure control indicated high priority */
4140 if (sas_device_priv_data->ncq_prio_enable) {
4141 class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4142 if (class == IOPRIO_CLASS_RT)
4143 mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
4144 }
4145 /* Make sure Device is not raid volume.
4146 * We do not expose raid functionality to upper layer for warpdrive.
4147 */
4148 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
4149 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
4150 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4151
4152 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4153 if (!smid) {
4154 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4155 ioc->name, __func__);
4156 goto out;
4157 }
4158 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4159 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4160 _scsih_setup_eedp(ioc, scmd, mpi_request);
4161
4162 if (scmd->cmd_len == 32)
4163 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4164 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4165 if (sas_device_priv_data->sas_target->flags &
4166 MPT_TARGET_FLAGS_RAID_COMPONENT)
4167 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4168 else
4169 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4170 mpi_request->DevHandle = cpu_to_le16(handle);
4171 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4172 mpi_request->Control = cpu_to_le32(mpi_control);
4173 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4174 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4175 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4176 mpi_request->SenseBufferLowAddress =
4177 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4178 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4179 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4180 mpi_request->LUN);
4181 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4182
4183 if (mpi_request->DataLength) {
4184 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4185 mpt3sas_base_free_smid(ioc, smid);
4186 goto out;
4187 }
4188 } else
4189 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4190
4191 raid_device = sas_target_priv_data->raid_device;
4192 if (raid_device && raid_device->direct_io_enabled)
4193 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4194 smid);
4195
4196 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4197 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4198 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4199 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4200 ioc->put_smid_fast_path(ioc, smid, handle);
4201 } else
4202 ioc->put_smid_scsi_io(ioc, smid,
4203 le16_to_cpu(mpi_request->DevHandle));
4204 } else
4205 ioc->put_smid_default(ioc, smid);
4206 return 0;
4207
4208 out:
4209 return SCSI_MLQUEUE_HOST_BUSY;
4210 }
4211
4212 /**
4213 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4214 * @sense_buffer: sense data returned by target
4215 * @data: normalized skey/asc/ascq
4216 *
4217 * Return nothing.
4218 */
4219 static void
4220 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4221 {
4222 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4223 /* descriptor format */
4224 data->skey = sense_buffer[1] & 0x0F;
4225 data->asc = sense_buffer[2];
4226 data->ascq = sense_buffer[3];
4227 } else {
4228 /* fixed format */
4229 data->skey = sense_buffer[2] & 0x0F;
4230 data->asc = sense_buffer[12];
4231 data->ascq = sense_buffer[13];
4232 }
4233 }
4234
4235 /**
4236 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4237 * @ioc: per adapter object
4238 * @scmd: pointer to scsi command object
4239 * @mpi_reply: reply mf payload returned from firmware
4240 *
4241 * scsi_status - SCSI Status code returned from target device
4242 * scsi_state - state info associated with SCSI_IO determined by ioc
4243 * ioc_status - ioc supplied status info
4244 *
4245 * Return nothing.
4246 */
4247 static void
4248 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4249 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4250 {
4251 u32 response_info;
4252 u8 *response_bytes;
4253 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4254 MPI2_IOCSTATUS_MASK;
4255 u8 scsi_state = mpi_reply->SCSIState;
4256 u8 scsi_status = mpi_reply->SCSIStatus;
4257 char *desc_ioc_state = NULL;
4258 char *desc_scsi_status = NULL;
4259 char *desc_scsi_state = ioc->tmp_string;
4260 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4261 struct _sas_device *sas_device = NULL;
4262 struct scsi_target *starget = scmd->device->sdev_target;
4263 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4264 char *device_str = NULL;
4265
4266 if (!priv_target)
4267 return;
4268 if (ioc->hide_ir_msg)
4269 device_str = "WarpDrive";
4270 else
4271 device_str = "volume";
4272
4273 if (log_info == 0x31170000)
4274 return;
4275
4276 switch (ioc_status) {
4277 case MPI2_IOCSTATUS_SUCCESS:
4278 desc_ioc_state = "success";
4279 break;
4280 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4281 desc_ioc_state = "invalid function";
4282 break;
4283 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4284 desc_ioc_state = "scsi recovered error";
4285 break;
4286 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4287 desc_ioc_state = "scsi invalid dev handle";
4288 break;
4289 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4290 desc_ioc_state = "scsi device not there";
4291 break;
4292 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4293 desc_ioc_state = "scsi data overrun";
4294 break;
4295 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4296 desc_ioc_state = "scsi data underrun";
4297 break;
4298 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4299 desc_ioc_state = "scsi io data error";
4300 break;
4301 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4302 desc_ioc_state = "scsi protocol error";
4303 break;
4304 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4305 desc_ioc_state = "scsi task terminated";
4306 break;
4307 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4308 desc_ioc_state = "scsi residual mismatch";
4309 break;
4310 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4311 desc_ioc_state = "scsi task mgmt failed";
4312 break;
4313 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4314 desc_ioc_state = "scsi ioc terminated";
4315 break;
4316 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4317 desc_ioc_state = "scsi ext terminated";
4318 break;
4319 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4320 desc_ioc_state = "eedp guard error";
4321 break;
4322 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4323 desc_ioc_state = "eedp ref tag error";
4324 break;
4325 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4326 desc_ioc_state = "eedp app tag error";
4327 break;
4328 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4329 desc_ioc_state = "insufficient power";
4330 break;
4331 default:
4332 desc_ioc_state = "unknown";
4333 break;
4334 }
4335
4336 switch (scsi_status) {
4337 case MPI2_SCSI_STATUS_GOOD:
4338 desc_scsi_status = "good";
4339 break;
4340 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4341 desc_scsi_status = "check condition";
4342 break;
4343 case MPI2_SCSI_STATUS_CONDITION_MET:
4344 desc_scsi_status = "condition met";
4345 break;
4346 case MPI2_SCSI_STATUS_BUSY:
4347 desc_scsi_status = "busy";
4348 break;
4349 case MPI2_SCSI_STATUS_INTERMEDIATE:
4350 desc_scsi_status = "intermediate";
4351 break;
4352 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4353 desc_scsi_status = "intermediate condmet";
4354 break;
4355 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4356 desc_scsi_status = "reservation conflict";
4357 break;
4358 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4359 desc_scsi_status = "command terminated";
4360 break;
4361 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4362 desc_scsi_status = "task set full";
4363 break;
4364 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4365 desc_scsi_status = "aca active";
4366 break;
4367 case MPI2_SCSI_STATUS_TASK_ABORTED:
4368 desc_scsi_status = "task aborted";
4369 break;
4370 default:
4371 desc_scsi_status = "unknown";
4372 break;
4373 }
4374
4375 desc_scsi_state[0] = '\0';
4376 if (!scsi_state)
4377 desc_scsi_state = " ";
4378 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4379 strcat(desc_scsi_state, "response info ");
4380 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4381 strcat(desc_scsi_state, "state terminated ");
4382 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4383 strcat(desc_scsi_state, "no status ");
4384 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4385 strcat(desc_scsi_state, "autosense failed ");
4386 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4387 strcat(desc_scsi_state, "autosense valid ");
4388
4389 scsi_print_command(scmd);
4390
4391 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4392 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4393 device_str, (unsigned long long)priv_target->sas_address);
4394 } else {
4395 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4396 if (sas_device) {
4397 pr_warn(MPT3SAS_FMT
4398 "\tsas_address(0x%016llx), phy(%d)\n",
4399 ioc->name, (unsigned long long)
4400 sas_device->sas_address, sas_device->phy);
4401 if (sas_device->enclosure_handle != 0)
4402 pr_warn(MPT3SAS_FMT
4403 "\tenclosure_logical_id(0x%016llx),"
4404 "slot(%d)\n", ioc->name,
4405 (unsigned long long)
4406 sas_device->enclosure_logical_id,
4407 sas_device->slot);
4408 if (sas_device->connector_name[0])
4409 pr_warn(MPT3SAS_FMT
4410 "\tenclosure level(0x%04x),"
4411 " connector name( %s)\n", ioc->name,
4412 sas_device->enclosure_level,
4413 sas_device->connector_name);
4414
4415 sas_device_put(sas_device);
4416 }
4417 }
4418
4419 pr_warn(MPT3SAS_FMT
4420 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4421 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4422 desc_ioc_state, ioc_status, smid);
4423 pr_warn(MPT3SAS_FMT
4424 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4425 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4426 scsi_get_resid(scmd));
4427 pr_warn(MPT3SAS_FMT
4428 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4429 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4430 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4431 pr_warn(MPT3SAS_FMT
4432 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4433 ioc->name, desc_scsi_status,
4434 scsi_status, desc_scsi_state, scsi_state);
4435
4436 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4437 struct sense_info data;
4438 _scsih_normalize_sense(scmd->sense_buffer, &data);
4439 pr_warn(MPT3SAS_FMT
4440 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4441 ioc->name, data.skey,
4442 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4443 }
4444
4445 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4446 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4447 response_bytes = (u8 *)&response_info;
4448 _scsih_response_code(ioc, response_bytes[0]);
4449 }
4450 }
4451
4452 /**
4453 * _scsih_turn_on_pfa_led - illuminate PFA LED
4454 * @ioc: per adapter object
4455 * @handle: device handle
4456 * Context: process
4457 *
4458 * Return nothing.
4459 */
4460 static void
4461 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4462 {
4463 Mpi2SepReply_t mpi_reply;
4464 Mpi2SepRequest_t mpi_request;
4465 struct _sas_device *sas_device;
4466
4467 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4468 if (!sas_device)
4469 return;
4470
4471 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4472 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4473 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4474 mpi_request.SlotStatus =
4475 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4476 mpi_request.DevHandle = cpu_to_le16(handle);
4477 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4478 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4479 &mpi_request)) != 0) {
4480 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4481 __FILE__, __LINE__, __func__);
4482 goto out;
4483 }
4484 sas_device->pfa_led_on = 1;
4485
4486 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4487 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4488 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4489 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4490 le32_to_cpu(mpi_reply.IOCLogInfo)));
4491 goto out;
4492 }
4493 out:
4494 sas_device_put(sas_device);
4495 }
4496
4497 /**
4498 * _scsih_turn_off_pfa_led - turn off Fault LED
4499 * @ioc: per adapter object
4500 * @sas_device: sas device whose PFA LED has to turned off
4501 * Context: process
4502 *
4503 * Return nothing.
4504 */
4505 static void
4506 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4507 struct _sas_device *sas_device)
4508 {
4509 Mpi2SepReply_t mpi_reply;
4510 Mpi2SepRequest_t mpi_request;
4511
4512 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4513 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4514 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4515 mpi_request.SlotStatus = 0;
4516 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4517 mpi_request.DevHandle = 0;
4518 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4519 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4520 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4521 &mpi_request)) != 0) {
4522 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4523 __FILE__, __LINE__, __func__);
4524 return;
4525 }
4526
4527 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4528 dewtprintk(ioc, printk(MPT3SAS_FMT
4529 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4530 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4531 le32_to_cpu(mpi_reply.IOCLogInfo)));
4532 return;
4533 }
4534 }
4535
4536 /**
4537 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4538 * @ioc: per adapter object
4539 * @handle: device handle
4540 * Context: interrupt.
4541 *
4542 * Return nothing.
4543 */
4544 static void
4545 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4546 {
4547 struct fw_event_work *fw_event;
4548
4549 fw_event = alloc_fw_event_work(0);
4550 if (!fw_event)
4551 return;
4552 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4553 fw_event->device_handle = handle;
4554 fw_event->ioc = ioc;
4555 _scsih_fw_event_add(ioc, fw_event);
4556 fw_event_work_put(fw_event);
4557 }
4558
4559 /**
4560 * _scsih_smart_predicted_fault - process smart errors
4561 * @ioc: per adapter object
4562 * @handle: device handle
4563 * Context: interrupt.
4564 *
4565 * Return nothing.
4566 */
4567 static void
4568 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4569 {
4570 struct scsi_target *starget;
4571 struct MPT3SAS_TARGET *sas_target_priv_data;
4572 Mpi2EventNotificationReply_t *event_reply;
4573 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4574 struct _sas_device *sas_device;
4575 ssize_t sz;
4576 unsigned long flags;
4577
4578 /* only handle non-raid devices */
4579 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4580 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4581 if (!sas_device)
4582 goto out_unlock;
4583
4584 starget = sas_device->starget;
4585 sas_target_priv_data = starget->hostdata;
4586
4587 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4588 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4589 goto out_unlock;
4590
4591 if (sas_device->enclosure_handle != 0)
4592 starget_printk(KERN_INFO, starget, "predicted fault, "
4593 "enclosure logical id(0x%016llx), slot(%d)\n",
4594 (unsigned long long)sas_device->enclosure_logical_id,
4595 sas_device->slot);
4596 if (sas_device->connector_name[0] != '\0')
4597 starget_printk(KERN_WARNING, starget, "predicted fault, "
4598 "enclosure level(0x%04x), connector name( %s)\n",
4599 sas_device->enclosure_level,
4600 sas_device->connector_name);
4601 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4602
4603 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4604 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4605
4606 /* insert into event log */
4607 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4608 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4609 event_reply = kzalloc(sz, GFP_KERNEL);
4610 if (!event_reply) {
4611 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4612 ioc->name, __FILE__, __LINE__, __func__);
4613 goto out;
4614 }
4615
4616 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4617 event_reply->Event =
4618 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4619 event_reply->MsgLength = sz/4;
4620 event_reply->EventDataLength =
4621 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4622 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4623 event_reply->EventData;
4624 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4625 event_data->ASC = 0x5D;
4626 event_data->DevHandle = cpu_to_le16(handle);
4627 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4628 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4629 kfree(event_reply);
4630 out:
4631 if (sas_device)
4632 sas_device_put(sas_device);
4633 return;
4634
4635 out_unlock:
4636 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4637 goto out;
4638 }
4639
4640 /**
4641 * _scsih_io_done - scsi request callback
4642 * @ioc: per adapter object
4643 * @smid: system request message index
4644 * @msix_index: MSIX table index supplied by the OS
4645 * @reply: reply message frame(lower 32bit addr)
4646 *
4647 * Callback handler when using _scsih_qcmd.
4648 *
4649 * Return 1 meaning mf should be freed from _base_interrupt
4650 * 0 means the mf is freed from this function.
4651 */
4652 static u8
4653 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4654 {
4655 Mpi2SCSIIORequest_t *mpi_request;
4656 Mpi2SCSIIOReply_t *mpi_reply;
4657 struct scsi_cmnd *scmd;
4658 u16 ioc_status;
4659 u32 xfer_cnt;
4660 u8 scsi_state;
4661 u8 scsi_status;
4662 u32 log_info;
4663 struct MPT3SAS_DEVICE *sas_device_priv_data;
4664 u32 response_code = 0;
4665 unsigned long flags;
4666
4667 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4668
4669 if (ioc->broadcast_aen_busy || ioc->pci_error_recovery ||
4670 ioc->got_task_abort_from_ioctl)
4671 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4672 else
4673 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
4674
4675 if (scmd == NULL)
4676 return 1;
4677
4678 if (ata_12_16_cmd(scmd))
4679 scsi_internal_device_unblock(scmd->device, SDEV_RUNNING);
4680
4681 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4682
4683 if (mpi_reply == NULL) {
4684 scmd->result = DID_OK << 16;
4685 goto out;
4686 }
4687
4688 sas_device_priv_data = scmd->device->hostdata;
4689 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4690 sas_device_priv_data->sas_target->deleted) {
4691 scmd->result = DID_NO_CONNECT << 16;
4692 goto out;
4693 }
4694 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4695
4696 /*
4697 * WARPDRIVE: If direct_io is set then it is directIO,
4698 * the failed direct I/O should be redirected to volume
4699 */
4700 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
4701 ((ioc_status & MPI2_IOCSTATUS_MASK)
4702 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4703 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4704 ioc->scsi_lookup[smid - 1].scmd = scmd;
4705 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4706 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
4707 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4708 mpi_request->DevHandle =
4709 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4710 ioc->put_smid_scsi_io(ioc, smid,
4711 sas_device_priv_data->sas_target->handle);
4712 return 0;
4713 }
4714 /* turning off TLR */
4715 scsi_state = mpi_reply->SCSIState;
4716 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4717 response_code =
4718 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4719 if (!sas_device_priv_data->tlr_snoop_check) {
4720 sas_device_priv_data->tlr_snoop_check++;
4721 if (!ioc->is_warpdrive &&
4722 !scsih_is_raid(&scmd->device->sdev_gendev) &&
4723 sas_is_tlr_enabled(scmd->device) &&
4724 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4725 sas_disable_tlr(scmd->device);
4726 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4727 }
4728 }
4729
4730 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4731 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4732 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4733 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4734 else
4735 log_info = 0;
4736 ioc_status &= MPI2_IOCSTATUS_MASK;
4737 scsi_status = mpi_reply->SCSIStatus;
4738
4739 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4740 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4741 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4742 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4743 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4744 }
4745
4746 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4747 struct sense_info data;
4748 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4749 smid);
4750 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4751 le32_to_cpu(mpi_reply->SenseCount));
4752 memcpy(scmd->sense_buffer, sense_data, sz);
4753 _scsih_normalize_sense(scmd->sense_buffer, &data);
4754 /* failure prediction threshold exceeded */
4755 if (data.asc == 0x5D)
4756 _scsih_smart_predicted_fault(ioc,
4757 le16_to_cpu(mpi_reply->DevHandle));
4758 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4759
4760 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
4761 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4762 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4763 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4764 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4765 }
4766 switch (ioc_status) {
4767 case MPI2_IOCSTATUS_BUSY:
4768 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4769 scmd->result = SAM_STAT_BUSY;
4770 break;
4771
4772 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4773 scmd->result = DID_NO_CONNECT << 16;
4774 break;
4775
4776 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4777 if (sas_device_priv_data->block) {
4778 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4779 goto out;
4780 }
4781 if (log_info == 0x31110630) {
4782 if (scmd->retries > 2) {
4783 scmd->result = DID_NO_CONNECT << 16;
4784 scsi_device_set_state(scmd->device,
4785 SDEV_OFFLINE);
4786 } else {
4787 scmd->result = DID_SOFT_ERROR << 16;
4788 scmd->device->expecting_cc_ua = 1;
4789 }
4790 break;
4791 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4792 scmd->result = DID_RESET << 16;
4793 break;
4794 }
4795 scmd->result = DID_SOFT_ERROR << 16;
4796 break;
4797 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4798 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4799 scmd->result = DID_RESET << 16;
4800 break;
4801
4802 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4803 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4804 scmd->result = DID_SOFT_ERROR << 16;
4805 else
4806 scmd->result = (DID_OK << 16) | scsi_status;
4807 break;
4808
4809 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4810 scmd->result = (DID_OK << 16) | scsi_status;
4811
4812 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4813 break;
4814
4815 if (xfer_cnt < scmd->underflow) {
4816 if (scsi_status == SAM_STAT_BUSY)
4817 scmd->result = SAM_STAT_BUSY;
4818 else
4819 scmd->result = DID_SOFT_ERROR << 16;
4820 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4821 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4822 scmd->result = DID_SOFT_ERROR << 16;
4823 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4824 scmd->result = DID_RESET << 16;
4825 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4826 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4827 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4828 scmd->result = (DRIVER_SENSE << 24) |
4829 SAM_STAT_CHECK_CONDITION;
4830 scmd->sense_buffer[0] = 0x70;
4831 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4832 scmd->sense_buffer[12] = 0x20;
4833 scmd->sense_buffer[13] = 0;
4834 }
4835 break;
4836
4837 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4838 scsi_set_resid(scmd, 0);
4839 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4840 case MPI2_IOCSTATUS_SUCCESS:
4841 scmd->result = (DID_OK << 16) | scsi_status;
4842 if (response_code ==
4843 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4844 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4845 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4846 scmd->result = DID_SOFT_ERROR << 16;
4847 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4848 scmd->result = DID_RESET << 16;
4849 break;
4850
4851 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4852 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4853 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4854 _scsih_eedp_error_handling(scmd, ioc_status);
4855 break;
4856
4857 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4858 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4859 case MPI2_IOCSTATUS_INVALID_SGL:
4860 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4861 case MPI2_IOCSTATUS_INVALID_FIELD:
4862 case MPI2_IOCSTATUS_INVALID_STATE:
4863 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4864 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4865 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4866 default:
4867 scmd->result = DID_SOFT_ERROR << 16;
4868 break;
4869
4870 }
4871
4872 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4873 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4874
4875 out:
4876
4877 scsi_dma_unmap(scmd);
4878
4879 scmd->scsi_done(scmd);
4880 return 1;
4881 }
4882
4883 /**
4884 * _scsih_sas_host_refresh - refreshing sas host object contents
4885 * @ioc: per adapter object
4886 * Context: user
4887 *
4888 * During port enable, fw will send topology events for every device. Its
4889 * possible that the handles may change from the previous setting, so this
4890 * code keeping handles updating if changed.
4891 *
4892 * Return nothing.
4893 */
4894 static void
4895 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4896 {
4897 u16 sz;
4898 u16 ioc_status;
4899 int i;
4900 Mpi2ConfigReply_t mpi_reply;
4901 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4902 u16 attached_handle;
4903 u8 link_rate;
4904
4905 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4906 "updating handles for sas_host(0x%016llx)\n",
4907 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4908
4909 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4910 * sizeof(Mpi2SasIOUnit0PhyData_t));
4911 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4912 if (!sas_iounit_pg0) {
4913 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4914 ioc->name, __FILE__, __LINE__, __func__);
4915 return;
4916 }
4917
4918 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4919 sas_iounit_pg0, sz)) != 0)
4920 goto out;
4921 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4922 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4923 goto out;
4924 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4925 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4926 if (i == 0)
4927 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4928 PhyData[0].ControllerDevHandle);
4929 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4930 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4931 AttachedDevHandle);
4932 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4933 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4934 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4935 attached_handle, i, link_rate);
4936 }
4937 out:
4938 kfree(sas_iounit_pg0);
4939 }
4940
4941 /**
4942 * _scsih_sas_host_add - create sas host object
4943 * @ioc: per adapter object
4944 *
4945 * Creating host side data object, stored in ioc->sas_hba
4946 *
4947 * Return nothing.
4948 */
4949 static void
4950 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4951 {
4952 int i;
4953 Mpi2ConfigReply_t mpi_reply;
4954 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4955 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4956 Mpi2SasPhyPage0_t phy_pg0;
4957 Mpi2SasDevicePage0_t sas_device_pg0;
4958 Mpi2SasEnclosurePage0_t enclosure_pg0;
4959 u16 ioc_status;
4960 u16 sz;
4961 u8 device_missing_delay;
4962 u8 num_phys;
4963
4964 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4965 if (!num_phys) {
4966 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4967 ioc->name, __FILE__, __LINE__, __func__);
4968 return;
4969 }
4970 ioc->sas_hba.phy = kcalloc(num_phys,
4971 sizeof(struct _sas_phy), GFP_KERNEL);
4972 if (!ioc->sas_hba.phy) {
4973 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4974 ioc->name, __FILE__, __LINE__, __func__);
4975 goto out;
4976 }
4977 ioc->sas_hba.num_phys = num_phys;
4978
4979 /* sas_iounit page 0 */
4980 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4981 sizeof(Mpi2SasIOUnit0PhyData_t));
4982 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4983 if (!sas_iounit_pg0) {
4984 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4985 ioc->name, __FILE__, __LINE__, __func__);
4986 return;
4987 }
4988 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4989 sas_iounit_pg0, sz))) {
4990 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4991 ioc->name, __FILE__, __LINE__, __func__);
4992 goto out;
4993 }
4994 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4995 MPI2_IOCSTATUS_MASK;
4996 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4997 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4998 ioc->name, __FILE__, __LINE__, __func__);
4999 goto out;
5000 }
5001
5002 /* sas_iounit page 1 */
5003 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
5004 sizeof(Mpi2SasIOUnit1PhyData_t));
5005 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
5006 if (!sas_iounit_pg1) {
5007 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5008 ioc->name, __FILE__, __LINE__, __func__);
5009 goto out;
5010 }
5011 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
5012 sas_iounit_pg1, sz))) {
5013 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5014 ioc->name, __FILE__, __LINE__, __func__);
5015 goto out;
5016 }
5017 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5018 MPI2_IOCSTATUS_MASK;
5019 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5020 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5021 ioc->name, __FILE__, __LINE__, __func__);
5022 goto out;
5023 }
5024
5025 ioc->io_missing_delay =
5026 sas_iounit_pg1->IODeviceMissingDelay;
5027 device_missing_delay =
5028 sas_iounit_pg1->ReportDeviceMissingDelay;
5029 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
5030 ioc->device_missing_delay = (device_missing_delay &
5031 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
5032 else
5033 ioc->device_missing_delay = device_missing_delay &
5034 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
5035
5036 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
5037 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
5038 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
5039 i))) {
5040 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5041 ioc->name, __FILE__, __LINE__, __func__);
5042 goto out;
5043 }
5044 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5045 MPI2_IOCSTATUS_MASK;
5046 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5047 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5048 ioc->name, __FILE__, __LINE__, __func__);
5049 goto out;
5050 }
5051
5052 if (i == 0)
5053 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
5054 PhyData[0].ControllerDevHandle);
5055 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
5056 ioc->sas_hba.phy[i].phy_id = i;
5057 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
5058 phy_pg0, ioc->sas_hba.parent_dev);
5059 }
5060 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5061 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
5062 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5063 ioc->name, __FILE__, __LINE__, __func__);
5064 goto out;
5065 }
5066 ioc->sas_hba.enclosure_handle =
5067 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5068 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5069 pr_info(MPT3SAS_FMT
5070 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5071 ioc->name, ioc->sas_hba.handle,
5072 (unsigned long long) ioc->sas_hba.sas_address,
5073 ioc->sas_hba.num_phys) ;
5074
5075 if (ioc->sas_hba.enclosure_handle) {
5076 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5077 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5078 ioc->sas_hba.enclosure_handle)))
5079 ioc->sas_hba.enclosure_logical_id =
5080 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5081 }
5082
5083 out:
5084 kfree(sas_iounit_pg1);
5085 kfree(sas_iounit_pg0);
5086 }
5087
5088 /**
5089 * _scsih_expander_add - creating expander object
5090 * @ioc: per adapter object
5091 * @handle: expander handle
5092 *
5093 * Creating expander object, stored in ioc->sas_expander_list.
5094 *
5095 * Return 0 for success, else error.
5096 */
5097 static int
5098 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5099 {
5100 struct _sas_node *sas_expander;
5101 Mpi2ConfigReply_t mpi_reply;
5102 Mpi2ExpanderPage0_t expander_pg0;
5103 Mpi2ExpanderPage1_t expander_pg1;
5104 Mpi2SasEnclosurePage0_t enclosure_pg0;
5105 u32 ioc_status;
5106 u16 parent_handle;
5107 u64 sas_address, sas_address_parent = 0;
5108 int i;
5109 unsigned long flags;
5110 struct _sas_port *mpt3sas_port = NULL;
5111
5112 int rc = 0;
5113
5114 if (!handle)
5115 return -1;
5116
5117 if (ioc->shost_recovery || ioc->pci_error_recovery)
5118 return -1;
5119
5120 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5121 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5122 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5123 ioc->name, __FILE__, __LINE__, __func__);
5124 return -1;
5125 }
5126
5127 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5128 MPI2_IOCSTATUS_MASK;
5129 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5130 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5131 ioc->name, __FILE__, __LINE__, __func__);
5132 return -1;
5133 }
5134
5135 /* handle out of order topology events */
5136 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5137 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5138 != 0) {
5139 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5140 ioc->name, __FILE__, __LINE__, __func__);
5141 return -1;
5142 }
5143 if (sas_address_parent != ioc->sas_hba.sas_address) {
5144 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5145 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5146 sas_address_parent);
5147 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5148 if (!sas_expander) {
5149 rc = _scsih_expander_add(ioc, parent_handle);
5150 if (rc != 0)
5151 return rc;
5152 }
5153 }
5154
5155 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5156 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5157 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5158 sas_address);
5159 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5160
5161 if (sas_expander)
5162 return 0;
5163
5164 sas_expander = kzalloc(sizeof(struct _sas_node),
5165 GFP_KERNEL);
5166 if (!sas_expander) {
5167 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5168 ioc->name, __FILE__, __LINE__, __func__);
5169 return -1;
5170 }
5171
5172 sas_expander->handle = handle;
5173 sas_expander->num_phys = expander_pg0.NumPhys;
5174 sas_expander->sas_address_parent = sas_address_parent;
5175 sas_expander->sas_address = sas_address;
5176
5177 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5178 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5179 handle, parent_handle, (unsigned long long)
5180 sas_expander->sas_address, sas_expander->num_phys);
5181
5182 if (!sas_expander->num_phys)
5183 goto out_fail;
5184 sas_expander->phy = kcalloc(sas_expander->num_phys,
5185 sizeof(struct _sas_phy), GFP_KERNEL);
5186 if (!sas_expander->phy) {
5187 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5188 ioc->name, __FILE__, __LINE__, __func__);
5189 rc = -1;
5190 goto out_fail;
5191 }
5192
5193 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5194 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5195 sas_address_parent);
5196 if (!mpt3sas_port) {
5197 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5198 ioc->name, __FILE__, __LINE__, __func__);
5199 rc = -1;
5200 goto out_fail;
5201 }
5202 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5203
5204 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5205 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5206 &expander_pg1, i, handle))) {
5207 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5208 ioc->name, __FILE__, __LINE__, __func__);
5209 rc = -1;
5210 goto out_fail;
5211 }
5212 sas_expander->phy[i].handle = handle;
5213 sas_expander->phy[i].phy_id = i;
5214
5215 if ((mpt3sas_transport_add_expander_phy(ioc,
5216 &sas_expander->phy[i], expander_pg1,
5217 sas_expander->parent_dev))) {
5218 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5219 ioc->name, __FILE__, __LINE__, __func__);
5220 rc = -1;
5221 goto out_fail;
5222 }
5223 }
5224
5225 if (sas_expander->enclosure_handle) {
5226 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5227 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5228 sas_expander->enclosure_handle)))
5229 sas_expander->enclosure_logical_id =
5230 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5231 }
5232
5233 _scsih_expander_node_add(ioc, sas_expander);
5234 return 0;
5235
5236 out_fail:
5237
5238 if (mpt3sas_port)
5239 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5240 sas_address_parent);
5241 kfree(sas_expander);
5242 return rc;
5243 }
5244
5245 /**
5246 * mpt3sas_expander_remove - removing expander object
5247 * @ioc: per adapter object
5248 * @sas_address: expander sas_address
5249 *
5250 * Return nothing.
5251 */
5252 void
5253 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5254 {
5255 struct _sas_node *sas_expander;
5256 unsigned long flags;
5257
5258 if (ioc->shost_recovery)
5259 return;
5260
5261 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5262 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5263 sas_address);
5264 if (sas_expander)
5265 list_del(&sas_expander->list);
5266 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5267 if (sas_expander)
5268 _scsih_expander_node_remove(ioc, sas_expander);
5269 }
5270
5271 /**
5272 * _scsih_done - internal SCSI_IO callback handler.
5273 * @ioc: per adapter object
5274 * @smid: system request message index
5275 * @msix_index: MSIX table index supplied by the OS
5276 * @reply: reply message frame(lower 32bit addr)
5277 *
5278 * Callback handler when sending internal generated SCSI_IO.
5279 * The callback index passed is `ioc->scsih_cb_idx`
5280 *
5281 * Return 1 meaning mf should be freed from _base_interrupt
5282 * 0 means the mf is freed from this function.
5283 */
5284 static u8
5285 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5286 {
5287 MPI2DefaultReply_t *mpi_reply;
5288
5289 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5290 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5291 return 1;
5292 if (ioc->scsih_cmds.smid != smid)
5293 return 1;
5294 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5295 if (mpi_reply) {
5296 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5297 mpi_reply->MsgLength*4);
5298 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5299 }
5300 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5301 complete(&ioc->scsih_cmds.done);
5302 return 1;
5303 }
5304
5305
5306
5307
5308 #define MPT3_MAX_LUNS (255)
5309
5310
5311 /**
5312 * _scsih_check_access_status - check access flags
5313 * @ioc: per adapter object
5314 * @sas_address: sas address
5315 * @handle: sas device handle
5316 * @access_flags: errors returned during discovery of the device
5317 *
5318 * Return 0 for success, else failure
5319 */
5320 static u8
5321 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5322 u16 handle, u8 access_status)
5323 {
5324 u8 rc = 1;
5325 char *desc = NULL;
5326
5327 switch (access_status) {
5328 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5329 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5330 rc = 0;
5331 break;
5332 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5333 desc = "sata capability failed";
5334 break;
5335 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5336 desc = "sata affiliation conflict";
5337 break;
5338 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5339 desc = "route not addressable";
5340 break;
5341 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5342 desc = "smp error not addressable";
5343 break;
5344 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5345 desc = "device blocked";
5346 break;
5347 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5348 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5349 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5350 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5351 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5352 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5353 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5354 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5355 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5356 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5357 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5358 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5359 desc = "sata initialization failed";
5360 break;
5361 default:
5362 desc = "unknown";
5363 break;
5364 }
5365
5366 if (!rc)
5367 return 0;
5368
5369 pr_err(MPT3SAS_FMT
5370 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5371 ioc->name, desc, (unsigned long long)sas_address, handle);
5372 return rc;
5373 }
5374
5375 /**
5376 * _scsih_check_device - checking device responsiveness
5377 * @ioc: per adapter object
5378 * @parent_sas_address: sas address of parent expander or sas host
5379 * @handle: attached device handle
5380 * @phy_numberv: phy number
5381 * @link_rate: new link rate
5382 *
5383 * Returns nothing.
5384 */
5385 static void
5386 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5387 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5388 {
5389 Mpi2ConfigReply_t mpi_reply;
5390 Mpi2SasDevicePage0_t sas_device_pg0;
5391 struct _sas_device *sas_device;
5392 u32 ioc_status;
5393 unsigned long flags;
5394 u64 sas_address;
5395 struct scsi_target *starget;
5396 struct MPT3SAS_TARGET *sas_target_priv_data;
5397 u32 device_info;
5398
5399
5400 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5401 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5402 return;
5403
5404 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5405 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5406 return;
5407
5408 /* wide port handling ~ we need only handle device once for the phy that
5409 * is matched in sas device page zero
5410 */
5411 if (phy_number != sas_device_pg0.PhyNum)
5412 return;
5413
5414 /* check if this is end device */
5415 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5416 if (!(_scsih_is_end_device(device_info)))
5417 return;
5418
5419 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5420 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5421 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5422 sas_address);
5423
5424 if (!sas_device)
5425 goto out_unlock;
5426
5427 if (unlikely(sas_device->handle != handle)) {
5428 starget = sas_device->starget;
5429 sas_target_priv_data = starget->hostdata;
5430 starget_printk(KERN_INFO, starget,
5431 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5432 sas_device->handle, handle);
5433 sas_target_priv_data->handle = handle;
5434 sas_device->handle = handle;
5435 if (le16_to_cpu(sas_device_pg0.Flags) &
5436 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5437 sas_device->enclosure_level =
5438 sas_device_pg0.EnclosureLevel;
5439 memcpy(sas_device->connector_name,
5440 sas_device_pg0.ConnectorName, 4);
5441 sas_device->connector_name[4] = '\0';
5442 } else {
5443 sas_device->enclosure_level = 0;
5444 sas_device->connector_name[0] = '\0';
5445 }
5446 }
5447
5448 /* check if device is present */
5449 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5450 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5451 pr_err(MPT3SAS_FMT
5452 "device is not present handle(0x%04x), flags!!!\n",
5453 ioc->name, handle);
5454 goto out_unlock;
5455 }
5456
5457 /* check if there were any issues with discovery */
5458 if (_scsih_check_access_status(ioc, sas_address, handle,
5459 sas_device_pg0.AccessStatus))
5460 goto out_unlock;
5461
5462 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5463 _scsih_ublock_io_device(ioc, sas_address);
5464
5465 if (sas_device)
5466 sas_device_put(sas_device);
5467 return;
5468
5469 out_unlock:
5470 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5471 if (sas_device)
5472 sas_device_put(sas_device);
5473 }
5474
5475 /**
5476 * _scsih_add_device - creating sas device object
5477 * @ioc: per adapter object
5478 * @handle: sas device handle
5479 * @phy_num: phy number end device attached to
5480 * @is_pd: is this hidden raid component
5481 *
5482 * Creating end device object, stored in ioc->sas_device_list.
5483 *
5484 * Returns 0 for success, non-zero for failure.
5485 */
5486 static int
5487 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5488 u8 is_pd)
5489 {
5490 Mpi2ConfigReply_t mpi_reply;
5491 Mpi2SasDevicePage0_t sas_device_pg0;
5492 Mpi2SasEnclosurePage0_t enclosure_pg0;
5493 struct _sas_device *sas_device;
5494 u32 ioc_status;
5495 u64 sas_address;
5496 u32 device_info;
5497
5498 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5499 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5500 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5501 ioc->name, __FILE__, __LINE__, __func__);
5502 return -1;
5503 }
5504
5505 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5506 MPI2_IOCSTATUS_MASK;
5507 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5508 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5509 ioc->name, __FILE__, __LINE__, __func__);
5510 return -1;
5511 }
5512
5513 /* check if this is end device */
5514 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5515 if (!(_scsih_is_end_device(device_info)))
5516 return -1;
5517 set_bit(handle, ioc->pend_os_device_add);
5518 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5519
5520 /* check if device is present */
5521 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5522 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5523 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5524 ioc->name, handle);
5525 return -1;
5526 }
5527
5528 /* check if there were any issues with discovery */
5529 if (_scsih_check_access_status(ioc, sas_address, handle,
5530 sas_device_pg0.AccessStatus))
5531 return -1;
5532
5533 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5534 sas_address);
5535 if (sas_device) {
5536 clear_bit(handle, ioc->pend_os_device_add);
5537 sas_device_put(sas_device);
5538 return -1;
5539 }
5540
5541 sas_device = kzalloc(sizeof(struct _sas_device),
5542 GFP_KERNEL);
5543 if (!sas_device) {
5544 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5545 ioc->name, __FILE__, __LINE__, __func__);
5546 return 0;
5547 }
5548
5549 kref_init(&sas_device->refcount);
5550 sas_device->handle = handle;
5551 if (_scsih_get_sas_address(ioc,
5552 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5553 &sas_device->sas_address_parent) != 0)
5554 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5555 ioc->name, __FILE__, __LINE__, __func__);
5556 sas_device->enclosure_handle =
5557 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5558 if (sas_device->enclosure_handle != 0)
5559 sas_device->slot =
5560 le16_to_cpu(sas_device_pg0.Slot);
5561 sas_device->device_info = device_info;
5562 sas_device->sas_address = sas_address;
5563 sas_device->phy = sas_device_pg0.PhyNum;
5564 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5565 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5566
5567 if (le16_to_cpu(sas_device_pg0.Flags)
5568 & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5569 sas_device->enclosure_level =
5570 sas_device_pg0.EnclosureLevel;
5571 memcpy(sas_device->connector_name,
5572 sas_device_pg0.ConnectorName, 4);
5573 sas_device->connector_name[4] = '\0';
5574 } else {
5575 sas_device->enclosure_level = 0;
5576 sas_device->connector_name[0] = '\0';
5577 }
5578 /* get enclosure_logical_id */
5579 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5580 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5581 sas_device->enclosure_handle)))
5582 sas_device->enclosure_logical_id =
5583 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5584
5585 /* get device name */
5586 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5587
5588 if (ioc->wait_for_discovery_to_complete)
5589 _scsih_sas_device_init_add(ioc, sas_device);
5590 else
5591 _scsih_sas_device_add(ioc, sas_device);
5592
5593 sas_device_put(sas_device);
5594 return 0;
5595 }
5596
5597 /**
5598 * _scsih_remove_device - removing sas device object
5599 * @ioc: per adapter object
5600 * @sas_device_delete: the sas_device object
5601 *
5602 * Return nothing.
5603 */
5604 static void
5605 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5606 struct _sas_device *sas_device)
5607 {
5608 struct MPT3SAS_TARGET *sas_target_priv_data;
5609
5610 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5611 (sas_device->pfa_led_on)) {
5612 _scsih_turn_off_pfa_led(ioc, sas_device);
5613 sas_device->pfa_led_on = 0;
5614 }
5615 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5616 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5617 ioc->name, __func__,
5618 sas_device->handle, (unsigned long long)
5619 sas_device->sas_address));
5620 if (sas_device->enclosure_handle != 0)
5621 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5622 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5623 ioc->name, __func__,
5624 (unsigned long long)sas_device->enclosure_logical_id,
5625 sas_device->slot));
5626 if (sas_device->connector_name[0] != '\0')
5627 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5628 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5629 ioc->name, __func__,
5630 sas_device->enclosure_level,
5631 sas_device->connector_name));
5632
5633 if (sas_device->starget && sas_device->starget->hostdata) {
5634 sas_target_priv_data = sas_device->starget->hostdata;
5635 sas_target_priv_data->deleted = 1;
5636 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5637 sas_target_priv_data->handle =
5638 MPT3SAS_INVALID_DEVICE_HANDLE;
5639 }
5640
5641 if (!ioc->hide_drives)
5642 mpt3sas_transport_port_remove(ioc,
5643 sas_device->sas_address,
5644 sas_device->sas_address_parent);
5645
5646 pr_info(MPT3SAS_FMT
5647 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5648 ioc->name, sas_device->handle,
5649 (unsigned long long) sas_device->sas_address);
5650 if (sas_device->enclosure_handle != 0)
5651 pr_info(MPT3SAS_FMT
5652 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5653 ioc->name,
5654 (unsigned long long)sas_device->enclosure_logical_id,
5655 sas_device->slot);
5656 if (sas_device->connector_name[0] != '\0')
5657 pr_info(MPT3SAS_FMT
5658 "removing enclosure level(0x%04x), connector name( %s)\n",
5659 ioc->name, sas_device->enclosure_level,
5660 sas_device->connector_name);
5661
5662 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5663 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5664 ioc->name, __func__,
5665 sas_device->handle, (unsigned long long)
5666 sas_device->sas_address));
5667 if (sas_device->enclosure_handle != 0)
5668 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5669 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5670 ioc->name, __func__,
5671 (unsigned long long)sas_device->enclosure_logical_id,
5672 sas_device->slot));
5673 if (sas_device->connector_name[0] != '\0')
5674 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5675 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5676 ioc->name, __func__, sas_device->enclosure_level,
5677 sas_device->connector_name));
5678 }
5679
5680 /**
5681 * _scsih_sas_topology_change_event_debug - debug for topology event
5682 * @ioc: per adapter object
5683 * @event_data: event data payload
5684 * Context: user.
5685 */
5686 static void
5687 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5688 Mpi2EventDataSasTopologyChangeList_t *event_data)
5689 {
5690 int i;
5691 u16 handle;
5692 u16 reason_code;
5693 u8 phy_number;
5694 char *status_str = NULL;
5695 u8 link_rate, prev_link_rate;
5696
5697 switch (event_data->ExpStatus) {
5698 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5699 status_str = "add";
5700 break;
5701 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5702 status_str = "remove";
5703 break;
5704 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5705 case 0:
5706 status_str = "responding";
5707 break;
5708 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5709 status_str = "remove delay";
5710 break;
5711 default:
5712 status_str = "unknown status";
5713 break;
5714 }
5715 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5716 ioc->name, status_str);
5717 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5718 "start_phy(%02d), count(%d)\n",
5719 le16_to_cpu(event_data->ExpanderDevHandle),
5720 le16_to_cpu(event_data->EnclosureHandle),
5721 event_data->StartPhyNum, event_data->NumEntries);
5722 for (i = 0; i < event_data->NumEntries; i++) {
5723 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5724 if (!handle)
5725 continue;
5726 phy_number = event_data->StartPhyNum + i;
5727 reason_code = event_data->PHY[i].PhyStatus &
5728 MPI2_EVENT_SAS_TOPO_RC_MASK;
5729 switch (reason_code) {
5730 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5731 status_str = "target add";
5732 break;
5733 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5734 status_str = "target remove";
5735 break;
5736 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5737 status_str = "delay target remove";
5738 break;
5739 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5740 status_str = "link rate change";
5741 break;
5742 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5743 status_str = "target responding";
5744 break;
5745 default:
5746 status_str = "unknown";
5747 break;
5748 }
5749 link_rate = event_data->PHY[i].LinkRate >> 4;
5750 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5751 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5752 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5753 handle, status_str, link_rate, prev_link_rate);
5754
5755 }
5756 }
5757
5758 /**
5759 * _scsih_sas_topology_change_event - handle topology changes
5760 * @ioc: per adapter object
5761 * @fw_event: The fw_event_work object
5762 * Context: user.
5763 *
5764 */
5765 static int
5766 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5767 struct fw_event_work *fw_event)
5768 {
5769 int i;
5770 u16 parent_handle, handle;
5771 u16 reason_code;
5772 u8 phy_number, max_phys;
5773 struct _sas_node *sas_expander;
5774 u64 sas_address;
5775 unsigned long flags;
5776 u8 link_rate, prev_link_rate;
5777 Mpi2EventDataSasTopologyChangeList_t *event_data =
5778 (Mpi2EventDataSasTopologyChangeList_t *)
5779 fw_event->event_data;
5780
5781 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5782 _scsih_sas_topology_change_event_debug(ioc, event_data);
5783
5784 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5785 return 0;
5786
5787 if (!ioc->sas_hba.num_phys)
5788 _scsih_sas_host_add(ioc);
5789 else
5790 _scsih_sas_host_refresh(ioc);
5791
5792 if (fw_event->ignore) {
5793 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5794 "ignoring expander event\n", ioc->name));
5795 return 0;
5796 }
5797
5798 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5799
5800 /* handle expander add */
5801 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5802 if (_scsih_expander_add(ioc, parent_handle) != 0)
5803 return 0;
5804
5805 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5806 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5807 parent_handle);
5808 if (sas_expander) {
5809 sas_address = sas_expander->sas_address;
5810 max_phys = sas_expander->num_phys;
5811 } else if (parent_handle < ioc->sas_hba.num_phys) {
5812 sas_address = ioc->sas_hba.sas_address;
5813 max_phys = ioc->sas_hba.num_phys;
5814 } else {
5815 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5816 return 0;
5817 }
5818 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5819
5820 /* handle siblings events */
5821 for (i = 0; i < event_data->NumEntries; i++) {
5822 if (fw_event->ignore) {
5823 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5824 "ignoring expander event\n", ioc->name));
5825 return 0;
5826 }
5827 if (ioc->remove_host || ioc->pci_error_recovery)
5828 return 0;
5829 phy_number = event_data->StartPhyNum + i;
5830 if (phy_number >= max_phys)
5831 continue;
5832 reason_code = event_data->PHY[i].PhyStatus &
5833 MPI2_EVENT_SAS_TOPO_RC_MASK;
5834 if ((event_data->PHY[i].PhyStatus &
5835 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5836 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5837 continue;
5838 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5839 if (!handle)
5840 continue;
5841 link_rate = event_data->PHY[i].LinkRate >> 4;
5842 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5843 switch (reason_code) {
5844 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5845
5846 if (ioc->shost_recovery)
5847 break;
5848
5849 if (link_rate == prev_link_rate)
5850 break;
5851
5852 mpt3sas_transport_update_links(ioc, sas_address,
5853 handle, phy_number, link_rate);
5854
5855 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5856 break;
5857
5858 _scsih_check_device(ioc, sas_address, handle,
5859 phy_number, link_rate);
5860
5861 if (!test_bit(handle, ioc->pend_os_device_add))
5862 break;
5863
5864
5865 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5866
5867 if (ioc->shost_recovery)
5868 break;
5869
5870 mpt3sas_transport_update_links(ioc, sas_address,
5871 handle, phy_number, link_rate);
5872
5873 _scsih_add_device(ioc, handle, phy_number, 0);
5874
5875 break;
5876 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5877
5878 _scsih_device_remove_by_handle(ioc, handle);
5879 break;
5880 }
5881 }
5882
5883 /* handle expander removal */
5884 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5885 sas_expander)
5886 mpt3sas_expander_remove(ioc, sas_address);
5887
5888 return 0;
5889 }
5890
5891 /**
5892 * _scsih_sas_device_status_change_event_debug - debug for device event
5893 * @event_data: event data payload
5894 * Context: user.
5895 *
5896 * Return nothing.
5897 */
5898 static void
5899 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5900 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5901 {
5902 char *reason_str = NULL;
5903
5904 switch (event_data->ReasonCode) {
5905 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5906 reason_str = "smart data";
5907 break;
5908 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5909 reason_str = "unsupported device discovered";
5910 break;
5911 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5912 reason_str = "internal device reset";
5913 break;
5914 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5915 reason_str = "internal task abort";
5916 break;
5917 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5918 reason_str = "internal task abort set";
5919 break;
5920 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5921 reason_str = "internal clear task set";
5922 break;
5923 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5924 reason_str = "internal query task";
5925 break;
5926 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5927 reason_str = "sata init failure";
5928 break;
5929 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5930 reason_str = "internal device reset complete";
5931 break;
5932 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5933 reason_str = "internal task abort complete";
5934 break;
5935 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5936 reason_str = "internal async notification";
5937 break;
5938 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5939 reason_str = "expander reduced functionality";
5940 break;
5941 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5942 reason_str = "expander reduced functionality complete";
5943 break;
5944 default:
5945 reason_str = "unknown reason";
5946 break;
5947 }
5948 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5949 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5950 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5951 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5952 le16_to_cpu(event_data->TaskTag));
5953 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5954 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5955 event_data->ASC, event_data->ASCQ);
5956 pr_info("\n");
5957 }
5958
5959 /**
5960 * _scsih_sas_device_status_change_event - handle device status change
5961 * @ioc: per adapter object
5962 * @fw_event: The fw_event_work object
5963 * Context: user.
5964 *
5965 * Return nothing.
5966 */
5967 static void
5968 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5969 struct fw_event_work *fw_event)
5970 {
5971 struct MPT3SAS_TARGET *target_priv_data;
5972 struct _sas_device *sas_device;
5973 u64 sas_address;
5974 unsigned long flags;
5975 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5976 (Mpi2EventDataSasDeviceStatusChange_t *)
5977 fw_event->event_data;
5978
5979 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5980 _scsih_sas_device_status_change_event_debug(ioc,
5981 event_data);
5982
5983 /* In MPI Revision K (0xC), the internal device reset complete was
5984 * implemented, so avoid setting tm_busy flag for older firmware.
5985 */
5986 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5987 return;
5988
5989 if (event_data->ReasonCode !=
5990 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5991 event_data->ReasonCode !=
5992 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5993 return;
5994
5995 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5996 sas_address = le64_to_cpu(event_data->SASAddress);
5997 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5998 sas_address);
5999
6000 if (!sas_device || !sas_device->starget)
6001 goto out;
6002
6003 target_priv_data = sas_device->starget->hostdata;
6004 if (!target_priv_data)
6005 goto out;
6006
6007 if (event_data->ReasonCode ==
6008 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
6009 target_priv_data->tm_busy = 1;
6010 else
6011 target_priv_data->tm_busy = 0;
6012
6013 out:
6014 if (sas_device)
6015 sas_device_put(sas_device);
6016
6017 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6018
6019 }
6020
6021 /**
6022 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
6023 * event
6024 * @ioc: per adapter object
6025 * @event_data: event data payload
6026 * Context: user.
6027 *
6028 * Return nothing.
6029 */
6030 static void
6031 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6032 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
6033 {
6034 char *reason_str = NULL;
6035
6036 switch (event_data->ReasonCode) {
6037 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
6038 reason_str = "enclosure add";
6039 break;
6040 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
6041 reason_str = "enclosure remove";
6042 break;
6043 default:
6044 reason_str = "unknown reason";
6045 break;
6046 }
6047
6048 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
6049 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
6050 " number slots(%d)\n", ioc->name, reason_str,
6051 le16_to_cpu(event_data->EnclosureHandle),
6052 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
6053 le16_to_cpu(event_data->StartSlot));
6054 }
6055
6056 /**
6057 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
6058 * @ioc: per adapter object
6059 * @fw_event: The fw_event_work object
6060 * Context: user.
6061 *
6062 * Return nothing.
6063 */
6064 static void
6065 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
6066 struct fw_event_work *fw_event)
6067 {
6068 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6069 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
6070 (Mpi2EventDataSasEnclDevStatusChange_t *)
6071 fw_event->event_data);
6072 }
6073
6074 /**
6075 * _scsih_sas_broadcast_primitive_event - handle broadcast events
6076 * @ioc: per adapter object
6077 * @fw_event: The fw_event_work object
6078 * Context: user.
6079 *
6080 * Return nothing.
6081 */
6082 static void
6083 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6084 struct fw_event_work *fw_event)
6085 {
6086 struct scsi_cmnd *scmd;
6087 struct scsi_device *sdev;
6088 u16 smid, handle;
6089 u32 lun;
6090 struct MPT3SAS_DEVICE *sas_device_priv_data;
6091 u32 termination_count;
6092 u32 query_count;
6093 Mpi2SCSITaskManagementReply_t *mpi_reply;
6094 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6095 (Mpi2EventDataSasBroadcastPrimitive_t *)
6096 fw_event->event_data;
6097 u16 ioc_status;
6098 unsigned long flags;
6099 int r;
6100 u8 max_retries = 0;
6101 u8 task_abort_retries;
6102
6103 mutex_lock(&ioc->tm_cmds.mutex);
6104 pr_info(MPT3SAS_FMT
6105 "%s: enter: phy number(%d), width(%d)\n",
6106 ioc->name, __func__, event_data->PhyNum,
6107 event_data->PortWidth);
6108
6109 _scsih_block_io_all_device(ioc);
6110
6111 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6112 mpi_reply = ioc->tm_cmds.reply;
6113 broadcast_aen_retry:
6114
6115 /* sanity checks for retrying this loop */
6116 if (max_retries++ == 5) {
6117 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6118 ioc->name, __func__));
6119 goto out;
6120 } else if (max_retries > 1)
6121 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6122 ioc->name, __func__, max_retries - 1));
6123
6124 termination_count = 0;
6125 query_count = 0;
6126 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6127 if (ioc->shost_recovery)
6128 goto out;
6129 scmd = _scsih_scsi_lookup_get(ioc, smid);
6130 if (!scmd)
6131 continue;
6132 sdev = scmd->device;
6133 sas_device_priv_data = sdev->hostdata;
6134 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6135 continue;
6136 /* skip hidden raid components */
6137 if (sas_device_priv_data->sas_target->flags &
6138 MPT_TARGET_FLAGS_RAID_COMPONENT)
6139 continue;
6140 /* skip volumes */
6141 if (sas_device_priv_data->sas_target->flags &
6142 MPT_TARGET_FLAGS_VOLUME)
6143 continue;
6144
6145 handle = sas_device_priv_data->sas_target->handle;
6146 lun = sas_device_priv_data->lun;
6147 query_count++;
6148
6149 if (ioc->shost_recovery)
6150 goto out;
6151
6152 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6153 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
6154 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
6155 if (r == FAILED) {
6156 sdev_printk(KERN_WARNING, sdev,
6157 "mpt3sas_scsih_issue_tm: FAILED when sending "
6158 "QUERY_TASK: scmd(%p)\n", scmd);
6159 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6160 goto broadcast_aen_retry;
6161 }
6162 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6163 & MPI2_IOCSTATUS_MASK;
6164 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6165 sdev_printk(KERN_WARNING, sdev,
6166 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6167 ioc_status, scmd);
6168 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6169 goto broadcast_aen_retry;
6170 }
6171
6172 /* see if IO is still owned by IOC and target */
6173 if (mpi_reply->ResponseCode ==
6174 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6175 mpi_reply->ResponseCode ==
6176 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6177 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6178 continue;
6179 }
6180 task_abort_retries = 0;
6181 tm_retry:
6182 if (task_abort_retries++ == 60) {
6183 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6184 "%s: ABORT_TASK: giving up\n", ioc->name,
6185 __func__));
6186 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6187 goto broadcast_aen_retry;
6188 }
6189
6190 if (ioc->shost_recovery)
6191 goto out_no_lock;
6192
6193 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
6194 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
6195 30);
6196 if (r == FAILED) {
6197 sdev_printk(KERN_WARNING, sdev,
6198 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6199 "scmd(%p)\n", scmd);
6200 goto tm_retry;
6201 }
6202
6203 if (task_abort_retries > 1)
6204 sdev_printk(KERN_WARNING, sdev,
6205 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6206 " scmd(%p)\n",
6207 task_abort_retries - 1, scmd);
6208
6209 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6210 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6211 }
6212
6213 if (ioc->broadcast_aen_pending) {
6214 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6215 "%s: loop back due to pending AEN\n",
6216 ioc->name, __func__));
6217 ioc->broadcast_aen_pending = 0;
6218 goto broadcast_aen_retry;
6219 }
6220
6221 out:
6222 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6223 out_no_lock:
6224
6225 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6226 "%s - exit, query_count = %d termination_count = %d\n",
6227 ioc->name, __func__, query_count, termination_count));
6228
6229 ioc->broadcast_aen_busy = 0;
6230 if (!ioc->shost_recovery)
6231 _scsih_ublock_io_all_device(ioc);
6232 mutex_unlock(&ioc->tm_cmds.mutex);
6233 }
6234
6235 /**
6236 * _scsih_sas_discovery_event - handle discovery events
6237 * @ioc: per adapter object
6238 * @fw_event: The fw_event_work object
6239 * Context: user.
6240 *
6241 * Return nothing.
6242 */
6243 static void
6244 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6245 struct fw_event_work *fw_event)
6246 {
6247 Mpi2EventDataSasDiscovery_t *event_data =
6248 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
6249
6250 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6251 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6252 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6253 "start" : "stop");
6254 if (event_data->DiscoveryStatus)
6255 pr_info("discovery_status(0x%08x)",
6256 le32_to_cpu(event_data->DiscoveryStatus));
6257 pr_info("\n");
6258 }
6259
6260 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6261 !ioc->sas_hba.num_phys) {
6262 if (disable_discovery > 0 && ioc->shost_recovery) {
6263 /* Wait for the reset to complete */
6264 while (ioc->shost_recovery)
6265 ssleep(1);
6266 }
6267 _scsih_sas_host_add(ioc);
6268 }
6269 }
6270
6271 /**
6272 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6273 * @ioc: per adapter object
6274 * @handle: device handle for physical disk
6275 * @phys_disk_num: physical disk number
6276 *
6277 * Return 0 for success, else failure.
6278 */
6279 static int
6280 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6281 {
6282 Mpi2RaidActionRequest_t *mpi_request;
6283 Mpi2RaidActionReply_t *mpi_reply;
6284 u16 smid;
6285 u8 issue_reset = 0;
6286 int rc = 0;
6287 u16 ioc_status;
6288 u32 log_info;
6289
6290 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6291 return rc;
6292
6293 mutex_lock(&ioc->scsih_cmds.mutex);
6294
6295 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6296 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6297 ioc->name, __func__);
6298 rc = -EAGAIN;
6299 goto out;
6300 }
6301 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6302
6303 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6304 if (!smid) {
6305 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6306 ioc->name, __func__);
6307 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6308 rc = -EAGAIN;
6309 goto out;
6310 }
6311
6312 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6313 ioc->scsih_cmds.smid = smid;
6314 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6315
6316 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6317 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6318 mpi_request->PhysDiskNum = phys_disk_num;
6319
6320 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6321 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6322 handle, phys_disk_num));
6323
6324 init_completion(&ioc->scsih_cmds.done);
6325 ioc->put_smid_default(ioc, smid);
6326 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6327
6328 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6329 pr_err(MPT3SAS_FMT "%s: timeout\n",
6330 ioc->name, __func__);
6331 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6332 issue_reset = 1;
6333 rc = -EFAULT;
6334 goto out;
6335 }
6336
6337 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6338
6339 mpi_reply = ioc->scsih_cmds.reply;
6340 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6341 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6342 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6343 else
6344 log_info = 0;
6345 ioc_status &= MPI2_IOCSTATUS_MASK;
6346 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6347 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6348 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6349 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6350 log_info));
6351 rc = -EFAULT;
6352 } else
6353 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6354 "IR RAID_ACTION: completed successfully\n",
6355 ioc->name));
6356 }
6357
6358 out:
6359 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6360 mutex_unlock(&ioc->scsih_cmds.mutex);
6361
6362 if (issue_reset)
6363 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
6364 return rc;
6365 }
6366
6367 /**
6368 * _scsih_reprobe_lun - reprobing lun
6369 * @sdev: scsi device struct
6370 * @no_uld_attach: sdev->no_uld_attach flag setting
6371 *
6372 **/
6373 static void
6374 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6375 {
6376 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6377 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6378 sdev->no_uld_attach ? "hiding" : "exposing");
6379 WARN_ON(scsi_device_reprobe(sdev));
6380 }
6381
6382 /**
6383 * _scsih_sas_volume_add - add new volume
6384 * @ioc: per adapter object
6385 * @element: IR config element data
6386 * Context: user.
6387 *
6388 * Return nothing.
6389 */
6390 static void
6391 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6392 Mpi2EventIrConfigElement_t *element)
6393 {
6394 struct _raid_device *raid_device;
6395 unsigned long flags;
6396 u64 wwid;
6397 u16 handle = le16_to_cpu(element->VolDevHandle);
6398 int rc;
6399
6400 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6401 if (!wwid) {
6402 pr_err(MPT3SAS_FMT
6403 "failure at %s:%d/%s()!\n", ioc->name,
6404 __FILE__, __LINE__, __func__);
6405 return;
6406 }
6407
6408 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6409 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6410 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6411
6412 if (raid_device)
6413 return;
6414
6415 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6416 if (!raid_device) {
6417 pr_err(MPT3SAS_FMT
6418 "failure at %s:%d/%s()!\n", ioc->name,
6419 __FILE__, __LINE__, __func__);
6420 return;
6421 }
6422
6423 raid_device->id = ioc->sas_id++;
6424 raid_device->channel = RAID_CHANNEL;
6425 raid_device->handle = handle;
6426 raid_device->wwid = wwid;
6427 _scsih_raid_device_add(ioc, raid_device);
6428 if (!ioc->wait_for_discovery_to_complete) {
6429 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6430 raid_device->id, 0);
6431 if (rc)
6432 _scsih_raid_device_remove(ioc, raid_device);
6433 } else {
6434 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6435 _scsih_determine_boot_device(ioc, raid_device, 1);
6436 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6437 }
6438 }
6439
6440 /**
6441 * _scsih_sas_volume_delete - delete volume
6442 * @ioc: per adapter object
6443 * @handle: volume device handle
6444 * Context: user.
6445 *
6446 * Return nothing.
6447 */
6448 static void
6449 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6450 {
6451 struct _raid_device *raid_device;
6452 unsigned long flags;
6453 struct MPT3SAS_TARGET *sas_target_priv_data;
6454 struct scsi_target *starget = NULL;
6455
6456 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6457 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6458 if (raid_device) {
6459 if (raid_device->starget) {
6460 starget = raid_device->starget;
6461 sas_target_priv_data = starget->hostdata;
6462 sas_target_priv_data->deleted = 1;
6463 }
6464 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6465 ioc->name, raid_device->handle,
6466 (unsigned long long) raid_device->wwid);
6467 list_del(&raid_device->list);
6468 kfree(raid_device);
6469 }
6470 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6471 if (starget)
6472 scsi_remove_target(&starget->dev);
6473 }
6474
6475 /**
6476 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6477 * @ioc: per adapter object
6478 * @element: IR config element data
6479 * Context: user.
6480 *
6481 * Return nothing.
6482 */
6483 static void
6484 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6485 Mpi2EventIrConfigElement_t *element)
6486 {
6487 struct _sas_device *sas_device;
6488 struct scsi_target *starget = NULL;
6489 struct MPT3SAS_TARGET *sas_target_priv_data;
6490 unsigned long flags;
6491 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6492
6493 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6494 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6495 if (sas_device) {
6496 sas_device->volume_handle = 0;
6497 sas_device->volume_wwid = 0;
6498 clear_bit(handle, ioc->pd_handles);
6499 if (sas_device->starget && sas_device->starget->hostdata) {
6500 starget = sas_device->starget;
6501 sas_target_priv_data = starget->hostdata;
6502 sas_target_priv_data->flags &=
6503 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6504 }
6505 }
6506 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6507 if (!sas_device)
6508 return;
6509
6510 /* exposing raid component */
6511 if (starget)
6512 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6513
6514 sas_device_put(sas_device);
6515 }
6516
6517 /**
6518 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6519 * @ioc: per adapter object
6520 * @element: IR config element data
6521 * Context: user.
6522 *
6523 * Return nothing.
6524 */
6525 static void
6526 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6527 Mpi2EventIrConfigElement_t *element)
6528 {
6529 struct _sas_device *sas_device;
6530 struct scsi_target *starget = NULL;
6531 struct MPT3SAS_TARGET *sas_target_priv_data;
6532 unsigned long flags;
6533 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6534 u16 volume_handle = 0;
6535 u64 volume_wwid = 0;
6536
6537 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6538 if (volume_handle)
6539 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6540 &volume_wwid);
6541
6542 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6543 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6544 if (sas_device) {
6545 set_bit(handle, ioc->pd_handles);
6546 if (sas_device->starget && sas_device->starget->hostdata) {
6547 starget = sas_device->starget;
6548 sas_target_priv_data = starget->hostdata;
6549 sas_target_priv_data->flags |=
6550 MPT_TARGET_FLAGS_RAID_COMPONENT;
6551 sas_device->volume_handle = volume_handle;
6552 sas_device->volume_wwid = volume_wwid;
6553 }
6554 }
6555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6556 if (!sas_device)
6557 return;
6558
6559 /* hiding raid component */
6560 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6561
6562 if (starget)
6563 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6564
6565 sas_device_put(sas_device);
6566 }
6567
6568 /**
6569 * _scsih_sas_pd_delete - delete pd component
6570 * @ioc: per adapter object
6571 * @element: IR config element data
6572 * Context: user.
6573 *
6574 * Return nothing.
6575 */
6576 static void
6577 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6578 Mpi2EventIrConfigElement_t *element)
6579 {
6580 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6581
6582 _scsih_device_remove_by_handle(ioc, handle);
6583 }
6584
6585 /**
6586 * _scsih_sas_pd_add - remove pd component
6587 * @ioc: per adapter object
6588 * @element: IR config element data
6589 * Context: user.
6590 *
6591 * Return nothing.
6592 */
6593 static void
6594 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6595 Mpi2EventIrConfigElement_t *element)
6596 {
6597 struct _sas_device *sas_device;
6598 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6599 Mpi2ConfigReply_t mpi_reply;
6600 Mpi2SasDevicePage0_t sas_device_pg0;
6601 u32 ioc_status;
6602 u64 sas_address;
6603 u16 parent_handle;
6604
6605 set_bit(handle, ioc->pd_handles);
6606
6607 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6608 if (sas_device) {
6609 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6610 sas_device_put(sas_device);
6611 return;
6612 }
6613
6614 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6615 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6616 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6617 ioc->name, __FILE__, __LINE__, __func__);
6618 return;
6619 }
6620
6621 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6622 MPI2_IOCSTATUS_MASK;
6623 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6624 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6625 ioc->name, __FILE__, __LINE__, __func__);
6626 return;
6627 }
6628
6629 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6630 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6631 mpt3sas_transport_update_links(ioc, sas_address, handle,
6632 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6633
6634 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6635 _scsih_add_device(ioc, handle, 0, 1);
6636 }
6637
6638 /**
6639 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6640 * @ioc: per adapter object
6641 * @event_data: event data payload
6642 * Context: user.
6643 *
6644 * Return nothing.
6645 */
6646 static void
6647 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6648 Mpi2EventDataIrConfigChangeList_t *event_data)
6649 {
6650 Mpi2EventIrConfigElement_t *element;
6651 u8 element_type;
6652 int i;
6653 char *reason_str = NULL, *element_str = NULL;
6654
6655 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6656
6657 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6658 ioc->name, (le32_to_cpu(event_data->Flags) &
6659 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6660 "foreign" : "native", event_data->NumElements);
6661 for (i = 0; i < event_data->NumElements; i++, element++) {
6662 switch (element->ReasonCode) {
6663 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6664 reason_str = "add";
6665 break;
6666 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6667 reason_str = "remove";
6668 break;
6669 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6670 reason_str = "no change";
6671 break;
6672 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6673 reason_str = "hide";
6674 break;
6675 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6676 reason_str = "unhide";
6677 break;
6678 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6679 reason_str = "volume_created";
6680 break;
6681 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6682 reason_str = "volume_deleted";
6683 break;
6684 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6685 reason_str = "pd_created";
6686 break;
6687 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6688 reason_str = "pd_deleted";
6689 break;
6690 default:
6691 reason_str = "unknown reason";
6692 break;
6693 }
6694 element_type = le16_to_cpu(element->ElementFlags) &
6695 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6696 switch (element_type) {
6697 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6698 element_str = "volume";
6699 break;
6700 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6701 element_str = "phys disk";
6702 break;
6703 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6704 element_str = "hot spare";
6705 break;
6706 default:
6707 element_str = "unknown element";
6708 break;
6709 }
6710 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6711 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6712 reason_str, le16_to_cpu(element->VolDevHandle),
6713 le16_to_cpu(element->PhysDiskDevHandle),
6714 element->PhysDiskNum);
6715 }
6716 }
6717
6718 /**
6719 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6720 * @ioc: per adapter object
6721 * @fw_event: The fw_event_work object
6722 * Context: user.
6723 *
6724 * Return nothing.
6725 */
6726 static void
6727 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6728 struct fw_event_work *fw_event)
6729 {
6730 Mpi2EventIrConfigElement_t *element;
6731 int i;
6732 u8 foreign_config;
6733 Mpi2EventDataIrConfigChangeList_t *event_data =
6734 (Mpi2EventDataIrConfigChangeList_t *)
6735 fw_event->event_data;
6736
6737 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6738 (!ioc->hide_ir_msg))
6739 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6740
6741 foreign_config = (le32_to_cpu(event_data->Flags) &
6742 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6743
6744 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6745 if (ioc->shost_recovery &&
6746 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
6747 for (i = 0; i < event_data->NumElements; i++, element++) {
6748 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6749 _scsih_ir_fastpath(ioc,
6750 le16_to_cpu(element->PhysDiskDevHandle),
6751 element->PhysDiskNum);
6752 }
6753 return;
6754 }
6755
6756 for (i = 0; i < event_data->NumElements; i++, element++) {
6757
6758 switch (element->ReasonCode) {
6759 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6760 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6761 if (!foreign_config)
6762 _scsih_sas_volume_add(ioc, element);
6763 break;
6764 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6765 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6766 if (!foreign_config)
6767 _scsih_sas_volume_delete(ioc,
6768 le16_to_cpu(element->VolDevHandle));
6769 break;
6770 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6771 if (!ioc->is_warpdrive)
6772 _scsih_sas_pd_hide(ioc, element);
6773 break;
6774 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6775 if (!ioc->is_warpdrive)
6776 _scsih_sas_pd_expose(ioc, element);
6777 break;
6778 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6779 if (!ioc->is_warpdrive)
6780 _scsih_sas_pd_add(ioc, element);
6781 break;
6782 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6783 if (!ioc->is_warpdrive)
6784 _scsih_sas_pd_delete(ioc, element);
6785 break;
6786 }
6787 }
6788 }
6789
6790 /**
6791 * _scsih_sas_ir_volume_event - IR volume event
6792 * @ioc: per adapter object
6793 * @fw_event: The fw_event_work object
6794 * Context: user.
6795 *
6796 * Return nothing.
6797 */
6798 static void
6799 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6800 struct fw_event_work *fw_event)
6801 {
6802 u64 wwid;
6803 unsigned long flags;
6804 struct _raid_device *raid_device;
6805 u16 handle;
6806 u32 state;
6807 int rc;
6808 Mpi2EventDataIrVolume_t *event_data =
6809 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6810
6811 if (ioc->shost_recovery)
6812 return;
6813
6814 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6815 return;
6816
6817 handle = le16_to_cpu(event_data->VolDevHandle);
6818 state = le32_to_cpu(event_data->NewValue);
6819 if (!ioc->hide_ir_msg)
6820 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6821 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6822 ioc->name, __func__, handle,
6823 le32_to_cpu(event_data->PreviousValue), state));
6824 switch (state) {
6825 case MPI2_RAID_VOL_STATE_MISSING:
6826 case MPI2_RAID_VOL_STATE_FAILED:
6827 _scsih_sas_volume_delete(ioc, handle);
6828 break;
6829
6830 case MPI2_RAID_VOL_STATE_ONLINE:
6831 case MPI2_RAID_VOL_STATE_DEGRADED:
6832 case MPI2_RAID_VOL_STATE_OPTIMAL:
6833
6834 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6835 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
6836 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6837
6838 if (raid_device)
6839 break;
6840
6841 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6842 if (!wwid) {
6843 pr_err(MPT3SAS_FMT
6844 "failure at %s:%d/%s()!\n", ioc->name,
6845 __FILE__, __LINE__, __func__);
6846 break;
6847 }
6848
6849 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6850 if (!raid_device) {
6851 pr_err(MPT3SAS_FMT
6852 "failure at %s:%d/%s()!\n", ioc->name,
6853 __FILE__, __LINE__, __func__);
6854 break;
6855 }
6856
6857 raid_device->id = ioc->sas_id++;
6858 raid_device->channel = RAID_CHANNEL;
6859 raid_device->handle = handle;
6860 raid_device->wwid = wwid;
6861 _scsih_raid_device_add(ioc, raid_device);
6862 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6863 raid_device->id, 0);
6864 if (rc)
6865 _scsih_raid_device_remove(ioc, raid_device);
6866 break;
6867
6868 case MPI2_RAID_VOL_STATE_INITIALIZING:
6869 default:
6870 break;
6871 }
6872 }
6873
6874 /**
6875 * _scsih_sas_ir_physical_disk_event - PD event
6876 * @ioc: per adapter object
6877 * @fw_event: The fw_event_work object
6878 * Context: user.
6879 *
6880 * Return nothing.
6881 */
6882 static void
6883 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6884 struct fw_event_work *fw_event)
6885 {
6886 u16 handle, parent_handle;
6887 u32 state;
6888 struct _sas_device *sas_device;
6889 Mpi2ConfigReply_t mpi_reply;
6890 Mpi2SasDevicePage0_t sas_device_pg0;
6891 u32 ioc_status;
6892 Mpi2EventDataIrPhysicalDisk_t *event_data =
6893 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6894 u64 sas_address;
6895
6896 if (ioc->shost_recovery)
6897 return;
6898
6899 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6900 return;
6901
6902 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6903 state = le32_to_cpu(event_data->NewValue);
6904
6905 if (!ioc->hide_ir_msg)
6906 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6907 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6908 ioc->name, __func__, handle,
6909 le32_to_cpu(event_data->PreviousValue), state));
6910
6911 switch (state) {
6912 case MPI2_RAID_PD_STATE_ONLINE:
6913 case MPI2_RAID_PD_STATE_DEGRADED:
6914 case MPI2_RAID_PD_STATE_REBUILDING:
6915 case MPI2_RAID_PD_STATE_OPTIMAL:
6916 case MPI2_RAID_PD_STATE_HOT_SPARE:
6917
6918 if (!ioc->is_warpdrive)
6919 set_bit(handle, ioc->pd_handles);
6920
6921 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6922 if (sas_device) {
6923 sas_device_put(sas_device);
6924 return;
6925 }
6926
6927 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6928 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6929 handle))) {
6930 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6931 ioc->name, __FILE__, __LINE__, __func__);
6932 return;
6933 }
6934
6935 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6936 MPI2_IOCSTATUS_MASK;
6937 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6938 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6939 ioc->name, __FILE__, __LINE__, __func__);
6940 return;
6941 }
6942
6943 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6944 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6945 mpt3sas_transport_update_links(ioc, sas_address, handle,
6946 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6947
6948 _scsih_add_device(ioc, handle, 0, 1);
6949
6950 break;
6951
6952 case MPI2_RAID_PD_STATE_OFFLINE:
6953 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6954 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6955 default:
6956 break;
6957 }
6958 }
6959
6960 /**
6961 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6962 * @ioc: per adapter object
6963 * @event_data: event data payload
6964 * Context: user.
6965 *
6966 * Return nothing.
6967 */
6968 static void
6969 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6970 Mpi2EventDataIrOperationStatus_t *event_data)
6971 {
6972 char *reason_str = NULL;
6973
6974 switch (event_data->RAIDOperation) {
6975 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6976 reason_str = "resync";
6977 break;
6978 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6979 reason_str = "online capacity expansion";
6980 break;
6981 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6982 reason_str = "consistency check";
6983 break;
6984 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6985 reason_str = "background init";
6986 break;
6987 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6988 reason_str = "make data consistent";
6989 break;
6990 }
6991
6992 if (!reason_str)
6993 return;
6994
6995 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6996 "\thandle(0x%04x), percent complete(%d)\n",
6997 ioc->name, reason_str,
6998 le16_to_cpu(event_data->VolDevHandle),
6999 event_data->PercentComplete);
7000 }
7001
7002 /**
7003 * _scsih_sas_ir_operation_status_event - handle RAID operation events
7004 * @ioc: per adapter object
7005 * @fw_event: The fw_event_work object
7006 * Context: user.
7007 *
7008 * Return nothing.
7009 */
7010 static void
7011 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
7012 struct fw_event_work *fw_event)
7013 {
7014 Mpi2EventDataIrOperationStatus_t *event_data =
7015 (Mpi2EventDataIrOperationStatus_t *)
7016 fw_event->event_data;
7017 static struct _raid_device *raid_device;
7018 unsigned long flags;
7019 u16 handle;
7020
7021 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
7022 (!ioc->hide_ir_msg))
7023 _scsih_sas_ir_operation_status_event_debug(ioc,
7024 event_data);
7025
7026 /* code added for raid transport support */
7027 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
7028
7029 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7030 handle = le16_to_cpu(event_data->VolDevHandle);
7031 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
7032 if (raid_device)
7033 raid_device->percent_complete =
7034 event_data->PercentComplete;
7035 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7036 }
7037 }
7038
7039 /**
7040 * _scsih_prep_device_scan - initialize parameters prior to device scan
7041 * @ioc: per adapter object
7042 *
7043 * Set the deleted flag prior to device scan. If the device is found during
7044 * the scan, then we clear the deleted flag.
7045 */
7046 static void
7047 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
7048 {
7049 struct MPT3SAS_DEVICE *sas_device_priv_data;
7050 struct scsi_device *sdev;
7051
7052 shost_for_each_device(sdev, ioc->shost) {
7053 sas_device_priv_data = sdev->hostdata;
7054 if (sas_device_priv_data && sas_device_priv_data->sas_target)
7055 sas_device_priv_data->sas_target->deleted = 1;
7056 }
7057 }
7058
7059 /**
7060 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
7061 * @ioc: per adapter object
7062 * @sas_device_pg0: SAS Device page 0
7063 *
7064 * After host reset, find out whether devices are still responding.
7065 * Used in _scsih_remove_unresponsive_sas_devices.
7066 *
7067 * Return nothing.
7068 */
7069 static void
7070 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7071 Mpi2SasDevicePage0_t *sas_device_pg0)
7072 {
7073 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7074 struct scsi_target *starget;
7075 struct _sas_device *sas_device;
7076 unsigned long flags;
7077
7078 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7079 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
7080 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7081 (sas_device->slot == sas_device_pg0->Slot)) {
7082 sas_device->responding = 1;
7083 starget = sas_device->starget;
7084 if (starget && starget->hostdata) {
7085 sas_target_priv_data = starget->hostdata;
7086 sas_target_priv_data->tm_busy = 0;
7087 sas_target_priv_data->deleted = 0;
7088 } else
7089 sas_target_priv_data = NULL;
7090 if (starget) {
7091 starget_printk(KERN_INFO, starget,
7092 "handle(0x%04x), sas_addr(0x%016llx)\n",
7093 sas_device_pg0->DevHandle,
7094 (unsigned long long)
7095 sas_device->sas_address);
7096
7097 if (sas_device->enclosure_handle != 0)
7098 starget_printk(KERN_INFO, starget,
7099 "enclosure logical id(0x%016llx),"
7100 " slot(%d)\n",
7101 (unsigned long long)
7102 sas_device->enclosure_logical_id,
7103 sas_device->slot);
7104 }
7105 if (sas_device_pg0->Flags &
7106 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7107 sas_device->enclosure_level =
7108 sas_device_pg0->EnclosureLevel;
7109 memcpy(&sas_device->connector_name[0],
7110 &sas_device_pg0->ConnectorName[0], 4);
7111 } else {
7112 sas_device->enclosure_level = 0;
7113 sas_device->connector_name[0] = '\0';
7114 }
7115
7116 if (sas_device->handle == sas_device_pg0->DevHandle)
7117 goto out;
7118 pr_info("\thandle changed from(0x%04x)!!!\n",
7119 sas_device->handle);
7120 sas_device->handle = sas_device_pg0->DevHandle;
7121 if (sas_target_priv_data)
7122 sas_target_priv_data->handle =
7123 sas_device_pg0->DevHandle;
7124 goto out;
7125 }
7126 }
7127 out:
7128 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7129 }
7130
7131 /**
7132 * _scsih_search_responding_sas_devices -
7133 * @ioc: per adapter object
7134 *
7135 * After host reset, find out whether devices are still responding.
7136 * If not remove.
7137 *
7138 * Return nothing.
7139 */
7140 static void
7141 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7142 {
7143 Mpi2SasDevicePage0_t sas_device_pg0;
7144 Mpi2ConfigReply_t mpi_reply;
7145 u16 ioc_status;
7146 u16 handle;
7147 u32 device_info;
7148
7149 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7150
7151 if (list_empty(&ioc->sas_device_list))
7152 goto out;
7153
7154 handle = 0xFFFF;
7155 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7156 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7157 handle))) {
7158 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7159 MPI2_IOCSTATUS_MASK;
7160 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7161 break;
7162 handle = sas_device_pg0.DevHandle =
7163 le16_to_cpu(sas_device_pg0.DevHandle);
7164 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7165 if (!(_scsih_is_end_device(device_info)))
7166 continue;
7167 sas_device_pg0.SASAddress =
7168 le64_to_cpu(sas_device_pg0.SASAddress);
7169 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7170 sas_device_pg0.Flags = le16_to_cpu(sas_device_pg0.Flags);
7171 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
7172 }
7173
7174 out:
7175 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7176 ioc->name);
7177 }
7178
7179 /**
7180 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7181 * @ioc: per adapter object
7182 * @wwid: world wide identifier for raid volume
7183 * @handle: device handle
7184 *
7185 * After host reset, find out whether devices are still responding.
7186 * Used in _scsih_remove_unresponsive_raid_devices.
7187 *
7188 * Return nothing.
7189 */
7190 static void
7191 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7192 u16 handle)
7193 {
7194 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7195 struct scsi_target *starget;
7196 struct _raid_device *raid_device;
7197 unsigned long flags;
7198
7199 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7200 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7201 if (raid_device->wwid == wwid && raid_device->starget) {
7202 starget = raid_device->starget;
7203 if (starget && starget->hostdata) {
7204 sas_target_priv_data = starget->hostdata;
7205 sas_target_priv_data->deleted = 0;
7206 } else
7207 sas_target_priv_data = NULL;
7208 raid_device->responding = 1;
7209 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7210 starget_printk(KERN_INFO, raid_device->starget,
7211 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7212 (unsigned long long)raid_device->wwid);
7213
7214 /*
7215 * WARPDRIVE: The handles of the PDs might have changed
7216 * across the host reset so re-initialize the
7217 * required data for Direct IO
7218 */
7219 mpt3sas_init_warpdrive_properties(ioc, raid_device);
7220 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7221 if (raid_device->handle == handle) {
7222 spin_unlock_irqrestore(&ioc->raid_device_lock,
7223 flags);
7224 return;
7225 }
7226 pr_info("\thandle changed from(0x%04x)!!!\n",
7227 raid_device->handle);
7228 raid_device->handle = handle;
7229 if (sas_target_priv_data)
7230 sas_target_priv_data->handle = handle;
7231 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7232 return;
7233 }
7234 }
7235 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7236 }
7237
7238 /**
7239 * _scsih_search_responding_raid_devices -
7240 * @ioc: per adapter object
7241 *
7242 * After host reset, find out whether devices are still responding.
7243 * If not remove.
7244 *
7245 * Return nothing.
7246 */
7247 static void
7248 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7249 {
7250 Mpi2RaidVolPage1_t volume_pg1;
7251 Mpi2RaidVolPage0_t volume_pg0;
7252 Mpi2RaidPhysDiskPage0_t pd_pg0;
7253 Mpi2ConfigReply_t mpi_reply;
7254 u16 ioc_status;
7255 u16 handle;
7256 u8 phys_disk_num;
7257
7258 if (!ioc->ir_firmware)
7259 return;
7260
7261 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7262 ioc->name);
7263
7264 if (list_empty(&ioc->raid_device_list))
7265 goto out;
7266
7267 handle = 0xFFFF;
7268 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7269 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7270 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7271 MPI2_IOCSTATUS_MASK;
7272 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7273 break;
7274 handle = le16_to_cpu(volume_pg1.DevHandle);
7275
7276 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7277 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7278 sizeof(Mpi2RaidVolPage0_t)))
7279 continue;
7280
7281 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7282 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7283 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7284 _scsih_mark_responding_raid_device(ioc,
7285 le64_to_cpu(volume_pg1.WWID), handle);
7286 }
7287
7288 /* refresh the pd_handles */
7289 if (!ioc->is_warpdrive) {
7290 phys_disk_num = 0xFF;
7291 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7292 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7293 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7294 phys_disk_num))) {
7295 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7296 MPI2_IOCSTATUS_MASK;
7297 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7298 break;
7299 phys_disk_num = pd_pg0.PhysDiskNum;
7300 handle = le16_to_cpu(pd_pg0.DevHandle);
7301 set_bit(handle, ioc->pd_handles);
7302 }
7303 }
7304 out:
7305 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7306 ioc->name);
7307 }
7308
7309 /**
7310 * _scsih_mark_responding_expander - mark a expander as responding
7311 * @ioc: per adapter object
7312 * @sas_address: sas address
7313 * @handle:
7314 *
7315 * After host reset, find out whether devices are still responding.
7316 * Used in _scsih_remove_unresponsive_expanders.
7317 *
7318 * Return nothing.
7319 */
7320 static void
7321 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7322 u16 handle)
7323 {
7324 struct _sas_node *sas_expander;
7325 unsigned long flags;
7326 int i;
7327
7328 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7329 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7330 if (sas_expander->sas_address != sas_address)
7331 continue;
7332 sas_expander->responding = 1;
7333 if (sas_expander->handle == handle)
7334 goto out;
7335 pr_info("\texpander(0x%016llx): handle changed" \
7336 " from(0x%04x) to (0x%04x)!!!\n",
7337 (unsigned long long)sas_expander->sas_address,
7338 sas_expander->handle, handle);
7339 sas_expander->handle = handle;
7340 for (i = 0 ; i < sas_expander->num_phys ; i++)
7341 sas_expander->phy[i].handle = handle;
7342 goto out;
7343 }
7344 out:
7345 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7346 }
7347
7348 /**
7349 * _scsih_search_responding_expanders -
7350 * @ioc: per adapter object
7351 *
7352 * After host reset, find out whether devices are still responding.
7353 * If not remove.
7354 *
7355 * Return nothing.
7356 */
7357 static void
7358 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7359 {
7360 Mpi2ExpanderPage0_t expander_pg0;
7361 Mpi2ConfigReply_t mpi_reply;
7362 u16 ioc_status;
7363 u64 sas_address;
7364 u16 handle;
7365
7366 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7367
7368 if (list_empty(&ioc->sas_expander_list))
7369 goto out;
7370
7371 handle = 0xFFFF;
7372 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7373 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7374
7375 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7376 MPI2_IOCSTATUS_MASK;
7377 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7378 break;
7379
7380 handle = le16_to_cpu(expander_pg0.DevHandle);
7381 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7382 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7383 handle,
7384 (unsigned long long)sas_address);
7385 _scsih_mark_responding_expander(ioc, sas_address, handle);
7386 }
7387
7388 out:
7389 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7390 }
7391
7392 /**
7393 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7394 * @ioc: per adapter object
7395 *
7396 * Return nothing.
7397 */
7398 static void
7399 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7400 {
7401 struct _sas_device *sas_device, *sas_device_next;
7402 struct _sas_node *sas_expander, *sas_expander_next;
7403 struct _raid_device *raid_device, *raid_device_next;
7404 struct list_head tmp_list;
7405 unsigned long flags;
7406 LIST_HEAD(head);
7407
7408 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7409 ioc->name);
7410
7411 /* removing unresponding end devices */
7412 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7413 ioc->name);
7414 /*
7415 * Iterate, pulling off devices marked as non-responding. We become the
7416 * owner for the reference the list had on any object we prune.
7417 */
7418 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7419 list_for_each_entry_safe(sas_device, sas_device_next,
7420 &ioc->sas_device_list, list) {
7421 if (!sas_device->responding)
7422 list_move_tail(&sas_device->list, &head);
7423 else
7424 sas_device->responding = 0;
7425 }
7426 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7427
7428 /*
7429 * Now, uninitialize and remove the unresponding devices we pruned.
7430 */
7431 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7432 _scsih_remove_device(ioc, sas_device);
7433 list_del_init(&sas_device->list);
7434 sas_device_put(sas_device);
7435 }
7436
7437 /* removing unresponding volumes */
7438 if (ioc->ir_firmware) {
7439 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7440 ioc->name);
7441 list_for_each_entry_safe(raid_device, raid_device_next,
7442 &ioc->raid_device_list, list) {
7443 if (!raid_device->responding)
7444 _scsih_sas_volume_delete(ioc,
7445 raid_device->handle);
7446 else
7447 raid_device->responding = 0;
7448 }
7449 }
7450
7451 /* removing unresponding expanders */
7452 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7453 ioc->name);
7454 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7455 INIT_LIST_HEAD(&tmp_list);
7456 list_for_each_entry_safe(sas_expander, sas_expander_next,
7457 &ioc->sas_expander_list, list) {
7458 if (!sas_expander->responding)
7459 list_move_tail(&sas_expander->list, &tmp_list);
7460 else
7461 sas_expander->responding = 0;
7462 }
7463 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7464 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7465 list) {
7466 list_del(&sas_expander->list);
7467 _scsih_expander_node_remove(ioc, sas_expander);
7468 }
7469
7470 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7471 ioc->name);
7472
7473 /* unblock devices */
7474 _scsih_ublock_io_all_device(ioc);
7475 }
7476
7477 static void
7478 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7479 struct _sas_node *sas_expander, u16 handle)
7480 {
7481 Mpi2ExpanderPage1_t expander_pg1;
7482 Mpi2ConfigReply_t mpi_reply;
7483 int i;
7484
7485 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7486 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7487 &expander_pg1, i, handle))) {
7488 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7489 ioc->name, __FILE__, __LINE__, __func__);
7490 return;
7491 }
7492
7493 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7494 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7495 expander_pg1.NegotiatedLinkRate >> 4);
7496 }
7497 }
7498
7499 /**
7500 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7501 * @ioc: per adapter object
7502 *
7503 * Return nothing.
7504 */
7505 static void
7506 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7507 {
7508 Mpi2ExpanderPage0_t expander_pg0;
7509 Mpi2SasDevicePage0_t sas_device_pg0;
7510 Mpi2RaidVolPage1_t volume_pg1;
7511 Mpi2RaidVolPage0_t volume_pg0;
7512 Mpi2RaidPhysDiskPage0_t pd_pg0;
7513 Mpi2EventIrConfigElement_t element;
7514 Mpi2ConfigReply_t mpi_reply;
7515 u8 phys_disk_num;
7516 u16 ioc_status;
7517 u16 handle, parent_handle;
7518 u64 sas_address;
7519 struct _sas_device *sas_device;
7520 struct _sas_node *expander_device;
7521 static struct _raid_device *raid_device;
7522 u8 retry_count;
7523 unsigned long flags;
7524
7525 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7526
7527 _scsih_sas_host_refresh(ioc);
7528
7529 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7530
7531 /* expanders */
7532 handle = 0xFFFF;
7533 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7534 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7535 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7536 MPI2_IOCSTATUS_MASK;
7537 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7538 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7539 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7540 ioc->name, ioc_status,
7541 le32_to_cpu(mpi_reply.IOCLogInfo));
7542 break;
7543 }
7544 handle = le16_to_cpu(expander_pg0.DevHandle);
7545 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7546 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7547 ioc, le64_to_cpu(expander_pg0.SASAddress));
7548 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7549 if (expander_device)
7550 _scsih_refresh_expander_links(ioc, expander_device,
7551 handle);
7552 else {
7553 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7554 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7555 handle, (unsigned long long)
7556 le64_to_cpu(expander_pg0.SASAddress));
7557 _scsih_expander_add(ioc, handle);
7558 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7559 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7560 handle, (unsigned long long)
7561 le64_to_cpu(expander_pg0.SASAddress));
7562 }
7563 }
7564
7565 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7566 ioc->name);
7567
7568 if (!ioc->ir_firmware)
7569 goto skip_to_sas;
7570
7571 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7572
7573 /* phys disk */
7574 phys_disk_num = 0xFF;
7575 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7576 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7577 phys_disk_num))) {
7578 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7579 MPI2_IOCSTATUS_MASK;
7580 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7581 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7582 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7583 ioc->name, ioc_status,
7584 le32_to_cpu(mpi_reply.IOCLogInfo));
7585 break;
7586 }
7587 phys_disk_num = pd_pg0.PhysDiskNum;
7588 handle = le16_to_cpu(pd_pg0.DevHandle);
7589 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7590 if (sas_device) {
7591 sas_device_put(sas_device);
7592 continue;
7593 }
7594 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7595 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7596 handle) != 0)
7597 continue;
7598 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7599 MPI2_IOCSTATUS_MASK;
7600 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7601 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7602 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7603 ioc->name, ioc_status,
7604 le32_to_cpu(mpi_reply.IOCLogInfo));
7605 break;
7606 }
7607 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7608 if (!_scsih_get_sas_address(ioc, parent_handle,
7609 &sas_address)) {
7610 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7611 " handle (0x%04x), sas_addr(0x%016llx)\n",
7612 ioc->name, handle, (unsigned long long)
7613 le64_to_cpu(sas_device_pg0.SASAddress));
7614 mpt3sas_transport_update_links(ioc, sas_address,
7615 handle, sas_device_pg0.PhyNum,
7616 MPI2_SAS_NEG_LINK_RATE_1_5);
7617 set_bit(handle, ioc->pd_handles);
7618 retry_count = 0;
7619 /* This will retry adding the end device.
7620 * _scsih_add_device() will decide on retries and
7621 * return "1" when it should be retried
7622 */
7623 while (_scsih_add_device(ioc, handle, retry_count++,
7624 1)) {
7625 ssleep(1);
7626 }
7627 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7628 " handle (0x%04x), sas_addr(0x%016llx)\n",
7629 ioc->name, handle, (unsigned long long)
7630 le64_to_cpu(sas_device_pg0.SASAddress));
7631 }
7632 }
7633
7634 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7635 ioc->name);
7636
7637 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7638
7639 /* volumes */
7640 handle = 0xFFFF;
7641 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7642 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7643 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7644 MPI2_IOCSTATUS_MASK;
7645 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7646 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7647 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7648 ioc->name, ioc_status,
7649 le32_to_cpu(mpi_reply.IOCLogInfo));
7650 break;
7651 }
7652 handle = le16_to_cpu(volume_pg1.DevHandle);
7653 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7654 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7655 le64_to_cpu(volume_pg1.WWID));
7656 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7657 if (raid_device)
7658 continue;
7659 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7660 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7661 sizeof(Mpi2RaidVolPage0_t)))
7662 continue;
7663 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7664 MPI2_IOCSTATUS_MASK;
7665 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7666 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7667 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7668 ioc->name, ioc_status,
7669 le32_to_cpu(mpi_reply.IOCLogInfo));
7670 break;
7671 }
7672 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7673 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7674 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7675 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7676 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7677 element.VolDevHandle = volume_pg1.DevHandle;
7678 pr_info(MPT3SAS_FMT
7679 "\tBEFORE adding volume: handle (0x%04x)\n",
7680 ioc->name, volume_pg1.DevHandle);
7681 _scsih_sas_volume_add(ioc, &element);
7682 pr_info(MPT3SAS_FMT
7683 "\tAFTER adding volume: handle (0x%04x)\n",
7684 ioc->name, volume_pg1.DevHandle);
7685 }
7686 }
7687
7688 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7689 ioc->name);
7690
7691 skip_to_sas:
7692
7693 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7694 ioc->name);
7695
7696 /* sas devices */
7697 handle = 0xFFFF;
7698 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7699 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7700 handle))) {
7701 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7702 MPI2_IOCSTATUS_MASK;
7703 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7704 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7705 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7706 ioc->name, ioc_status,
7707 le32_to_cpu(mpi_reply.IOCLogInfo));
7708 break;
7709 }
7710 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7711 if (!(_scsih_is_end_device(
7712 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7713 continue;
7714 sas_device = mpt3sas_get_sdev_by_addr(ioc,
7715 le64_to_cpu(sas_device_pg0.SASAddress));
7716 if (sas_device) {
7717 sas_device_put(sas_device);
7718 continue;
7719 }
7720 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7721 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7722 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7723 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7724 handle, (unsigned long long)
7725 le64_to_cpu(sas_device_pg0.SASAddress));
7726 mpt3sas_transport_update_links(ioc, sas_address, handle,
7727 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7728 retry_count = 0;
7729 /* This will retry adding the end device.
7730 * _scsih_add_device() will decide on retries and
7731 * return "1" when it should be retried
7732 */
7733 while (_scsih_add_device(ioc, handle, retry_count++,
7734 0)) {
7735 ssleep(1);
7736 }
7737 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7738 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7739 handle, (unsigned long long)
7740 le64_to_cpu(sas_device_pg0.SASAddress));
7741 }
7742 }
7743 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7744 ioc->name);
7745
7746 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7747 }
7748 /**
7749 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7750 * @ioc: per adapter object
7751 * @reset_phase: phase
7752 *
7753 * The handler for doing any required cleanup or initialization.
7754 *
7755 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7756 * MPT3_IOC_DONE_RESET
7757 *
7758 * Return nothing.
7759 */
7760 void
7761 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7762 {
7763 switch (reset_phase) {
7764 case MPT3_IOC_PRE_RESET:
7765 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7766 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7767 break;
7768 case MPT3_IOC_AFTER_RESET:
7769 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7770 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7771 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7772 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7773 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7774 complete(&ioc->scsih_cmds.done);
7775 }
7776 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7777 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7778 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7779 complete(&ioc->tm_cmds.done);
7780 }
7781
7782 memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
7783 memset(ioc->device_remove_in_progress, 0,
7784 ioc->device_remove_in_progress_sz);
7785 _scsih_fw_event_cleanup_queue(ioc);
7786 _scsih_flush_running_cmds(ioc);
7787 break;
7788 case MPT3_IOC_DONE_RESET:
7789 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7790 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7791 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7792 !ioc->sas_hba.num_phys)) {
7793 _scsih_prep_device_scan(ioc);
7794 _scsih_search_responding_sas_devices(ioc);
7795 _scsih_search_responding_raid_devices(ioc);
7796 _scsih_search_responding_expanders(ioc);
7797 _scsih_error_recovery_delete_devices(ioc);
7798 }
7799 break;
7800 }
7801 }
7802
7803 /**
7804 * _mpt3sas_fw_work - delayed task for processing firmware events
7805 * @ioc: per adapter object
7806 * @fw_event: The fw_event_work object
7807 * Context: user.
7808 *
7809 * Return nothing.
7810 */
7811 static void
7812 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7813 {
7814 _scsih_fw_event_del_from_list(ioc, fw_event);
7815
7816 /* the queue is being flushed so ignore this event */
7817 if (ioc->remove_host || ioc->pci_error_recovery) {
7818 fw_event_work_put(fw_event);
7819 return;
7820 }
7821
7822 switch (fw_event->event) {
7823 case MPT3SAS_PROCESS_TRIGGER_DIAG:
7824 mpt3sas_process_trigger_data(ioc,
7825 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7826 fw_event->event_data);
7827 break;
7828 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7829 while (scsi_host_in_recovery(ioc->shost) ||
7830 ioc->shost_recovery) {
7831 /*
7832 * If we're unloading, bail. Otherwise, this can become
7833 * an infinite loop.
7834 */
7835 if (ioc->remove_host)
7836 goto out;
7837 ssleep(1);
7838 }
7839 _scsih_remove_unresponding_sas_devices(ioc);
7840 _scsih_scan_for_devices_after_reset(ioc);
7841 break;
7842 case MPT3SAS_PORT_ENABLE_COMPLETE:
7843 ioc->start_scan = 0;
7844 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7845 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7846 missing_delay[1]);
7847 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7848 "port enable: complete from worker thread\n",
7849 ioc->name));
7850 break;
7851 case MPT3SAS_TURN_ON_PFA_LED:
7852 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7853 break;
7854 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7855 _scsih_sas_topology_change_event(ioc, fw_event);
7856 break;
7857 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7858 _scsih_sas_device_status_change_event(ioc, fw_event);
7859 break;
7860 case MPI2_EVENT_SAS_DISCOVERY:
7861 _scsih_sas_discovery_event(ioc, fw_event);
7862 break;
7863 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7864 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7865 break;
7866 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7867 _scsih_sas_enclosure_dev_status_change_event(ioc,
7868 fw_event);
7869 break;
7870 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7871 _scsih_sas_ir_config_change_event(ioc, fw_event);
7872 break;
7873 case MPI2_EVENT_IR_VOLUME:
7874 _scsih_sas_ir_volume_event(ioc, fw_event);
7875 break;
7876 case MPI2_EVENT_IR_PHYSICAL_DISK:
7877 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7878 break;
7879 case MPI2_EVENT_IR_OPERATION_STATUS:
7880 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7881 break;
7882 }
7883 out:
7884 fw_event_work_put(fw_event);
7885 }
7886
7887 /**
7888 * _firmware_event_work
7889 * @ioc: per adapter object
7890 * @work: The fw_event_work object
7891 * Context: user.
7892 *
7893 * wrappers for the work thread handling firmware events
7894 *
7895 * Return nothing.
7896 */
7897
7898 static void
7899 _firmware_event_work(struct work_struct *work)
7900 {
7901 struct fw_event_work *fw_event = container_of(work,
7902 struct fw_event_work, work);
7903
7904 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7905 }
7906
7907 /**
7908 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7909 * @ioc: per adapter object
7910 * @msix_index: MSIX table index supplied by the OS
7911 * @reply: reply message frame(lower 32bit addr)
7912 * Context: interrupt.
7913 *
7914 * This function merely adds a new work task into ioc->firmware_event_thread.
7915 * The tasks are worked from _firmware_event_work in user context.
7916 *
7917 * Return 1 meaning mf should be freed from _base_interrupt
7918 * 0 means the mf is freed from this function.
7919 */
7920 u8
7921 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7922 u32 reply)
7923 {
7924 struct fw_event_work *fw_event;
7925 Mpi2EventNotificationReply_t *mpi_reply;
7926 u16 event;
7927 u16 sz;
7928 Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
7929
7930 /* events turned off due to host reset or driver unloading */
7931 if (ioc->remove_host || ioc->pci_error_recovery)
7932 return 1;
7933
7934 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7935
7936 if (unlikely(!mpi_reply)) {
7937 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7938 ioc->name, __FILE__, __LINE__, __func__);
7939 return 1;
7940 }
7941
7942 event = le16_to_cpu(mpi_reply->Event);
7943
7944 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7945 mpt3sas_trigger_event(ioc, event, 0);
7946
7947 switch (event) {
7948 /* handle these */
7949 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7950 {
7951 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7952 (Mpi2EventDataSasBroadcastPrimitive_t *)
7953 mpi_reply->EventData;
7954
7955 if (baen_data->Primitive !=
7956 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7957 return 1;
7958
7959 if (ioc->broadcast_aen_busy) {
7960 ioc->broadcast_aen_pending++;
7961 return 1;
7962 } else
7963 ioc->broadcast_aen_busy = 1;
7964 break;
7965 }
7966
7967 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7968 _scsih_check_topo_delete_events(ioc,
7969 (Mpi2EventDataSasTopologyChangeList_t *)
7970 mpi_reply->EventData);
7971 break;
7972 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7973 _scsih_check_ir_config_unhide_events(ioc,
7974 (Mpi2EventDataIrConfigChangeList_t *)
7975 mpi_reply->EventData);
7976 break;
7977 case MPI2_EVENT_IR_VOLUME:
7978 _scsih_check_volume_delete_events(ioc,
7979 (Mpi2EventDataIrVolume_t *)
7980 mpi_reply->EventData);
7981 break;
7982 case MPI2_EVENT_LOG_ENTRY_ADDED:
7983 {
7984 Mpi2EventDataLogEntryAdded_t *log_entry;
7985 u32 *log_code;
7986
7987 if (!ioc->is_warpdrive)
7988 break;
7989
7990 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7991 mpi_reply->EventData;
7992 log_code = (u32 *)log_entry->LogData;
7993
7994 if (le16_to_cpu(log_entry->LogEntryQualifier)
7995 != MPT2_WARPDRIVE_LOGENTRY)
7996 break;
7997
7998 switch (le32_to_cpu(*log_code)) {
7999 case MPT2_WARPDRIVE_LC_SSDT:
8000 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8001 "IO Throttling has occurred in the WarpDrive "
8002 "subsystem. Check WarpDrive documentation for "
8003 "additional details.\n", ioc->name);
8004 break;
8005 case MPT2_WARPDRIVE_LC_SSDLW:
8006 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8007 "Program/Erase Cycles for the WarpDrive subsystem "
8008 "in degraded range. Check WarpDrive documentation "
8009 "for additional details.\n", ioc->name);
8010 break;
8011 case MPT2_WARPDRIVE_LC_SSDLF:
8012 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8013 "There are no Program/Erase Cycles for the "
8014 "WarpDrive subsystem. The storage device will be "
8015 "in read-only mode. Check WarpDrive documentation "
8016 "for additional details.\n", ioc->name);
8017 break;
8018 case MPT2_WARPDRIVE_LC_BRMF:
8019 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8020 "The Backup Rail Monitor has failed on the "
8021 "WarpDrive subsystem. Check WarpDrive "
8022 "documentation for additional details.\n",
8023 ioc->name);
8024 break;
8025 }
8026
8027 break;
8028 }
8029 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
8030 case MPI2_EVENT_IR_OPERATION_STATUS:
8031 case MPI2_EVENT_SAS_DISCOVERY:
8032 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
8033 case MPI2_EVENT_IR_PHYSICAL_DISK:
8034 break;
8035
8036 case MPI2_EVENT_TEMP_THRESHOLD:
8037 _scsih_temp_threshold_events(ioc,
8038 (Mpi2EventDataTemperature_t *)
8039 mpi_reply->EventData);
8040 break;
8041 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
8042 ActiveCableEventData =
8043 (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
8044 switch (ActiveCableEventData->ReasonCode) {
8045 case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
8046 pr_notice(MPT3SAS_FMT "Receptacle ID %d: This active cable"
8047 " requires %d mW of power\n", ioc->name,
8048 ActiveCableEventData->ReceptacleID,
8049 ActiveCableEventData->ActiveCablePowerRequirement);
8050 pr_notice(MPT3SAS_FMT "Receptacle ID %d: Devices connected"
8051 " to this active cable will not be seen\n",
8052 ioc->name, ActiveCableEventData->ReceptacleID);
8053 break;
8054
8055 case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
8056 pr_notice(MPT3SAS_FMT "ReceptacleID %d: This cable",
8057 ioc->name, ActiveCableEventData->ReceptacleID);
8058 pr_notice(" is not running at an optimal speed(12 Gb/s)\n");
8059 break;
8060 }
8061
8062 break;
8063
8064 default: /* ignore the rest */
8065 return 1;
8066 }
8067
8068 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
8069 fw_event = alloc_fw_event_work(sz);
8070 if (!fw_event) {
8071 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8072 ioc->name, __FILE__, __LINE__, __func__);
8073 return 1;
8074 }
8075
8076 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
8077 fw_event->ioc = ioc;
8078 fw_event->VF_ID = mpi_reply->VF_ID;
8079 fw_event->VP_ID = mpi_reply->VP_ID;
8080 fw_event->event = event;
8081 _scsih_fw_event_add(ioc, fw_event);
8082 fw_event_work_put(fw_event);
8083 return 1;
8084 }
8085
8086 /**
8087 * _scsih_expander_node_remove - removing expander device from list.
8088 * @ioc: per adapter object
8089 * @sas_expander: the sas_device object
8090 * Context: Calling function should acquire ioc->sas_node_lock.
8091 *
8092 * Removing object and freeing associated memory from the
8093 * ioc->sas_expander_list.
8094 *
8095 * Return nothing.
8096 */
8097 static void
8098 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8099 struct _sas_node *sas_expander)
8100 {
8101 struct _sas_port *mpt3sas_port, *next;
8102
8103 /* remove sibling ports attached to this expander */
8104 list_for_each_entry_safe(mpt3sas_port, next,
8105 &sas_expander->sas_port_list, port_list) {
8106 if (ioc->shost_recovery)
8107 return;
8108 if (mpt3sas_port->remote_identify.device_type ==
8109 SAS_END_DEVICE)
8110 mpt3sas_device_remove_by_sas_address(ioc,
8111 mpt3sas_port->remote_identify.sas_address);
8112 else if (mpt3sas_port->remote_identify.device_type ==
8113 SAS_EDGE_EXPANDER_DEVICE ||
8114 mpt3sas_port->remote_identify.device_type ==
8115 SAS_FANOUT_EXPANDER_DEVICE)
8116 mpt3sas_expander_remove(ioc,
8117 mpt3sas_port->remote_identify.sas_address);
8118 }
8119
8120 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8121 sas_expander->sas_address_parent);
8122
8123 pr_info(MPT3SAS_FMT
8124 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8125 ioc->name,
8126 sas_expander->handle, (unsigned long long)
8127 sas_expander->sas_address);
8128
8129 kfree(sas_expander->phy);
8130 kfree(sas_expander);
8131 }
8132
8133 /**
8134 * _scsih_ir_shutdown - IR shutdown notification
8135 * @ioc: per adapter object
8136 *
8137 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8138 * the host system is shutting down.
8139 *
8140 * Return nothing.
8141 */
8142 static void
8143 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8144 {
8145 Mpi2RaidActionRequest_t *mpi_request;
8146 Mpi2RaidActionReply_t *mpi_reply;
8147 u16 smid;
8148
8149 /* is IR firmware build loaded ? */
8150 if (!ioc->ir_firmware)
8151 return;
8152
8153 /* are there any volumes ? */
8154 if (list_empty(&ioc->raid_device_list))
8155 return;
8156
8157 mutex_lock(&ioc->scsih_cmds.mutex);
8158
8159 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8160 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8161 ioc->name, __func__);
8162 goto out;
8163 }
8164 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8165
8166 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8167 if (!smid) {
8168 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8169 ioc->name, __func__);
8170 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8171 goto out;
8172 }
8173
8174 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8175 ioc->scsih_cmds.smid = smid;
8176 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8177
8178 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8179 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8180
8181 if (!ioc->hide_ir_msg)
8182 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
8183 init_completion(&ioc->scsih_cmds.done);
8184 ioc->put_smid_default(ioc, smid);
8185 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8186
8187 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8188 pr_err(MPT3SAS_FMT "%s: timeout\n",
8189 ioc->name, __func__);
8190 goto out;
8191 }
8192
8193 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8194 mpi_reply = ioc->scsih_cmds.reply;
8195 if (!ioc->hide_ir_msg)
8196 pr_info(MPT3SAS_FMT "IR shutdown "
8197 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8198 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8199 le32_to_cpu(mpi_reply->IOCLogInfo));
8200 }
8201
8202 out:
8203 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8204 mutex_unlock(&ioc->scsih_cmds.mutex);
8205 }
8206
8207 /**
8208 * scsih_remove - detach and remove add host
8209 * @pdev: PCI device struct
8210 *
8211 * Routine called when unloading the driver.
8212 * Return nothing.
8213 */
8214 static void scsih_remove(struct pci_dev *pdev)
8215 {
8216 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8217 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8218 struct _sas_port *mpt3sas_port, *next_port;
8219 struct _raid_device *raid_device, *next;
8220 struct MPT3SAS_TARGET *sas_target_priv_data;
8221 struct workqueue_struct *wq;
8222 unsigned long flags;
8223
8224 ioc->remove_host = 1;
8225 _scsih_fw_event_cleanup_queue(ioc);
8226
8227 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8228 wq = ioc->firmware_event_thread;
8229 ioc->firmware_event_thread = NULL;
8230 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8231 if (wq)
8232 destroy_workqueue(wq);
8233
8234 /* release all the volumes */
8235 _scsih_ir_shutdown(ioc);
8236 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8237 list) {
8238 if (raid_device->starget) {
8239 sas_target_priv_data =
8240 raid_device->starget->hostdata;
8241 sas_target_priv_data->deleted = 1;
8242 scsi_remove_target(&raid_device->starget->dev);
8243 }
8244 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8245 ioc->name, raid_device->handle,
8246 (unsigned long long) raid_device->wwid);
8247 _scsih_raid_device_remove(ioc, raid_device);
8248 }
8249
8250 /* free ports attached to the sas_host */
8251 list_for_each_entry_safe(mpt3sas_port, next_port,
8252 &ioc->sas_hba.sas_port_list, port_list) {
8253 if (mpt3sas_port->remote_identify.device_type ==
8254 SAS_END_DEVICE)
8255 mpt3sas_device_remove_by_sas_address(ioc,
8256 mpt3sas_port->remote_identify.sas_address);
8257 else if (mpt3sas_port->remote_identify.device_type ==
8258 SAS_EDGE_EXPANDER_DEVICE ||
8259 mpt3sas_port->remote_identify.device_type ==
8260 SAS_FANOUT_EXPANDER_DEVICE)
8261 mpt3sas_expander_remove(ioc,
8262 mpt3sas_port->remote_identify.sas_address);
8263 }
8264
8265 /* free phys attached to the sas_host */
8266 if (ioc->sas_hba.num_phys) {
8267 kfree(ioc->sas_hba.phy);
8268 ioc->sas_hba.phy = NULL;
8269 ioc->sas_hba.num_phys = 0;
8270 }
8271
8272 sas_remove_host(shost);
8273 scsi_remove_host(shost);
8274 mpt3sas_base_detach(ioc);
8275 spin_lock(&gioc_lock);
8276 list_del(&ioc->list);
8277 spin_unlock(&gioc_lock);
8278 scsi_host_put(shost);
8279 }
8280
8281 /**
8282 * scsih_shutdown - routine call during system shutdown
8283 * @pdev: PCI device struct
8284 *
8285 * Return nothing.
8286 */
8287 static void
8288 scsih_shutdown(struct pci_dev *pdev)
8289 {
8290 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8291 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8292 struct workqueue_struct *wq;
8293 unsigned long flags;
8294
8295 ioc->remove_host = 1;
8296 _scsih_fw_event_cleanup_queue(ioc);
8297
8298 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8299 wq = ioc->firmware_event_thread;
8300 ioc->firmware_event_thread = NULL;
8301 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8302 if (wq)
8303 destroy_workqueue(wq);
8304
8305 _scsih_ir_shutdown(ioc);
8306 mpt3sas_base_detach(ioc);
8307 }
8308
8309
8310 /**
8311 * _scsih_probe_boot_devices - reports 1st device
8312 * @ioc: per adapter object
8313 *
8314 * If specified in bios page 2, this routine reports the 1st
8315 * device scsi-ml or sas transport for persistent boot device
8316 * purposes. Please refer to function _scsih_determine_boot_device()
8317 */
8318 static void
8319 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8320 {
8321 u8 is_raid;
8322 void *device;
8323 struct _sas_device *sas_device;
8324 struct _raid_device *raid_device;
8325 u16 handle;
8326 u64 sas_address_parent;
8327 u64 sas_address;
8328 unsigned long flags;
8329 int rc;
8330
8331 /* no Bios, return immediately */
8332 if (!ioc->bios_pg3.BiosVersion)
8333 return;
8334
8335 device = NULL;
8336 is_raid = 0;
8337 if (ioc->req_boot_device.device) {
8338 device = ioc->req_boot_device.device;
8339 is_raid = ioc->req_boot_device.is_raid;
8340 } else if (ioc->req_alt_boot_device.device) {
8341 device = ioc->req_alt_boot_device.device;
8342 is_raid = ioc->req_alt_boot_device.is_raid;
8343 } else if (ioc->current_boot_device.device) {
8344 device = ioc->current_boot_device.device;
8345 is_raid = ioc->current_boot_device.is_raid;
8346 }
8347
8348 if (!device)
8349 return;
8350
8351 if (is_raid) {
8352 raid_device = device;
8353 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8354 raid_device->id, 0);
8355 if (rc)
8356 _scsih_raid_device_remove(ioc, raid_device);
8357 } else {
8358 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8359 sas_device = device;
8360 handle = sas_device->handle;
8361 sas_address_parent = sas_device->sas_address_parent;
8362 sas_address = sas_device->sas_address;
8363 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8364 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8365
8366 if (ioc->hide_drives)
8367 return;
8368 if (!mpt3sas_transport_port_add(ioc, handle,
8369 sas_address_parent)) {
8370 _scsih_sas_device_remove(ioc, sas_device);
8371 } else if (!sas_device->starget) {
8372 if (!ioc->is_driver_loading) {
8373 mpt3sas_transport_port_remove(ioc,
8374 sas_address,
8375 sas_address_parent);
8376 _scsih_sas_device_remove(ioc, sas_device);
8377 }
8378 }
8379 }
8380 }
8381
8382 /**
8383 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8384 * @ioc: per adapter object
8385 *
8386 * Called during initial loading of the driver.
8387 */
8388 static void
8389 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8390 {
8391 struct _raid_device *raid_device, *raid_next;
8392 int rc;
8393
8394 list_for_each_entry_safe(raid_device, raid_next,
8395 &ioc->raid_device_list, list) {
8396 if (raid_device->starget)
8397 continue;
8398 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8399 raid_device->id, 0);
8400 if (rc)
8401 _scsih_raid_device_remove(ioc, raid_device);
8402 }
8403 }
8404
8405 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8406 {
8407 struct _sas_device *sas_device = NULL;
8408 unsigned long flags;
8409
8410 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8411 if (!list_empty(&ioc->sas_device_init_list)) {
8412 sas_device = list_first_entry(&ioc->sas_device_init_list,
8413 struct _sas_device, list);
8414 sas_device_get(sas_device);
8415 }
8416 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8417
8418 return sas_device;
8419 }
8420
8421 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8422 struct _sas_device *sas_device)
8423 {
8424 unsigned long flags;
8425
8426 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8427
8428 /*
8429 * Since we dropped the lock during the call to port_add(), we need to
8430 * be careful here that somebody else didn't move or delete this item
8431 * while we were busy with other things.
8432 *
8433 * If it was on the list, we need a put() for the reference the list
8434 * had. Either way, we need a get() for the destination list.
8435 */
8436 if (!list_empty(&sas_device->list)) {
8437 list_del_init(&sas_device->list);
8438 sas_device_put(sas_device);
8439 }
8440
8441 sas_device_get(sas_device);
8442 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8443
8444 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8445 }
8446
8447 /**
8448 * _scsih_probe_sas - reporting sas devices to sas transport
8449 * @ioc: per adapter object
8450 *
8451 * Called during initial loading of the driver.
8452 */
8453 static void
8454 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8455 {
8456 struct _sas_device *sas_device;
8457
8458 if (ioc->hide_drives)
8459 return;
8460
8461 while ((sas_device = get_next_sas_device(ioc))) {
8462 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8463 sas_device->sas_address_parent)) {
8464 _scsih_sas_device_remove(ioc, sas_device);
8465 sas_device_put(sas_device);
8466 continue;
8467 } else if (!sas_device->starget) {
8468 /*
8469 * When asyn scanning is enabled, its not possible to
8470 * remove devices while scanning is turned on due to an
8471 * oops in scsi_sysfs_add_sdev()->add_device()->
8472 * sysfs_addrm_start()
8473 */
8474 if (!ioc->is_driver_loading) {
8475 mpt3sas_transport_port_remove(ioc,
8476 sas_device->sas_address,
8477 sas_device->sas_address_parent);
8478 _scsih_sas_device_remove(ioc, sas_device);
8479 sas_device_put(sas_device);
8480 continue;
8481 }
8482 }
8483 sas_device_make_active(ioc, sas_device);
8484 sas_device_put(sas_device);
8485 }
8486 }
8487
8488 /**
8489 * _scsih_probe_devices - probing for devices
8490 * @ioc: per adapter object
8491 *
8492 * Called during initial loading of the driver.
8493 */
8494 static void
8495 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8496 {
8497 u16 volume_mapping_flags;
8498
8499 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8500 return; /* return when IOC doesn't support initiator mode */
8501
8502 _scsih_probe_boot_devices(ioc);
8503
8504 if (ioc->ir_firmware) {
8505 volume_mapping_flags =
8506 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8507 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8508 if (volume_mapping_flags ==
8509 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8510 _scsih_probe_raid(ioc);
8511 _scsih_probe_sas(ioc);
8512 } else {
8513 _scsih_probe_sas(ioc);
8514 _scsih_probe_raid(ioc);
8515 }
8516 } else
8517 _scsih_probe_sas(ioc);
8518 }
8519
8520 /**
8521 * scsih_scan_start - scsi lld callback for .scan_start
8522 * @shost: SCSI host pointer
8523 *
8524 * The shost has the ability to discover targets on its own instead
8525 * of scanning the entire bus. In our implemention, we will kick off
8526 * firmware discovery.
8527 */
8528 static void
8529 scsih_scan_start(struct Scsi_Host *shost)
8530 {
8531 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8532 int rc;
8533 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8534 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8535
8536 if (disable_discovery > 0)
8537 return;
8538
8539 ioc->start_scan = 1;
8540 rc = mpt3sas_port_enable(ioc);
8541
8542 if (rc != 0)
8543 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8544 }
8545
8546 /**
8547 * scsih_scan_finished - scsi lld callback for .scan_finished
8548 * @shost: SCSI host pointer
8549 * @time: elapsed time of the scan in jiffies
8550 *
8551 * This function will be called periodicallyn until it returns 1 with the
8552 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8553 * we wait for firmware discovery to complete, then return 1.
8554 */
8555 static int
8556 scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8557 {
8558 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8559
8560 if (disable_discovery > 0) {
8561 ioc->is_driver_loading = 0;
8562 ioc->wait_for_discovery_to_complete = 0;
8563 return 1;
8564 }
8565
8566 if (time >= (300 * HZ)) {
8567 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8568 pr_info(MPT3SAS_FMT
8569 "port enable: FAILED with timeout (timeout=300s)\n",
8570 ioc->name);
8571 ioc->is_driver_loading = 0;
8572 return 1;
8573 }
8574
8575 if (ioc->start_scan)
8576 return 0;
8577
8578 if (ioc->start_scan_failed) {
8579 pr_info(MPT3SAS_FMT
8580 "port enable: FAILED with (ioc_status=0x%08x)\n",
8581 ioc->name, ioc->start_scan_failed);
8582 ioc->is_driver_loading = 0;
8583 ioc->wait_for_discovery_to_complete = 0;
8584 ioc->remove_host = 1;
8585 return 1;
8586 }
8587
8588 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8589 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8590
8591 if (ioc->wait_for_discovery_to_complete) {
8592 ioc->wait_for_discovery_to_complete = 0;
8593 _scsih_probe_devices(ioc);
8594 }
8595 mpt3sas_base_start_watchdog(ioc);
8596 ioc->is_driver_loading = 0;
8597 return 1;
8598 }
8599
8600 /* shost template for SAS 2.0 HBA devices */
8601 static struct scsi_host_template mpt2sas_driver_template = {
8602 .module = THIS_MODULE,
8603 .name = "Fusion MPT SAS Host",
8604 .proc_name = MPT2SAS_DRIVER_NAME,
8605 .queuecommand = scsih_qcmd,
8606 .target_alloc = scsih_target_alloc,
8607 .slave_alloc = scsih_slave_alloc,
8608 .slave_configure = scsih_slave_configure,
8609 .target_destroy = scsih_target_destroy,
8610 .slave_destroy = scsih_slave_destroy,
8611 .scan_finished = scsih_scan_finished,
8612 .scan_start = scsih_scan_start,
8613 .change_queue_depth = scsih_change_queue_depth,
8614 .eh_abort_handler = scsih_abort,
8615 .eh_device_reset_handler = scsih_dev_reset,
8616 .eh_target_reset_handler = scsih_target_reset,
8617 .eh_host_reset_handler = scsih_host_reset,
8618 .bios_param = scsih_bios_param,
8619 .can_queue = 1,
8620 .this_id = -1,
8621 .sg_tablesize = MPT2SAS_SG_DEPTH,
8622 .max_sectors = 32767,
8623 .cmd_per_lun = 7,
8624 .use_clustering = ENABLE_CLUSTERING,
8625 .shost_attrs = mpt3sas_host_attrs,
8626 .sdev_attrs = mpt3sas_dev_attrs,
8627 .track_queue_depth = 1,
8628 };
8629
8630 /* raid transport support for SAS 2.0 HBA devices */
8631 static struct raid_function_template mpt2sas_raid_functions = {
8632 .cookie = &mpt2sas_driver_template,
8633 .is_raid = scsih_is_raid,
8634 .get_resync = scsih_get_resync,
8635 .get_state = scsih_get_state,
8636 };
8637
8638 /* shost template for SAS 3.0 HBA devices */
8639 static struct scsi_host_template mpt3sas_driver_template = {
8640 .module = THIS_MODULE,
8641 .name = "Fusion MPT SAS Host",
8642 .proc_name = MPT3SAS_DRIVER_NAME,
8643 .queuecommand = scsih_qcmd,
8644 .target_alloc = scsih_target_alloc,
8645 .slave_alloc = scsih_slave_alloc,
8646 .slave_configure = scsih_slave_configure,
8647 .target_destroy = scsih_target_destroy,
8648 .slave_destroy = scsih_slave_destroy,
8649 .scan_finished = scsih_scan_finished,
8650 .scan_start = scsih_scan_start,
8651 .change_queue_depth = scsih_change_queue_depth,
8652 .eh_abort_handler = scsih_abort,
8653 .eh_device_reset_handler = scsih_dev_reset,
8654 .eh_target_reset_handler = scsih_target_reset,
8655 .eh_host_reset_handler = scsih_host_reset,
8656 .bios_param = scsih_bios_param,
8657 .can_queue = 1,
8658 .this_id = -1,
8659 .sg_tablesize = MPT3SAS_SG_DEPTH,
8660 .max_sectors = 32767,
8661 .cmd_per_lun = 7,
8662 .use_clustering = ENABLE_CLUSTERING,
8663 .shost_attrs = mpt3sas_host_attrs,
8664 .sdev_attrs = mpt3sas_dev_attrs,
8665 .track_queue_depth = 1,
8666 };
8667
8668 /* raid transport support for SAS 3.0 HBA devices */
8669 static struct raid_function_template mpt3sas_raid_functions = {
8670 .cookie = &mpt3sas_driver_template,
8671 .is_raid = scsih_is_raid,
8672 .get_resync = scsih_get_resync,
8673 .get_state = scsih_get_state,
8674 };
8675
8676 /**
8677 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8678 * this device belongs to.
8679 * @pdev: PCI device struct
8680 *
8681 * return MPI2_VERSION for SAS 2.0 HBA devices,
8682 * MPI25_VERSION for SAS 3.0 HBA devices, and
8683 * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
8684 */
8685 static u16
8686 _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8687 {
8688
8689 switch (pdev->device) {
8690 case MPI2_MFGPAGE_DEVID_SSS6200:
8691 case MPI2_MFGPAGE_DEVID_SAS2004:
8692 case MPI2_MFGPAGE_DEVID_SAS2008:
8693 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8694 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8695 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8696 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8697 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8698 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8699 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8700 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8701 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8702 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8703 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8704 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8705 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8706 case MPI2_MFGPAGE_DEVID_SAS2308_3:
8707 return MPI2_VERSION;
8708 case MPI25_MFGPAGE_DEVID_SAS3004:
8709 case MPI25_MFGPAGE_DEVID_SAS3008:
8710 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8711 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8712 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8713 case MPI25_MFGPAGE_DEVID_SAS3108_6:
8714 return MPI25_VERSION;
8715 case MPI26_MFGPAGE_DEVID_SAS3216:
8716 case MPI26_MFGPAGE_DEVID_SAS3224:
8717 case MPI26_MFGPAGE_DEVID_SAS3316_1:
8718 case MPI26_MFGPAGE_DEVID_SAS3316_2:
8719 case MPI26_MFGPAGE_DEVID_SAS3316_3:
8720 case MPI26_MFGPAGE_DEVID_SAS3316_4:
8721 case MPI26_MFGPAGE_DEVID_SAS3324_1:
8722 case MPI26_MFGPAGE_DEVID_SAS3324_2:
8723 case MPI26_MFGPAGE_DEVID_SAS3324_3:
8724 case MPI26_MFGPAGE_DEVID_SAS3324_4:
8725 case MPI26_MFGPAGE_DEVID_SAS3508:
8726 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8727 case MPI26_MFGPAGE_DEVID_SAS3408:
8728 case MPI26_MFGPAGE_DEVID_SAS3516:
8729 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8730 case MPI26_MFGPAGE_DEVID_SAS3416:
8731 return MPI26_VERSION;
8732 }
8733 return 0;
8734 }
8735
8736 /**
8737 * _scsih_probe - attach and add scsi host
8738 * @pdev: PCI device struct
8739 * @id: pci device id
8740 *
8741 * Returns 0 success, anything else error.
8742 */
8743 static int
8744 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8745 {
8746 struct MPT3SAS_ADAPTER *ioc;
8747 struct Scsi_Host *shost = NULL;
8748 int rv;
8749 u16 hba_mpi_version;
8750
8751 /* Determine in which MPI version class this pci device belongs */
8752 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8753 if (hba_mpi_version == 0)
8754 return -ENODEV;
8755
8756 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8757 * for other generation HBA's return with -ENODEV
8758 */
8759 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8760 return -ENODEV;
8761
8762 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8763 * for other generation HBA's return with -ENODEV
8764 */
8765 if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
8766 || hba_mpi_version == MPI26_VERSION)))
8767 return -ENODEV;
8768
8769 switch (hba_mpi_version) {
8770 case MPI2_VERSION:
8771 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8772 shost = scsi_host_alloc(&mpt2sas_driver_template,
8773 sizeof(struct MPT3SAS_ADAPTER));
8774 if (!shost)
8775 return -ENODEV;
8776 ioc = shost_priv(shost);
8777 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8778 ioc->hba_mpi_version_belonged = hba_mpi_version;
8779 ioc->id = mpt2_ids++;
8780 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8781 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8782 ioc->is_warpdrive = 1;
8783 ioc->hide_ir_msg = 1;
8784 } else
8785 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8786 break;
8787 case MPI25_VERSION:
8788 case MPI26_VERSION:
8789 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8790 shost = scsi_host_alloc(&mpt3sas_driver_template,
8791 sizeof(struct MPT3SAS_ADAPTER));
8792 if (!shost)
8793 return -ENODEV;
8794 ioc = shost_priv(shost);
8795 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8796 ioc->hba_mpi_version_belonged = hba_mpi_version;
8797 ioc->id = mpt3_ids++;
8798 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
8799 switch (pdev->device) {
8800 case MPI26_MFGPAGE_DEVID_SAS3508:
8801 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8802 case MPI26_MFGPAGE_DEVID_SAS3408:
8803 case MPI26_MFGPAGE_DEVID_SAS3516:
8804 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8805 case MPI26_MFGPAGE_DEVID_SAS3416:
8806 ioc->is_gen35_ioc = 1;
8807 break;
8808 default:
8809 ioc->is_gen35_ioc = 0;
8810 }
8811 if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8812 pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
8813 (ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
8814 ioc->combined_reply_queue = 1;
8815 if (ioc->is_gen35_ioc)
8816 ioc->combined_reply_index_count =
8817 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
8818 else
8819 ioc->combined_reply_index_count =
8820 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
8821 }
8822 break;
8823 default:
8824 return -ENODEV;
8825 }
8826
8827 INIT_LIST_HEAD(&ioc->list);
8828 spin_lock(&gioc_lock);
8829 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8830 spin_unlock(&gioc_lock);
8831 ioc->shost = shost;
8832 ioc->pdev = pdev;
8833 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8834 ioc->tm_cb_idx = tm_cb_idx;
8835 ioc->ctl_cb_idx = ctl_cb_idx;
8836 ioc->base_cb_idx = base_cb_idx;
8837 ioc->port_enable_cb_idx = port_enable_cb_idx;
8838 ioc->transport_cb_idx = transport_cb_idx;
8839 ioc->scsih_cb_idx = scsih_cb_idx;
8840 ioc->config_cb_idx = config_cb_idx;
8841 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8842 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8843 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8844 ioc->logging_level = logging_level;
8845 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8846 /* misc semaphores and spin locks */
8847 mutex_init(&ioc->reset_in_progress_mutex);
8848 /* initializing pci_access_mutex lock */
8849 mutex_init(&ioc->pci_access_mutex);
8850 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8851 spin_lock_init(&ioc->scsi_lookup_lock);
8852 spin_lock_init(&ioc->sas_device_lock);
8853 spin_lock_init(&ioc->sas_node_lock);
8854 spin_lock_init(&ioc->fw_event_lock);
8855 spin_lock_init(&ioc->raid_device_lock);
8856 spin_lock_init(&ioc->diag_trigger_lock);
8857
8858 INIT_LIST_HEAD(&ioc->sas_device_list);
8859 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8860 INIT_LIST_HEAD(&ioc->sas_expander_list);
8861 INIT_LIST_HEAD(&ioc->fw_event_list);
8862 INIT_LIST_HEAD(&ioc->raid_device_list);
8863 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8864 INIT_LIST_HEAD(&ioc->delayed_tr_list);
8865 INIT_LIST_HEAD(&ioc->delayed_sc_list);
8866 INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
8867 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8868 INIT_LIST_HEAD(&ioc->reply_queue_list);
8869
8870 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
8871
8872 /* init shost parameters */
8873 shost->max_cmd_len = 32;
8874 shost->max_lun = max_lun;
8875 shost->transportt = mpt3sas_transport_template;
8876 shost->unique_id = ioc->id;
8877
8878 if (max_sectors != 0xFFFF) {
8879 if (max_sectors < 64) {
8880 shost->max_sectors = 64;
8881 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8882 "for max_sectors, range is 64 to 32767. Assigning "
8883 "value of 64.\n", ioc->name, max_sectors);
8884 } else if (max_sectors > 32767) {
8885 shost->max_sectors = 32767;
8886 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8887 "for max_sectors, range is 64 to 32767. Assigning "
8888 "default value of 32767.\n", ioc->name,
8889 max_sectors);
8890 } else {
8891 shost->max_sectors = max_sectors & 0xFFFE;
8892 pr_info(MPT3SAS_FMT
8893 "The max_sectors value is set to %d\n",
8894 ioc->name, shost->max_sectors);
8895 }
8896 }
8897
8898 /* register EEDP capabilities with SCSI layer */
8899 if (prot_mask > 0)
8900 scsi_host_set_prot(shost, prot_mask);
8901 else
8902 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8903 | SHOST_DIF_TYPE2_PROTECTION
8904 | SHOST_DIF_TYPE3_PROTECTION);
8905
8906 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8907
8908 /* event thread */
8909 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8910 "fw_event_%s%d", ioc->driver_name, ioc->id);
8911 ioc->firmware_event_thread = alloc_ordered_workqueue(
8912 ioc->firmware_event_name, WQ_MEM_RECLAIM);
8913 if (!ioc->firmware_event_thread) {
8914 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8915 ioc->name, __FILE__, __LINE__, __func__);
8916 rv = -ENODEV;
8917 goto out_thread_fail;
8918 }
8919
8920 ioc->is_driver_loading = 1;
8921 if ((mpt3sas_base_attach(ioc))) {
8922 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8923 ioc->name, __FILE__, __LINE__, __func__);
8924 rv = -ENODEV;
8925 goto out_attach_fail;
8926 }
8927
8928 if (ioc->is_warpdrive) {
8929 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8930 ioc->hide_drives = 0;
8931 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8932 ioc->hide_drives = 1;
8933 else {
8934 if (mpt3sas_get_num_volumes(ioc))
8935 ioc->hide_drives = 1;
8936 else
8937 ioc->hide_drives = 0;
8938 }
8939 } else
8940 ioc->hide_drives = 0;
8941
8942 rv = scsi_add_host(shost, &pdev->dev);
8943 if (rv) {
8944 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8945 ioc->name, __FILE__, __LINE__, __func__);
8946 goto out_add_shost_fail;
8947 }
8948
8949 scsi_scan_host(shost);
8950 return 0;
8951 out_add_shost_fail:
8952 mpt3sas_base_detach(ioc);
8953 out_attach_fail:
8954 destroy_workqueue(ioc->firmware_event_thread);
8955 out_thread_fail:
8956 spin_lock(&gioc_lock);
8957 list_del(&ioc->list);
8958 spin_unlock(&gioc_lock);
8959 scsi_host_put(shost);
8960 return rv;
8961 }
8962
8963 #ifdef CONFIG_PM
8964 /**
8965 * scsih_suspend - power management suspend main entry point
8966 * @pdev: PCI device struct
8967 * @state: PM state change to (usually PCI_D3)
8968 *
8969 * Returns 0 success, anything else error.
8970 */
8971 static int
8972 scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8973 {
8974 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8975 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8976 pci_power_t device_state;
8977
8978 mpt3sas_base_stop_watchdog(ioc);
8979 flush_scheduled_work();
8980 scsi_block_requests(shost);
8981 device_state = pci_choose_state(pdev, state);
8982 pr_info(MPT3SAS_FMT
8983 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8984 ioc->name, pdev, pci_name(pdev), device_state);
8985
8986 pci_save_state(pdev);
8987 mpt3sas_base_free_resources(ioc);
8988 pci_set_power_state(pdev, device_state);
8989 return 0;
8990 }
8991
8992 /**
8993 * scsih_resume - power management resume main entry point
8994 * @pdev: PCI device struct
8995 *
8996 * Returns 0 success, anything else error.
8997 */
8998 static int
8999 scsih_resume(struct pci_dev *pdev)
9000 {
9001 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9002 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9003 pci_power_t device_state = pdev->current_state;
9004 int r;
9005
9006 pr_info(MPT3SAS_FMT
9007 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
9008 ioc->name, pdev, pci_name(pdev), device_state);
9009
9010 pci_set_power_state(pdev, PCI_D0);
9011 pci_enable_wake(pdev, PCI_D0, 0);
9012 pci_restore_state(pdev);
9013 ioc->pdev = pdev;
9014 r = mpt3sas_base_map_resources(ioc);
9015 if (r)
9016 return r;
9017
9018 mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
9019 scsi_unblock_requests(shost);
9020 mpt3sas_base_start_watchdog(ioc);
9021 return 0;
9022 }
9023 #endif /* CONFIG_PM */
9024
9025 /**
9026 * scsih_pci_error_detected - Called when a PCI error is detected.
9027 * @pdev: PCI device struct
9028 * @state: PCI channel state
9029 *
9030 * Description: Called when a PCI error is detected.
9031 *
9032 * Return value:
9033 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
9034 */
9035 static pci_ers_result_t
9036 scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9037 {
9038 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9039 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9040
9041 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
9042 ioc->name, state);
9043
9044 switch (state) {
9045 case pci_channel_io_normal:
9046 return PCI_ERS_RESULT_CAN_RECOVER;
9047 case pci_channel_io_frozen:
9048 /* Fatal error, prepare for slot reset */
9049 ioc->pci_error_recovery = 1;
9050 scsi_block_requests(ioc->shost);
9051 mpt3sas_base_stop_watchdog(ioc);
9052 mpt3sas_base_free_resources(ioc);
9053 return PCI_ERS_RESULT_NEED_RESET;
9054 case pci_channel_io_perm_failure:
9055 /* Permanent error, prepare for device removal */
9056 ioc->pci_error_recovery = 1;
9057 mpt3sas_base_stop_watchdog(ioc);
9058 _scsih_flush_running_cmds(ioc);
9059 return PCI_ERS_RESULT_DISCONNECT;
9060 }
9061 return PCI_ERS_RESULT_NEED_RESET;
9062 }
9063
9064 /**
9065 * scsih_pci_slot_reset - Called when PCI slot has been reset.
9066 * @pdev: PCI device struct
9067 *
9068 * Description: This routine is called by the pci error recovery
9069 * code after the PCI slot has been reset, just before we
9070 * should resume normal operations.
9071 */
9072 static pci_ers_result_t
9073 scsih_pci_slot_reset(struct pci_dev *pdev)
9074 {
9075 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9076 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9077 int rc;
9078
9079 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
9080 ioc->name);
9081
9082 ioc->pci_error_recovery = 0;
9083 ioc->pdev = pdev;
9084 pci_restore_state(pdev);
9085 rc = mpt3sas_base_map_resources(ioc);
9086 if (rc)
9087 return PCI_ERS_RESULT_DISCONNECT;
9088
9089 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
9090
9091 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
9092 (rc == 0) ? "success" : "failed");
9093
9094 if (!rc)
9095 return PCI_ERS_RESULT_RECOVERED;
9096 else
9097 return PCI_ERS_RESULT_DISCONNECT;
9098 }
9099
9100 /**
9101 * scsih_pci_resume() - resume normal ops after PCI reset
9102 * @pdev: pointer to PCI device
9103 *
9104 * Called when the error recovery driver tells us that its
9105 * OK to resume normal operation. Use completion to allow
9106 * halted scsi ops to resume.
9107 */
9108 static void
9109 scsih_pci_resume(struct pci_dev *pdev)
9110 {
9111 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9112 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9113
9114 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
9115
9116 pci_cleanup_aer_uncorrect_error_status(pdev);
9117 mpt3sas_base_start_watchdog(ioc);
9118 scsi_unblock_requests(ioc->shost);
9119 }
9120
9121 /**
9122 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
9123 * @pdev: pointer to PCI device
9124 */
9125 static pci_ers_result_t
9126 scsih_pci_mmio_enabled(struct pci_dev *pdev)
9127 {
9128 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9129 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9130
9131 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9132 ioc->name);
9133
9134 /* TODO - dump whatever for debugging purposes */
9135
9136 /* This called only if scsih_pci_error_detected returns
9137 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
9138 * works, no need to reset slot.
9139 */
9140 return PCI_ERS_RESULT_RECOVERED;
9141 }
9142
9143 /**
9144 * scsih__ncq_prio_supp - Check for NCQ command priority support
9145 * @sdev: scsi device struct
9146 *
9147 * This is called when a user indicates they would like to enable
9148 * ncq command priorities. This works only on SATA devices.
9149 */
9150 bool scsih_ncq_prio_supp(struct scsi_device *sdev)
9151 {
9152 unsigned char *buf;
9153 bool ncq_prio_supp = false;
9154
9155 if (!scsi_device_supports_vpd(sdev))
9156 return ncq_prio_supp;
9157
9158 buf = kmalloc(SCSI_VPD_PG_LEN, GFP_KERNEL);
9159 if (!buf)
9160 return ncq_prio_supp;
9161
9162 if (!scsi_get_vpd_page(sdev, 0x89, buf, SCSI_VPD_PG_LEN))
9163 ncq_prio_supp = (buf[213] >> 4) & 1;
9164
9165 kfree(buf);
9166 return ncq_prio_supp;
9167 }
9168 /*
9169 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9170 */
9171 static const struct pci_device_id mpt3sas_pci_table[] = {
9172 /* Spitfire ~ 2004 */
9173 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9174 PCI_ANY_ID, PCI_ANY_ID },
9175 /* Falcon ~ 2008 */
9176 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9177 PCI_ANY_ID, PCI_ANY_ID },
9178 /* Liberator ~ 2108 */
9179 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9180 PCI_ANY_ID, PCI_ANY_ID },
9181 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9182 PCI_ANY_ID, PCI_ANY_ID },
9183 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9184 PCI_ANY_ID, PCI_ANY_ID },
9185 /* Meteor ~ 2116 */
9186 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9187 PCI_ANY_ID, PCI_ANY_ID },
9188 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9189 PCI_ANY_ID, PCI_ANY_ID },
9190 /* Thunderbolt ~ 2208 */
9191 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9192 PCI_ANY_ID, PCI_ANY_ID },
9193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9194 PCI_ANY_ID, PCI_ANY_ID },
9195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9196 PCI_ANY_ID, PCI_ANY_ID },
9197 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9198 PCI_ANY_ID, PCI_ANY_ID },
9199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9200 PCI_ANY_ID, PCI_ANY_ID },
9201 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9202 PCI_ANY_ID, PCI_ANY_ID },
9203 /* Mustang ~ 2308 */
9204 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9205 PCI_ANY_ID, PCI_ANY_ID },
9206 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9207 PCI_ANY_ID, PCI_ANY_ID },
9208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9209 PCI_ANY_ID, PCI_ANY_ID },
9210 /* SSS6200 */
9211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9212 PCI_ANY_ID, PCI_ANY_ID },
9213 /* Fury ~ 3004 and 3008 */
9214 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9215 PCI_ANY_ID, PCI_ANY_ID },
9216 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9217 PCI_ANY_ID, PCI_ANY_ID },
9218 /* Invader ~ 3108 */
9219 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9220 PCI_ANY_ID, PCI_ANY_ID },
9221 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9222 PCI_ANY_ID, PCI_ANY_ID },
9223 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9224 PCI_ANY_ID, PCI_ANY_ID },
9225 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9226 PCI_ANY_ID, PCI_ANY_ID },
9227 /* Cutlass ~ 3216 and 3224 */
9228 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9229 PCI_ANY_ID, PCI_ANY_ID },
9230 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9231 PCI_ANY_ID, PCI_ANY_ID },
9232 /* Intruder ~ 3316 and 3324 */
9233 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9234 PCI_ANY_ID, PCI_ANY_ID },
9235 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9236 PCI_ANY_ID, PCI_ANY_ID },
9237 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9238 PCI_ANY_ID, PCI_ANY_ID },
9239 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9240 PCI_ANY_ID, PCI_ANY_ID },
9241 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9242 PCI_ANY_ID, PCI_ANY_ID },
9243 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9244 PCI_ANY_ID, PCI_ANY_ID },
9245 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9246 PCI_ANY_ID, PCI_ANY_ID },
9247 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9248 PCI_ANY_ID, PCI_ANY_ID },
9249 /* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
9250 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
9251 PCI_ANY_ID, PCI_ANY_ID },
9252 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
9253 PCI_ANY_ID, PCI_ANY_ID },
9254 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
9255 PCI_ANY_ID, PCI_ANY_ID },
9256 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
9257 PCI_ANY_ID, PCI_ANY_ID },
9258 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
9259 PCI_ANY_ID, PCI_ANY_ID },
9260 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
9261 PCI_ANY_ID, PCI_ANY_ID },
9262 {0} /* Terminating entry */
9263 };
9264 MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9265
9266 static struct pci_error_handlers _mpt3sas_err_handler = {
9267 .error_detected = scsih_pci_error_detected,
9268 .mmio_enabled = scsih_pci_mmio_enabled,
9269 .slot_reset = scsih_pci_slot_reset,
9270 .resume = scsih_pci_resume,
9271 };
9272
9273 static struct pci_driver mpt3sas_driver = {
9274 .name = MPT3SAS_DRIVER_NAME,
9275 .id_table = mpt3sas_pci_table,
9276 .probe = _scsih_probe,
9277 .remove = scsih_remove,
9278 .shutdown = scsih_shutdown,
9279 .err_handler = &_mpt3sas_err_handler,
9280 #ifdef CONFIG_PM
9281 .suspend = scsih_suspend,
9282 .resume = scsih_resume,
9283 #endif
9284 };
9285
9286 /**
9287 * scsih_init - main entry point for this driver.
9288 *
9289 * Returns 0 success, anything else error.
9290 */
9291 static int
9292 scsih_init(void)
9293 {
9294 mpt2_ids = 0;
9295 mpt3_ids = 0;
9296
9297 mpt3sas_base_initialize_callback_handler();
9298
9299 /* queuecommand callback hander */
9300 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9301
9302 /* task management callback handler */
9303 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9304
9305 /* base internal commands callback handler */
9306 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9307 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9308 mpt3sas_port_enable_done);
9309
9310 /* transport internal commands callback handler */
9311 transport_cb_idx = mpt3sas_base_register_callback_handler(
9312 mpt3sas_transport_done);
9313
9314 /* scsih internal commands callback handler */
9315 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9316
9317 /* configuration page API internal commands callback handler */
9318 config_cb_idx = mpt3sas_base_register_callback_handler(
9319 mpt3sas_config_done);
9320
9321 /* ctl module callback handler */
9322 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9323
9324 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9325 _scsih_tm_tr_complete);
9326
9327 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9328 _scsih_tm_volume_tr_complete);
9329
9330 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9331 _scsih_sas_control_complete);
9332
9333 return 0;
9334 }
9335
9336 /**
9337 * scsih_exit - exit point for this driver (when it is a module).
9338 *
9339 * Returns 0 success, anything else error.
9340 */
9341 static void
9342 scsih_exit(void)
9343 {
9344
9345 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9346 mpt3sas_base_release_callback_handler(tm_cb_idx);
9347 mpt3sas_base_release_callback_handler(base_cb_idx);
9348 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9349 mpt3sas_base_release_callback_handler(transport_cb_idx);
9350 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9351 mpt3sas_base_release_callback_handler(config_cb_idx);
9352 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9353
9354 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9355 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9356 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9357
9358 /* raid transport support */
9359 if (hbas_to_enumerate != 1)
9360 raid_class_release(mpt3sas_raid_template);
9361 if (hbas_to_enumerate != 2)
9362 raid_class_release(mpt2sas_raid_template);
9363 sas_release_transport(mpt3sas_transport_template);
9364 }
9365
9366 /**
9367 * _mpt3sas_init - main entry point for this driver.
9368 *
9369 * Returns 0 success, anything else error.
9370 */
9371 static int __init
9372 _mpt3sas_init(void)
9373 {
9374 int error;
9375
9376 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9377 MPT3SAS_DRIVER_VERSION);
9378
9379 mpt3sas_transport_template =
9380 sas_attach_transport(&mpt3sas_transport_functions);
9381 if (!mpt3sas_transport_template)
9382 return -ENODEV;
9383
9384 /* No need attach mpt3sas raid functions template
9385 * if hbas_to_enumarate value is one.
9386 */
9387 if (hbas_to_enumerate != 1) {
9388 mpt3sas_raid_template =
9389 raid_class_attach(&mpt3sas_raid_functions);
9390 if (!mpt3sas_raid_template) {
9391 sas_release_transport(mpt3sas_transport_template);
9392 return -ENODEV;
9393 }
9394 }
9395
9396 /* No need to attach mpt2sas raid functions template
9397 * if hbas_to_enumarate value is two
9398 */
9399 if (hbas_to_enumerate != 2) {
9400 mpt2sas_raid_template =
9401 raid_class_attach(&mpt2sas_raid_functions);
9402 if (!mpt2sas_raid_template) {
9403 sas_release_transport(mpt3sas_transport_template);
9404 return -ENODEV;
9405 }
9406 }
9407
9408 error = scsih_init();
9409 if (error) {
9410 scsih_exit();
9411 return error;
9412 }
9413
9414 mpt3sas_ctl_init(hbas_to_enumerate);
9415
9416 error = pci_register_driver(&mpt3sas_driver);
9417 if (error)
9418 scsih_exit();
9419
9420 return error;
9421 }
9422
9423 /**
9424 * _mpt3sas_exit - exit point for this driver (when it is a module).
9425 *
9426 */
9427 static void __exit
9428 _mpt3sas_exit(void)
9429 {
9430 pr_info("mpt3sas version %s unloading\n",
9431 MPT3SAS_DRIVER_VERSION);
9432
9433 pci_unregister_driver(&mpt3sas_driver);
9434
9435 mpt3sas_ctl_exit(hbas_to_enumerate);
9436
9437 scsih_exit();
9438 }
9439
9440 module_init(_mpt3sas_init);
9441 module_exit(_mpt3sas_exit);