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