[SCSI] mpt2sas: Adding MPI Headers - revision L
[linux-2.6-block.git] / drivers / scsi / mpt2sas / mpt2sas_scsih.c
CommitLineData
635374e7
EM
1/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
19d3ebe3 5 * Copyright (C) 2007-2009 LSI Corporation
635374e7
EM
6 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
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
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
ba33fadf 71LIST_HEAD(mpt2sas_ioc_list);
635374e7
EM
72
73/* local parameters */
635374e7
EM
74static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
77e63ed4
KD
82static u8 tm_tr_cb_idx = -1 ;
83static u8 tm_sas_control_cb_idx = -1;
84
635374e7 85/* command line options */
ba33fadf 86static u32 logging_level;
635374e7
EM
87MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
88 "(default=0)");
89
90/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
91#define MPT2SAS_MAX_LUN (16895)
92static int max_lun = MPT2SAS_MAX_LUN;
93module_param(max_lun, int, 0);
94MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
95
96/**
97 * struct sense_info - common structure for obtaining sense keys
98 * @skey: sense key
99 * @asc: additional sense code
100 * @ascq: additional sense code qualifier
101 */
102struct sense_info {
103 u8 skey;
104 u8 asc;
105 u8 ascq;
106};
107
108
635374e7
EM
109/**
110 * struct fw_event_work - firmware event struct
111 * @list: link list framework
112 * @work: work object (ioc->fault_reset_work_q)
113 * @ioc: per adapter object
114 * @VF_ID: virtual function id
7b936b02 115 * @VP_ID: virtual port id
635374e7
EM
116 * @host_reset_handling: handling events during host reset
117 * @ignore: flag meaning this event has been marked to ignore
118 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
119 * @event_data: reply event data payload follows
120 *
121 * This object stored on ioc->fw_event_list.
122 */
123struct fw_event_work {
124 struct list_head list;
6f92a7a0 125 struct work_struct work;
635374e7
EM
126 struct MPT2SAS_ADAPTER *ioc;
127 u8 VF_ID;
7b936b02 128 u8 VP_ID;
635374e7
EM
129 u8 host_reset_handling;
130 u8 ignore;
131 u16 event;
132 void *event_data;
133};
134
135/**
136 * struct _scsi_io_transfer - scsi io transfer
137 * @handle: sas device handle (assigned by firmware)
138 * @is_raid: flag set for hidden raid components
139 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
140 * @data_length: data transfer length
141 * @data_dma: dma pointer to data
142 * @sense: sense data
143 * @lun: lun number
144 * @cdb_length: cdb length
145 * @cdb: cdb contents
635374e7 146 * @timeout: timeout for this command
7b936b02
KD
147 * @VF_ID: virtual function id
148 * @VP_ID: virtual port id
149 * @valid_reply: flag set for reply message
635374e7
EM
150 * @sense_length: sense length
151 * @ioc_status: ioc status
152 * @scsi_state: scsi state
153 * @scsi_status: scsi staus
154 * @log_info: log information
155 * @transfer_length: data length transfer when there is a reply message
156 *
157 * Used for sending internal scsi commands to devices within this module.
158 * Refer to _scsi_send_scsi_io().
159 */
160struct _scsi_io_transfer {
161 u16 handle;
162 u8 is_raid;
163 enum dma_data_direction dir;
164 u32 data_length;
165 dma_addr_t data_dma;
166 u8 sense[SCSI_SENSE_BUFFERSIZE];
167 u32 lun;
168 u8 cdb_length;
169 u8 cdb[32];
170 u8 timeout;
7b936b02
KD
171 u8 VF_ID;
172 u8 VP_ID;
635374e7
EM
173 u8 valid_reply;
174 /* the following bits are only valid when 'valid_reply = 1' */
175 u32 sense_length;
176 u16 ioc_status;
177 u8 scsi_state;
178 u8 scsi_status;
179 u32 log_info;
180 u32 transfer_length;
181};
182
183/*
184 * The pci device ids are defined in mpi/mpi2_cnfg.h.
185 */
186static struct pci_device_id scsih_pci_table[] = {
187 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
188 PCI_ANY_ID, PCI_ANY_ID },
189 /* Falcon ~ 2008*/
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
191 PCI_ANY_ID, PCI_ANY_ID },
192 /* Liberator ~ 2108 */
193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
194 PCI_ANY_ID, PCI_ANY_ID },
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
196 PCI_ANY_ID, PCI_ANY_ID },
197 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
198 PCI_ANY_ID, PCI_ANY_ID },
db27136a 199 /* Meteor ~ 2116 */
635374e7
EM
200 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
201 PCI_ANY_ID, PCI_ANY_ID },
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
203 PCI_ANY_ID, PCI_ANY_ID },
db27136a
KD
204 /* Thunderbolt ~ 2208 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
210 PCI_ANY_ID, PCI_ANY_ID },
211 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
212 PCI_ANY_ID, PCI_ANY_ID },
213 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
214 PCI_ANY_ID, PCI_ANY_ID },
215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
220 PCI_ANY_ID, PCI_ANY_ID },
635374e7
EM
221 {0} /* Terminating entry */
222};
223MODULE_DEVICE_TABLE(pci, scsih_pci_table);
224
225/**
d5d135b3 226 * _scsih_set_debug_level - global setting of ioc->logging_level.
635374e7
EM
227 *
228 * Note: The logging levels are defined in mpt2sas_debug.h.
229 */
230static int
d5d135b3 231_scsih_set_debug_level(const char *val, struct kernel_param *kp)
635374e7
EM
232{
233 int ret = param_set_int(val, kp);
234 struct MPT2SAS_ADAPTER *ioc;
235
236 if (ret)
237 return ret;
238
239 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
ba33fadf 240 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
635374e7
EM
241 ioc->logging_level = logging_level;
242 return 0;
243}
d5d135b3 244module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
635374e7
EM
245 &logging_level, 0644);
246
247/**
248 * _scsih_srch_boot_sas_address - search based on sas_address
249 * @sas_address: sas address
250 * @boot_device: boot device object from bios page 2
251 *
252 * Returns 1 when there's a match, 0 means no match.
253 */
254static inline int
255_scsih_srch_boot_sas_address(u64 sas_address,
256 Mpi2BootDeviceSasWwid_t *boot_device)
257{
258 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
259}
260
261/**
262 * _scsih_srch_boot_device_name - search based on device name
263 * @device_name: device name specified in INDENTIFY fram
264 * @boot_device: boot device object from bios page 2
265 *
266 * Returns 1 when there's a match, 0 means no match.
267 */
268static inline int
269_scsih_srch_boot_device_name(u64 device_name,
270 Mpi2BootDeviceDeviceName_t *boot_device)
271{
272 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
273}
274
275/**
276 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
277 * @enclosure_logical_id: enclosure logical id
278 * @slot_number: slot number
279 * @boot_device: boot device object from bios page 2
280 *
281 * Returns 1 when there's a match, 0 means no match.
282 */
283static inline int
284_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
285 Mpi2BootDeviceEnclosureSlot_t *boot_device)
286{
287 return (enclosure_logical_id == le64_to_cpu(boot_device->
288 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
289 SlotNumber)) ? 1 : 0;
290}
291
292/**
293 * _scsih_is_boot_device - search for matching boot device.
294 * @sas_address: sas address
295 * @device_name: device name specified in INDENTIFY fram
296 * @enclosure_logical_id: enclosure logical id
297 * @slot_number: slot number
298 * @form: specifies boot device form
299 * @boot_device: boot device object from bios page 2
300 *
301 * Returns 1 when there's a match, 0 means no match.
302 */
303static int
304_scsih_is_boot_device(u64 sas_address, u64 device_name,
305 u64 enclosure_logical_id, u16 slot, u8 form,
306 Mpi2BiosPage2BootDevice_t *boot_device)
307{
308 int rc = 0;
309
310 switch (form) {
311 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
312 if (!sas_address)
313 break;
314 rc = _scsih_srch_boot_sas_address(
315 sas_address, &boot_device->SasWwid);
316 break;
317 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
318 if (!enclosure_logical_id)
319 break;
320 rc = _scsih_srch_boot_encl_slot(
321 enclosure_logical_id,
322 slot, &boot_device->EnclosureSlot);
323 break;
324 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
325 if (!device_name)
326 break;
327 rc = _scsih_srch_boot_device_name(
328 device_name, &boot_device->DeviceName);
329 break;
330 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
331 break;
332 }
333
334 return rc;
335}
336
c5e039be
KD
337/**
338 * _scsih_get_sas_address - set the sas_address for given device handle
339 * @handle: device handle
340 * @sas_address: sas address
341 *
342 * Returns 0 success, non-zero when failure
343 */
344static int
345_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
346 u64 *sas_address)
347{
348 Mpi2SasDevicePage0_t sas_device_pg0;
349 Mpi2ConfigReply_t mpi_reply;
350 u32 ioc_status;
351
352 if (handle <= ioc->sas_hba.num_phys) {
353 *sas_address = ioc->sas_hba.sas_address;
354 return 0;
355 } else
356 *sas_address = 0;
357
358 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
359 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
360 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
361 ioc->name, __FILE__, __LINE__, __func__);
362 return -ENXIO;
363 }
364
365 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
366 MPI2_IOCSTATUS_MASK;
367 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
368 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
369 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
370 __FILE__, __LINE__, __func__);
371 return -EIO;
372 }
373
374 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
375 return 0;
376}
377
635374e7
EM
378/**
379 * _scsih_determine_boot_device - determine boot device.
380 * @ioc: per adapter object
381 * @device: either sas_device or raid_device object
382 * @is_raid: [flag] 1 = raid object, 0 = sas object
383 *
384 * Determines whether this device should be first reported device to
385 * to scsi-ml or sas transport, this purpose is for persistant boot device.
386 * There are primary, alternate, and current entries in bios page 2. The order
387 * priority is primary, alternate, then current. This routine saves
388 * the corresponding device object and is_raid flag in the ioc object.
389 * The saved data to be used later in _scsih_probe_boot_devices().
390 */
391static void
392_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
393 void *device, u8 is_raid)
394{
395 struct _sas_device *sas_device;
396 struct _raid_device *raid_device;
397 u64 sas_address;
398 u64 device_name;
399 u64 enclosure_logical_id;
400 u16 slot;
401
402 /* only process this function when driver loads */
403 if (!ioc->wait_for_port_enable_to_complete)
404 return;
405
406 if (!is_raid) {
407 sas_device = device;
408 sas_address = sas_device->sas_address;
409 device_name = sas_device->device_name;
410 enclosure_logical_id = sas_device->enclosure_logical_id;
411 slot = sas_device->slot;
412 } else {
413 raid_device = device;
414 sas_address = raid_device->wwid;
415 device_name = 0;
416 enclosure_logical_id = 0;
417 slot = 0;
418 }
419
420 if (!ioc->req_boot_device.device) {
421 if (_scsih_is_boot_device(sas_address, device_name,
422 enclosure_logical_id, slot,
423 (ioc->bios_pg2.ReqBootDeviceForm &
424 MPI2_BIOSPAGE2_FORM_MASK),
425 &ioc->bios_pg2.RequestedBootDevice)) {
426 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
427 "%s: req_boot_device(0x%016llx)\n",
428 ioc->name, __func__,
429 (unsigned long long)sas_address));
430 ioc->req_boot_device.device = device;
431 ioc->req_boot_device.is_raid = is_raid;
432 }
433 }
434
435 if (!ioc->req_alt_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqAltBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedAltBootDevice)) {
441 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
442 "%s: req_alt_boot_device(0x%016llx)\n",
443 ioc->name, __func__,
444 (unsigned long long)sas_address));
445 ioc->req_alt_boot_device.device = device;
446 ioc->req_alt_boot_device.is_raid = is_raid;
447 }
448 }
449
450 if (!ioc->current_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.CurrentBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.CurrentBootDevice)) {
456 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
457 "%s: current_boot_device(0x%016llx)\n",
458 ioc->name, __func__,
459 (unsigned long long)sas_address));
460 ioc->current_boot_device.device = device;
461 ioc->current_boot_device.is_raid = is_raid;
462 }
463 }
464}
465
466/**
467 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
468 * @ioc: per adapter object
469 * @sas_address: sas address
470 * Context: Calling function should acquire ioc->sas_device_lock
471 *
472 * This searches for sas_device based on sas_address, then return sas_device
473 * object.
474 */
475struct _sas_device *
476mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
477 u64 sas_address)
478{
479 struct _sas_device *sas_device, *r;
480
481 r = NULL;
482 /* check the sas_device_init_list */
483 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
484 list) {
485 if (sas_device->sas_address != sas_address)
486 continue;
487 r = sas_device;
488 goto out;
489 }
490
491 /* then check the sas_device_list */
492 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
493 if (sas_device->sas_address != sas_address)
494 continue;
495 r = sas_device;
496 goto out;
497 }
498 out:
499 return r;
500}
501
502/**
503 * _scsih_sas_device_find_by_handle - sas device search
504 * @ioc: per adapter object
505 * @handle: sas device handle (assigned by firmware)
506 * Context: Calling function should acquire ioc->sas_device_lock
507 *
508 * This searches for sas_device based on sas_address, then return sas_device
509 * object.
510 */
511static struct _sas_device *
512_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
513{
514 struct _sas_device *sas_device, *r;
515
516 r = NULL;
517 if (ioc->wait_for_port_enable_to_complete) {
518 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
519 list) {
520 if (sas_device->handle != handle)
521 continue;
522 r = sas_device;
523 goto out;
524 }
525 } else {
526 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
527 if (sas_device->handle != handle)
528 continue;
529 r = sas_device;
530 goto out;
531 }
532 }
533
534 out:
535 return r;
536}
537
538/**
539 * _scsih_sas_device_remove - remove sas_device from list.
540 * @ioc: per adapter object
541 * @sas_device: the sas_device object
542 * Context: This function will acquire ioc->sas_device_lock.
543 *
544 * Removing object and freeing associated memory from the ioc->sas_device_list.
545 */
546static void
547_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
548 struct _sas_device *sas_device)
549{
550 unsigned long flags;
551
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 list_del(&sas_device->list);
554 memset(sas_device, 0, sizeof(struct _sas_device));
555 kfree(sas_device);
556 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
557}
558
559/**
560 * _scsih_sas_device_add - insert sas_device to the list.
561 * @ioc: per adapter object
562 * @sas_device: the sas_device object
563 * Context: This function will acquire ioc->sas_device_lock.
564 *
565 * Adding new object to the ioc->sas_device_list.
566 */
567static void
568_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
569 struct _sas_device *sas_device)
570{
571 unsigned long flags;
635374e7
EM
572
573 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
574 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
575 sas_device->handle, (unsigned long long)sas_device->sas_address));
576
577 spin_lock_irqsave(&ioc->sas_device_lock, flags);
578 list_add_tail(&sas_device->list, &ioc->sas_device_list);
579 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
580
c5e039be
KD
581 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
582 sas_device->sas_address_parent))
635374e7 583 _scsih_sas_device_remove(ioc, sas_device);
635374e7
EM
584}
585
586/**
587 * _scsih_sas_device_init_add - insert sas_device to the list.
588 * @ioc: per adapter object
589 * @sas_device: the sas_device object
590 * Context: This function will acquire ioc->sas_device_lock.
591 *
592 * Adding new object at driver load time to the ioc->sas_device_init_list.
593 */
594static void
595_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
596 struct _sas_device *sas_device)
597{
598 unsigned long flags;
599
600 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
601 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
602 sas_device->handle, (unsigned long long)sas_device->sas_address));
603
604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
605 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
607 _scsih_determine_boot_device(ioc, sas_device, 0);
608}
609
635374e7
EM
610/**
611 * _scsih_raid_device_find_by_id - raid device search
612 * @ioc: per adapter object
613 * @id: sas device target id
614 * @channel: sas device channel
615 * Context: Calling function should acquire ioc->raid_device_lock
616 *
617 * This searches for raid_device based on target id, then return raid_device
618 * object.
619 */
620static struct _raid_device *
621_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
622{
623 struct _raid_device *raid_device, *r;
624
625 r = NULL;
626 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
627 if (raid_device->id == id && raid_device->channel == channel) {
628 r = raid_device;
629 goto out;
630 }
631 }
632
633 out:
634 return r;
635}
636
637/**
638 * _scsih_raid_device_find_by_handle - raid device search
639 * @ioc: per adapter object
640 * @handle: sas device handle (assigned by firmware)
641 * Context: Calling function should acquire ioc->raid_device_lock
642 *
643 * This searches for raid_device based on handle, then return raid_device
644 * object.
645 */
646static struct _raid_device *
647_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
648{
649 struct _raid_device *raid_device, *r;
650
651 r = NULL;
652 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
653 if (raid_device->handle != handle)
654 continue;
655 r = raid_device;
656 goto out;
657 }
658
659 out:
660 return r;
661}
662
663/**
664 * _scsih_raid_device_find_by_wwid - raid device search
665 * @ioc: per adapter object
666 * @handle: sas device handle (assigned by firmware)
667 * Context: Calling function should acquire ioc->raid_device_lock
668 *
669 * This searches for raid_device based on wwid, then return raid_device
670 * object.
671 */
672static struct _raid_device *
673_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
674{
675 struct _raid_device *raid_device, *r;
676
677 r = NULL;
678 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
679 if (raid_device->wwid != wwid)
680 continue;
681 r = raid_device;
682 goto out;
683 }
684
685 out:
686 return r;
687}
688
689/**
690 * _scsih_raid_device_add - add raid_device object
691 * @ioc: per adapter object
692 * @raid_device: raid_device object
693 *
694 * This is added to the raid_device_list link list.
695 */
696static void
697_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
698 struct _raid_device *raid_device)
699{
700 unsigned long flags;
701
702 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
703 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
704 raid_device->handle, (unsigned long long)raid_device->wwid));
705
706 spin_lock_irqsave(&ioc->raid_device_lock, flags);
707 list_add_tail(&raid_device->list, &ioc->raid_device_list);
708 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
709}
710
711/**
712 * _scsih_raid_device_remove - delete raid_device object
713 * @ioc: per adapter object
714 * @raid_device: raid_device object
715 *
716 * This is removed from the raid_device_list link list.
717 */
718static void
719_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
720 struct _raid_device *raid_device)
721{
722 unsigned long flags;
723
724 spin_lock_irqsave(&ioc->raid_device_lock, flags);
725 list_del(&raid_device->list);
726 memset(raid_device, 0, sizeof(struct _raid_device));
727 kfree(raid_device);
728 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
729}
730
c5e039be
KD
731/**
732 * mpt2sas_scsih_expander_find_by_handle - expander device search
733 * @ioc: per adapter object
734 * @handle: expander handle (assigned by firmware)
735 * Context: Calling function should acquire ioc->sas_device_lock
736 *
737 * This searches for expander device based on handle, then returns the
738 * sas_node object.
739 */
740struct _sas_node *
741mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
742{
743 struct _sas_node *sas_expander, *r;
744
745 r = NULL;
746 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
747 if (sas_expander->handle != handle)
748 continue;
749 r = sas_expander;
750 goto out;
751 }
752 out:
753 return r;
754}
755
635374e7
EM
756/**
757 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
758 * @ioc: per adapter object
759 * @sas_address: sas address
760 * Context: Calling function should acquire ioc->sas_node_lock.
761 *
762 * This searches for expander device based on sas_address, then returns the
763 * sas_node object.
764 */
765struct _sas_node *
766mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
767 u64 sas_address)
768{
769 struct _sas_node *sas_expander, *r;
770
771 r = NULL;
772 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
773 if (sas_expander->sas_address != sas_address)
774 continue;
775 r = sas_expander;
776 goto out;
777 }
778 out:
779 return r;
780}
781
782/**
783 * _scsih_expander_node_add - insert expander device to the list.
784 * @ioc: per adapter object
785 * @sas_expander: the sas_device object
786 * Context: This function will acquire ioc->sas_node_lock.
787 *
788 * Adding new object to the ioc->sas_expander_list.
789 *
790 * Return nothing.
791 */
792static void
793_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
794 struct _sas_node *sas_expander)
795{
796 unsigned long flags;
797
798 spin_lock_irqsave(&ioc->sas_node_lock, flags);
799 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
800 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
801}
802
803/**
804 * _scsih_is_end_device - determines if device is an end device
805 * @device_info: bitfield providing information about the device.
806 * Context: none
807 *
808 * Returns 1 if end device.
809 */
810static int
811_scsih_is_end_device(u32 device_info)
812{
813 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
814 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
816 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
817 return 1;
818 else
819 return 0;
820}
821
822/**
595bb0bd 823 * mptscsih_get_scsi_lookup - returns scmd entry
635374e7
EM
824 * @ioc: per adapter object
825 * @smid: system request message index
635374e7
EM
826 *
827 * Returns the smid stored scmd pointer.
828 */
829static struct scsi_cmnd *
830_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
831{
595bb0bd 832 return ioc->scsi_lookup[smid - 1].scmd;
635374e7
EM
833}
834
835/**
836 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
837 * @ioc: per adapter object
838 * @smid: system request message index
839 * @scmd: pointer to scsi command object
840 * Context: This function will acquire ioc->scsi_lookup_lock.
841 *
842 * This will search for a scmd pointer in the scsi_lookup array,
843 * returning the revelent smid. A returned value of zero means invalid.
844 */
845static u16
846_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
847 *scmd)
848{
849 u16 smid;
850 unsigned long flags;
851 int i;
852
853 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
854 smid = 0;
595bb0bd 855 for (i = 0; i < ioc->scsiio_depth; i++) {
635374e7 856 if (ioc->scsi_lookup[i].scmd == scmd) {
595bb0bd 857 smid = ioc->scsi_lookup[i].smid;
635374e7
EM
858 goto out;
859 }
860 }
861 out:
862 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
863 return smid;
864}
865
866/**
867 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
868 * @ioc: per adapter object
869 * @id: target id
870 * @channel: channel
871 * Context: This function will acquire ioc->scsi_lookup_lock.
872 *
873 * This will search for a matching channel:id in the scsi_lookup array,
874 * returning 1 if found.
875 */
876static u8
877_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
878 int channel)
879{
880 u8 found;
881 unsigned long flags;
882 int i;
883
884 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
885 found = 0;
595bb0bd 886 for (i = 0 ; i < ioc->scsiio_depth; i++) {
635374e7
EM
887 if (ioc->scsi_lookup[i].scmd &&
888 (ioc->scsi_lookup[i].scmd->device->id == id &&
889 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
890 found = 1;
891 goto out;
892 }
893 }
894 out:
895 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
896 return found;
897}
898
993e0da7
EM
899/**
900 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
901 * @ioc: per adapter object
902 * @id: target id
903 * @lun: lun number
904 * @channel: channel
905 * Context: This function will acquire ioc->scsi_lookup_lock.
906 *
907 * This will search for a matching channel:id:lun in the scsi_lookup array,
908 * returning 1 if found.
909 */
910static u8
911_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
912 unsigned int lun, int channel)
913{
914 u8 found;
915 unsigned long flags;
916 int i;
917
918 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
919 found = 0;
595bb0bd 920 for (i = 0 ; i < ioc->scsiio_depth; i++) {
993e0da7
EM
921 if (ioc->scsi_lookup[i].scmd &&
922 (ioc->scsi_lookup[i].scmd->device->id == id &&
923 ioc->scsi_lookup[i].scmd->device->channel == channel &&
924 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
925 found = 1;
926 goto out;
927 }
928 }
929 out:
930 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
931 return found;
932}
933
635374e7
EM
934/**
935 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
936 * @ioc: per adapter object
937 * @smid: system request message index
938 *
939 * Returns phys pointer to chain buffer.
940 */
941static dma_addr_t
942_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943{
944 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
945 ioc->chains_needed_per_io));
946}
947
948/**
949 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
950 * @ioc: per adapter object
951 * @smid: system request message index
952 *
953 * Returns virt pointer to chain buffer.
954 */
955static void *
956_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
957{
958 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
959 ioc->chains_needed_per_io)));
960}
961
962/**
963 * _scsih_build_scatter_gather - main sg creation routine
964 * @ioc: per adapter object
965 * @scmd: scsi command
966 * @smid: system request message index
967 * Context: none.
968 *
969 * The main routine that builds scatter gather table from a given
970 * scsi request sent via the .queuecommand main handler.
971 *
972 * Returns 0 success, anything else error
973 */
974static int
975_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
976 struct scsi_cmnd *scmd, u16 smid)
977{
978 Mpi2SCSIIORequest_t *mpi_request;
979 dma_addr_t chain_dma;
980 struct scatterlist *sg_scmd;
981 void *sg_local, *chain;
982 u32 chain_offset;
983 u32 chain_length;
984 u32 chain_flags;
985 u32 sges_left;
986 u32 sges_in_segment;
987 u32 sgl_flags;
988 u32 sgl_flags_last_element;
989 u32 sgl_flags_end_buffer;
990
991 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
992
993 /* init scatter gather flags */
994 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
995 if (scmd->sc_data_direction == DMA_TO_DEVICE)
996 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
997 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
998 << MPI2_SGE_FLAGS_SHIFT;
999 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1000 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1001 << MPI2_SGE_FLAGS_SHIFT;
1002 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1003
1004 sg_scmd = scsi_sglist(scmd);
1005 sges_left = scsi_dma_map(scmd);
1006 if (!sges_left) {
1007 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1008 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1009 return -ENOMEM;
1010 }
1011
1012 sg_local = &mpi_request->SGL;
1013 sges_in_segment = ioc->max_sges_in_main_message;
1014 if (sges_left <= sges_in_segment)
1015 goto fill_in_last_segment;
1016
1017 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1018 (sges_in_segment * ioc->sge_size))/4;
1019
1020 /* fill in main message segment when there is a chain following */
1021 while (sges_in_segment) {
1022 if (sges_in_segment == 1)
1023 ioc->base_add_sg_single(sg_local,
1024 sgl_flags_last_element | sg_dma_len(sg_scmd),
1025 sg_dma_address(sg_scmd));
1026 else
1027 ioc->base_add_sg_single(sg_local, sgl_flags |
1028 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1029 sg_scmd = sg_next(sg_scmd);
1030 sg_local += ioc->sge_size;
1031 sges_left--;
1032 sges_in_segment--;
1033 }
1034
1035 /* initializing the chain flags and pointers */
1036 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1037 chain = _scsih_get_chain_buffer(ioc, smid);
1038 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1039 do {
1040 sges_in_segment = (sges_left <=
1041 ioc->max_sges_in_chain_message) ? sges_left :
1042 ioc->max_sges_in_chain_message;
1043 chain_offset = (sges_left == sges_in_segment) ?
1044 0 : (sges_in_segment * ioc->sge_size)/4;
1045 chain_length = sges_in_segment * ioc->sge_size;
1046 if (chain_offset) {
1047 chain_offset = chain_offset <<
1048 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1049 chain_length += ioc->sge_size;
1050 }
1051 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1052 chain_length, chain_dma);
1053 sg_local = chain;
1054 if (!chain_offset)
1055 goto fill_in_last_segment;
1056
1057 /* fill in chain segments */
1058 while (sges_in_segment) {
1059 if (sges_in_segment == 1)
1060 ioc->base_add_sg_single(sg_local,
1061 sgl_flags_last_element |
1062 sg_dma_len(sg_scmd),
1063 sg_dma_address(sg_scmd));
1064 else
1065 ioc->base_add_sg_single(sg_local, sgl_flags |
1066 sg_dma_len(sg_scmd),
1067 sg_dma_address(sg_scmd));
1068 sg_scmd = sg_next(sg_scmd);
1069 sg_local += ioc->sge_size;
1070 sges_left--;
1071 sges_in_segment--;
1072 }
1073
1074 chain_dma += ioc->request_sz;
1075 chain += ioc->request_sz;
1076 } while (1);
1077
1078
1079 fill_in_last_segment:
1080
1081 /* fill the last segment */
1082 while (sges_left) {
1083 if (sges_left == 1)
1084 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1085 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1086 else
1087 ioc->base_add_sg_single(sg_local, sgl_flags |
1088 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1089 sg_scmd = sg_next(sg_scmd);
1090 sg_local += ioc->sge_size;
1091 sges_left--;
1092 }
1093
1094 return 0;
1095}
1096
1097/**
d5d135b3 1098 * _scsih_change_queue_depth - setting device queue depth
635374e7
EM
1099 * @sdev: scsi device struct
1100 * @qdepth: requested queue depth
1101 *
1102 * Returns queue depth.
1103 */
1104static int
d5d135b3 1105_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
635374e7
EM
1106{
1107 struct Scsi_Host *shost = sdev->host;
1108 int max_depth;
1109 int tag_type;
1110
1111 max_depth = shost->can_queue;
1112 if (!sdev->tagged_supported)
1113 max_depth = 1;
1114 if (qdepth > max_depth)
1115 qdepth = max_depth;
1116 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1117 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1118
1119 if (sdev->inquiry_len > 7)
1120 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1121 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1122 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1123 sdev->ordered_tags, sdev->scsi_level,
1124 (sdev->inquiry[7] & 2) >> 1);
1125
1126 return sdev->queue_depth;
1127}
1128
1129/**
595bb0bd 1130 * _scsih_change_queue_type - changing device queue tag type
635374e7
EM
1131 * @sdev: scsi device struct
1132 * @tag_type: requested tag type
1133 *
1134 * Returns queue tag type.
1135 */
1136static int
d5d135b3 1137_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
635374e7
EM
1138{
1139 if (sdev->tagged_supported) {
1140 scsi_set_tag_type(sdev, tag_type);
1141 if (tag_type)
1142 scsi_activate_tcq(sdev, sdev->queue_depth);
1143 else
1144 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1145 } else
1146 tag_type = 0;
1147
1148 return tag_type;
1149}
1150
1151/**
d5d135b3 1152 * _scsih_target_alloc - target add routine
635374e7
EM
1153 * @starget: scsi target struct
1154 *
1155 * Returns 0 if ok. Any other return is assumed to be an error and
1156 * the device is ignored.
1157 */
1158static int
d5d135b3 1159_scsih_target_alloc(struct scsi_target *starget)
635374e7
EM
1160{
1161 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1162 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1163 struct MPT2SAS_TARGET *sas_target_priv_data;
1164 struct _sas_device *sas_device;
1165 struct _raid_device *raid_device;
1166 unsigned long flags;
1167 struct sas_rphy *rphy;
1168
1169 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1170 if (!sas_target_priv_data)
1171 return -ENOMEM;
1172
1173 starget->hostdata = sas_target_priv_data;
1174 sas_target_priv_data->starget = starget;
1175 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1176
1177 /* RAID volumes */
1178 if (starget->channel == RAID_CHANNEL) {
1179 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1180 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1181 starget->channel);
1182 if (raid_device) {
1183 sas_target_priv_data->handle = raid_device->handle;
1184 sas_target_priv_data->sas_address = raid_device->wwid;
1185 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1186 raid_device->starget = starget;
1187 }
1188 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1189 return 0;
1190 }
1191
1192 /* sas/sata devices */
1193 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1194 rphy = dev_to_rphy(starget->dev.parent);
1195 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1196 rphy->identify.sas_address);
1197
1198 if (sas_device) {
1199 sas_target_priv_data->handle = sas_device->handle;
1200 sas_target_priv_data->sas_address = sas_device->sas_address;
1201 sas_device->starget = starget;
1202 sas_device->id = starget->id;
1203 sas_device->channel = starget->channel;
1204 if (sas_device->hidden_raid_component)
1205 sas_target_priv_data->flags |=
1206 MPT_TARGET_FLAGS_RAID_COMPONENT;
1207 }
1208 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1209
1210 return 0;
1211}
1212
1213/**
d5d135b3 1214 * _scsih_target_destroy - target destroy routine
635374e7
EM
1215 * @starget: scsi target struct
1216 *
1217 * Returns nothing.
1218 */
1219static void
d5d135b3 1220_scsih_target_destroy(struct scsi_target *starget)
635374e7
EM
1221{
1222 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1223 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1224 struct MPT2SAS_TARGET *sas_target_priv_data;
1225 struct _sas_device *sas_device;
1226 struct _raid_device *raid_device;
1227 unsigned long flags;
1228 struct sas_rphy *rphy;
1229
1230 sas_target_priv_data = starget->hostdata;
1231 if (!sas_target_priv_data)
1232 return;
1233
1234 if (starget->channel == RAID_CHANNEL) {
1235 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1236 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1237 starget->channel);
1238 if (raid_device) {
1239 raid_device->starget = NULL;
1240 raid_device->sdev = NULL;
1241 }
1242 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1243 goto out;
1244 }
1245
1246 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1247 rphy = dev_to_rphy(starget->dev.parent);
1248 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1249 rphy->identify.sas_address);
8901cbb4
EM
1250 if (sas_device && (sas_device->starget == starget) &&
1251 (sas_device->id == starget->id) &&
1252 (sas_device->channel == starget->channel))
635374e7
EM
1253 sas_device->starget = NULL;
1254
1255 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1256
1257 out:
1258 kfree(sas_target_priv_data);
1259 starget->hostdata = NULL;
1260}
1261
1262/**
d5d135b3 1263 * _scsih_slave_alloc - device add routine
635374e7
EM
1264 * @sdev: scsi device struct
1265 *
1266 * Returns 0 if ok. Any other return is assumed to be an error and
1267 * the device is ignored.
1268 */
1269static int
d5d135b3 1270_scsih_slave_alloc(struct scsi_device *sdev)
635374e7
EM
1271{
1272 struct Scsi_Host *shost;
1273 struct MPT2SAS_ADAPTER *ioc;
1274 struct MPT2SAS_TARGET *sas_target_priv_data;
1275 struct MPT2SAS_DEVICE *sas_device_priv_data;
1276 struct scsi_target *starget;
1277 struct _raid_device *raid_device;
1278 struct _sas_device *sas_device;
1279 unsigned long flags;
1280
1281 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1282 if (!sas_device_priv_data)
1283 return -ENOMEM;
1284
1285 sas_device_priv_data->lun = sdev->lun;
1286 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1287
1288 starget = scsi_target(sdev);
1289 sas_target_priv_data = starget->hostdata;
1290 sas_target_priv_data->num_luns++;
1291 sas_device_priv_data->sas_target = sas_target_priv_data;
1292 sdev->hostdata = sas_device_priv_data;
1293 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1294 sdev->no_uld_attach = 1;
1295
1296 shost = dev_to_shost(&starget->dev);
1297 ioc = shost_priv(shost);
1298 if (starget->channel == RAID_CHANNEL) {
1299 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1300 raid_device = _scsih_raid_device_find_by_id(ioc,
1301 starget->id, starget->channel);
1302 if (raid_device)
1303 raid_device->sdev = sdev; /* raid is single lun */
1304 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1305 } else {
1306 /* set TLR bit for SSP devices */
1307 if (!(ioc->facts.IOCCapabilities &
1308 MPI2_IOCFACTS_CAPABILITY_TLR))
1309 goto out;
1310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1311 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1312 sas_device_priv_data->sas_target->sas_address);
1313 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1314 if (sas_device && sas_device->device_info &
1315 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1316 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1317 }
1318
1319 out:
1320 return 0;
1321}
1322
1323/**
d5d135b3 1324 * _scsih_slave_destroy - device destroy routine
635374e7
EM
1325 * @sdev: scsi device struct
1326 *
1327 * Returns nothing.
1328 */
1329static void
d5d135b3 1330_scsih_slave_destroy(struct scsi_device *sdev)
635374e7
EM
1331{
1332 struct MPT2SAS_TARGET *sas_target_priv_data;
1333 struct scsi_target *starget;
1334
1335 if (!sdev->hostdata)
1336 return;
1337
1338 starget = scsi_target(sdev);
1339 sas_target_priv_data = starget->hostdata;
1340 sas_target_priv_data->num_luns--;
1341 kfree(sdev->hostdata);
1342 sdev->hostdata = NULL;
1343}
1344
1345/**
d5d135b3 1346 * _scsih_display_sata_capabilities - sata capabilities
635374e7
EM
1347 * @ioc: per adapter object
1348 * @sas_device: the sas_device object
1349 * @sdev: scsi device struct
1350 */
1351static void
d5d135b3 1352_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
1353 struct _sas_device *sas_device, struct scsi_device *sdev)
1354{
1355 Mpi2ConfigReply_t mpi_reply;
1356 Mpi2SasDevicePage0_t sas_device_pg0;
1357 u32 ioc_status;
1358 u16 flags;
1359 u32 device_info;
1360
1361 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1362 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1363 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1364 ioc->name, __FILE__, __LINE__, __func__);
1365 return;
1366 }
1367
1368 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1369 MPI2_IOCSTATUS_MASK;
1370 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1371 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1372 ioc->name, __FILE__, __LINE__, __func__);
1373 return;
1374 }
1375
1376 flags = le16_to_cpu(sas_device_pg0.Flags);
1377 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1378
1379 sdev_printk(KERN_INFO, sdev,
1380 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1381 "sw_preserve(%s)\n",
1382 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1383 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1384 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1385 "n",
1386 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1387 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1388 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1389}
1390
1391/**
1392 * _scsih_get_volume_capabilities - volume capabilities
1393 * @ioc: per adapter object
1394 * @sas_device: the raid_device object
1395 */
1396static void
1397_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1398 struct _raid_device *raid_device)
1399{
1400 Mpi2RaidVolPage0_t *vol_pg0;
1401 Mpi2RaidPhysDiskPage0_t pd_pg0;
1402 Mpi2SasDevicePage0_t sas_device_pg0;
1403 Mpi2ConfigReply_t mpi_reply;
1404 u16 sz;
1405 u8 num_pds;
1406
1407 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1408 &num_pds)) || !num_pds) {
1409 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1410 ioc->name, __FILE__, __LINE__, __func__);
1411 return;
1412 }
1413
1414 raid_device->num_pds = num_pds;
1415 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1416 sizeof(Mpi2RaidVol0PhysDisk_t));
1417 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1418 if (!vol_pg0) {
1419 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1420 ioc->name, __FILE__, __LINE__, __func__);
1421 return;
1422 }
1423
1424 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1425 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1426 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1427 ioc->name, __FILE__, __LINE__, __func__);
1428 kfree(vol_pg0);
1429 return;
1430 }
1431
1432 raid_device->volume_type = vol_pg0->VolumeType;
1433
1434 /* figure out what the underlying devices are by
1435 * obtaining the device_info bits for the 1st device
1436 */
1437 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1438 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1439 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1440 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1441 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1442 le16_to_cpu(pd_pg0.DevHandle)))) {
1443 raid_device->device_info =
1444 le32_to_cpu(sas_device_pg0.DeviceInfo);
1445 }
1446 }
1447
1448 kfree(vol_pg0);
1449}
1450
1451/**
d5d135b3 1452 * _scsih_slave_configure - device configure routine.
635374e7
EM
1453 * @sdev: scsi device struct
1454 *
1455 * Returns 0 if ok. Any other return is assumed to be an error and
1456 * the device is ignored.
1457 */
1458static int
d5d135b3 1459_scsih_slave_configure(struct scsi_device *sdev)
635374e7
EM
1460{
1461 struct Scsi_Host *shost = sdev->host;
1462 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1463 struct MPT2SAS_DEVICE *sas_device_priv_data;
1464 struct MPT2SAS_TARGET *sas_target_priv_data;
1465 struct _sas_device *sas_device;
1466 struct _raid_device *raid_device;
1467 unsigned long flags;
1468 int qdepth;
1469 u8 ssp_target = 0;
1470 char *ds = "";
1471 char *r_level = "";
1472
1473 qdepth = 1;
1474 sas_device_priv_data = sdev->hostdata;
1475 sas_device_priv_data->configured_lun = 1;
1476 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1477 sas_target_priv_data = sas_device_priv_data->sas_target;
1478
1479 /* raid volume handling */
1480 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1481
1482 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1483 raid_device = _scsih_raid_device_find_by_handle(ioc,
1484 sas_target_priv_data->handle);
1485 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1486 if (!raid_device) {
1487 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488 ioc->name, __FILE__, __LINE__, __func__);
1489 return 0;
1490 }
1491
1492 _scsih_get_volume_capabilities(ioc, raid_device);
1493
1494 /* RAID Queue Depth Support
1495 * IS volume = underlying qdepth of drive type, either
1496 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1497 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1498 */
1499 if (raid_device->device_info &
1500 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1501 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1502 ds = "SSP";
1503 } else {
1504 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1505 if (raid_device->device_info &
1506 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1507 ds = "SATA";
1508 else
1509 ds = "STP";
1510 }
1511
1512 switch (raid_device->volume_type) {
1513 case MPI2_RAID_VOL_TYPE_RAID0:
1514 r_level = "RAID0";
1515 break;
1516 case MPI2_RAID_VOL_TYPE_RAID1E:
1517 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
ed79f128
KD
1518 if (ioc->manu_pg10.OEMIdentifier &&
1519 (ioc->manu_pg10.GenericFlags0 &
1520 MFG10_GF0_R10_DISPLAY) &&
1521 !(raid_device->num_pds % 2))
1522 r_level = "RAID10";
1523 else
1524 r_level = "RAID1E";
635374e7
EM
1525 break;
1526 case MPI2_RAID_VOL_TYPE_RAID1:
1527 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1528 r_level = "RAID1";
1529 break;
1530 case MPI2_RAID_VOL_TYPE_RAID10:
1531 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1532 r_level = "RAID10";
1533 break;
1534 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1535 default:
1536 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1537 r_level = "RAIDX";
1538 break;
1539 }
1540
1541 sdev_printk(KERN_INFO, sdev, "%s: "
1542 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1543 r_level, raid_device->handle,
1544 (unsigned long long)raid_device->wwid,
1545 raid_device->num_pds, ds);
d5d135b3 1546 _scsih_change_queue_depth(sdev, qdepth);
635374e7
EM
1547 return 0;
1548 }
1549
1550 /* non-raid handling */
1551 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1552 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1553 sas_device_priv_data->sas_target->sas_address);
1554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1555 if (sas_device) {
1556 if (sas_target_priv_data->flags &
1557 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1558 mpt2sas_config_get_volume_handle(ioc,
1559 sas_device->handle, &sas_device->volume_handle);
1560 mpt2sas_config_get_volume_wwid(ioc,
1561 sas_device->volume_handle,
1562 &sas_device->volume_wwid);
1563 }
1564 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1565 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1566 ssp_target = 1;
1567 ds = "SSP";
1568 } else {
1569 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1570 if (sas_device->device_info &
1571 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1572 ds = "STP";
1573 else if (sas_device->device_info &
1574 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1575 ds = "SATA";
1576 }
1577
1578 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1579 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1580 ds, sas_device->handle,
1581 (unsigned long long)sas_device->sas_address,
1582 (unsigned long long)sas_device->device_name);
1583 sdev_printk(KERN_INFO, sdev, "%s: "
1584 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1585 (unsigned long long) sas_device->enclosure_logical_id,
1586 sas_device->slot);
1587
1588 if (!ssp_target)
d5d135b3 1589 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
635374e7
EM
1590 }
1591
d5d135b3 1592 _scsih_change_queue_depth(sdev, qdepth);
635374e7
EM
1593
1594 if (ssp_target)
1595 sas_read_port_mode_page(sdev);
1596 return 0;
1597}
1598
1599/**
d5d135b3 1600 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
635374e7
EM
1601 * @sdev: scsi device struct
1602 * @bdev: pointer to block device context
1603 * @capacity: device size (in 512 byte sectors)
1604 * @params: three element array to place output:
1605 * params[0] number of heads (max 255)
1606 * params[1] number of sectors (max 63)
1607 * params[2] number of cylinders
1608 *
1609 * Return nothing.
1610 */
1611static int
d5d135b3 1612_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
635374e7
EM
1613 sector_t capacity, int params[])
1614{
1615 int heads;
1616 int sectors;
1617 sector_t cylinders;
1618 ulong dummy;
1619
1620 heads = 64;
1621 sectors = 32;
1622
1623 dummy = heads * sectors;
1624 cylinders = capacity;
1625 sector_div(cylinders, dummy);
1626
1627 /*
1628 * Handle extended translation size for logical drives
1629 * > 1Gb
1630 */
1631 if ((ulong)capacity >= 0x200000) {
1632 heads = 255;
1633 sectors = 63;
1634 dummy = heads * sectors;
1635 cylinders = capacity;
1636 sector_div(cylinders, dummy);
1637 }
1638
1639 /* return result */
1640 params[0] = heads;
1641 params[1] = sectors;
1642 params[2] = cylinders;
1643
1644 return 0;
1645}
1646
1647/**
1648 * _scsih_response_code - translation of device response code
1649 * @ioc: per adapter object
1650 * @response_code: response code returned by the device
1651 *
1652 * Return nothing.
1653 */
1654static void
1655_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1656{
1657 char *desc;
1658
1659 switch (response_code) {
1660 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1661 desc = "task management request completed";
1662 break;
1663 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1664 desc = "invalid frame";
1665 break;
1666 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1667 desc = "task management request not supported";
1668 break;
1669 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1670 desc = "task management request failed";
1671 break;
1672 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1673 desc = "task management request succeeded";
1674 break;
1675 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1676 desc = "invalid lun";
1677 break;
1678 case 0xA:
1679 desc = "overlapped tag attempted";
1680 break;
1681 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1682 desc = "task queued, however not sent to target";
1683 break;
1684 default:
1685 desc = "unknown";
1686 break;
1687 }
1688 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1689 ioc->name, response_code, desc);
1690}
1691
1692/**
d5d135b3 1693 * _scsih_tm_done - tm completion routine
635374e7
EM
1694 * @ioc: per adapter object
1695 * @smid: system request message index
7b936b02 1696 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
1697 * @reply: reply message frame(lower 32bit addr)
1698 * Context: none.
1699 *
1700 * The callback handler when using scsih_issue_tm.
1701 *
77e63ed4
KD
1702 * Return 1 meaning mf should be freed from _base_interrupt
1703 * 0 means the mf is freed from this function.
635374e7 1704 */
77e63ed4 1705static u8
7b936b02 1706_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
1707{
1708 MPI2DefaultReply_t *mpi_reply;
1709
1710 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
77e63ed4 1711 return 1;
635374e7 1712 if (ioc->tm_cmds.smid != smid)
77e63ed4 1713 return 1;
635374e7
EM
1714 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1715 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1716 if (mpi_reply) {
1717 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1718 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1719 }
1720 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1721 complete(&ioc->tm_cmds.done);
77e63ed4 1722 return 1;
635374e7
EM
1723}
1724
1725/**
1726 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1727 * @ioc: per adapter object
1728 * @handle: device handle
1729 *
1730 * During taskmangement request, we need to freeze the device queue.
1731 */
1732void
1733mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1734{
1735 struct MPT2SAS_DEVICE *sas_device_priv_data;
1736 struct scsi_device *sdev;
1737 u8 skip = 0;
1738
1739 shost_for_each_device(sdev, ioc->shost) {
1740 if (skip)
1741 continue;
1742 sas_device_priv_data = sdev->hostdata;
1743 if (!sas_device_priv_data)
1744 continue;
1745 if (sas_device_priv_data->sas_target->handle == handle) {
1746 sas_device_priv_data->sas_target->tm_busy = 1;
1747 skip = 1;
1748 ioc->ignore_loginfos = 1;
1749 }
1750 }
1751}
1752
1753/**
1754 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1755 * @ioc: per adapter object
1756 * @handle: device handle
1757 *
1758 * During taskmangement request, we need to freeze the device queue.
1759 */
1760void
1761mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1762{
1763 struct MPT2SAS_DEVICE *sas_device_priv_data;
1764 struct scsi_device *sdev;
1765 u8 skip = 0;
1766
1767 shost_for_each_device(sdev, ioc->shost) {
1768 if (skip)
1769 continue;
1770 sas_device_priv_data = sdev->hostdata;
1771 if (!sas_device_priv_data)
1772 continue;
1773 if (sas_device_priv_data->sas_target->handle == handle) {
1774 sas_device_priv_data->sas_target->tm_busy = 0;
1775 skip = 1;
1776 ioc->ignore_loginfos = 0;
1777 }
1778 }
1779}
1780
1781/**
1782 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1783 * @ioc: per adapter struct
1784 * @device_handle: device handle
1785 * @lun: lun number
1786 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1787 * @smid_task: smid assigned to the task
1788 * @timeout: timeout in seconds
1789 * Context: The calling function needs to acquire the tm_cmds.mutex
1790 *
1791 * A generic API for sending task management requests to firmware.
1792 *
1793 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1794 * this API.
1795 *
1796 * The callback index is set inside `ioc->tm_cb_idx`.
1797 *
1798 * Return nothing.
1799 */
1800void
1801mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1802 u8 type, u16 smid_task, ulong timeout)
1803{
1804 Mpi2SCSITaskManagementRequest_t *mpi_request;
1805 Mpi2SCSITaskManagementReply_t *mpi_reply;
1806 u16 smid = 0;
1807 u32 ioc_state;
1808 unsigned long timeleft;
635374e7 1809
155dd4c7
KD
1810 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1811 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1812 __func__, ioc->name);
1813 return;
1814 }
1815
1816 if (ioc->shost_recovery) {
635374e7
EM
1817 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1818 __func__, ioc->name);
1819 return;
1820 }
635374e7
EM
1821
1822 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1823 if (ioc_state & MPI2_DOORBELL_USED) {
1824 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1825 "active!\n", ioc->name));
1826 goto issue_host_reset;
1827 }
1828
1829 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1830 mpt2sas_base_fault_info(ioc, ioc_state &
1831 MPI2_DOORBELL_DATA_MASK);
1832 goto issue_host_reset;
1833 }
1834
595bb0bd 1835 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
635374e7
EM
1836 if (!smid) {
1837 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1838 ioc->name, __func__);
1839 return;
1840 }
1841
1842 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
595bb0bd
KD
1843 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
1844 smid_task));
635374e7
EM
1845 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1846 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1847 ioc->tm_cmds.smid = smid;
1848 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1849 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1850 mpi_request->DevHandle = cpu_to_le16(handle);
1851 mpi_request->TaskType = type;
1852 mpi_request->TaskMID = cpu_to_le16(smid_task);
7b936b02
KD
1853 mpi_request->VP_ID = 0; /* TODO */
1854 mpi_request->VF_ID = 0;
635374e7
EM
1855 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1856 mpt2sas_scsih_set_tm_flag(ioc, handle);
5b768581 1857 init_completion(&ioc->tm_cmds.done);
7b936b02 1858 mpt2sas_base_put_smid_hi_priority(ioc, smid);
635374e7
EM
1859 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1860 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1861 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1862 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1863 ioc->name, __func__);
1864 _debug_dump_mf(mpi_request,
1865 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1866 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1867 goto issue_host_reset;
1868 }
1869
1870 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1871 mpi_reply = ioc->tm_cmds.reply;
1872 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1873 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1874 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1875 le32_to_cpu(mpi_reply->IOCLogInfo),
1876 le32_to_cpu(mpi_reply->TerminationCount)));
1877 if (ioc->logging_level & MPT_DEBUG_TM)
1878 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1879 }
1880 return;
1881 issue_host_reset:
1882 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1883}
1884
1885/**
d5d135b3 1886 * _scsih_abort - eh threads main abort routine
635374e7
EM
1887 * @sdev: scsi device struct
1888 *
1889 * Returns SUCCESS if command aborted else FAILED
1890 */
1891static int
d5d135b3 1892_scsih_abort(struct scsi_cmnd *scmd)
635374e7
EM
1893{
1894 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1895 struct MPT2SAS_DEVICE *sas_device_priv_data;
1896 u16 smid;
1897 u16 handle;
1898 int r;
1899 struct scsi_cmnd *scmd_lookup;
1900
1901 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1902 ioc->name, scmd);
1903 scsi_print_command(scmd);
1904
1905 sas_device_priv_data = scmd->device->hostdata;
1906 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1907 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1908 ioc->name, scmd);
1909 scmd->result = DID_NO_CONNECT << 16;
1910 scmd->scsi_done(scmd);
1911 r = SUCCESS;
1912 goto out;
1913 }
1914
1915 /* search for the command */
1916 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1917 if (!smid) {
1918 scmd->result = DID_RESET << 16;
1919 r = SUCCESS;
1920 goto out;
1921 }
1922
1923 /* for hidden raid components and volumes this is not supported */
1924 if (sas_device_priv_data->sas_target->flags &
1925 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1926 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1927 scmd->result = DID_RESET << 16;
1928 r = FAILED;
1929 goto out;
1930 }
1931
1932 mutex_lock(&ioc->tm_cmds.mutex);
1933 handle = sas_device_priv_data->sas_target->handle;
1934 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1935 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1936
1937 /* sanity check - see whether command actually completed */
1938 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1939 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1940 r = FAILED;
1941 else
1942 r = SUCCESS;
1943 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1944 mutex_unlock(&ioc->tm_cmds.mutex);
1945
1946 out:
1947 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1948 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1949 return r;
1950}
1951
635374e7 1952/**
d5d135b3 1953 * _scsih_dev_reset - eh threads main device reset routine
635374e7
EM
1954 * @sdev: scsi device struct
1955 *
1956 * Returns SUCCESS if command aborted else FAILED
1957 */
1958static int
d5d135b3 1959_scsih_dev_reset(struct scsi_cmnd *scmd)
635374e7
EM
1960{
1961 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1962 struct MPT2SAS_DEVICE *sas_device_priv_data;
1963 struct _sas_device *sas_device;
1964 unsigned long flags;
1965 u16 handle;
1966 int r;
1967
993e0da7 1968 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
635374e7
EM
1969 ioc->name, scmd);
1970 scsi_print_command(scmd);
1971
1972 sas_device_priv_data = scmd->device->hostdata;
1973 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1974 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1975 ioc->name, scmd);
1976 scmd->result = DID_NO_CONNECT << 16;
1977 scmd->scsi_done(scmd);
1978 r = SUCCESS;
1979 goto out;
1980 }
1981
1982 /* for hidden raid components obtain the volume_handle */
1983 handle = 0;
1984 if (sas_device_priv_data->sas_target->flags &
1985 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1986 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1987 sas_device = _scsih_sas_device_find_by_handle(ioc,
1988 sas_device_priv_data->sas_target->handle);
1989 if (sas_device)
1990 handle = sas_device->volume_handle;
1991 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1992 } else
1993 handle = sas_device_priv_data->sas_target->handle;
1994
1995 if (!handle) {
1996 scmd->result = DID_RESET << 16;
1997 r = FAILED;
1998 goto out;
1999 }
2000
993e0da7
EM
2001 mutex_lock(&ioc->tm_cmds.mutex);
2002 mpt2sas_scsih_issue_tm(ioc, handle, 0,
2003 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
2004 30);
2005
2006 /*
2007 * sanity check see whether all commands to this device been
2008 * completed
2009 */
2010 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
2011 scmd->device->lun, scmd->device->channel))
2012 r = FAILED;
2013 else
2014 r = SUCCESS;
2015 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2016 mutex_unlock(&ioc->tm_cmds.mutex);
2017
2018 out:
2019 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2020 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2021 return r;
2022}
2023
2024/**
d5d135b3 2025 * _scsih_target_reset - eh threads main target reset routine
993e0da7
EM
2026 * @sdev: scsi device struct
2027 *
2028 * Returns SUCCESS if command aborted else FAILED
2029 */
2030static int
d5d135b3 2031_scsih_target_reset(struct scsi_cmnd *scmd)
993e0da7
EM
2032{
2033 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2034 struct MPT2SAS_DEVICE *sas_device_priv_data;
2035 struct _sas_device *sas_device;
2036 unsigned long flags;
2037 u16 handle;
2038 int r;
2039
2040 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2041 ioc->name, scmd);
2042 scsi_print_command(scmd);
2043
2044 sas_device_priv_data = scmd->device->hostdata;
2045 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2046 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2047 ioc->name, scmd);
2048 scmd->result = DID_NO_CONNECT << 16;
2049 scmd->scsi_done(scmd);
2050 r = SUCCESS;
2051 goto out;
2052 }
2053
2054 /* for hidden raid components obtain the volume_handle */
2055 handle = 0;
2056 if (sas_device_priv_data->sas_target->flags &
2057 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2058 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2059 sas_device = _scsih_sas_device_find_by_handle(ioc,
2060 sas_device_priv_data->sas_target->handle);
2061 if (sas_device)
2062 handle = sas_device->volume_handle;
2063 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2064 } else
2065 handle = sas_device_priv_data->sas_target->handle;
2066
2067 if (!handle) {
2068 scmd->result = DID_RESET << 16;
2069 r = FAILED;
2070 goto out;
2071 }
2072
635374e7
EM
2073 mutex_lock(&ioc->tm_cmds.mutex);
2074 mpt2sas_scsih_issue_tm(ioc, handle, 0,
2075 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2076
2077 /*
2078 * sanity check see whether all commands to this target been
2079 * completed
2080 */
2081 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2082 scmd->device->channel))
2083 r = FAILED;
2084 else
2085 r = SUCCESS;
2086 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2087 mutex_unlock(&ioc->tm_cmds.mutex);
2088
2089 out:
2090 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2091 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2092 return r;
2093}
2094
2095/**
595bb0bd 2096 * _scsih_host_reset - eh threads main host reset routine
635374e7
EM
2097 * @sdev: scsi device struct
2098 *
2099 * Returns SUCCESS if command aborted else FAILED
2100 */
2101static int
d5d135b3 2102_scsih_host_reset(struct scsi_cmnd *scmd)
635374e7
EM
2103{
2104 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2105 int r, retval;
2106
2107 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2108 ioc->name, scmd);
2109 scsi_print_command(scmd);
2110
2111 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2112 FORCE_BIG_HAMMER);
2113 r = (retval < 0) ? FAILED : SUCCESS;
2114 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2115 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2116
2117 return r;
2118}
2119
2120/**
2121 * _scsih_fw_event_add - insert and queue up fw_event
2122 * @ioc: per adapter object
2123 * @fw_event: object describing the event
2124 * Context: This function will acquire ioc->fw_event_lock.
2125 *
2126 * This adds the firmware event object into link list, then queues it up to
2127 * be processed from user context.
2128 *
2129 * Return nothing.
2130 */
2131static void
2132_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2133{
2134 unsigned long flags;
2135
2136 if (ioc->firmware_event_thread == NULL)
2137 return;
2138
2139 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2140 list_add_tail(&fw_event->list, &ioc->fw_event_list);
6f92a7a0
EM
2141 INIT_WORK(&fw_event->work, _firmware_event_work);
2142 queue_work(ioc->firmware_event_thread, &fw_event->work);
635374e7
EM
2143 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2144}
2145
2146/**
2147 * _scsih_fw_event_free - delete fw_event
2148 * @ioc: per adapter object
2149 * @fw_event: object describing the event
2150 * Context: This function will acquire ioc->fw_event_lock.
2151 *
2152 * This removes firmware event object from link list, frees associated memory.
2153 *
2154 * Return nothing.
2155 */
2156static void
2157_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2158 *fw_event)
2159{
2160 unsigned long flags;
2161
2162 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2163 list_del(&fw_event->list);
2164 kfree(fw_event->event_data);
2165 kfree(fw_event);
2166 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2167}
2168
2169/**
2170 * _scsih_fw_event_add - requeue an event
2171 * @ioc: per adapter object
2172 * @fw_event: object describing the event
2173 * Context: This function will acquire ioc->fw_event_lock.
2174 *
2175 * Return nothing.
2176 */
2177static void
2178_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2179 *fw_event, unsigned long delay)
2180{
2181 unsigned long flags;
2182 if (ioc->firmware_event_thread == NULL)
2183 return;
2184
2185 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6f92a7a0 2186 queue_work(ioc->firmware_event_thread, &fw_event->work);
635374e7
EM
2187 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2188}
2189
2190/**
2191 * _scsih_fw_event_off - turn flag off preventing event handling
2192 * @ioc: per adapter object
2193 *
2194 * Used to prevent handling of firmware events during adapter reset
2195 * driver unload.
2196 *
2197 * Return nothing.
2198 */
2199static void
2200_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2201{
2202 unsigned long flags;
2203
2204 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2205 ioc->fw_events_off = 1;
2206 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2207
2208}
2209
2210/**
2211 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2212 * @ioc: per adapter object
2213 *
2214 * Returns nothing.
2215 */
2216static void
2217_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2218{
2219 unsigned long flags;
2220
2221 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2222 ioc->fw_events_off = 0;
2223 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2224}
2225
2226/**
2227 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2228 * @ioc: per adapter object
2229 * @handle: device handle
2230 *
2231 * During device pull we need to appropiately set the sdev state.
2232 */
2233static void
2234_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2235{
2236 struct MPT2SAS_DEVICE *sas_device_priv_data;
2237 struct scsi_device *sdev;
2238
2239 shost_for_each_device(sdev, ioc->shost) {
2240 sas_device_priv_data = sdev->hostdata;
2241 if (!sas_device_priv_data)
2242 continue;
2243 if (!sas_device_priv_data->block)
2244 continue;
2245 if (sas_device_priv_data->sas_target->handle == handle) {
2246 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2247 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2248 "handle(0x%04x)\n", ioc->name, handle));
2249 sas_device_priv_data->block = 0;
34a03bef 2250 scsi_internal_device_unblock(sdev);
635374e7
EM
2251 }
2252 }
2253}
2254
2255/**
2256 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2257 * @ioc: per adapter object
2258 * @handle: device handle
2259 *
2260 * During device pull we need to appropiately set the sdev state.
2261 */
2262static void
2263_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2264{
2265 struct MPT2SAS_DEVICE *sas_device_priv_data;
2266 struct scsi_device *sdev;
2267
2268 shost_for_each_device(sdev, ioc->shost) {
2269 sas_device_priv_data = sdev->hostdata;
2270 if (!sas_device_priv_data)
2271 continue;
2272 if (sas_device_priv_data->block)
2273 continue;
2274 if (sas_device_priv_data->sas_target->handle == handle) {
2275 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2276 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2277 "handle(0x%04x)\n", ioc->name, handle));
2278 sas_device_priv_data->block = 1;
34a03bef 2279 scsi_internal_device_block(sdev);
635374e7
EM
2280 }
2281 }
2282}
2283
2284/**
2285 * _scsih_block_io_to_children_attached_to_ex
2286 * @ioc: per adapter object
2287 * @sas_expander: the sas_device object
2288 *
2289 * This routine set sdev state to SDEV_BLOCK for all devices
2290 * attached to this expander. This function called when expander is
2291 * pulled.
2292 */
2293static void
2294_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2295 struct _sas_node *sas_expander)
2296{
2297 struct _sas_port *mpt2sas_port;
2298 struct _sas_device *sas_device;
2299 struct _sas_node *expander_sibling;
2300 unsigned long flags;
2301
2302 if (!sas_expander)
2303 return;
2304
2305 list_for_each_entry(mpt2sas_port,
2306 &sas_expander->sas_port_list, port_list) {
2307 if (mpt2sas_port->remote_identify.device_type ==
2308 SAS_END_DEVICE) {
2309 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2310 sas_device =
2311 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2312 mpt2sas_port->remote_identify.sas_address);
2313 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2314 if (!sas_device)
2315 continue;
2316 _scsih_block_io_device(ioc, sas_device->handle);
2317 }
2318 }
2319
2320 list_for_each_entry(mpt2sas_port,
2321 &sas_expander->sas_port_list, port_list) {
2322
2323 if (mpt2sas_port->remote_identify.device_type ==
2324 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2325 mpt2sas_port->remote_identify.device_type ==
2326 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2327
2328 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2329 expander_sibling =
2330 mpt2sas_scsih_expander_find_by_sas_address(
2331 ioc, mpt2sas_port->remote_identify.sas_address);
2332 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2333 _scsih_block_io_to_children_attached_to_ex(ioc,
2334 expander_sibling);
2335 }
2336 }
2337}
2338
2339/**
2340 * _scsih_block_io_to_children_attached_directly
2341 * @ioc: per adapter object
2342 * @event_data: topology change event data
2343 *
2344 * This routine set sdev state to SDEV_BLOCK for all devices
2345 * direct attached during device pull.
2346 */
2347static void
2348_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2349 Mpi2EventDataSasTopologyChangeList_t *event_data)
2350{
2351 int i;
2352 u16 handle;
2353 u16 reason_code;
2354 u8 phy_number;
34a03bef 2355 u8 link_rate;
635374e7
EM
2356
2357 for (i = 0; i < event_data->NumEntries; i++) {
2358 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2359 if (!handle)
2360 continue;
2361 phy_number = event_data->StartPhyNum + i;
2362 reason_code = event_data->PHY[i].PhyStatus &
2363 MPI2_EVENT_SAS_TOPO_RC_MASK;
2364 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2365 _scsih_block_io_device(ioc, handle);
34a03bef
KD
2366 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2367 link_rate = event_data->PHY[i].LinkRate >> 4;
2368 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2369 _scsih_ublock_io_device(ioc, handle);
2370 }
635374e7
EM
2371 }
2372}
2373
77e63ed4
KD
2374/**
2375 * _scsih_tm_tr_send - send task management request
2376 * @ioc: per adapter object
2377 * @handle: device handle
2378 * Context: interrupt time.
2379 *
2380 * This code is to initiate the device removal handshake protocal
2381 * with controller firmware. This function will issue target reset
2382 * using high priority request queue. It will send a sas iounit
2383 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2384 *
2385 * This is designed to send muliple task management request at the same
2386 * time to the fifo. If the fifo is full, we will append the request,
2387 * and process it in a future completion.
2388 */
2389static void
2390_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2391{
2392 Mpi2SCSITaskManagementRequest_t *mpi_request;
2393 struct MPT2SAS_TARGET *sas_target_priv_data;
2394 u16 smid;
2395 struct _sas_device *sas_device;
2396 unsigned long flags;
2397 struct _tr_list *delayed_tr;
2398
2399 if (ioc->shost_recovery) {
2400 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2401 __func__, ioc->name);
2402 return;
2403 }
2404
2405 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2406 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
77e63ed4
KD
2407 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2408
2409 /* skip is hidden raid component */
a28eb222 2410 if (sas_device && sas_device->hidden_raid_component)
77e63ed4
KD
2411 return;
2412
2413 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2414 if (!smid) {
2415 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2416 if (!delayed_tr)
2417 return;
2418 INIT_LIST_HEAD(&delayed_tr->list);
2419 delayed_tr->handle = handle;
2420 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2421 list_add_tail(&delayed_tr->list,
2422 &ioc->delayed_tr_list);
a28eb222 2423 if (sas_device && sas_device->starget) {
77e63ed4
KD
2424 dewtprintk(ioc, starget_printk(KERN_INFO,
2425 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
a28eb222
KD
2426 "(open)\n", handle));
2427 } else {
2428 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2429 "DELAYED:tr:handle(0x%04x), (open)\n",
2430 ioc->name, handle));
2431 }
77e63ed4
KD
2432 return;
2433 }
2434
a28eb222
KD
2435 if (sas_device) {
2436 sas_device->state |= MPTSAS_STATE_TR_SEND;
2437 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2438 if (sas_device->starget && sas_device->starget->hostdata) {
2439 sas_target_priv_data = sas_device->starget->hostdata;
2440 sas_target_priv_data->tm_busy = 1;
2441 dewtprintk(ioc, starget_printk(KERN_INFO,
2442 sas_device->starget, "tr:handle(0x%04x), (open)\n",
2443 handle));
2444 }
2445 } else {
2446 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2447 "tr:handle(0x%04x), (open)\n", ioc->name, handle));
77e63ed4
KD
2448 }
2449
2450 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2451 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2452 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2453 mpi_request->DevHandle = cpu_to_le16(handle);
2454 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
77e63ed4
KD
2455 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2456}
2457
2458
2459
2460/**
2461 * _scsih_sas_control_complete - completion routine
2462 * @ioc: per adapter object
2463 * @smid: system request message index
2464 * @msix_index: MSIX table index supplied by the OS
2465 * @reply: reply message frame(lower 32bit addr)
2466 * Context: interrupt time.
2467 *
2468 * This is the sas iounit controll completion routine.
2469 * This code is part of the code to initiate the device removal
2470 * handshake protocal with controller firmware.
2471 *
2472 * Return 1 meaning mf should be freed from _base_interrupt
2473 * 0 means the mf is freed from this function.
2474 */
2475static u8
2476_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2477 u8 msix_index, u32 reply)
2478{
2479 unsigned long flags;
2480 u16 handle;
2481 struct _sas_device *sas_device;
2482 Mpi2SasIoUnitControlReply_t *mpi_reply =
2483 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2484
2485 handle = le16_to_cpu(mpi_reply->DevHandle);
2486
2487 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2488 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
77e63ed4
KD
2489 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2490
a28eb222
KD
2491 if (sas_device) {
2492 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2493 if (sas_device->starget)
2494 dewtprintk(ioc, starget_printk(KERN_INFO,
2495 sas_device->starget,
2496 "sc_complete:handle(0x%04x), "
2497 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2498 handle, le16_to_cpu(mpi_reply->IOCStatus),
2499 le32_to_cpu(mpi_reply->IOCLogInfo)));
2500 } else {
2501 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
77e63ed4
KD
2502 "sc_complete:handle(0x%04x), "
2503 "ioc_status(0x%04x), loginfo(0x%08x)\n",
a28eb222 2504 ioc->name, handle, le16_to_cpu(mpi_reply->IOCStatus),
77e63ed4 2505 le32_to_cpu(mpi_reply->IOCLogInfo)));
a28eb222
KD
2506 }
2507
77e63ed4
KD
2508 return 1;
2509}
2510
2511/**
2512 * _scsih_tm_tr_complete -
2513 * @ioc: per adapter object
2514 * @smid: system request message index
2515 * @msix_index: MSIX table index supplied by the OS
2516 * @reply: reply message frame(lower 32bit addr)
2517 * Context: interrupt time.
2518 *
2519 * This is the target reset completion routine.
2520 * This code is part of the code to initiate the device removal
2521 * handshake protocal with controller firmware.
2522 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2523 *
2524 * Return 1 meaning mf should be freed from _base_interrupt
2525 * 0 means the mf is freed from this function.
2526 */
2527static u8
2528_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2529 u32 reply)
2530{
2531 unsigned long flags;
2532 u16 handle;
2533 struct _sas_device *sas_device;
2534 Mpi2SCSITaskManagementReply_t *mpi_reply =
2535 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2536 Mpi2SasIoUnitControlRequest_t *mpi_request;
2537 u16 smid_sas_ctrl;
2538 struct MPT2SAS_TARGET *sas_target_priv_data;
2539 struct _tr_list *delayed_tr;
2540 u8 rc;
2541
2542 handle = le16_to_cpu(mpi_reply->DevHandle);
2543 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2544 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
77e63ed4
KD
2545 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2546
a28eb222
KD
2547 if (sas_device) {
2548 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2549 if (sas_device->starget) {
2550 dewtprintk(ioc, starget_printk(KERN_INFO,
2551 sas_device->starget, "tr_complete:handle(0x%04x), "
2552 "(%s) ioc_status(0x%04x), loginfo(0x%08x), "
2553 "completed(%d)\n", sas_device->handle,
2554 (sas_device->state & MPT2SAS_REQ_SAS_CNTRL) ?
2555 "open" : "active",
2556 le16_to_cpu(mpi_reply->IOCStatus),
2557 le32_to_cpu(mpi_reply->IOCLogInfo),
2558 le32_to_cpu(mpi_reply->TerminationCount)));
2559 if (sas_device->starget->hostdata) {
2560 sas_target_priv_data =
2561 sas_device->starget->hostdata;
2562 sas_target_priv_data->tm_busy = 0;
2563 }
2564 }
2565 } else {
2566 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2567 "tr_complete:handle(0x%04x), (open) ioc_status(0x%04x), "
2568 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2569 handle, le16_to_cpu(mpi_reply->IOCStatus),
77e63ed4
KD
2570 le32_to_cpu(mpi_reply->IOCLogInfo),
2571 le32_to_cpu(mpi_reply->TerminationCount)));
77e63ed4
KD
2572 }
2573
2574 if (!list_empty(&ioc->delayed_tr_list)) {
2575 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2576 struct _tr_list, list);
2577 mpt2sas_base_free_smid(ioc, smid);
2578 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2579 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2580 list_del(&delayed_tr->list);
2581 kfree(delayed_tr);
2582 rc = 0; /* tells base_interrupt not to free mf */
2583 } else
2584 rc = 1;
2585
a28eb222 2586 if (sas_device && !(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
77e63ed4
KD
2587 return rc;
2588
2589 if (ioc->shost_recovery) {
2590 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2591 __func__, ioc->name);
2592 return rc;
2593 }
2594
2595 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2596 if (!smid_sas_ctrl) {
2597 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2598 ioc->name, __func__);
2599 return rc;
2600 }
2601
a28eb222
KD
2602 if (sas_device)
2603 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2604
77e63ed4
KD
2605 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2606 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2607 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2608 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2609 mpi_request->DevHandle = mpi_reply->DevHandle;
77e63ed4
KD
2610 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2611 return rc;
2612}
2613
635374e7
EM
2614/**
2615 * _scsih_check_topo_delete_events - sanity check on topo events
2616 * @ioc: per adapter object
2617 * @event_data: the event data payload
2618 *
2619 * This routine added to better handle cable breaker.
2620 *
2621 * This handles the case where driver recieves multiple expander
2622 * add and delete events in a single shot. When there is a delete event
2623 * the routine will void any pending add events waiting in the event queue.
2624 *
2625 * Return nothing.
2626 */
2627static void
2628_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2629 Mpi2EventDataSasTopologyChangeList_t *event_data)
2630{
2631 struct fw_event_work *fw_event;
2632 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2633 u16 expander_handle;
2634 struct _sas_node *sas_expander;
2635 unsigned long flags;
77e63ed4
KD
2636 int i, reason_code;
2637 u16 handle;
2638
2639 for (i = 0 ; i < event_data->NumEntries; i++) {
2640 if (event_data->PHY[i].PhyStatus &
2641 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2642 continue;
2643 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2644 if (!handle)
2645 continue;
2646 reason_code = event_data->PHY[i].PhyStatus &
2647 MPI2_EVENT_SAS_TOPO_RC_MASK;
2648 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2649 _scsih_tm_tr_send(ioc, handle);
2650 }
635374e7
EM
2651
2652 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2653 if (expander_handle < ioc->sas_hba.num_phys) {
2654 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2655 return;
2656 }
2657
2658 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2659 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2660 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2661 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2662 expander_handle);
2663 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2664 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2665 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2666 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2667
2668 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2669 return;
2670
2671 /* mark ignore flag for pending events */
2672 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2673 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2674 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2675 fw_event->ignore)
2676 continue;
2677 local_event_data = fw_event->event_data;
2678 if (local_event_data->ExpStatus ==
2679 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2680 local_event_data->ExpStatus ==
2681 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2682 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2683 expander_handle) {
2684 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2685 "setting ignoring flag\n", ioc->name));
2686 fw_event->ignore = 1;
2687 }
2688 }
2689 }
2690 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2691}
2692
635374e7
EM
2693/**
2694 * _scsih_flush_running_cmds - completing outstanding commands.
2695 * @ioc: per adapter object
2696 *
2697 * The flushing out of all pending scmd commands following host reset,
2698 * where all IO is dropped to the floor.
2699 *
2700 * Return nothing.
2701 */
2702static void
2703_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2704{
2705 struct scsi_cmnd *scmd;
2706 u16 smid;
2707 u16 count = 0;
2708
595bb0bd
KD
2709 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2710 scmd = _scsih_scsi_lookup_get(ioc, smid);
635374e7
EM
2711 if (!scmd)
2712 continue;
2713 count++;
2714 mpt2sas_base_free_smid(ioc, smid);
2715 scsi_dma_unmap(scmd);
2716 scmd->result = DID_RESET << 16;
2717 scmd->scsi_done(scmd);
2718 }
2719 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2720 ioc->name, count));
2721}
2722
3c621b3e
EM
2723/**
2724 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2725 * @scmd: pointer to scsi command object
2726 * @mpi_request: pointer to the SCSI_IO reqest message frame
2727 *
2728 * Supporting protection 1 and 3.
2729 *
2730 * Returns nothing
2731 */
2732static void
2733_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2734{
2735 u16 eedp_flags;
2736 unsigned char prot_op = scsi_get_prot_op(scmd);
2737 unsigned char prot_type = scsi_get_prot_type(scmd);
2738
2739 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2740 prot_type == SCSI_PROT_DIF_TYPE2 ||
2741 prot_op == SCSI_PROT_NORMAL)
2742 return;
2743
2744 if (prot_op == SCSI_PROT_READ_STRIP)
2745 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2746 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2747 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2748 else
2749 return;
2750
2751 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2752
2753 switch (prot_type) {
2754 case SCSI_PROT_DIF_TYPE1:
2755
2756 /*
2757 * enable ref/guard checking
2758 * auto increment ref tag
2759 */
2760 mpi_request->EEDPFlags = eedp_flags |
2761 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2762 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2763 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2764 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2765 cpu_to_be32(scsi_get_lba(scmd));
2766
2767 break;
2768
2769 case SCSI_PROT_DIF_TYPE3:
2770
2771 /*
2772 * enable guard checking
2773 */
2774 mpi_request->EEDPFlags = eedp_flags |
2775 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2776
2777 break;
2778 }
2779}
2780
2781/**
2782 * _scsih_eedp_error_handling - return sense code for EEDP errors
2783 * @scmd: pointer to scsi command object
2784 * @ioc_status: ioc status
2785 *
2786 * Returns nothing
2787 */
2788static void
2789_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2790{
2791 u8 ascq;
2792 u8 sk;
2793 u8 host_byte;
2794
2795 switch (ioc_status) {
2796 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2797 ascq = 0x01;
2798 break;
2799 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2800 ascq = 0x02;
2801 break;
2802 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2803 ascq = 0x03;
2804 break;
2805 default:
2806 ascq = 0x00;
2807 break;
2808 }
2809
2810 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2811 sk = ILLEGAL_REQUEST;
2812 host_byte = DID_ABORT;
2813 } else {
2814 sk = ABORTED_COMMAND;
2815 host_byte = DID_OK;
2816 }
2817
2818 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2819 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2820 SAM_STAT_CHECK_CONDITION;
2821}
2822
635374e7 2823/**
d5d135b3 2824 * _scsih_qcmd - main scsi request entry point
635374e7
EM
2825 * @scmd: pointer to scsi command object
2826 * @done: function pointer to be invoked on completion
2827 *
2828 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2829 *
2830 * Returns 0 on success. If there's a failure, return either:
2831 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2832 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2833 */
2834static int
d5d135b3 2835_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
635374e7
EM
2836{
2837 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2838 struct MPT2SAS_DEVICE *sas_device_priv_data;
2839 struct MPT2SAS_TARGET *sas_target_priv_data;
2840 Mpi2SCSIIORequest_t *mpi_request;
2841 u32 mpi_control;
2842 u16 smid;
635374e7
EM
2843
2844 scmd->scsi_done = done;
2845 sas_device_priv_data = scmd->device->hostdata;
2846 if (!sas_device_priv_data) {
2847 scmd->result = DID_NO_CONNECT << 16;
2848 scmd->scsi_done(scmd);
2849 return 0;
2850 }
2851
2852 sas_target_priv_data = sas_device_priv_data->sas_target;
2853 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2854 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2855 scmd->result = DID_NO_CONNECT << 16;
2856 scmd->scsi_done(scmd);
2857 return 0;
2858 }
2859
2860 /* see if we are busy with task managment stuff */
155dd4c7
KD
2861 if (sas_target_priv_data->tm_busy)
2862 return SCSI_MLQUEUE_DEVICE_BUSY;
2863 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
635374e7 2864 return SCSI_MLQUEUE_HOST_BUSY;
635374e7
EM
2865
2866 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2867 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2868 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2869 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2870 else
2871 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2872
2873 /* set tags */
2874 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2875 if (scmd->device->tagged_supported) {
2876 if (scmd->device->ordered_tags)
2877 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2878 else
2879 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2880 } else
2881/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2882/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2883 */
2884 mpi_control |= (0x500);
2885
2886 } else
2887 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2888
2889 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2890 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2891
595bb0bd 2892 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
635374e7
EM
2893 if (!smid) {
2894 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2895 ioc->name, __func__);
2896 goto out;
2897 }
2898 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2899 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3c621b3e 2900 _scsih_setup_eedp(scmd, mpi_request);
635374e7
EM
2901 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2902 if (sas_device_priv_data->sas_target->flags &
2903 MPT_TARGET_FLAGS_RAID_COMPONENT)
2904 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2905 else
2906 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2907 mpi_request->DevHandle =
2908 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2909 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2910 mpi_request->Control = cpu_to_le32(mpi_control);
2911 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2912 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2913 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2914 mpi_request->SenseBufferLowAddress =
2915 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2916 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2917 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2918 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
7b936b02
KD
2919 mpi_request->VF_ID = 0; /* TODO */
2920 mpi_request->VP_ID = 0;
635374e7
EM
2921 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2922 mpi_request->LUN);
2923 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2924
2925 if (!mpi_request->DataLength) {
2926 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2927 } else {
2928 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2929 mpt2sas_base_free_smid(ioc, smid);
2930 goto out;
2931 }
2932 }
2933
7b936b02 2934 mpt2sas_base_put_smid_scsi_io(ioc, smid,
635374e7
EM
2935 sas_device_priv_data->sas_target->handle);
2936 return 0;
2937
2938 out:
2939 return SCSI_MLQUEUE_HOST_BUSY;
2940}
2941
2942/**
2943 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2944 * @sense_buffer: sense data returned by target
2945 * @data: normalized skey/asc/ascq
2946 *
2947 * Return nothing.
2948 */
2949static void
2950_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2951{
2952 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2953 /* descriptor format */
2954 data->skey = sense_buffer[1] & 0x0F;
2955 data->asc = sense_buffer[2];
2956 data->ascq = sense_buffer[3];
2957 } else {
2958 /* fixed format */
2959 data->skey = sense_buffer[2] & 0x0F;
2960 data->asc = sense_buffer[12];
2961 data->ascq = sense_buffer[13];
2962 }
2963}
2964
2965#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2966/**
2967 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2968 * @ioc: per adapter object
2969 * @scmd: pointer to scsi command object
2970 * @mpi_reply: reply mf payload returned from firmware
2971 *
2972 * scsi_status - SCSI Status code returned from target device
2973 * scsi_state - state info associated with SCSI_IO determined by ioc
2974 * ioc_status - ioc supplied status info
2975 *
2976 * Return nothing.
2977 */
2978static void
2979_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2980 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2981{
2982 u32 response_info;
2983 u8 *response_bytes;
2984 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2985 MPI2_IOCSTATUS_MASK;
2986 u8 scsi_state = mpi_reply->SCSIState;
2987 u8 scsi_status = mpi_reply->SCSIStatus;
2988 char *desc_ioc_state = NULL;
2989 char *desc_scsi_status = NULL;
2990 char *desc_scsi_state = ioc->tmp_string;
be9e8cd7
KD
2991 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2992
2993 if (log_info == 0x31170000)
2994 return;
635374e7
EM
2995
2996 switch (ioc_status) {
2997 case MPI2_IOCSTATUS_SUCCESS:
2998 desc_ioc_state = "success";
2999 break;
3000 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3001 desc_ioc_state = "invalid function";
3002 break;
3003 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3004 desc_ioc_state = "scsi recovered error";
3005 break;
3006 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3007 desc_ioc_state = "scsi invalid dev handle";
3008 break;
3009 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3010 desc_ioc_state = "scsi device not there";
3011 break;
3012 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3013 desc_ioc_state = "scsi data overrun";
3014 break;
3015 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3016 desc_ioc_state = "scsi data underrun";
3017 break;
3018 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3019 desc_ioc_state = "scsi io data error";
3020 break;
3021 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3022 desc_ioc_state = "scsi protocol error";
3023 break;
3024 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3025 desc_ioc_state = "scsi task terminated";
3026 break;
3027 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3028 desc_ioc_state = "scsi residual mismatch";
3029 break;
3030 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3031 desc_ioc_state = "scsi task mgmt failed";
3032 break;
3033 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3034 desc_ioc_state = "scsi ioc terminated";
3035 break;
3036 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3037 desc_ioc_state = "scsi ext terminated";
3038 break;
3c621b3e
EM
3039 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3040 desc_ioc_state = "eedp guard error";
3041 break;
3042 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3043 desc_ioc_state = "eedp ref tag error";
3044 break;
3045 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3046 desc_ioc_state = "eedp app tag error";
3047 break;
635374e7
EM
3048 default:
3049 desc_ioc_state = "unknown";
3050 break;
3051 }
3052
3053 switch (scsi_status) {
3054 case MPI2_SCSI_STATUS_GOOD:
3055 desc_scsi_status = "good";
3056 break;
3057 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3058 desc_scsi_status = "check condition";
3059 break;
3060 case MPI2_SCSI_STATUS_CONDITION_MET:
3061 desc_scsi_status = "condition met";
3062 break;
3063 case MPI2_SCSI_STATUS_BUSY:
3064 desc_scsi_status = "busy";
3065 break;
3066 case MPI2_SCSI_STATUS_INTERMEDIATE:
3067 desc_scsi_status = "intermediate";
3068 break;
3069 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3070 desc_scsi_status = "intermediate condmet";
3071 break;
3072 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3073 desc_scsi_status = "reservation conflict";
3074 break;
3075 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3076 desc_scsi_status = "command terminated";
3077 break;
3078 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3079 desc_scsi_status = "task set full";
3080 break;
3081 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3082 desc_scsi_status = "aca active";
3083 break;
3084 case MPI2_SCSI_STATUS_TASK_ABORTED:
3085 desc_scsi_status = "task aborted";
3086 break;
3087 default:
3088 desc_scsi_status = "unknown";
3089 break;
3090 }
3091
3092 desc_scsi_state[0] = '\0';
3093 if (!scsi_state)
3094 desc_scsi_state = " ";
3095 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3096 strcat(desc_scsi_state, "response info ");
3097 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3098 strcat(desc_scsi_state, "state terminated ");
3099 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3100 strcat(desc_scsi_state, "no status ");
3101 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3102 strcat(desc_scsi_state, "autosense failed ");
3103 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3104 strcat(desc_scsi_state, "autosense valid ");
3105
3106 scsi_print_command(scmd);
3107 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3108 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3109 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3110 ioc_status, smid);
3111 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3112 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3113 scsi_get_resid(scmd));
3114 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3115 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3116 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3117 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3118 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3119 scsi_status, desc_scsi_state, scsi_state);
3120
3121 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3122 struct sense_info data;
3123 _scsih_normalize_sense(scmd->sense_buffer, &data);
3124 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3125 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3126 data.asc, data.ascq);
3127 }
3128
3129 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3130 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3131 response_bytes = (u8 *)&response_info;
9982f594 3132 _scsih_response_code(ioc, response_bytes[0]);
635374e7
EM
3133 }
3134}
3135#endif
3136
3137/**
3138 * _scsih_smart_predicted_fault - illuminate Fault LED
3139 * @ioc: per adapter object
3140 * @handle: device handle
3141 *
3142 * Return nothing.
3143 */
3144static void
3145_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3146{
3147 Mpi2SepReply_t mpi_reply;
3148 Mpi2SepRequest_t mpi_request;
3149 struct scsi_target *starget;
3150 struct MPT2SAS_TARGET *sas_target_priv_data;
3151 Mpi2EventNotificationReply_t *event_reply;
3152 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3153 struct _sas_device *sas_device;
3154 ssize_t sz;
3155 unsigned long flags;
3156
3157 /* only handle non-raid devices */
3158 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3159 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3160 if (!sas_device) {
3161 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3162 return;
3163 }
3164 starget = sas_device->starget;
3165 sas_target_priv_data = starget->hostdata;
3166
3167 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3168 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3169 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3170 return;
3171 }
3172 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3173 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3174
3175 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3176 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3177 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3178 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3179 mpi_request.SlotStatus =
3180 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3181 mpi_request.DevHandle = cpu_to_le16(handle);
3182 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3183 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3184 &mpi_request)) != 0) {
3185 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3186 ioc->name, __FILE__, __LINE__, __func__);
3187 return;
3188 }
3189
3190 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3191 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3192 "enclosure_processor: ioc_status (0x%04x), "
3193 "loginfo(0x%08x)\n", ioc->name,
3194 le16_to_cpu(mpi_reply.IOCStatus),
3195 le32_to_cpu(mpi_reply.IOCLogInfo)));
3196 return;
3197 }
3198 }
3199
3200 /* insert into event log */
3201 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3202 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3203 event_reply = kzalloc(sz, GFP_KERNEL);
3204 if (!event_reply) {
3205 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3206 ioc->name, __FILE__, __LINE__, __func__);
3207 return;
3208 }
3209
3210 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3211 event_reply->Event =
3212 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3213 event_reply->MsgLength = sz/4;
3214 event_reply->EventDataLength =
3215 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3216 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3217 event_reply->EventData;
3218 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3219 event_data->ASC = 0x5D;
3220 event_data->DevHandle = cpu_to_le16(handle);
3221 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3222 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3223 kfree(event_reply);
3224}
3225
3226/**
d5d135b3 3227 * _scsih_io_done - scsi request callback
635374e7
EM
3228 * @ioc: per adapter object
3229 * @smid: system request message index
7b936b02 3230 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
3231 * @reply: reply message frame(lower 32bit addr)
3232 *
77e63ed4 3233 * Callback handler when using _scsih_qcmd.
635374e7 3234 *
77e63ed4
KD
3235 * Return 1 meaning mf should be freed from _base_interrupt
3236 * 0 means the mf is freed from this function.
635374e7 3237 */
77e63ed4 3238static u8
7b936b02 3239_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
3240{
3241 Mpi2SCSIIORequest_t *mpi_request;
3242 Mpi2SCSIIOReply_t *mpi_reply;
3243 struct scsi_cmnd *scmd;
3244 u16 ioc_status;
3245 u32 xfer_cnt;
3246 u8 scsi_state;
3247 u8 scsi_status;
3248 u32 log_info;
3249 struct MPT2SAS_DEVICE *sas_device_priv_data;
9982f594 3250 u32 response_code = 0;
635374e7
EM
3251
3252 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
595bb0bd 3253 scmd = _scsih_scsi_lookup_get(ioc, smid);
635374e7 3254 if (scmd == NULL)
77e63ed4 3255 return 1;
635374e7
EM
3256
3257 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3258
3259 if (mpi_reply == NULL) {
3260 scmd->result = DID_OK << 16;
3261 goto out;
3262 }
3263
3264 sas_device_priv_data = scmd->device->hostdata;
3265 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3266 sas_device_priv_data->sas_target->deleted) {
3267 scmd->result = DID_NO_CONNECT << 16;
3268 goto out;
3269 }
3270
3271 /* turning off TLR */
9982f594
KD
3272 scsi_state = mpi_reply->SCSIState;
3273 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3274 response_code =
3275 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
635374e7
EM
3276 if (!sas_device_priv_data->tlr_snoop_check) {
3277 sas_device_priv_data->tlr_snoop_check++;
9982f594
KD
3278 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3279 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3280 sas_device_priv_data->flags &=
3281 ~MPT_DEVICE_TLR_ON;
635374e7
EM
3282 }
3283
3284 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3285 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3286 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3287 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3288 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3289 else
3290 log_info = 0;
3291 ioc_status &= MPI2_IOCSTATUS_MASK;
635374e7
EM
3292 scsi_status = mpi_reply->SCSIStatus;
3293
3294 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3295 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3296 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3297 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3298 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3299 }
3300
3301 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3302 struct sense_info data;
3303 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3304 smid);
0d04df9b 3305 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
635374e7 3306 le32_to_cpu(mpi_reply->SenseCount));
0d04df9b 3307 memcpy(scmd->sense_buffer, sense_data, sz);
635374e7
EM
3308 _scsih_normalize_sense(scmd->sense_buffer, &data);
3309 /* failure prediction threshold exceeded */
3310 if (data.asc == 0x5D)
3311 _scsih_smart_predicted_fault(ioc,
3312 le16_to_cpu(mpi_reply->DevHandle));
3313 }
3314
3315 switch (ioc_status) {
3316 case MPI2_IOCSTATUS_BUSY:
3317 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3318 scmd->result = SAM_STAT_BUSY;
3319 break;
3320
3321 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3322 scmd->result = DID_NO_CONNECT << 16;
3323 break;
3324
3325 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3326 if (sas_device_priv_data->block) {
3327 scmd->result = (DID_BUS_BUSY << 16);
3328 break;
3329 }
3330
3331 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3332 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3333 scmd->result = DID_RESET << 16;
3334 break;
3335
3336 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3337 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3338 scmd->result = DID_SOFT_ERROR << 16;
3339 else
3340 scmd->result = (DID_OK << 16) | scsi_status;
3341 break;
3342
3343 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3344 scmd->result = (DID_OK << 16) | scsi_status;
3345
3346 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3347 break;
3348
3349 if (xfer_cnt < scmd->underflow) {
3350 if (scsi_status == SAM_STAT_BUSY)
3351 scmd->result = SAM_STAT_BUSY;
3352 else
3353 scmd->result = DID_SOFT_ERROR << 16;
3354 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3355 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3356 scmd->result = DID_SOFT_ERROR << 16;
3357 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3358 scmd->result = DID_RESET << 16;
3359 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3360 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3361 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3362 scmd->result = (DRIVER_SENSE << 24) |
3363 SAM_STAT_CHECK_CONDITION;
3364 scmd->sense_buffer[0] = 0x70;
3365 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3366 scmd->sense_buffer[12] = 0x20;
3367 scmd->sense_buffer[13] = 0;
3368 }
3369 break;
3370
3371 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3372 scsi_set_resid(scmd, 0);
3373 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3374 case MPI2_IOCSTATUS_SUCCESS:
3375 scmd->result = (DID_OK << 16) | scsi_status;
9982f594
KD
3376 if (response_code ==
3377 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3378 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3379 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
635374e7
EM
3380 scmd->result = DID_SOFT_ERROR << 16;
3381 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3382 scmd->result = DID_RESET << 16;
3383 break;
3384
3c621b3e
EM
3385 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3386 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3387 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3388 _scsih_eedp_error_handling(scmd, ioc_status);
3389 break;
635374e7
EM
3390 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3391 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3392 case MPI2_IOCSTATUS_INVALID_SGL:
3393 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3394 case MPI2_IOCSTATUS_INVALID_FIELD:
3395 case MPI2_IOCSTATUS_INVALID_STATE:
3396 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3397 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3398 default:
3399 scmd->result = DID_SOFT_ERROR << 16;
3400 break;
3401
3402 }
3403
3404#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3405 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3406 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3407#endif
3408
3409 out:
3410 scsi_dma_unmap(scmd);
3411 scmd->scsi_done(scmd);
77e63ed4 3412 return 1;
635374e7
EM
3413}
3414
635374e7
EM
3415/**
3416 * _scsih_sas_host_refresh - refreshing sas host object contents
3417 * @ioc: per adapter object
635374e7
EM
3418 * Context: user
3419 *
3420 * During port enable, fw will send topology events for every device. Its
3421 * possible that the handles may change from the previous setting, so this
3422 * code keeping handles updating if changed.
3423 *
3424 * Return nothing.
3425 */
3426static void
c5e039be 3427_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
635374e7
EM
3428{
3429 u16 sz;
3430 u16 ioc_status;
3431 int i;
3432 Mpi2ConfigReply_t mpi_reply;
3433 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
c5e039be 3434 u16 attached_handle;
635374e7
EM
3435
3436 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3437 "updating handles for sas_host(0x%016llx)\n",
3438 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3439
3440 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3441 * sizeof(Mpi2SasIOUnit0PhyData_t));
3442 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3443 if (!sas_iounit_pg0) {
3444 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3445 ioc->name, __FILE__, __LINE__, __func__);
3446 return;
3447 }
635374e7 3448
c5e039be
KD
3449 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3450 sas_iounit_pg0, sz)) != 0)
3451 goto out;
3452 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3453 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3454 goto out;
3455 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3456 if (i == 0)
3457 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3458 PhyData[0].ControllerDevHandle);
3459 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3460 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3461 AttachedDevHandle);
3462 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3463 attached_handle, i, sas_iounit_pg0->PhyData[i].
3464 NegotiatedLinkRate >> 4);
3465 }
635374e7
EM
3466 out:
3467 kfree(sas_iounit_pg0);
3468}
3469
3470/**
3471 * _scsih_sas_host_add - create sas host object
3472 * @ioc: per adapter object
3473 *
3474 * Creating host side data object, stored in ioc->sas_hba
3475 *
3476 * Return nothing.
3477 */
3478static void
3479_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3480{
3481 int i;
3482 Mpi2ConfigReply_t mpi_reply;
3483 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3484 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3485 Mpi2SasPhyPage0_t phy_pg0;
3486 Mpi2SasDevicePage0_t sas_device_pg0;
3487 Mpi2SasEnclosurePage0_t enclosure_pg0;
3488 u16 ioc_status;
3489 u16 sz;
3490 u16 device_missing_delay;
3491
3492 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3493 if (!ioc->sas_hba.num_phys) {
3494 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3495 ioc->name, __FILE__, __LINE__, __func__);
3496 return;
3497 }
3498
3499 /* sas_iounit page 0 */
3500 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3501 sizeof(Mpi2SasIOUnit0PhyData_t));
3502 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3503 if (!sas_iounit_pg0) {
3504 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3505 ioc->name, __FILE__, __LINE__, __func__);
3506 return;
3507 }
3508 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3509 sas_iounit_pg0, sz))) {
3510 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3511 ioc->name, __FILE__, __LINE__, __func__);
3512 goto out;
3513 }
3514 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3515 MPI2_IOCSTATUS_MASK;
3516 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3517 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3518 ioc->name, __FILE__, __LINE__, __func__);
3519 goto out;
3520 }
3521
3522 /* sas_iounit page 1 */
3523 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3524 sizeof(Mpi2SasIOUnit1PhyData_t));
3525 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3526 if (!sas_iounit_pg1) {
3527 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3528 ioc->name, __FILE__, __LINE__, __func__);
3529 goto out;
3530 }
3531 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3532 sas_iounit_pg1, sz))) {
3533 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3534 ioc->name, __FILE__, __LINE__, __func__);
3535 goto out;
3536 }
3537 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3538 MPI2_IOCSTATUS_MASK;
3539 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3540 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3541 ioc->name, __FILE__, __LINE__, __func__);
3542 goto out;
3543 }
3544
3545 ioc->io_missing_delay =
3546 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3547 device_missing_delay =
3548 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3549 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3550 ioc->device_missing_delay = (device_missing_delay &
3551 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3552 else
3553 ioc->device_missing_delay = device_missing_delay &
3554 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3555
3556 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3557 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3558 sizeof(struct _sas_phy), GFP_KERNEL);
3559 if (!ioc->sas_hba.phy) {
3560 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3561 ioc->name, __FILE__, __LINE__, __func__);
3562 goto out;
3563 }
3564 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3565 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3566 i))) {
3567 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3568 ioc->name, __FILE__, __LINE__, __func__);
3569 goto out;
3570 }
3571 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3572 MPI2_IOCSTATUS_MASK;
3573 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3574 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3575 ioc->name, __FILE__, __LINE__, __func__);
3576 goto out;
3577 }
c5e039be
KD
3578
3579 if (i == 0)
3580 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3581 PhyData[0].ControllerDevHandle);
3582 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
635374e7
EM
3583 ioc->sas_hba.phy[i].phy_id = i;
3584 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3585 phy_pg0, ioc->sas_hba.parent_dev);
3586 }
3587 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
c5e039be 3588 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
635374e7
EM
3589 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3590 ioc->name, __FILE__, __LINE__, __func__);
3591 goto out;
3592 }
635374e7
EM
3593 ioc->sas_hba.enclosure_handle =
3594 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3595 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3596 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3597 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3598 (unsigned long long) ioc->sas_hba.sas_address,
3599 ioc->sas_hba.num_phys) ;
3600
3601 if (ioc->sas_hba.enclosure_handle) {
3602 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3603 &enclosure_pg0,
3604 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3605 ioc->sas_hba.enclosure_handle))) {
3606 ioc->sas_hba.enclosure_logical_id =
3607 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3608 }
3609 }
3610
3611 out:
3612 kfree(sas_iounit_pg1);
3613 kfree(sas_iounit_pg0);
3614}
3615
3616/**
3617 * _scsih_expander_add - creating expander object
3618 * @ioc: per adapter object
3619 * @handle: expander handle
3620 *
3621 * Creating expander object, stored in ioc->sas_expander_list.
3622 *
3623 * Return 0 for success, else error.
3624 */
3625static int
3626_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3627{
3628 struct _sas_node *sas_expander;
3629 Mpi2ConfigReply_t mpi_reply;
3630 Mpi2ExpanderPage0_t expander_pg0;
3631 Mpi2ExpanderPage1_t expander_pg1;
3632 Mpi2SasEnclosurePage0_t enclosure_pg0;
3633 u32 ioc_status;
3634 u16 parent_handle;
c5e039be 3635 __le64 sas_address, sas_address_parent = 0;
635374e7
EM
3636 int i;
3637 unsigned long flags;
20f5895d 3638 struct _sas_port *mpt2sas_port = NULL;
635374e7
EM
3639 int rc = 0;
3640
3641 if (!handle)
3642 return -1;
3643
155dd4c7
KD
3644 if (ioc->shost_recovery)
3645 return -1;
3646
635374e7
EM
3647 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3648 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3649 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3650 ioc->name, __FILE__, __LINE__, __func__);
3651 return -1;
3652 }
3653
3654 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3655 MPI2_IOCSTATUS_MASK;
3656 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3657 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3658 ioc->name, __FILE__, __LINE__, __func__);
3659 return -1;
3660 }
3661
3662 /* handle out of order topology events */
3663 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
c5e039be
KD
3664 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
3665 != 0) {
3666 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3667 ioc->name, __FILE__, __LINE__, __func__);
3668 return -1;
3669 }
3670 if (sas_address_parent != ioc->sas_hba.sas_address) {
635374e7 3671 spin_lock_irqsave(&ioc->sas_node_lock, flags);
c5e039be
KD
3672 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3673 sas_address_parent);
635374e7
EM
3674 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3675 if (!sas_expander) {
3676 rc = _scsih_expander_add(ioc, parent_handle);
3677 if (rc != 0)
3678 return rc;
3679 }
3680 }
3681
635374e7 3682 spin_lock_irqsave(&ioc->sas_node_lock, flags);
595bb0bd 3683 sas_address = le64_to_cpu(expander_pg0.SASAddress);
635374e7
EM
3684 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3685 sas_address);
3686 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3687
3688 if (sas_expander)
3689 return 0;
3690
3691 sas_expander = kzalloc(sizeof(struct _sas_node),
3692 GFP_KERNEL);
3693 if (!sas_expander) {
3694 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3695 ioc->name, __FILE__, __LINE__, __func__);
3696 return -1;
3697 }
3698
3699 sas_expander->handle = handle;
3700 sas_expander->num_phys = expander_pg0.NumPhys;
c5e039be 3701 sas_expander->sas_address_parent = sas_address_parent;
635374e7
EM
3702 sas_expander->sas_address = sas_address;
3703
3704 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3705 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
c5e039be 3706 handle, parent_handle, (unsigned long long)
635374e7
EM
3707 sas_expander->sas_address, sas_expander->num_phys);
3708
3709 if (!sas_expander->num_phys)
3710 goto out_fail;
3711 sas_expander->phy = kcalloc(sas_expander->num_phys,
3712 sizeof(struct _sas_phy), GFP_KERNEL);
3713 if (!sas_expander->phy) {
3714 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3715 ioc->name, __FILE__, __LINE__, __func__);
3716 rc = -1;
3717 goto out_fail;
3718 }
3719
3720 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3721 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
c5e039be 3722 sas_address_parent);
635374e7
EM
3723 if (!mpt2sas_port) {
3724 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3725 ioc->name, __FILE__, __LINE__, __func__);
3726 rc = -1;
3727 goto out_fail;
3728 }
3729 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3730
3731 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3732 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3733 &expander_pg1, i, handle))) {
3734 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3735 ioc->name, __FILE__, __LINE__, __func__);
20f5895d
KD
3736 rc = -1;
3737 goto out_fail;
635374e7
EM
3738 }
3739 sas_expander->phy[i].handle = handle;
3740 sas_expander->phy[i].phy_id = i;
20f5895d
KD
3741
3742 if ((mpt2sas_transport_add_expander_phy(ioc,
3743 &sas_expander->phy[i], expander_pg1,
3744 sas_expander->parent_dev))) {
3745 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3746 ioc->name, __FILE__, __LINE__, __func__);
3747 rc = -1;
3748 goto out_fail;
3749 }
635374e7
EM
3750 }
3751
3752 if (sas_expander->enclosure_handle) {
3753 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3754 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3755 sas_expander->enclosure_handle))) {
3756 sas_expander->enclosure_logical_id =
3757 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3758 }
3759 }
3760
3761 _scsih_expander_node_add(ioc, sas_expander);
3762 return 0;
3763
3764 out_fail:
3765
20f5895d
KD
3766 if (mpt2sas_port)
3767 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
c5e039be 3768 sas_address_parent);
635374e7
EM
3769 kfree(sas_expander);
3770 return rc;
3771}
3772
3773/**
3774 * _scsih_expander_remove - removing expander object
3775 * @ioc: per adapter object
c5e039be 3776 * @sas_address: expander sas_address
635374e7
EM
3777 *
3778 * Return nothing.
3779 */
3780static void
c5e039be 3781_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
635374e7
EM
3782{
3783 struct _sas_node *sas_expander;
3784 unsigned long flags;
3785
155dd4c7
KD
3786 if (ioc->shost_recovery)
3787 return;
3788
635374e7 3789 spin_lock_irqsave(&ioc->sas_node_lock, flags);
c5e039be
KD
3790 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3791 sas_address);
635374e7
EM
3792 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3793 _scsih_expander_node_remove(ioc, sas_expander);
3794}
3795
3796/**
3797 * _scsih_add_device - creating sas device object
3798 * @ioc: per adapter object
3799 * @handle: sas device handle
3800 * @phy_num: phy number end device attached to
3801 * @is_pd: is this hidden raid component
3802 *
3803 * Creating end device object, stored in ioc->sas_device_list.
3804 *
3805 * Returns 0 for success, non-zero for failure.
3806 */
3807static int
3808_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3809{
3810 Mpi2ConfigReply_t mpi_reply;
3811 Mpi2SasDevicePage0_t sas_device_pg0;
3812 Mpi2SasEnclosurePage0_t enclosure_pg0;
3813 struct _sas_device *sas_device;
3814 u32 ioc_status;
3815 __le64 sas_address;
3816 u32 device_info;
3817 unsigned long flags;
3818
3819 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3820 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3821 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3822 ioc->name, __FILE__, __LINE__, __func__);
3823 return -1;
3824 }
3825
3826 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3827 MPI2_IOCSTATUS_MASK;
3828 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3829 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3830 ioc->name, __FILE__, __LINE__, __func__);
3831 return -1;
3832 }
3833
3834 /* check if device is present */
3835 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3836 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3837 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3838 ioc->name, __FILE__, __LINE__, __func__);
3839 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3840 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3841 return -1;
3842 }
3843
3844 /* check if there were any issus with discovery */
3845 if (sas_device_pg0.AccessStatus ==
3846 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3847 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3848 ioc->name, __FILE__, __LINE__, __func__);
3849 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3850 ioc->name, sas_device_pg0.AccessStatus);
3851 return -1;
3852 }
3853
3854 /* check if this is end device */
3855 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3856 if (!(_scsih_is_end_device(device_info))) {
3857 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3858 ioc->name, __FILE__, __LINE__, __func__);
3859 return -1;
3860 }
3861
3862 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3863
3864 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3865 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3866 sas_address);
3867 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3868
3869 if (sas_device) {
3870 _scsih_ublock_io_device(ioc, handle);
3871 return 0;
3872 }
3873
3874 sas_device = kzalloc(sizeof(struct _sas_device),
3875 GFP_KERNEL);
3876 if (!sas_device) {
3877 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3878 ioc->name, __FILE__, __LINE__, __func__);
3879 return -1;
3880 }
3881
3882 sas_device->handle = handle;
c5e039be
KD
3883 if (_scsih_get_sas_address(ioc, le16_to_cpu
3884 (sas_device_pg0.ParentDevHandle),
3885 &sas_device->sas_address_parent) != 0)
3886 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3887 ioc->name, __FILE__, __LINE__, __func__);
635374e7
EM
3888 sas_device->enclosure_handle =
3889 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3890 sas_device->slot =
3891 le16_to_cpu(sas_device_pg0.Slot);
3892 sas_device->device_info = device_info;
3893 sas_device->sas_address = sas_address;
3894 sas_device->hidden_raid_component = is_pd;
3895
3896 /* get enclosure_logical_id */
15052c9e
KD
3897 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3898 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3899 sas_device->enclosure_handle)))
635374e7
EM
3900 sas_device->enclosure_logical_id =
3901 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
635374e7
EM
3902
3903 /* get device name */
3904 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3905
3906 if (ioc->wait_for_port_enable_to_complete)
3907 _scsih_sas_device_init_add(ioc, sas_device);
3908 else
3909 _scsih_sas_device_add(ioc, sas_device);
3910
3911 return 0;
3912}
3913
3914/**
3915 * _scsih_remove_device - removing sas device object
3916 * @ioc: per adapter object
c5e039be 3917 * @sas_device: the sas_device object
635374e7
EM
3918 *
3919 * Return nothing.
3920 */
3921static void
c5e039be
KD
3922_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
3923 *sas_device)
635374e7
EM
3924{
3925 struct MPT2SAS_TARGET *sas_target_priv_data;
635374e7
EM
3926 Mpi2SasIoUnitControlReply_t mpi_reply;
3927 Mpi2SasIoUnitControlRequest_t mpi_request;
c5e039be 3928 u16 device_handle, handle;
635374e7 3929
c5e039be 3930 if (!sas_device)
635374e7 3931 return;
635374e7 3932
c5e039be
KD
3933 handle = sas_device->handle;
3934 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
3935 " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
3936 (unsigned long long) sas_device->sas_address));
635374e7
EM
3937
3938 if (sas_device->starget && sas_device->starget->hostdata) {
3939 sas_target_priv_data = sas_device->starget->hostdata;
3940 sas_target_priv_data->deleted = 1;
3941 }
635374e7 3942
c5e039be 3943 if (ioc->remove_host || ioc->shost_recovery || !handle)
635374e7
EM
3944 goto out;
3945
77e63ed4
KD
3946 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
3947 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
c5e039be
KD
3948 "target_reset handle(0x%04x)\n", ioc->name,
3949 handle));
77e63ed4
KD
3950 goto skip_tr;
3951 }
3952
635374e7
EM
3953 /* Target Reset to flush out all the outstanding IO */
3954 device_handle = (sas_device->hidden_raid_component) ?
3955 sas_device->volume_handle : handle;
3956 if (device_handle) {
3957 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3958 "handle(0x%04x)\n", ioc->name, device_handle));
3959 mutex_lock(&ioc->tm_cmds.mutex);
3960 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3961 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3962 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3963 mutex_unlock(&ioc->tm_cmds.mutex);
3964 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3965 "done: handle(0x%04x)\n", ioc->name, device_handle));
155dd4c7
KD
3966 if (ioc->shost_recovery)
3967 goto out;
635374e7 3968 }
77e63ed4
KD
3969 skip_tr:
3970
3971 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
3972 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3973 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
3974 goto out;
3975 }
635374e7
EM
3976
3977 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3978 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3979 "(0x%04x)\n", ioc->name, handle));
3980 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3981 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3982 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3983 mpi_request.DevHandle = handle;
7b936b02
KD
3984 mpi_request.VF_ID = 0; /* TODO */
3985 mpi_request.VP_ID = 0;
635374e7
EM
3986 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3987 &mpi_request)) != 0) {
3988 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3989 ioc->name, __FILE__, __LINE__, __func__);
3990 }
3991
3992 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3993 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3994 le16_to_cpu(mpi_reply.IOCStatus),
3995 le32_to_cpu(mpi_reply.IOCLogInfo)));
3996
3997 out:
34a03bef
KD
3998
3999 _scsih_ublock_io_device(ioc, handle);
4000
635374e7 4001 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
c5e039be 4002 sas_device->sas_address_parent);
635374e7
EM
4003
4004 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
c5e039be 4005 "(0x%016llx)\n", ioc->name, handle,
635374e7
EM
4006 (unsigned long long) sas_device->sas_address);
4007 _scsih_sas_device_remove(ioc, sas_device);
4008
4009 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
4010 "(0x%04x)\n", ioc->name, __func__, handle));
4011}
4012
4013#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4014/**
4015 * _scsih_sas_topology_change_event_debug - debug for topology event
4016 * @ioc: per adapter object
4017 * @event_data: event data payload
4018 * Context: user.
4019 */
4020static void
4021_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4022 Mpi2EventDataSasTopologyChangeList_t *event_data)
4023{
4024 int i;
4025 u16 handle;
4026 u16 reason_code;
4027 u8 phy_number;
4028 char *status_str = NULL;
4029 char link_rate[25];
4030
4031 switch (event_data->ExpStatus) {
4032 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4033 status_str = "add";
4034 break;
4035 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4036 status_str = "remove";
4037 break;
4038 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
4039 status_str = "responding";
4040 break;
4041 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4042 status_str = "remove delay";
4043 break;
4044 default:
4045 status_str = "unknown status";
4046 break;
4047 }
4048 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
4049 ioc->name, status_str);
4050 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
4051 "start_phy(%02d), count(%d)\n",
4052 le16_to_cpu(event_data->ExpanderDevHandle),
4053 le16_to_cpu(event_data->EnclosureHandle),
4054 event_data->StartPhyNum, event_data->NumEntries);
4055 for (i = 0; i < event_data->NumEntries; i++) {
4056 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4057 if (!handle)
4058 continue;
4059 phy_number = event_data->StartPhyNum + i;
4060 reason_code = event_data->PHY[i].PhyStatus &
4061 MPI2_EVENT_SAS_TOPO_RC_MASK;
4062 switch (reason_code) {
4063 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4064 snprintf(link_rate, 25, ": add, link(0x%02x)",
4065 (event_data->PHY[i].LinkRate >> 4));
4066 status_str = link_rate;
4067 break;
4068 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4069 status_str = ": remove";
4070 break;
4071 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
4072 status_str = ": remove_delay";
4073 break;
4074 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4075 snprintf(link_rate, 25, ": link(0x%02x)",
4076 (event_data->PHY[i].LinkRate >> 4));
4077 status_str = link_rate;
4078 break;
4079 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4080 status_str = ": responding";
4081 break;
4082 default:
4083 status_str = ": unknown";
4084 break;
4085 }
4086 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
4087 phy_number, handle, status_str);
4088 }
4089}
4090#endif
4091
4092/**
4093 * _scsih_sas_topology_change_event - handle topology changes
4094 * @ioc: per adapter object
7b936b02 4095 * @fw_event: The fw_event_work object
635374e7
EM
4096 * Context: user.
4097 *
4098 */
4099static void
7b936b02 4100_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
4101 struct fw_event_work *fw_event)
4102{
4103 int i;
4104 u16 parent_handle, handle;
4105 u16 reason_code;
4106 u8 phy_number;
4107 struct _sas_node *sas_expander;
c5e039be
KD
4108 struct _sas_device *sas_device;
4109 u64 sas_address;
635374e7 4110 unsigned long flags;
c5e039be 4111 u8 link_rate;
7b936b02 4112 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
635374e7
EM
4113
4114#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4115 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4116 _scsih_sas_topology_change_event_debug(ioc, event_data);
4117#endif
4118
c5e039be
KD
4119 if (ioc->shost_recovery)
4120 return;
4121
635374e7
EM
4122 if (!ioc->sas_hba.num_phys)
4123 _scsih_sas_host_add(ioc);
4124 else
c5e039be 4125 _scsih_sas_host_refresh(ioc);
635374e7
EM
4126
4127 if (fw_event->ignore) {
4128 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4129 "event\n", ioc->name));
4130 return;
4131 }
4132
4133 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4134
4135 /* handle expander add */
4136 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4137 if (_scsih_expander_add(ioc, parent_handle) != 0)
4138 return;
4139
c5e039be
KD
4140 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4141 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4142 parent_handle);
4143 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4144 if (sas_expander)
4145 sas_address = sas_expander->sas_address;
4146 else if (parent_handle < ioc->sas_hba.num_phys)
4147 sas_address = ioc->sas_hba.sas_address;
4148 else
4149 return;
4150
635374e7
EM
4151 /* handle siblings events */
4152 for (i = 0; i < event_data->NumEntries; i++) {
4153 if (fw_event->ignore) {
4154 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4155 "expander event\n", ioc->name));
4156 return;
4157 }
155dd4c7
KD
4158 if (ioc->shost_recovery)
4159 return;
308609c6
KD
4160 phy_number = event_data->StartPhyNum + i;
4161 reason_code = event_data->PHY[i].PhyStatus &
4162 MPI2_EVENT_SAS_TOPO_RC_MASK;
4163 if ((event_data->PHY[i].PhyStatus &
4164 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4165 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
635374e7
EM
4166 continue;
4167 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4168 if (!handle)
4169 continue;
c5e039be 4170 link_rate = event_data->PHY[i].LinkRate >> 4;
635374e7
EM
4171 switch (reason_code) {
4172 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4173 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
c5e039be
KD
4174
4175 mpt2sas_transport_update_links(ioc, sas_address,
4176 handle, phy_number, link_rate);
4177
4178 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4179 break;
635374e7 4180 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
635374e7
EM
4181 _scsih_add_device(ioc, handle, phy_number, 0);
4182 }
4183 break;
4184 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
c5e039be
KD
4185
4186 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4187 sas_device = _scsih_sas_device_find_by_handle(ioc,
4188 handle);
4189 if (!sas_device) {
4190 spin_unlock_irqrestore(&ioc->sas_device_lock,
4191 flags);
4192 break;
4193 }
4194 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4195 _scsih_remove_device(ioc, sas_device);
635374e7
EM
4196 break;
4197 }
4198 }
4199
4200 /* handle expander removal */
c5e039be
KD
4201 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4202 sas_expander)
4203 _scsih_expander_remove(ioc, sas_address);
635374e7
EM
4204
4205}
4206
4207#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4208/**
4209 * _scsih_sas_device_status_change_event_debug - debug for device event
4210 * @event_data: event data payload
4211 * Context: user.
4212 *
4213 * Return nothing.
4214 */
4215static void
4216_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4217 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4218{
4219 char *reason_str = NULL;
4220
4221 switch (event_data->ReasonCode) {
4222 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4223 reason_str = "smart data";
4224 break;
4225 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4226 reason_str = "unsupported device discovered";
4227 break;
4228 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4229 reason_str = "internal device reset";
4230 break;
4231 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4232 reason_str = "internal task abort";
4233 break;
4234 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4235 reason_str = "internal task abort set";
4236 break;
4237 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4238 reason_str = "internal clear task set";
4239 break;
4240 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4241 reason_str = "internal query task";
4242 break;
4243 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4244 reason_str = "sata init failure";
4245 break;
4246 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4247 reason_str = "internal device reset complete";
4248 break;
4249 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4250 reason_str = "internal task abort complete";
4251 break;
4252 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4253 reason_str = "internal async notification";
4254 break;
4255 default:
4256 reason_str = "unknown reason";
4257 break;
4258 }
4259 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4260 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4261 reason_str, le16_to_cpu(event_data->DevHandle),
4262 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4263 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4264 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4265 event_data->ASC, event_data->ASCQ);
4266 printk(KERN_INFO "\n");
4267}
4268#endif
4269
4270/**
4271 * _scsih_sas_device_status_change_event - handle device status change
4272 * @ioc: per adapter object
7b936b02 4273 * @fw_event: The fw_event_work object
635374e7
EM
4274 * Context: user.
4275 *
4276 * Return nothing.
4277 */
4278static void
7b936b02
KD
4279_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4280 struct fw_event_work *fw_event)
635374e7
EM
4281{
4282#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4283 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
7b936b02
KD
4284 _scsih_sas_device_status_change_event_debug(ioc,
4285 fw_event->event_data);
635374e7
EM
4286#endif
4287}
4288
4289#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4290/**
4291 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4292 * @ioc: per adapter object
4293 * @event_data: event data payload
4294 * Context: user.
4295 *
4296 * Return nothing.
4297 */
4298static void
4299_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4300 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4301{
4302 char *reason_str = NULL;
4303
4304 switch (event_data->ReasonCode) {
4305 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4306 reason_str = "enclosure add";
4307 break;
4308 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4309 reason_str = "enclosure remove";
4310 break;
4311 default:
4312 reason_str = "unknown reason";
4313 break;
4314 }
4315
4316 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4317 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4318 " number slots(%d)\n", ioc->name, reason_str,
4319 le16_to_cpu(event_data->EnclosureHandle),
4320 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4321 le16_to_cpu(event_data->StartSlot));
4322}
4323#endif
4324
4325/**
4326 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4327 * @ioc: per adapter object
7b936b02 4328 * @fw_event: The fw_event_work object
635374e7
EM
4329 * Context: user.
4330 *
4331 * Return nothing.
4332 */
4333static void
4334_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
7b936b02 4335 struct fw_event_work *fw_event)
635374e7
EM
4336{
4337#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4338 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4339 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
7b936b02 4340 fw_event->event_data);
635374e7
EM
4341#endif
4342}
4343
4344/**
4345 * _scsih_sas_broadcast_primative_event - handle broadcast events
4346 * @ioc: per adapter object
7b936b02 4347 * @fw_event: The fw_event_work object
635374e7
EM
4348 * Context: user.
4349 *
4350 * Return nothing.
4351 */
4352static void
7b936b02
KD
4353_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4354 struct fw_event_work *fw_event)
635374e7
EM
4355{
4356 struct scsi_cmnd *scmd;
4357 u16 smid, handle;
4358 u32 lun;
4359 struct MPT2SAS_DEVICE *sas_device_priv_data;
4360 u32 termination_count;
4361 u32 query_count;
4362 Mpi2SCSITaskManagementReply_t *mpi_reply;
7b936b02
KD
4363#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4364 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4365#endif
635374e7
EM
4366 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4367 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4368 event_data->PortWidth));
635374e7
EM
4369 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4370 __func__));
4371
4372 mutex_lock(&ioc->tm_cmds.mutex);
4373 termination_count = 0;
4374 query_count = 0;
4375 mpi_reply = ioc->tm_cmds.reply;
595bb0bd 4376 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
635374e7
EM
4377 scmd = _scsih_scsi_lookup_get(ioc, smid);
4378 if (!scmd)
4379 continue;
4380 sas_device_priv_data = scmd->device->hostdata;
4381 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4382 continue;
4383 /* skip hidden raid components */
4384 if (sas_device_priv_data->sas_target->flags &
4385 MPT_TARGET_FLAGS_RAID_COMPONENT)
4386 continue;
4387 /* skip volumes */
4388 if (sas_device_priv_data->sas_target->flags &
4389 MPT_TARGET_FLAGS_VOLUME)
4390 continue;
4391
4392 handle = sas_device_priv_data->sas_target->handle;
4393 lun = sas_device_priv_data->lun;
4394 query_count++;
4395
4396 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4397 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
8901cbb4 4398 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
635374e7
EM
4399
4400 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4401 (mpi_reply->ResponseCode ==
4402 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4403 mpi_reply->ResponseCode ==
4404 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4405 continue;
4406
4407 mpt2sas_scsih_issue_tm(ioc, handle, lun,
8901cbb4
EM
4408 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4409 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
635374e7
EM
4410 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4411 }
635374e7
EM
4412 ioc->broadcast_aen_busy = 0;
4413 mutex_unlock(&ioc->tm_cmds.mutex);
4414
4415 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4416 "%s - exit, query_count = %d termination_count = %d\n",
4417 ioc->name, __func__, query_count, termination_count));
4418}
4419
4420/**
4421 * _scsih_sas_discovery_event - handle discovery events
4422 * @ioc: per adapter object
7b936b02 4423 * @fw_event: The fw_event_work object
635374e7
EM
4424 * Context: user.
4425 *
4426 * Return nothing.
4427 */
4428static void
7b936b02
KD
4429_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4430 struct fw_event_work *fw_event)
635374e7 4431{
7b936b02
KD
4432 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4433
635374e7
EM
4434#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4435 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4436 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4437 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4438 "start" : "stop");
4439 if (event_data->DiscoveryStatus)
595bb0bd
KD
4440 printk("discovery_status(0x%08x)",
4441 le32_to_cpu(event_data->DiscoveryStatus));
635374e7
EM
4442 printk("\n");
4443 }
4444#endif
4445
4446 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4447 !ioc->sas_hba.num_phys)
4448 _scsih_sas_host_add(ioc);
4449}
4450
4451/**
4452 * _scsih_reprobe_lun - reprobing lun
4453 * @sdev: scsi device struct
4454 * @no_uld_attach: sdev->no_uld_attach flag setting
4455 *
4456 **/
4457static void
4458_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4459{
4460 int rc;
4461
4462 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4463 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4464 sdev->no_uld_attach ? "hidding" : "exposing");
4465 rc = scsi_device_reprobe(sdev);
4466}
4467
4468/**
4469 * _scsih_reprobe_target - reprobing target
4470 * @starget: scsi target struct
4471 * @no_uld_attach: sdev->no_uld_attach flag setting
4472 *
4473 * Note: no_uld_attach flag determines whether the disk device is attached
4474 * to block layer. A value of `1` means to not attach.
4475 **/
4476static void
4477_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4478{
4479 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4480
4481 if (no_uld_attach)
4482 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4483 else
4484 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4485
4486 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4487 _scsih_reprobe_lun);
4488}
4489/**
4490 * _scsih_sas_volume_add - add new volume
4491 * @ioc: per adapter object
4492 * @element: IR config element data
4493 * Context: user.
4494 *
4495 * Return nothing.
4496 */
4497static void
4498_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4499 Mpi2EventIrConfigElement_t *element)
4500{
4501 struct _raid_device *raid_device;
4502 unsigned long flags;
4503 u64 wwid;
4504 u16 handle = le16_to_cpu(element->VolDevHandle);
4505 int rc;
4506
635374e7
EM
4507 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4508 if (!wwid) {
4509 printk(MPT2SAS_ERR_FMT
4510 "failure at %s:%d/%s()!\n", ioc->name,
4511 __FILE__, __LINE__, __func__);
4512 return;
4513 }
4514
4515 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4516 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4517 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4518
4519 if (raid_device)
4520 return;
4521
4522 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4523 if (!raid_device) {
4524 printk(MPT2SAS_ERR_FMT
4525 "failure at %s:%d/%s()!\n", ioc->name,
4526 __FILE__, __LINE__, __func__);
4527 return;
4528 }
4529
4530 raid_device->id = ioc->sas_id++;
4531 raid_device->channel = RAID_CHANNEL;
4532 raid_device->handle = handle;
4533 raid_device->wwid = wwid;
4534 _scsih_raid_device_add(ioc, raid_device);
4535 if (!ioc->wait_for_port_enable_to_complete) {
4536 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4537 raid_device->id, 0);
4538 if (rc)
4539 _scsih_raid_device_remove(ioc, raid_device);
4540 } else
4541 _scsih_determine_boot_device(ioc, raid_device, 1);
4542}
4543
4544/**
4545 * _scsih_sas_volume_delete - delete volume
4546 * @ioc: per adapter object
4547 * @element: IR config element data
4548 * Context: user.
4549 *
4550 * Return nothing.
4551 */
4552static void
4553_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4554 Mpi2EventIrConfigElement_t *element)
4555{
4556 struct _raid_device *raid_device;
4557 u16 handle = le16_to_cpu(element->VolDevHandle);
4558 unsigned long flags;
4559 struct MPT2SAS_TARGET *sas_target_priv_data;
4560
635374e7
EM
4561 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4562 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4563 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4564 if (!raid_device)
4565 return;
4566 if (raid_device->starget) {
4567 sas_target_priv_data = raid_device->starget->hostdata;
4568 sas_target_priv_data->deleted = 1;
4569 scsi_remove_target(&raid_device->starget->dev);
4570 }
4571 _scsih_raid_device_remove(ioc, raid_device);
4572}
4573
4574/**
4575 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4576 * @ioc: per adapter object
4577 * @element: IR config element data
4578 * Context: user.
4579 *
4580 * Return nothing.
4581 */
4582static void
4583_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4584 Mpi2EventIrConfigElement_t *element)
4585{
4586 struct _sas_device *sas_device;
4587 unsigned long flags;
4588 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4589
4590 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4591 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4592 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4593 if (!sas_device)
4594 return;
4595
4596 /* exposing raid component */
4597 sas_device->volume_handle = 0;
4598 sas_device->volume_wwid = 0;
4599 sas_device->hidden_raid_component = 0;
4600 _scsih_reprobe_target(sas_device->starget, 0);
4601}
4602
4603/**
4604 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4605 * @ioc: per adapter object
4606 * @element: IR config element data
4607 * Context: user.
4608 *
4609 * Return nothing.
4610 */
4611static void
4612_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4613 Mpi2EventIrConfigElement_t *element)
4614{
4615 struct _sas_device *sas_device;
4616 unsigned long flags;
4617 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4618
4619 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4620 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4621 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4622 if (!sas_device)
4623 return;
4624
4625 /* hiding raid component */
4626 mpt2sas_config_get_volume_handle(ioc, handle,
4627 &sas_device->volume_handle);
4628 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4629 &sas_device->volume_wwid);
4630 sas_device->hidden_raid_component = 1;
4631 _scsih_reprobe_target(sas_device->starget, 1);
4632}
4633
4634/**
4635 * _scsih_sas_pd_delete - delete pd component
4636 * @ioc: per adapter object
4637 * @element: IR config element data
4638 * Context: user.
4639 *
4640 * Return nothing.
4641 */
4642static void
4643_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4644 Mpi2EventIrConfigElement_t *element)
4645{
4646 struct _sas_device *sas_device;
4647 unsigned long flags;
4648 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4649
4650 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4651 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4652 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4653 if (!sas_device)
4654 return;
c5e039be 4655 _scsih_remove_device(ioc, sas_device);
635374e7
EM
4656}
4657
4658/**
4659 * _scsih_sas_pd_add - remove pd component
4660 * @ioc: per adapter object
4661 * @element: IR config element data
4662 * Context: user.
4663 *
4664 * Return nothing.
4665 */
4666static void
4667_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4668 Mpi2EventIrConfigElement_t *element)
4669{
4670 struct _sas_device *sas_device;
4671 unsigned long flags;
4672 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
62727a7b
KD
4673 Mpi2ConfigReply_t mpi_reply;
4674 Mpi2SasDevicePage0_t sas_device_pg0;
4675 u32 ioc_status;
c5e039be
KD
4676 u64 sas_address;
4677 u16 parent_handle;
635374e7
EM
4678
4679 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4680 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4681 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
62727a7b 4682 if (sas_device) {
635374e7 4683 sas_device->hidden_raid_component = 1;
62727a7b
KD
4684 return;
4685 }
4686
4687 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4688 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4690 ioc->name, __FILE__, __LINE__, __func__);
4691 return;
4692 }
4693
4694 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4695 MPI2_IOCSTATUS_MASK;
4696 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4697 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4698 ioc->name, __FILE__, __LINE__, __func__);
4699 return;
4700 }
4701
c5e039be
KD
4702 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4703 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4704 mpt2sas_transport_update_links(ioc, sas_address, handle,
4705 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
62727a7b
KD
4706
4707 _scsih_add_device(ioc, handle, 0, 1);
635374e7
EM
4708}
4709
4710#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4711/**
4712 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4713 * @ioc: per adapter object
4714 * @event_data: event data payload
4715 * Context: user.
4716 *
4717 * Return nothing.
4718 */
4719static void
4720_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4721 Mpi2EventDataIrConfigChangeList_t *event_data)
4722{
4723 Mpi2EventIrConfigElement_t *element;
4724 u8 element_type;
4725 int i;
4726 char *reason_str = NULL, *element_str = NULL;
4727
4728 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4729
4730 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4731 ioc->name, (le32_to_cpu(event_data->Flags) &
4732 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4733 "foreign" : "native", event_data->NumElements);
4734 for (i = 0; i < event_data->NumElements; i++, element++) {
4735 switch (element->ReasonCode) {
4736 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4737 reason_str = "add";
4738 break;
4739 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4740 reason_str = "remove";
4741 break;
4742 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4743 reason_str = "no change";
4744 break;
4745 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4746 reason_str = "hide";
4747 break;
4748 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4749 reason_str = "unhide";
4750 break;
4751 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4752 reason_str = "volume_created";
4753 break;
4754 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4755 reason_str = "volume_deleted";
4756 break;
4757 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4758 reason_str = "pd_created";
4759 break;
4760 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4761 reason_str = "pd_deleted";
4762 break;
4763 default:
4764 reason_str = "unknown reason";
4765 break;
4766 }
4767 element_type = le16_to_cpu(element->ElementFlags) &
4768 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4769 switch (element_type) {
4770 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4771 element_str = "volume";
4772 break;
4773 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4774 element_str = "phys disk";
4775 break;
4776 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4777 element_str = "hot spare";
4778 break;
4779 default:
4780 element_str = "unknown element";
4781 break;
4782 }
4783 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4784 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4785 reason_str, le16_to_cpu(element->VolDevHandle),
4786 le16_to_cpu(element->PhysDiskDevHandle),
4787 element->PhysDiskNum);
4788 }
4789}
4790#endif
4791
4792/**
4793 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4794 * @ioc: per adapter object
7b936b02 4795 * @fw_event: The fw_event_work object
635374e7
EM
4796 * Context: user.
4797 *
4798 * Return nothing.
4799 */
4800static void
7b936b02
KD
4801_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4802 struct fw_event_work *fw_event)
635374e7
EM
4803{
4804 Mpi2EventIrConfigElement_t *element;
4805 int i;
62727a7b 4806 u8 foreign_config;
7b936b02 4807 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
635374e7
EM
4808
4809#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4810 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4811 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4812
4813#endif
62727a7b
KD
4814 foreign_config = (le32_to_cpu(event_data->Flags) &
4815 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
635374e7
EM
4816
4817 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4818 for (i = 0; i < event_data->NumElements; i++, element++) {
4819
4820 switch (element->ReasonCode) {
4821 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4822 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
62727a7b
KD
4823 if (!foreign_config)
4824 _scsih_sas_volume_add(ioc, element);
635374e7
EM
4825 break;
4826 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4827 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
62727a7b
KD
4828 if (!foreign_config)
4829 _scsih_sas_volume_delete(ioc, element);
635374e7
EM
4830 break;
4831 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4832 _scsih_sas_pd_hide(ioc, element);
4833 break;
4834 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4835 _scsih_sas_pd_expose(ioc, element);
4836 break;
4837 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4838 _scsih_sas_pd_add(ioc, element);
4839 break;
4840 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4841 _scsih_sas_pd_delete(ioc, element);
4842 break;
4843 }
4844 }
4845}
4846
4847/**
4848 * _scsih_sas_ir_volume_event - IR volume event
4849 * @ioc: per adapter object
7b936b02 4850 * @fw_event: The fw_event_work object
635374e7
EM
4851 * Context: user.
4852 *
4853 * Return nothing.
4854 */
4855static void
7b936b02
KD
4856_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
4857 struct fw_event_work *fw_event)
635374e7
EM
4858{
4859 u64 wwid;
4860 unsigned long flags;
4861 struct _raid_device *raid_device;
4862 u16 handle;
4863 u32 state;
4864 int rc;
4865 struct MPT2SAS_TARGET *sas_target_priv_data;
7b936b02 4866 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
635374e7
EM
4867
4868 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4869 return;
4870
4871 handle = le16_to_cpu(event_data->VolDevHandle);
4872 state = le32_to_cpu(event_data->NewValue);
4873 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4874 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4875 le32_to_cpu(event_data->PreviousValue), state));
4876
4877 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4878 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4879 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4880
4881 switch (state) {
4882 case MPI2_RAID_VOL_STATE_MISSING:
4883 case MPI2_RAID_VOL_STATE_FAILED:
4884 if (!raid_device)
4885 break;
4886 if (raid_device->starget) {
4887 sas_target_priv_data = raid_device->starget->hostdata;
4888 sas_target_priv_data->deleted = 1;
4889 scsi_remove_target(&raid_device->starget->dev);
4890 }
4891 _scsih_raid_device_remove(ioc, raid_device);
4892 break;
4893
4894 case MPI2_RAID_VOL_STATE_ONLINE:
4895 case MPI2_RAID_VOL_STATE_DEGRADED:
4896 case MPI2_RAID_VOL_STATE_OPTIMAL:
4897 if (raid_device)
4898 break;
4899
4900 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4901 if (!wwid) {
4902 printk(MPT2SAS_ERR_FMT
4903 "failure at %s:%d/%s()!\n", ioc->name,
4904 __FILE__, __LINE__, __func__);
4905 break;
4906 }
4907
4908 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4909 if (!raid_device) {
4910 printk(MPT2SAS_ERR_FMT
4911 "failure at %s:%d/%s()!\n", ioc->name,
4912 __FILE__, __LINE__, __func__);
4913 break;
4914 }
4915
4916 raid_device->id = ioc->sas_id++;
4917 raid_device->channel = RAID_CHANNEL;
4918 raid_device->handle = handle;
4919 raid_device->wwid = wwid;
4920 _scsih_raid_device_add(ioc, raid_device);
4921 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4922 raid_device->id, 0);
4923 if (rc)
4924 _scsih_raid_device_remove(ioc, raid_device);
4925 break;
4926
4927 case MPI2_RAID_VOL_STATE_INITIALIZING:
4928 default:
4929 break;
4930 }
4931}
4932
4933/**
4934 * _scsih_sas_ir_physical_disk_event - PD event
4935 * @ioc: per adapter object
7b936b02 4936 * @fw_event: The fw_event_work object
635374e7
EM
4937 * Context: user.
4938 *
4939 * Return nothing.
4940 */
4941static void
7b936b02
KD
4942_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
4943 struct fw_event_work *fw_event)
635374e7 4944{
c5e039be 4945 u16 handle, parent_handle;
635374e7
EM
4946 u32 state;
4947 struct _sas_device *sas_device;
4948 unsigned long flags;
62727a7b
KD
4949 Mpi2ConfigReply_t mpi_reply;
4950 Mpi2SasDevicePage0_t sas_device_pg0;
4951 u32 ioc_status;
7b936b02 4952 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
c5e039be 4953 u64 sas_address;
635374e7
EM
4954
4955 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4956 return;
4957
4958 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4959 state = le32_to_cpu(event_data->NewValue);
4960
4961 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4962 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4963 le32_to_cpu(event_data->PreviousValue), state));
4964
4965 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4966 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4967 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4968
4969 switch (state) {
635374e7
EM
4970 case MPI2_RAID_PD_STATE_ONLINE:
4971 case MPI2_RAID_PD_STATE_DEGRADED:
4972 case MPI2_RAID_PD_STATE_REBUILDING:
4973 case MPI2_RAID_PD_STATE_OPTIMAL:
62727a7b 4974 if (sas_device) {
635374e7 4975 sas_device->hidden_raid_component = 1;
62727a7b
KD
4976 return;
4977 }
4978
4979 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4980 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4981 handle))) {
4982 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4983 ioc->name, __FILE__, __LINE__, __func__);
4984 return;
4985 }
4986
4987 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4988 MPI2_IOCSTATUS_MASK;
4989 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4990 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4991 ioc->name, __FILE__, __LINE__, __func__);
4992 return;
4993 }
4994
c5e039be
KD
4995 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
4996 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
4997 mpt2sas_transport_update_links(ioc, sas_address, handle,
4998 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
62727a7b
KD
4999
5000 _scsih_add_device(ioc, handle, 0, 1);
5001
635374e7
EM
5002 break;
5003
62727a7b 5004 case MPI2_RAID_PD_STATE_OFFLINE:
635374e7
EM
5005 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5006 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
5007 case MPI2_RAID_PD_STATE_HOT_SPARE:
5008 default:
5009 break;
5010 }
5011}
5012
5013#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5014/**
5015 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5016 * @ioc: per adapter object
5017 * @event_data: event data payload
5018 * Context: user.
5019 *
5020 * Return nothing.
5021 */
5022static void
5023_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5024 Mpi2EventDataIrOperationStatus_t *event_data)
5025{
5026 char *reason_str = NULL;
5027
5028 switch (event_data->RAIDOperation) {
5029 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5030 reason_str = "resync";
5031 break;
5032 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5033 reason_str = "online capacity expansion";
5034 break;
5035 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5036 reason_str = "consistency check";
5037 break;
5038 default:
5039 reason_str = "unknown reason";
5040 break;
5041 }
5042
5043 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5044 "\thandle(0x%04x), percent complete(%d)\n",
5045 ioc->name, reason_str,
5046 le16_to_cpu(event_data->VolDevHandle),
5047 event_data->PercentComplete);
5048}
5049#endif
5050
5051/**
5052 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5053 * @ioc: per adapter object
7b936b02 5054 * @fw_event: The fw_event_work object
635374e7
EM
5055 * Context: user.
5056 *
5057 * Return nothing.
5058 */
5059static void
7b936b02
KD
5060_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5061 struct fw_event_work *fw_event)
635374e7
EM
5062{
5063#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5064 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
7b936b02
KD
5065 _scsih_sas_ir_operation_status_event_debug(ioc,
5066 fw_event->event_data);
635374e7
EM
5067#endif
5068}
5069
5070/**
5071 * _scsih_task_set_full - handle task set full
5072 * @ioc: per adapter object
7b936b02 5073 * @fw_event: The fw_event_work object
635374e7
EM
5074 * Context: user.
5075 *
5076 * Throttle back qdepth.
5077 */
5078static void
7b936b02
KD
5079_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5080 *fw_event)
635374e7
EM
5081{
5082 unsigned long flags;
5083 struct _sas_device *sas_device;
5084 static struct _raid_device *raid_device;
5085 struct scsi_device *sdev;
5086 int depth;
5087 u16 current_depth;
5088 u16 handle;
5089 int id, channel;
5090 u64 sas_address;
7b936b02 5091 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
635374e7
EM
5092
5093 current_depth = le16_to_cpu(event_data->CurrentDepth);
5094 handle = le16_to_cpu(event_data->DevHandle);
5095 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5096 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5097 if (!sas_device) {
5098 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5099 return;
5100 }
5101 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5102 id = sas_device->id;
5103 channel = sas_device->channel;
5104 sas_address = sas_device->sas_address;
5105
5106 /* if hidden raid component, then change to volume characteristics */
5107 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5108 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5109 raid_device = _scsih_raid_device_find_by_handle(
5110 ioc, sas_device->volume_handle);
5111 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5112 if (raid_device) {
5113 id = raid_device->id;
5114 channel = raid_device->channel;
5115 handle = raid_device->handle;
5116 sas_address = raid_device->wwid;
5117 }
5118 }
5119
5120 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5121 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5122 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5123 handle, (unsigned long long)sas_address, current_depth);
5124
5125 shost_for_each_device(sdev, ioc->shost) {
5126 if (sdev->id == id && sdev->channel == channel) {
5127 if (current_depth > sdev->queue_depth) {
5128 if (ioc->logging_level &
5129 MPT_DEBUG_TASK_SET_FULL)
5130 sdev_printk(KERN_INFO, sdev, "strange "
5131 "observation, the queue depth is"
5132 " (%d) meanwhile fw queue depth "
5133 "is (%d)\n", sdev->queue_depth,
5134 current_depth);
5135 continue;
5136 }
5137 depth = scsi_track_queue_full(sdev,
5138 current_depth - 1);
5139 if (depth > 0)
5140 sdev_printk(KERN_INFO, sdev, "Queue depth "
5141 "reduced to (%d)\n", depth);
5142 else if (depth < 0)
5143 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5144 "Queueing is being disabled\n");
5145 else if (depth == 0)
5146 if (ioc->logging_level &
5147 MPT_DEBUG_TASK_SET_FULL)
5148 sdev_printk(KERN_INFO, sdev,
5149 "Queue depth not changed yet\n");
5150 }
5151 }
5152}
5153
5154/**
5155 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5156 * @ioc: per adapter object
5157 * @sas_address: sas address
5158 * @slot: enclosure slot id
5159 * @handle: device handle
5160 *
5161 * After host reset, find out whether devices are still responding.
5162 * Used in _scsi_remove_unresponsive_sas_devices.
5163 *
5164 * Return nothing.
5165 */
5166static void
5167_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5168 u16 slot, u16 handle)
5169{
5170 struct MPT2SAS_TARGET *sas_target_priv_data;
5171 struct scsi_target *starget;
5172 struct _sas_device *sas_device;
5173 unsigned long flags;
5174
5175 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5176 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5177 if (sas_device->sas_address == sas_address &&
5178 sas_device->slot == slot && sas_device->starget) {
5179 sas_device->responding = 1;
77e63ed4
KD
5180 sas_device->state = 0;
5181 starget = sas_device->starget;
5182 sas_target_priv_data = starget->hostdata;
5183 sas_target_priv_data->tm_busy = 0;
635374e7
EM
5184 starget_printk(KERN_INFO, sas_device->starget,
5185 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5186 "logical id(0x%016llx), slot(%d)\n", handle,
5187 (unsigned long long)sas_device->sas_address,
5188 (unsigned long long)
5189 sas_device->enclosure_logical_id,
5190 sas_device->slot);
5191 if (sas_device->handle == handle)
5192 goto out;
5193 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5194 sas_device->handle);
5195 sas_device->handle = handle;
635374e7
EM
5196 sas_target_priv_data->handle = handle;
5197 goto out;
5198 }
5199 }
5200 out:
5201 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5202}
5203
5204/**
5205 * _scsih_search_responding_sas_devices -
5206 * @ioc: per adapter object
5207 *
5208 * After host reset, find out whether devices are still responding.
5209 * If not remove.
5210 *
5211 * Return nothing.
5212 */
5213static void
5214_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5215{
5216 Mpi2SasDevicePage0_t sas_device_pg0;
5217 Mpi2ConfigReply_t mpi_reply;
5218 u16 ioc_status;
5219 __le64 sas_address;
5220 u16 handle;
5221 u32 device_info;
5222 u16 slot;
5223
5224 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5225
5226 if (list_empty(&ioc->sas_device_list))
5227 return;
5228
5229 handle = 0xFFFF;
5230 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5231 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5232 handle))) {
5233 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5234 MPI2_IOCSTATUS_MASK;
5235 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5236 break;
5237 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5238 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5239 if (!(_scsih_is_end_device(device_info)))
5240 continue;
5241 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5242 slot = le16_to_cpu(sas_device_pg0.Slot);
5243 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5244 handle);
5245 }
5246}
5247
5248/**
5249 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5250 * @ioc: per adapter object
5251 * @wwid: world wide identifier for raid volume
5252 * @handle: device handle
5253 *
5254 * After host reset, find out whether devices are still responding.
5255 * Used in _scsi_remove_unresponsive_raid_devices.
5256 *
5257 * Return nothing.
5258 */
5259static void
5260_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5261 u16 handle)
5262{
5263 struct MPT2SAS_TARGET *sas_target_priv_data;
5264 struct scsi_target *starget;
5265 struct _raid_device *raid_device;
5266 unsigned long flags;
5267
5268 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5269 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5270 if (raid_device->wwid == wwid && raid_device->starget) {
5271 raid_device->responding = 1;
5272 starget_printk(KERN_INFO, raid_device->starget,
5273 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5274 (unsigned long long)raid_device->wwid);
5275 if (raid_device->handle == handle)
5276 goto out;
5277 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5278 raid_device->handle);
5279 raid_device->handle = handle;
5280 starget = raid_device->starget;
5281 sas_target_priv_data = starget->hostdata;
5282 sas_target_priv_data->handle = handle;
5283 goto out;
5284 }
5285 }
5286 out:
5287 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5288}
5289
5290/**
5291 * _scsih_search_responding_raid_devices -
5292 * @ioc: per adapter object
5293 *
5294 * After host reset, find out whether devices are still responding.
5295 * If not remove.
5296 *
5297 * Return nothing.
5298 */
5299static void
5300_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5301{
5302 Mpi2RaidVolPage1_t volume_pg1;
5303 Mpi2ConfigReply_t mpi_reply;
5304 u16 ioc_status;
5305 u16 handle;
5306
5307 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5308
5309 if (list_empty(&ioc->raid_device_list))
5310 return;
5311
5312 handle = 0xFFFF;
5313 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5314 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5315 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5316 MPI2_IOCSTATUS_MASK;
5317 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5318 break;
5319 handle = le16_to_cpu(volume_pg1.DevHandle);
5320 _scsih_mark_responding_raid_device(ioc,
5321 le64_to_cpu(volume_pg1.WWID), handle);
5322 }
5323}
5324
5325/**
5326 * _scsih_mark_responding_expander - mark a expander as responding
5327 * @ioc: per adapter object
5328 * @sas_address: sas address
5329 * @handle:
5330 *
5331 * After host reset, find out whether devices are still responding.
5332 * Used in _scsi_remove_unresponsive_expanders.
5333 *
5334 * Return nothing.
5335 */
5336static void
5337_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5338 u16 handle)
5339{
5340 struct _sas_node *sas_expander;
5341 unsigned long flags;
c5e039be 5342 int i;
635374e7
EM
5343
5344 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5345 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
c5e039be
KD
5346 if (sas_expander->sas_address != sas_address)
5347 continue;
5348 sas_expander->responding = 1;
5349 if (sas_expander->handle == handle)
635374e7 5350 goto out;
c5e039be
KD
5351 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
5352 " from(0x%04x) to (0x%04x)!!!\n",
5353 (unsigned long long)sas_expander->sas_address,
5354 sas_expander->handle, handle);
5355 sas_expander->handle = handle;
5356 for (i = 0 ; i < sas_expander->num_phys ; i++)
5357 sas_expander->phy[i].handle = handle;
5358 goto out;
635374e7
EM
5359 }
5360 out:
5361 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5362}
5363
5364/**
5365 * _scsih_search_responding_expanders -
5366 * @ioc: per adapter object
5367 *
5368 * After host reset, find out whether devices are still responding.
5369 * If not remove.
5370 *
5371 * Return nothing.
5372 */
5373static void
5374_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5375{
5376 Mpi2ExpanderPage0_t expander_pg0;
5377 Mpi2ConfigReply_t mpi_reply;
5378 u16 ioc_status;
5379 __le64 sas_address;
5380 u16 handle;
5381
5382 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5383
5384 if (list_empty(&ioc->sas_expander_list))
5385 return;
5386
5387 handle = 0xFFFF;
5388 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5389 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5390
5391 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5392 MPI2_IOCSTATUS_MASK;
5393 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5394 break;
5395
5396 handle = le16_to_cpu(expander_pg0.DevHandle);
5397 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5398 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5399 "sas_addr(0x%016llx)\n", handle,
5400 (unsigned long long)sas_address);
5401 _scsih_mark_responding_expander(ioc, sas_address, handle);
5402 }
5403
5404}
5405
5406/**
5407 * _scsih_remove_unresponding_devices - removing unresponding devices
5408 * @ioc: per adapter object
5409 *
5410 * Return nothing.
5411 */
5412static void
5413_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5414{
5415 struct _sas_device *sas_device, *sas_device_next;
cd4e12e8 5416 struct _sas_node *sas_expander;
635374e7 5417 struct _raid_device *raid_device, *raid_device_next;
635374e7 5418
635374e7
EM
5419
5420 list_for_each_entry_safe(sas_device, sas_device_next,
5421 &ioc->sas_device_list, list) {
5422 if (sas_device->responding) {
5423 sas_device->responding = 0;
5424 continue;
5425 }
5426 if (sas_device->starget)
5427 starget_printk(KERN_INFO, sas_device->starget,
5428 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5429 "enclosure logical id(0x%016llx), slot(%d)\n",
5430 sas_device->handle,
5431 (unsigned long long)sas_device->sas_address,
5432 (unsigned long long)
5433 sas_device->enclosure_logical_id,
5434 sas_device->slot);
c5e039be
KD
5435 /* invalidate the device handle */
5436 sas_device->handle = 0;
5437 _scsih_remove_device(ioc, sas_device);
635374e7
EM
5438 }
5439
5440 list_for_each_entry_safe(raid_device, raid_device_next,
5441 &ioc->raid_device_list, list) {
5442 if (raid_device->responding) {
5443 raid_device->responding = 0;
5444 continue;
5445 }
5446 if (raid_device->starget) {
5447 starget_printk(KERN_INFO, raid_device->starget,
5448 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5449 raid_device->handle,
5450 (unsigned long long)raid_device->wwid);
5451 scsi_remove_target(&raid_device->starget->dev);
5452 }
5453 _scsih_raid_device_remove(ioc, raid_device);
5454 }
5455
cd4e12e8
KD
5456 retry_expander_search:
5457 sas_expander = NULL;
5458 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
635374e7
EM
5459 if (sas_expander->responding) {
5460 sas_expander->responding = 0;
5461 continue;
5462 }
c5e039be 5463 _scsih_expander_remove(ioc, sas_expander->sas_address);
cd4e12e8
KD
5464 goto retry_expander_search;
5465 }
5466}
5467
5468/**
5469 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5470 * @ioc: per adapter object
5471 * @reset_phase: phase
5472 *
5473 * The handler for doing any required cleanup or initialization.
5474 *
5475 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5476 * MPT2_IOC_DONE_RESET
5477 *
5478 * Return nothing.
5479 */
5480void
5481mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5482{
5483 switch (reset_phase) {
5484 case MPT2_IOC_PRE_RESET:
5485 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5486 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5487 _scsih_fw_event_off(ioc);
5488 break;
5489 case MPT2_IOC_AFTER_RESET:
5490 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5491 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5492 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5493 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5494 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5495 complete(&ioc->tm_cmds.done);
5496 }
5497 _scsih_fw_event_on(ioc);
5498 _scsih_flush_running_cmds(ioc);
5499 break;
5500 case MPT2_IOC_DONE_RESET:
5501 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5502 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
c5e039be 5503 _scsih_sas_host_refresh(ioc);
cd4e12e8
KD
5504 _scsih_search_responding_sas_devices(ioc);
5505 _scsih_search_responding_raid_devices(ioc);
5506 _scsih_search_responding_expanders(ioc);
5507 break;
5508 case MPT2_IOC_RUNNING:
5509 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5510 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5511 _scsih_remove_unresponding_devices(ioc);
5512 break;
635374e7
EM
5513 }
5514}
5515
5516/**
5517 * _firmware_event_work - delayed task for processing firmware events
5518 * @ioc: per adapter object
5519 * @work: equal to the fw_event_work object
5520 * Context: user.
5521 *
5522 * Return nothing.
5523 */
5524static void
5525_firmware_event_work(struct work_struct *work)
5526{
5527 struct fw_event_work *fw_event = container_of(work,
6f92a7a0 5528 struct fw_event_work, work);
635374e7
EM
5529 unsigned long flags;
5530 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5531
635374e7
EM
5532 /* the queue is being flushed so ignore this event */
5533 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5534 if (ioc->fw_events_off || ioc->remove_host) {
5535 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5536 _scsih_fw_event_free(ioc, fw_event);
5537 return;
5538 }
5539 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5540
635374e7 5541 if (ioc->shost_recovery) {
635374e7
EM
5542 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5543 return;
5544 }
635374e7
EM
5545
5546 switch (fw_event->event) {
5547 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7b936b02 5548 _scsih_sas_topology_change_event(ioc, fw_event);
635374e7
EM
5549 break;
5550 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7b936b02
KD
5551 _scsih_sas_device_status_change_event(ioc,
5552 fw_event);
635374e7
EM
5553 break;
5554 case MPI2_EVENT_SAS_DISCOVERY:
7b936b02
KD
5555 _scsih_sas_discovery_event(ioc,
5556 fw_event);
635374e7
EM
5557 break;
5558 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7b936b02
KD
5559 _scsih_sas_broadcast_primative_event(ioc,
5560 fw_event);
635374e7
EM
5561 break;
5562 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5563 _scsih_sas_enclosure_dev_status_change_event(ioc,
7b936b02 5564 fw_event);
635374e7
EM
5565 break;
5566 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7b936b02 5567 _scsih_sas_ir_config_change_event(ioc, fw_event);
635374e7
EM
5568 break;
5569 case MPI2_EVENT_IR_VOLUME:
7b936b02 5570 _scsih_sas_ir_volume_event(ioc, fw_event);
635374e7
EM
5571 break;
5572 case MPI2_EVENT_IR_PHYSICAL_DISK:
7b936b02 5573 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
635374e7
EM
5574 break;
5575 case MPI2_EVENT_IR_OPERATION_STATUS:
7b936b02 5576 _scsih_sas_ir_operation_status_event(ioc, fw_event);
635374e7
EM
5577 break;
5578 case MPI2_EVENT_TASK_SET_FULL:
7b936b02 5579 _scsih_task_set_full(ioc, fw_event);
635374e7
EM
5580 break;
5581 }
5582 _scsih_fw_event_free(ioc, fw_event);
5583}
5584
5585/**
5586 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5587 * @ioc: per adapter object
7b936b02 5588 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
5589 * @reply: reply message frame(lower 32bit addr)
5590 * Context: interrupt.
5591 *
5592 * This function merely adds a new work task into ioc->firmware_event_thread.
5593 * The tasks are worked from _firmware_event_work in user context.
5594 *
77e63ed4
KD
5595 * Return 1 meaning mf should be freed from _base_interrupt
5596 * 0 means the mf is freed from this function.
635374e7 5597 */
77e63ed4 5598u8
7b936b02
KD
5599mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5600 u32 reply)
635374e7
EM
5601{
5602 struct fw_event_work *fw_event;
5603 Mpi2EventNotificationReply_t *mpi_reply;
5604 unsigned long flags;
5605 u16 event;
5606
5607 /* events turned off due to host reset or driver unloading */
5608 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5609 if (ioc->fw_events_off || ioc->remove_host) {
5610 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
77e63ed4 5611 return 1;
635374e7
EM
5612 }
5613 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5614
77e63ed4 5615 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
635374e7
EM
5616 event = le16_to_cpu(mpi_reply->Event);
5617
5618 switch (event) {
5619 /* handle these */
5620 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5621 {
5622 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5623 (Mpi2EventDataSasBroadcastPrimitive_t *)
5624 mpi_reply->EventData;
5625
5626 if (baen_data->Primitive !=
5627 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5628 ioc->broadcast_aen_busy)
77e63ed4 5629 return 1;
635374e7
EM
5630 ioc->broadcast_aen_busy = 1;
5631 break;
5632 }
5633
5634 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5635 _scsih_check_topo_delete_events(ioc,
5636 (Mpi2EventDataSasTopologyChangeList_t *)
5637 mpi_reply->EventData);
5638 break;
5639
5640 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5641 case MPI2_EVENT_IR_OPERATION_STATUS:
5642 case MPI2_EVENT_SAS_DISCOVERY:
5643 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5644 case MPI2_EVENT_IR_VOLUME:
5645 case MPI2_EVENT_IR_PHYSICAL_DISK:
5646 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5647 case MPI2_EVENT_TASK_SET_FULL:
5648 break;
5649
5650 default: /* ignore the rest */
77e63ed4 5651 return 1;
635374e7
EM
5652 }
5653
5654 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5655 if (!fw_event) {
5656 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5657 ioc->name, __FILE__, __LINE__, __func__);
77e63ed4 5658 return 1;
635374e7
EM
5659 }
5660 fw_event->event_data =
5661 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5662 if (!fw_event->event_data) {
5663 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5664 ioc->name, __FILE__, __LINE__, __func__);
5665 kfree(fw_event);
77e63ed4 5666 return 1;
635374e7
EM
5667 }
5668
5669 memcpy(fw_event->event_data, mpi_reply->EventData,
5670 mpi_reply->EventDataLength*4);
5671 fw_event->ioc = ioc;
7b936b02
KD
5672 fw_event->VF_ID = mpi_reply->VF_ID;
5673 fw_event->VP_ID = mpi_reply->VP_ID;
635374e7
EM
5674 fw_event->event = event;
5675 _scsih_fw_event_add(ioc, fw_event);
77e63ed4 5676 return 1;
635374e7
EM
5677}
5678
5679/* shost template */
5680static struct scsi_host_template scsih_driver_template = {
5681 .module = THIS_MODULE,
5682 .name = "Fusion MPT SAS Host",
5683 .proc_name = MPT2SAS_DRIVER_NAME,
d5d135b3
EM
5684 .queuecommand = _scsih_qcmd,
5685 .target_alloc = _scsih_target_alloc,
5686 .slave_alloc = _scsih_slave_alloc,
5687 .slave_configure = _scsih_slave_configure,
5688 .target_destroy = _scsih_target_destroy,
5689 .slave_destroy = _scsih_slave_destroy,
5690 .change_queue_depth = _scsih_change_queue_depth,
5691 .change_queue_type = _scsih_change_queue_type,
5692 .eh_abort_handler = _scsih_abort,
5693 .eh_device_reset_handler = _scsih_dev_reset,
5694 .eh_target_reset_handler = _scsih_target_reset,
5695 .eh_host_reset_handler = _scsih_host_reset,
5696 .bios_param = _scsih_bios_param,
635374e7
EM
5697 .can_queue = 1,
5698 .this_id = -1,
5699 .sg_tablesize = MPT2SAS_SG_DEPTH,
5700 .max_sectors = 8192,
5701 .cmd_per_lun = 7,
5702 .use_clustering = ENABLE_CLUSTERING,
5703 .shost_attrs = mpt2sas_host_attrs,
5704 .sdev_attrs = mpt2sas_dev_attrs,
5705};
5706
5707/**
5708 * _scsih_expander_node_remove - removing expander device from list.
5709 * @ioc: per adapter object
5710 * @sas_expander: the sas_device object
5711 * Context: Calling function should acquire ioc->sas_node_lock.
5712 *
5713 * Removing object and freeing associated memory from the
5714 * ioc->sas_expander_list.
5715 *
5716 * Return nothing.
5717 */
5718static void
5719_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5720 struct _sas_node *sas_expander)
5721{
5722 struct _sas_port *mpt2sas_port;
5723 struct _sas_device *sas_device;
5724 struct _sas_node *expander_sibling;
5725 unsigned long flags;
5726
5727 if (!sas_expander)
5728 return;
5729
5730 /* remove sibling ports attached to this expander */
5731 retry_device_search:
5732 list_for_each_entry(mpt2sas_port,
5733 &sas_expander->sas_port_list, port_list) {
5734 if (mpt2sas_port->remote_identify.device_type ==
5735 SAS_END_DEVICE) {
5736 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5737 sas_device =
5738 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5739 mpt2sas_port->remote_identify.sas_address);
5740 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5741 if (!sas_device)
5742 continue;
c5e039be 5743 _scsih_remove_device(ioc, sas_device);
155dd4c7
KD
5744 if (ioc->shost_recovery)
5745 return;
635374e7
EM
5746 goto retry_device_search;
5747 }
5748 }
5749
5750 retry_expander_search:
5751 list_for_each_entry(mpt2sas_port,
5752 &sas_expander->sas_port_list, port_list) {
5753
5754 if (mpt2sas_port->remote_identify.device_type ==
5755 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5756 mpt2sas_port->remote_identify.device_type ==
5757 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5758
5759 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5760 expander_sibling =
5761 mpt2sas_scsih_expander_find_by_sas_address(
5762 ioc, mpt2sas_port->remote_identify.sas_address);
5763 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5764 if (!expander_sibling)
5765 continue;
c5e039be
KD
5766 _scsih_expander_remove(ioc,
5767 expander_sibling->sas_address);
155dd4c7
KD
5768 if (ioc->shost_recovery)
5769 return;
635374e7
EM
5770 goto retry_expander_search;
5771 }
5772 }
5773
5774 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
c5e039be 5775 sas_expander->sas_address_parent);
635374e7
EM
5776
5777 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5778 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5779 sas_expander->handle, (unsigned long long)
5780 sas_expander->sas_address);
5781
5782 list_del(&sas_expander->list);
5783 kfree(sas_expander->phy);
5784 kfree(sas_expander);
5785}
5786
5787/**
d5d135b3 5788 * _scsih_remove - detach and remove add host
635374e7
EM
5789 * @pdev: PCI device struct
5790 *
5791 * Return nothing.
5792 */
5793static void __devexit
d5d135b3 5794_scsih_remove(struct pci_dev *pdev)
635374e7
EM
5795{
5796 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5797 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5798 struct _sas_port *mpt2sas_port;
5799 struct _sas_device *sas_device;
5800 struct _sas_node *expander_sibling;
5801 struct workqueue_struct *wq;
5802 unsigned long flags;
5803
5804 ioc->remove_host = 1;
5805 _scsih_fw_event_off(ioc);
5806
5807 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5808 wq = ioc->firmware_event_thread;
5809 ioc->firmware_event_thread = NULL;
5810 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5811 if (wq)
5812 destroy_workqueue(wq);
5813
5814 /* free ports attached to the sas_host */
5815 retry_again:
5816 list_for_each_entry(mpt2sas_port,
5817 &ioc->sas_hba.sas_port_list, port_list) {
5818 if (mpt2sas_port->remote_identify.device_type ==
5819 SAS_END_DEVICE) {
5820 sas_device =
5821 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5822 mpt2sas_port->remote_identify.sas_address);
5823 if (sas_device) {
c5e039be 5824 _scsih_remove_device(ioc, sas_device);
635374e7
EM
5825 goto retry_again;
5826 }
5827 } else {
5828 expander_sibling =
5829 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5830 mpt2sas_port->remote_identify.sas_address);
5831 if (expander_sibling) {
5832 _scsih_expander_remove(ioc,
c5e039be 5833 expander_sibling->sas_address);
635374e7
EM
5834 goto retry_again;
5835 }
5836 }
5837 }
5838
5839 /* free phys attached to the sas_host */
5840 if (ioc->sas_hba.num_phys) {
5841 kfree(ioc->sas_hba.phy);
5842 ioc->sas_hba.phy = NULL;
5843 ioc->sas_hba.num_phys = 0;
5844 }
5845
5846 sas_remove_host(shost);
5847 mpt2sas_base_detach(ioc);
5848 list_del(&ioc->list);
5849 scsi_remove_host(shost);
5850 scsi_host_put(shost);
5851}
5852
5853/**
5854 * _scsih_probe_boot_devices - reports 1st device
5855 * @ioc: per adapter object
5856 *
5857 * If specified in bios page 2, this routine reports the 1st
5858 * device scsi-ml or sas transport for persistent boot device
5859 * purposes. Please refer to function _scsih_determine_boot_device()
5860 */
5861static void
5862_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5863{
5864 u8 is_raid;
5865 void *device;
5866 struct _sas_device *sas_device;
5867 struct _raid_device *raid_device;
c5e039be
KD
5868 u16 handle;
5869 u64 sas_address_parent;
635374e7
EM
5870 u64 sas_address;
5871 unsigned long flags;
5872 int rc;
5873
5874 device = NULL;
5875 if (ioc->req_boot_device.device) {
5876 device = ioc->req_boot_device.device;
5877 is_raid = ioc->req_boot_device.is_raid;
5878 } else if (ioc->req_alt_boot_device.device) {
5879 device = ioc->req_alt_boot_device.device;
5880 is_raid = ioc->req_alt_boot_device.is_raid;
5881 } else if (ioc->current_boot_device.device) {
5882 device = ioc->current_boot_device.device;
5883 is_raid = ioc->current_boot_device.is_raid;
5884 }
5885
5886 if (!device)
5887 return;
5888
5889 if (is_raid) {
5890 raid_device = device;
5891 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5892 raid_device->id, 0);
5893 if (rc)
5894 _scsih_raid_device_remove(ioc, raid_device);
5895 } else {
5896 sas_device = device;
5897 handle = sas_device->handle;
c5e039be 5898 sas_address_parent = sas_device->sas_address_parent;
635374e7
EM
5899 sas_address = sas_device->sas_address;
5900 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5901 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5902 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5903 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
c5e039be 5904 sas_device->sas_address_parent)) {
635374e7
EM
5905 _scsih_sas_device_remove(ioc, sas_device);
5906 } else if (!sas_device->starget) {
5907 mpt2sas_transport_port_remove(ioc, sas_address,
c5e039be 5908 sas_address_parent);
635374e7
EM
5909 _scsih_sas_device_remove(ioc, sas_device);
5910 }
5911 }
5912}
5913
5914/**
5915 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5916 * @ioc: per adapter object
5917 *
5918 * Called during initial loading of the driver.
5919 */
5920static void
5921_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5922{
5923 struct _raid_device *raid_device, *raid_next;
5924 int rc;
5925
5926 list_for_each_entry_safe(raid_device, raid_next,
5927 &ioc->raid_device_list, list) {
5928 if (raid_device->starget)
5929 continue;
5930 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5931 raid_device->id, 0);
5932 if (rc)
5933 _scsih_raid_device_remove(ioc, raid_device);
5934 }
5935}
5936
5937/**
77e63ed4 5938 * _scsih_probe_sas - reporting sas devices to sas transport
635374e7
EM
5939 * @ioc: per adapter object
5940 *
5941 * Called during initial loading of the driver.
5942 */
5943static void
5944_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5945{
5946 struct _sas_device *sas_device, *next;
5947 unsigned long flags;
635374e7
EM
5948
5949 /* SAS Device List */
5950 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5951 list) {
5952 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5953 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5954 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5955
c5e039be
KD
5956 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5957 sas_device->sas_address_parent)) {
635374e7
EM
5958 _scsih_sas_device_remove(ioc, sas_device);
5959 } else if (!sas_device->starget) {
c5e039be
KD
5960 mpt2sas_transport_port_remove(ioc,
5961 sas_device->sas_address,
5962 sas_device->sas_address_parent);
635374e7
EM
5963 _scsih_sas_device_remove(ioc, sas_device);
5964 }
5965 }
5966}
5967
5968/**
5969 * _scsih_probe_devices - probing for devices
5970 * @ioc: per adapter object
5971 *
5972 * Called during initial loading of the driver.
5973 */
5974static void
5975_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5976{
5977 u16 volume_mapping_flags =
5978 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5979 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5980
5981 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5982 return; /* return when IOC doesn't support initiator mode */
5983
5984 _scsih_probe_boot_devices(ioc);
5985
5986 if (ioc->ir_firmware) {
5987 if ((volume_mapping_flags &
5988 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5989 _scsih_probe_sas(ioc);
5990 _scsih_probe_raid(ioc);
5991 } else {
5992 _scsih_probe_raid(ioc);
5993 _scsih_probe_sas(ioc);
5994 }
5995 } else
5996 _scsih_probe_sas(ioc);
5997}
5998
5999/**
d5d135b3 6000 * _scsih_probe - attach and add scsi host
635374e7
EM
6001 * @pdev: PCI device struct
6002 * @id: pci device id
6003 *
6004 * Returns 0 success, anything else error.
6005 */
6006static int
d5d135b3 6007_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
635374e7
EM
6008{
6009 struct MPT2SAS_ADAPTER *ioc;
6010 struct Scsi_Host *shost;
6011
6012 shost = scsi_host_alloc(&scsih_driver_template,
6013 sizeof(struct MPT2SAS_ADAPTER));
6014 if (!shost)
6015 return -ENODEV;
6016
6017 /* init local params */
6018 ioc = shost_priv(shost);
6019 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6020 INIT_LIST_HEAD(&ioc->list);
ba33fadf 6021 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
635374e7
EM
6022 ioc->shost = shost;
6023 ioc->id = mpt_ids++;
6024 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6025 ioc->pdev = pdev;
6026 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6027 ioc->tm_cb_idx = tm_cb_idx;
6028 ioc->ctl_cb_idx = ctl_cb_idx;
6029 ioc->base_cb_idx = base_cb_idx;
6030 ioc->transport_cb_idx = transport_cb_idx;
6031 ioc->config_cb_idx = config_cb_idx;
77e63ed4
KD
6032 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
6033 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
635374e7
EM
6034 ioc->logging_level = logging_level;
6035 /* misc semaphores and spin locks */
6036 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6037 spin_lock_init(&ioc->scsi_lookup_lock);
6038 spin_lock_init(&ioc->sas_device_lock);
6039 spin_lock_init(&ioc->sas_node_lock);
6040 spin_lock_init(&ioc->fw_event_lock);
6041 spin_lock_init(&ioc->raid_device_lock);
6042
6043 INIT_LIST_HEAD(&ioc->sas_device_list);
6044 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6045 INIT_LIST_HEAD(&ioc->sas_expander_list);
6046 INIT_LIST_HEAD(&ioc->fw_event_list);
6047 INIT_LIST_HEAD(&ioc->raid_device_list);
6048 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
77e63ed4 6049 INIT_LIST_HEAD(&ioc->delayed_tr_list);
635374e7
EM
6050
6051 /* init shost parameters */
6052 shost->max_cmd_len = 16;
6053 shost->max_lun = max_lun;
6054 shost->transportt = mpt2sas_transport_template;
6055 shost->unique_id = ioc->id;
6056
6057 if ((scsi_add_host(shost, &pdev->dev))) {
6058 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6059 ioc->name, __FILE__, __LINE__, __func__);
6060 list_del(&ioc->list);
6061 goto out_add_shost_fail;
6062 }
6063
3c621b3e
EM
6064 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
6065 | SHOST_DIF_TYPE3_PROTECTION);
77e63ed4 6066 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
3c621b3e 6067
635374e7
EM
6068 /* event thread */
6069 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6070 "fw_event%d", ioc->id);
6071 ioc->firmware_event_thread = create_singlethread_workqueue(
6072 ioc->firmware_event_name);
6073 if (!ioc->firmware_event_thread) {
6074 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6075 ioc->name, __FILE__, __LINE__, __func__);
6076 goto out_thread_fail;
6077 }
6078
6079 ioc->wait_for_port_enable_to_complete = 1;
6080 if ((mpt2sas_base_attach(ioc))) {
6081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6082 ioc->name, __FILE__, __LINE__, __func__);
6083 goto out_attach_fail;
6084 }
6085
6086 ioc->wait_for_port_enable_to_complete = 0;
6087 _scsih_probe_devices(ioc);
6088 return 0;
6089
6090 out_attach_fail:
6091 destroy_workqueue(ioc->firmware_event_thread);
6092 out_thread_fail:
6093 list_del(&ioc->list);
6094 scsi_remove_host(shost);
6095 out_add_shost_fail:
6096 return -ENODEV;
6097}
6098
6099#ifdef CONFIG_PM
6100/**
d5d135b3 6101 * _scsih_suspend - power management suspend main entry point
635374e7
EM
6102 * @pdev: PCI device struct
6103 * @state: PM state change to (usually PCI_D3)
6104 *
6105 * Returns 0 success, anything else error.
6106 */
6107static int
d5d135b3 6108_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
635374e7
EM
6109{
6110 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6111 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6112 u32 device_state;
6113
e4750c98 6114 mpt2sas_base_stop_watchdog(ioc);
635374e7
EM
6115 flush_scheduled_work();
6116 scsi_block_requests(shost);
6117 device_state = pci_choose_state(pdev, state);
6118 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6119 "operating state [D%d]\n", ioc->name, pdev,
6120 pci_name(pdev), device_state);
6121
6122 mpt2sas_base_free_resources(ioc);
6123 pci_save_state(pdev);
6124 pci_disable_device(pdev);
6125 pci_set_power_state(pdev, device_state);
6126 return 0;
6127}
6128
6129/**
d5d135b3 6130 * _scsih_resume - power management resume main entry point
635374e7
EM
6131 * @pdev: PCI device struct
6132 *
6133 * Returns 0 success, anything else error.
6134 */
6135static int
d5d135b3 6136_scsih_resume(struct pci_dev *pdev)
635374e7
EM
6137{
6138 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6139 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6140 u32 device_state = pdev->current_state;
6141 int r;
6142
6143 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6144 "operating state [D%d]\n", ioc->name, pdev,
6145 pci_name(pdev), device_state);
6146
6147 pci_set_power_state(pdev, PCI_D0);
6148 pci_enable_wake(pdev, PCI_D0, 0);
6149 pci_restore_state(pdev);
6150 ioc->pdev = pdev;
6151 r = mpt2sas_base_map_resources(ioc);
6152 if (r)
6153 return r;
6154
6155 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6156 scsi_unblock_requests(shost);
e4750c98 6157 mpt2sas_base_start_watchdog(ioc);
635374e7
EM
6158 return 0;
6159}
6160#endif /* CONFIG_PM */
6161
6162
6163static struct pci_driver scsih_driver = {
6164 .name = MPT2SAS_DRIVER_NAME,
6165 .id_table = scsih_pci_table,
d5d135b3
EM
6166 .probe = _scsih_probe,
6167 .remove = __devexit_p(_scsih_remove),
635374e7 6168#ifdef CONFIG_PM
d5d135b3
EM
6169 .suspend = _scsih_suspend,
6170 .resume = _scsih_resume,
635374e7
EM
6171#endif
6172};
6173
6174
6175/**
d5d135b3 6176 * _scsih_init - main entry point for this driver.
635374e7
EM
6177 *
6178 * Returns 0 success, anything else error.
6179 */
6180static int __init
d5d135b3 6181_scsih_init(void)
635374e7
EM
6182{
6183 int error;
6184
6185 mpt_ids = 0;
6186 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6187 MPT2SAS_DRIVER_VERSION);
6188
6189 mpt2sas_transport_template =
6190 sas_attach_transport(&mpt2sas_transport_functions);
6191 if (!mpt2sas_transport_template)
6192 return -ENODEV;
6193
6194 mpt2sas_base_initialize_callback_handler();
6195
6196 /* queuecommand callback hander */
d5d135b3 6197 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
635374e7
EM
6198
6199 /* task managment callback handler */
d5d135b3 6200 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
635374e7
EM
6201
6202 /* base internal commands callback handler */
6203 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6204
6205 /* transport internal commands callback handler */
6206 transport_cb_idx = mpt2sas_base_register_callback_handler(
6207 mpt2sas_transport_done);
6208
6209 /* configuration page API internal commands callback handler */
6210 config_cb_idx = mpt2sas_base_register_callback_handler(
6211 mpt2sas_config_done);
6212
6213 /* ctl module callback handler */
6214 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6215
77e63ed4
KD
6216 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6217 _scsih_tm_tr_complete);
6218 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6219 _scsih_sas_control_complete);
6220
635374e7
EM
6221 mpt2sas_ctl_init();
6222
6223 error = pci_register_driver(&scsih_driver);
6224 if (error)
6225 sas_release_transport(mpt2sas_transport_template);
6226
6227 return error;
6228}
6229
6230/**
d5d135b3 6231 * _scsih_exit - exit point for this driver (when it is a module).
635374e7
EM
6232 *
6233 * Returns 0 success, anything else error.
6234 */
6235static void __exit
d5d135b3 6236_scsih_exit(void)
635374e7
EM
6237{
6238 printk(KERN_INFO "mpt2sas version %s unloading\n",
6239 MPT2SAS_DRIVER_VERSION);
6240
6241 pci_unregister_driver(&scsih_driver);
6242
6243 sas_release_transport(mpt2sas_transport_template);
6244 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6245 mpt2sas_base_release_callback_handler(tm_cb_idx);
6246 mpt2sas_base_release_callback_handler(base_cb_idx);
6247 mpt2sas_base_release_callback_handler(transport_cb_idx);
6248 mpt2sas_base_release_callback_handler(config_cb_idx);
6249 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6250
77e63ed4
KD
6251 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6252 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6253
635374e7
EM
6254 mpt2sas_ctl_exit();
6255}
6256
d5d135b3
EM
6257module_init(_scsih_init);
6258module_exit(_scsih_exit);