Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / message / fusion / mptfc.c
1 /*
2  *  linux/drivers/message/fusion/mptfc.c
3  *      For use with LSI Logic PCI chip/adapter(s)
4  *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
5  *
6  *  Copyright (c) 1999-2005 LSI Logic Corporation
7  *  (mailto:mpt_linux_developer@lsil.com)
8  *
9  */
10 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11 /*
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; version 2 of the License.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     NO WARRANTY
22     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
23     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
24     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
25     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
26     solely responsible for determining the appropriateness of using and
27     distributing the Program and assumes all risks associated with its
28     exercise of rights under this Agreement, including but not limited to
29     the risks and costs of program errors, damage to or loss of data,
30     programs or equipment, and unavailability or interruption of operations.
31
32     DISCLAIMER OF LIABILITY
33     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
34     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
36     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
39     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40
41     You should have received a copy of the GNU General Public License
42     along with this program; if not, write to the Free Software
43     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
44 */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
46 #include "linux_compat.h"       /* linux-2.6 tweaks */
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/errno.h>
51 #include <linux/kdev_t.h>
52 #include <linux/blkdev.h>
53 #include <linux/delay.h>        /* for mdelay */
54 #include <linux/interrupt.h>    /* needed for in_interrupt() proto */
55 #include <linux/reboot.h>       /* notifier code */
56 #include <linux/sched.h>
57 #include <linux/workqueue.h>
58 #include <linux/sort.h>
59
60 #include <scsi/scsi.h>
61 #include <scsi/scsi_cmnd.h>
62 #include <scsi/scsi_device.h>
63 #include <scsi/scsi_host.h>
64 #include <scsi/scsi_tcq.h>
65 #include <scsi/scsi_transport_fc.h>
66
67 #include "mptbase.h"
68 #include "mptscsih.h"
69
70 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
71 #define my_NAME         "Fusion MPT FC Host driver"
72 #define my_VERSION      MPT_LINUX_VERSION_COMMON
73 #define MYNAM           "mptfc"
74
75 MODULE_AUTHOR(MODULEAUTHOR);
76 MODULE_DESCRIPTION(my_NAME);
77 MODULE_LICENSE("GPL");
78
79 /* Command line args */
80 #define MPTFC_DEV_LOSS_TMO (60)
81 static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;     /* reasonable default */
82 module_param(mptfc_dev_loss_tmo, int, 0);
83 MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
84                                      " transport to wait for an rport to "
85                                      " return following a device loss event."
86                                      "  Default=60.");
87
88 static int      mptfcDoneCtx = -1;
89 static int      mptfcTaskCtx = -1;
90 static int      mptfcInternalCtx = -1; /* Used only for internal commands */
91
92 static int mptfc_target_alloc(struct scsi_target *starget);
93 static int mptfc_slave_alloc(struct scsi_device *sdev);
94 static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
95                       void (*done)(struct scsi_cmnd *));
96 static void mptfc_target_destroy(struct scsi_target *starget);
97 static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
98 static void __devexit mptfc_remove(struct pci_dev *pdev);
99
100 static struct scsi_host_template mptfc_driver_template = {
101         .module                         = THIS_MODULE,
102         .proc_name                      = "mptfc",
103         .proc_info                      = mptscsih_proc_info,
104         .name                           = "MPT FC Host",
105         .info                           = mptscsih_info,
106         .queuecommand                   = mptfc_qcmd,
107         .target_alloc                   = mptfc_target_alloc,
108         .slave_alloc                    = mptfc_slave_alloc,
109         .slave_configure                = mptscsih_slave_configure,
110         .target_destroy                 = mptfc_target_destroy,
111         .slave_destroy                  = mptscsih_slave_destroy,
112         .change_queue_depth             = mptscsih_change_queue_depth,
113         .eh_abort_handler               = mptscsih_abort,
114         .eh_device_reset_handler        = mptscsih_dev_reset,
115         .eh_bus_reset_handler           = mptscsih_bus_reset,
116         .eh_host_reset_handler          = mptscsih_host_reset,
117         .bios_param                     = mptscsih_bios_param,
118         .can_queue                      = MPT_FC_CAN_QUEUE,
119         .this_id                        = -1,
120         .sg_tablesize                   = MPT_SCSI_SG_DEPTH,
121         .max_sectors                    = 8192,
122         .cmd_per_lun                    = 7,
123         .use_clustering                 = ENABLE_CLUSTERING,
124 };
125
126 /****************************************************************************
127  * Supported hardware
128  */
129
130 static struct pci_device_id mptfc_pci_table[] = {
131         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC909,
132                 PCI_ANY_ID, PCI_ANY_ID },
133         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
134                 PCI_ANY_ID, PCI_ANY_ID },
135         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
136                 PCI_ANY_ID, PCI_ANY_ID },
137         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
138                 PCI_ANY_ID, PCI_ANY_ID },
139         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
140                 PCI_ANY_ID, PCI_ANY_ID },
141         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
142                 PCI_ANY_ID, PCI_ANY_ID },
143         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
144                 PCI_ANY_ID, PCI_ANY_ID },
145         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
146                 PCI_ANY_ID, PCI_ANY_ID },
147         {0}     /* Terminating entry */
148 };
149 MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
150
151 static struct scsi_transport_template *mptfc_transport_template = NULL;
152
153 static struct fc_function_template mptfc_transport_functions = {
154         .dd_fcrport_size = 8,
155         .show_host_node_name = 1,
156         .show_host_port_name = 1,
157         .show_host_supported_classes = 1,
158         .show_host_port_id = 1,
159         .show_rport_supported_classes = 1,
160         .show_starget_node_name = 1,
161         .show_starget_port_name = 1,
162         .show_starget_port_id = 1,
163         .set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
164         .show_rport_dev_loss_tmo = 1,
165
166 };
167
168 static void
169 mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
170 {
171         if (timeout > 0)
172                 rport->dev_loss_tmo = timeout;
173         else
174                 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
175 }
176
177 static int
178 mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
179 {
180         FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
181         FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
182
183         if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
184                 if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
185                         return 0;
186                 if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
187                         return -1;
188                 return 1;
189         }
190         if ((*aa)->CurrentBus < (*bb)->CurrentBus)
191                 return -1;
192         return 1;
193 }
194
195 static int
196 mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
197         void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
198 {
199         ConfigPageHeader_t       hdr;
200         CONFIGPARMS              cfg;
201         FCDevicePage0_t         *ppage0_alloc, *fc;
202         dma_addr_t               page0_dma;
203         int                      data_sz;
204         int                      ii;
205
206         FCDevicePage0_t         *p0_array=NULL, *p_p0;
207         FCDevicePage0_t         **pp0_array=NULL, **p_pp0;
208
209         int                      rc = -ENOMEM;
210         U32                      port_id = 0xffffff;
211         int                      num_targ = 0;
212         int                      max_bus = ioc->facts.MaxBuses;
213         int                      max_targ = ioc->facts.MaxDevices;
214
215         if (max_bus == 0 || max_targ == 0)
216                 goto out;
217
218         data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
219         p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
220         if (!p0_array)
221                 goto out;
222
223         data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
224         p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
225         if (!pp0_array)
226                 goto out;
227
228         do {
229                 /* Get FC Device Page 0 header */
230                 hdr.PageVersion = 0;
231                 hdr.PageLength = 0;
232                 hdr.PageNumber = 0;
233                 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
234                 cfg.cfghdr.hdr = &hdr;
235                 cfg.physAddr = -1;
236                 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
237                 cfg.dir = 0;
238                 cfg.pageAddr = port_id;
239                 cfg.timeout = 0;
240
241                 if ((rc = mpt_config(ioc, &cfg)) != 0)
242                         break;
243
244                 if (hdr.PageLength <= 0)
245                         break;
246
247                 data_sz = hdr.PageLength * 4;
248                 ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
249                                                         &page0_dma);
250                 rc = -ENOMEM;
251                 if (!ppage0_alloc)
252                         break;
253
254                 cfg.physAddr = page0_dma;
255                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
256
257                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
258                         ppage0_alloc->PortIdentifier =
259                                 le32_to_cpu(ppage0_alloc->PortIdentifier);
260
261                         ppage0_alloc->WWNN.Low =
262                                 le32_to_cpu(ppage0_alloc->WWNN.Low);
263
264                         ppage0_alloc->WWNN.High =
265                                 le32_to_cpu(ppage0_alloc->WWNN.High);
266
267                         ppage0_alloc->WWPN.Low =
268                                 le32_to_cpu(ppage0_alloc->WWPN.Low);
269
270                         ppage0_alloc->WWPN.High =
271                                 le32_to_cpu(ppage0_alloc->WWPN.High);
272
273                         ppage0_alloc->BBCredit =
274                                 le16_to_cpu(ppage0_alloc->BBCredit);
275
276                         ppage0_alloc->MaxRxFrameSize =
277                                 le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
278
279                         port_id = ppage0_alloc->PortIdentifier;
280                         num_targ++;
281                         *p_p0 = *ppage0_alloc;  /* save data */
282                         *p_pp0++ = p_p0++;      /* save addr */
283                 }
284                 pci_free_consistent(ioc->pcidev, data_sz,
285                                         (u8 *) ppage0_alloc, page0_dma);
286                 if (rc != 0)
287                         break;
288
289         } while (port_id <= 0xff0000);
290
291         if (num_targ) {
292                 /* sort array */
293                 if (num_targ > 1)
294                         sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
295                                 mptfc_FcDevPage0_cmp_func, NULL);
296                 /* call caller's func for each targ */
297                 for (ii = 0; ii < num_targ;  ii++) {
298                         fc = *(pp0_array+ii);
299                         func(ioc, ioc_port, fc);
300                 }
301         }
302
303  out:
304         kfree(pp0_array);
305         kfree(p0_array);
306         return rc;
307 }
308
309 static int
310 mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
311 {
312         /* not currently usable */
313         if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
314                           MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
315                 return -1;
316
317         if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
318                 return -1;
319
320         if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
321                 return -1;
322
323         /*
324          * board data structure already normalized to platform endianness
325          * shifted to avoid unaligned access on 64 bit architecture
326          */
327         rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
328         rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
329         rid->port_id =   pg0->PortIdentifier;
330         rid->roles = FC_RPORT_ROLE_UNKNOWN;
331
332         return 0;
333 }
334
335 static void
336 mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
337 {
338         struct fc_rport_identifiers rport_ids;
339         struct fc_rport         *rport;
340         struct mptfc_rport_info *ri;
341         int                     new_ri = 1;
342         u64                     pn, nn;
343         VirtTarget              *vtarget;
344         u32                     roles = FC_RPORT_ROLE_UNKNOWN;
345
346         if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
347                 return;
348
349         roles |= FC_RPORT_ROLE_FCP_TARGET;
350         if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
351                 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
352
353         /* scan list looking for a match */
354         list_for_each_entry(ri, &ioc->fc_rports, list) {
355                 pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
356                 if (pn == rport_ids.port_name) {        /* match */
357                         list_move_tail(&ri->list, &ioc->fc_rports);
358                         new_ri = 0;
359                         break;
360                 }
361         }
362         if (new_ri) {   /* allocate one */
363                 ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
364                 if (!ri)
365                         return;
366                 list_add_tail(&ri->list, &ioc->fc_rports);
367         }
368
369         ri->pg0 = *pg0; /* add/update pg0 data */
370         ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
371
372         /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
373         if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
374                 ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
375                 rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
376                 if (rport) {
377                         ri->rport = rport;
378                         if (new_ri) /* may have been reset by user */
379                                 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
380                         /*
381                          * if already mapped, remap here.  If not mapped,
382                          * target_alloc will allocate vtarget and map,
383                          * slave_alloc will fill in vdev from vtarget.
384                          */
385                         if (ri->starget) {
386                                 vtarget = ri->starget->hostdata;
387                                 if (vtarget) {
388                                         vtarget->target_id = pg0->CurrentTargetID;
389                                         vtarget->bus_id = pg0->CurrentBus;
390                                 }
391                         }
392                         *((struct mptfc_rport_info **)rport->dd_data) = ri;
393                         /* scan will be scheduled once rport becomes a target */
394                         fc_remote_port_rolechg(rport,roles);
395
396                         pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
397                         nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
398                         dfcprintk ((MYIOC_s_INFO_FMT
399                                 "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
400                                 "rport tid %d, tmo %d\n",
401                                         ioc->name,
402                                         ioc->sh->host_no,
403                                         pg0->PortIdentifier,
404                                         (unsigned long long)nn,
405                                         (unsigned long long)pn,
406                                         pg0->CurrentTargetID,
407                                         ri->rport->scsi_target_id,
408                                         ri->rport->dev_loss_tmo));
409                 } else {
410                         list_del(&ri->list);
411                         kfree(ri);
412                         ri = NULL;
413                 }
414         }
415 }
416
417 /*
418  *      OS entry point to allow for host driver to free allocated memory
419  *      Called if no device present or device being unloaded
420  */
421 static void
422 mptfc_target_destroy(struct scsi_target *starget)
423 {
424         struct fc_rport         *rport;
425         struct mptfc_rport_info *ri;
426
427         rport = starget_to_rport(starget);
428         if (rport) {
429                 ri = *((struct mptfc_rport_info **)rport->dd_data);
430                 if (ri) /* better be! */
431                         ri->starget = NULL;
432         }
433         if (starget->hostdata)
434                 kfree(starget->hostdata);
435         starget->hostdata = NULL;
436 }
437
438 /*
439  *      OS entry point to allow host driver to alloc memory
440  *      for each scsi target. Called once per device the bus scan.
441  *      Return non-zero if allocation fails.
442  */
443 static int
444 mptfc_target_alloc(struct scsi_target *starget)
445 {
446         VirtTarget              *vtarget;
447         struct fc_rport         *rport;
448         struct mptfc_rport_info *ri;
449         int                     rc;
450
451         vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
452         if (!vtarget)
453                 return -ENOMEM;
454         starget->hostdata = vtarget;
455
456         rc = -ENODEV;
457         rport = starget_to_rport(starget);
458         if (rport) {
459                 ri = *((struct mptfc_rport_info **)rport->dd_data);
460                 if (ri) {       /* better be! */
461                         vtarget->target_id = ri->pg0.CurrentTargetID;
462                         vtarget->bus_id = ri->pg0.CurrentBus;
463                         ri->starget = starget;
464                         rc = 0;
465                 }
466         }
467         if (rc != 0) {
468                 kfree(vtarget);
469                 starget->hostdata = NULL;
470         }
471
472         return rc;
473 }
474
475 /*
476  *      OS entry point to allow host driver to alloc memory
477  *      for each scsi device. Called once per device the bus scan.
478  *      Return non-zero if allocation fails.
479  *      Init memory once per LUN.
480  */
481 static int
482 mptfc_slave_alloc(struct scsi_device *sdev)
483 {
484         MPT_SCSI_HOST           *hd;
485         VirtTarget              *vtarget;
486         VirtDevice              *vdev;
487         struct scsi_target      *starget;
488         struct fc_rport         *rport;
489
490
491         starget = scsi_target(sdev);
492         rport = starget_to_rport(starget);
493
494         if (!rport || fc_remote_port_chkready(rport))
495                 return -ENXIO;
496
497         hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
498
499         vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
500         if (!vdev) {
501                 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
502                                 hd->ioc->name, sizeof(VirtDevice));
503                 return -ENOMEM;
504         }
505
506
507         sdev->hostdata = vdev;
508         vtarget = starget->hostdata;
509
510         if (vtarget->num_luns == 0) {
511                 vtarget->ioc_id = hd->ioc->id;
512                 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
513                 hd->Targets[sdev->id] = vtarget;
514         }
515
516         vdev->vtarget = vtarget;
517         vdev->lun = sdev->lun;
518
519         vtarget->num_luns++;
520
521
522 #ifdef DMPT_DEBUG_FC
523         {
524         u64 nn, pn;
525         struct mptfc_rport_info *ri;
526         ri = *((struct mptfc_rport_info **)rport->dd_data);
527         pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
528         nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
529         dfcprintk ((MYIOC_s_INFO_FMT
530                 "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
531                 "CurrentTargetID %d, %x %llx %llx\n",
532                 hd->ioc->name,
533                 sdev->host->host_no,
534                 vtarget->num_luns,
535                 sdev->id, ri->pg0.CurrentTargetID,
536                 ri->pg0.PortIdentifier,
537                 (unsigned long long)pn,
538                 (unsigned long long)nn));
539         }
540 #endif
541
542         return 0;
543 }
544
545 static int
546 mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
547 {
548         struct mptfc_rport_info *ri;
549         struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
550         int             err;
551
552         err = fc_remote_port_chkready(rport);
553         if (unlikely(err)) {
554                 SCpnt->result = err;
555                 done(SCpnt);
556                 return 0;
557         }
558
559         /* dd_data is null until finished adding target */
560         ri = *((struct mptfc_rport_info **)rport->dd_data);
561         if (unlikely(!ri)) {
562                 dfcprintk ((MYIOC_s_INFO_FMT
563                         "mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
564                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
565                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
566                         SCpnt->device->id,SCpnt->device->lun));
567                 SCpnt->result = DID_IMM_RETRY << 16;
568                 done(SCpnt);
569                 return 0;
570         }
571
572         err = mptscsih_qcmd(SCpnt,done);
573 #ifdef DMPT_DEBUG_FC
574         if (unlikely(err)) {
575                 dfcprintk ((MYIOC_s_INFO_FMT
576                         "mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
577                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
578                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
579                         SCpnt->device->id,SCpnt->device->lun,err));
580         }
581 #endif
582         return err;
583 }
584
585 /*
586  *      mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
587  *      @ioc: Pointer to MPT_ADAPTER structure
588  *      @portnum: IOC Port number
589  *
590  *      Return: 0 for success
591  *      -ENOMEM if no memory available
592  *              -EPERM if not allowed due to ISR context
593  *              -EAGAIN if no msg frames currently available
594  *              -EFAULT for non-successful reply or no reply (timeout)
595  *              -EINVAL portnum arg out of range (hardwired to two elements)
596  */
597 static int
598 mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
599 {
600         ConfigPageHeader_t       hdr;
601         CONFIGPARMS              cfg;
602         FCPortPage0_t           *ppage0_alloc;
603         FCPortPage0_t           *pp0dest;
604         dma_addr_t               page0_dma;
605         int                      data_sz;
606         int                      copy_sz;
607         int                      rc;
608         int                      count = 400;
609
610         if (portnum > 1)
611                 return -EINVAL;
612
613         /* Get FCPort Page 0 header */
614         hdr.PageVersion = 0;
615         hdr.PageLength = 0;
616         hdr.PageNumber = 0;
617         hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
618         cfg.cfghdr.hdr = &hdr;
619         cfg.physAddr = -1;
620         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
621         cfg.dir = 0;
622         cfg.pageAddr = portnum;
623         cfg.timeout = 0;
624
625         if ((rc = mpt_config(ioc, &cfg)) != 0)
626                 return rc;
627
628         if (hdr.PageLength == 0)
629                 return 0;
630
631         data_sz = hdr.PageLength * 4;
632         rc = -ENOMEM;
633         ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
634         if (ppage0_alloc) {
635
636  try_again:
637                 memset((u8 *)ppage0_alloc, 0, data_sz);
638                 cfg.physAddr = page0_dma;
639                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
640
641                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
642                         /* save the data */
643                         pp0dest = &ioc->fc_port_page0[portnum];
644                         copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
645                         memcpy(pp0dest, ppage0_alloc, copy_sz);
646
647                         /*
648                          *      Normalize endianness of structure data,
649                          *      by byte-swapping all > 1 byte fields!
650                          */
651                         pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
652                         pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
653                         pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
654                         pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
655                         pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
656                         pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
657                         pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
658                         pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
659                         pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
660                         pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
661                         pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
662                         pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
663                         pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
664                         pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
665                         pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
666                         pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
667
668                         /*
669                          * if still doing discovery,
670                          * hang loose a while until finished
671                          */
672                         if ((pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) ||
673                             (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_ONLINE &&
674                              (pp0dest->Flags & MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK)
675                               == MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT)) {
676                                 if (count-- > 0) {
677                                         msleep(100);
678                                         goto try_again;
679                                 }
680                                 printk(MYIOC_s_INFO_FMT "Firmware discovery not"
681                                                         " complete.\n",
682                                                 ioc->name);
683                         }
684                 }
685
686                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
687         }
688
689         return rc;
690 }
691
692 static int
693 mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
694 {
695         ConfigPageHeader_t       hdr;
696         CONFIGPARMS              cfg;
697         int                      rc;
698
699         if (portnum > 1)
700                 return -EINVAL;
701
702         if (!(ioc->fc_data.fc_port_page1[portnum].data))
703                 return -EINVAL;
704
705         /* get fcport page 1 header */
706         hdr.PageVersion = 0;
707         hdr.PageLength = 0;
708         hdr.PageNumber = 1;
709         hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
710         cfg.cfghdr.hdr = &hdr;
711         cfg.physAddr = -1;
712         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
713         cfg.dir = 0;
714         cfg.pageAddr = portnum;
715         cfg.timeout = 0;
716
717         if ((rc = mpt_config(ioc, &cfg)) != 0)
718                 return rc;
719
720         if (hdr.PageLength == 0)
721                 return -ENODEV;
722
723         if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
724                 return -EINVAL;
725
726         cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
727         cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
728         cfg.dir = 1;
729
730         rc = mpt_config(ioc, &cfg);
731
732         return rc;
733 }
734
735 static int
736 mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
737 {
738         ConfigPageHeader_t       hdr;
739         CONFIGPARMS              cfg;
740         FCPortPage1_t           *page1_alloc;
741         dma_addr_t               page1_dma;
742         int                      data_sz;
743         int                      rc;
744
745         if (portnum > 1)
746                 return -EINVAL;
747
748         /* get fcport page 1 header */
749         hdr.PageVersion = 0;
750         hdr.PageLength = 0;
751         hdr.PageNumber = 1;
752         hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
753         cfg.cfghdr.hdr = &hdr;
754         cfg.physAddr = -1;
755         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
756         cfg.dir = 0;
757         cfg.pageAddr = portnum;
758         cfg.timeout = 0;
759
760         if ((rc = mpt_config(ioc, &cfg)) != 0)
761                 return rc;
762
763         if (hdr.PageLength == 0)
764                 return -ENODEV;
765
766 start_over:
767
768         if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
769                 data_sz = hdr.PageLength * 4;
770                 if (data_sz < sizeof(FCPortPage1_t))
771                         data_sz = sizeof(FCPortPage1_t);
772
773                 page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
774                                                 data_sz,
775                                                 &page1_dma);
776                 if (!page1_alloc)
777                         return -ENOMEM;
778         }
779         else {
780                 page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
781                 page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
782                 data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
783                 if (hdr.PageLength * 4 > data_sz) {
784                         ioc->fc_data.fc_port_page1[portnum].data = NULL;
785                         pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
786                                 page1_alloc, page1_dma);
787                         goto start_over;
788                 }
789         }
790
791         memset(page1_alloc,0,data_sz);
792
793         cfg.physAddr = page1_dma;
794         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
795
796         if ((rc = mpt_config(ioc, &cfg)) == 0) {
797                 ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
798                 ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
799                 ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
800         }
801         else {
802                 ioc->fc_data.fc_port_page1[portnum].data = NULL;
803                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
804                         page1_alloc, page1_dma);
805         }
806
807         return rc;
808 }
809
810 static void
811 mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
812 {
813         int             ii;
814         FCPortPage1_t   *pp1;
815
816         #define MPTFC_FW_DEVICE_TIMEOUT (1)
817         #define MPTFC_FW_IO_PEND_TIMEOUT (1)
818         #define ON_FLAGS  (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
819         #define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
820
821         for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
822                 if (mptfc_GetFcPortPage1(ioc, ii) != 0)
823                         continue;
824                 pp1 = ioc->fc_data.fc_port_page1[ii].data;
825                 if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
826                  && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
827                  && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
828                  && ((pp1->Flags & OFF_FLAGS) == 0))
829                         continue;
830                 pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
831                 pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
832                 pp1->Flags &= ~OFF_FLAGS;
833                 pp1->Flags |= ON_FLAGS;
834                 mptfc_WriteFcPortPage1(ioc, ii);
835         }
836 }
837
838
839 static void
840 mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
841 {
842         unsigned class = 0, cos = 0;
843
844         /* don't know what to do as only one scsi (fc) host was allocated */
845         if (portnum != 0)
846                 return;
847
848         class = ioc->fc_port_page0[portnum].SupportedServiceClass;
849         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
850                 cos |= FC_COS_CLASS1;
851         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
852                 cos |= FC_COS_CLASS2;
853         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
854                 cos |= FC_COS_CLASS3;
855
856         fc_host_node_name(ioc->sh) =
857                 (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
858                     | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
859
860         fc_host_port_name(ioc->sh) =
861                 (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
862                     | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
863
864         fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
865
866         fc_host_supported_classes(ioc->sh) = cos;
867
868         fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
869 }
870
871 static void
872 mptfc_setup_reset(void *arg)
873 {
874         MPT_ADAPTER             *ioc = (MPT_ADAPTER *)arg;
875         u64                     pn;
876         struct mptfc_rport_info *ri;
877
878         /* reset about to happen, delete (block) all rports */
879         list_for_each_entry(ri, &ioc->fc_rports, list) {
880                 if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
881                         ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
882                         fc_remote_port_delete(ri->rport);       /* won't sleep */
883                         ri->rport = NULL;
884
885                         pn = (u64)ri->pg0.WWPN.High << 32 |
886                              (u64)ri->pg0.WWPN.Low;
887                         dfcprintk ((MYIOC_s_INFO_FMT
888                                 "mptfc_setup_reset.%d: %llx deleted\n",
889                                 ioc->name,
890                                 ioc->sh->host_no,
891                                 (unsigned long long)pn));
892                 }
893         }
894 }
895
896 static void
897 mptfc_rescan_devices(void *arg)
898 {
899         MPT_ADAPTER             *ioc = (MPT_ADAPTER *)arg;
900         int                     ii;
901         u64                     pn;
902         struct mptfc_rport_info *ri;
903
904         /* start by tagging all ports as missing */
905         list_for_each_entry(ri, &ioc->fc_rports, list) {
906                 if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
907                         ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
908                 }
909         }
910
911         /*
912          * now rescan devices known to adapter,
913          * will reregister existing rports
914          */
915         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
916                 (void) mptfc_GetFcPortPage0(ioc, ii);
917                 mptfc_init_host_attr(ioc, ii);  /* refresh */
918                 mptfc_GetFcDevPage0(ioc, ii, mptfc_register_dev);
919         }
920
921         /* delete devices still missing */
922         list_for_each_entry(ri, &ioc->fc_rports, list) {
923                 /* if newly missing, delete it */
924                 if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
925
926                         ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
927                                        MPT_RPORT_INFO_FLAGS_MISSING);
928                         fc_remote_port_delete(ri->rport);       /* won't sleep */
929                         ri->rport = NULL;
930
931                         pn = (u64)ri->pg0.WWPN.High << 32 |
932                              (u64)ri->pg0.WWPN.Low;
933                         dfcprintk ((MYIOC_s_INFO_FMT
934                                 "mptfc_rescan.%d: %llx deleted\n",
935                                 ioc->name,
936                                 ioc->sh->host_no,
937                                 (unsigned long long)pn));
938                 }
939         }
940 }
941
942 static int
943 mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
944 {
945         struct Scsi_Host        *sh;
946         MPT_SCSI_HOST           *hd;
947         MPT_ADAPTER             *ioc;
948         unsigned long            flags;
949         int                      ii;
950         int                      numSGE = 0;
951         int                      scale;
952         int                      ioc_cap;
953         int                     error=0;
954         int                     r;
955
956         if ((r = mpt_attach(pdev,id)) != 0)
957                 return r;
958
959         ioc = pci_get_drvdata(pdev);
960         ioc->DoneCtx = mptfcDoneCtx;
961         ioc->TaskCtx = mptfcTaskCtx;
962         ioc->InternalCtx = mptfcInternalCtx;
963
964         /*  Added sanity check on readiness of the MPT adapter.
965          */
966         if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
967                 printk(MYIOC_s_WARN_FMT
968                   "Skipping because it's not operational!\n",
969                   ioc->name);
970                 error = -ENODEV;
971                 goto out_mptfc_probe;
972         }
973
974         if (!ioc->active) {
975                 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
976                   ioc->name);
977                 error = -ENODEV;
978                 goto out_mptfc_probe;
979         }
980
981         /*  Sanity check - ensure at least 1 port is INITIATOR capable
982          */
983         ioc_cap = 0;
984         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
985                 if (ioc->pfacts[ii].ProtocolFlags &
986                     MPI_PORTFACTS_PROTOCOL_INITIATOR)
987                         ioc_cap ++;
988         }
989
990         if (!ioc_cap) {
991                 printk(MYIOC_s_WARN_FMT
992                         "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
993                         ioc->name, ioc);
994                 return -ENODEV;
995         }
996
997         sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
998
999         if (!sh) {
1000                 printk(MYIOC_s_WARN_FMT
1001                         "Unable to register controller with SCSI subsystem\n",
1002                         ioc->name);
1003                 error = -1;
1004                 goto out_mptfc_probe;
1005         }
1006
1007         spin_lock_init(&ioc->fc_rescan_work_lock);
1008         INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
1009         INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset, (void *)ioc);
1010
1011         spin_lock_irqsave(&ioc->FreeQlock, flags);
1012
1013         /* Attach the SCSI Host to the IOC structure
1014          */
1015         ioc->sh = sh;
1016
1017         sh->io_port = 0;
1018         sh->n_io_port = 0;
1019         sh->irq = 0;
1020
1021         /* set 16 byte cdb's */
1022         sh->max_cmd_len = 16;
1023
1024         sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
1025
1026         sh->max_lun = MPT_LAST_LUN + 1;
1027         sh->max_channel = 0;
1028         sh->this_id = ioc->pfacts[0].PortSCSIID;
1029
1030         /* Required entry.
1031          */
1032         sh->unique_id = ioc->id;
1033
1034         /* Verify that we won't exceed the maximum
1035          * number of chain buffers
1036          * We can optimize:  ZZ = req_sz/sizeof(SGE)
1037          * For 32bit SGE's:
1038          *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1039          *               + (req_sz - 64)/sizeof(SGE)
1040          * A slightly different algorithm is required for
1041          * 64bit SGEs.
1042          */
1043         scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
1044         if (sizeof(dma_addr_t) == sizeof(u64)) {
1045                 numSGE = (scale - 1) *
1046                   (ioc->facts.MaxChainDepth-1) + scale +
1047                   (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
1048                   sizeof(u32));
1049         } else {
1050                 numSGE = 1 + (scale - 1) *
1051                   (ioc->facts.MaxChainDepth-1) + scale +
1052                   (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
1053                   sizeof(u32));
1054         }
1055
1056         if (numSGE < sh->sg_tablesize) {
1057                 /* Reset this value */
1058                 dprintk((MYIOC_s_INFO_FMT
1059                   "Resetting sg_tablesize to %d from %d\n",
1060                   ioc->name, numSGE, sh->sg_tablesize));
1061                 sh->sg_tablesize = numSGE;
1062         }
1063
1064         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1065
1066         hd = (MPT_SCSI_HOST *) sh->hostdata;
1067         hd->ioc = ioc;
1068
1069         /* SCSI needs scsi_cmnd lookup table!
1070          * (with size equal to req_depth*PtrSz!)
1071          */
1072         hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1073         if (!hd->ScsiLookup) {
1074                 error = -ENOMEM;
1075                 goto out_mptfc_probe;
1076         }
1077
1078         dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1079                  ioc->name, hd->ScsiLookup));
1080
1081         /* Allocate memory for the device structures.
1082          * A non-Null pointer at an offset
1083          * indicates a device exists.
1084          * max_id = 1 + maximum id (hosts.h)
1085          */
1086         hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
1087         if (!hd->Targets) {
1088                 error = -ENOMEM;
1089                 goto out_mptfc_probe;
1090         }
1091
1092         dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
1093
1094         /* Clear the TM flags
1095          */
1096         hd->tmPending = 0;
1097         hd->tmState = TM_STATE_NONE;
1098         hd->resetPending = 0;
1099         hd->abortSCpnt = NULL;
1100
1101         /* Clear the pointer used to store
1102          * single-threaded commands, i.e., those
1103          * issued during a bus scan, dv and
1104          * configuration pages.
1105          */
1106         hd->cmdPtr = NULL;
1107
1108         /* Initialize this SCSI Hosts' timers
1109          * To use, set the timer expires field
1110          * and add_timer
1111          */
1112         init_timer(&hd->timer);
1113         hd->timer.data = (unsigned long) hd;
1114         hd->timer.function = mptscsih_timer_expired;
1115
1116         init_waitqueue_head(&hd->scandv_waitq);
1117         hd->scandv_wait_done = 0;
1118         hd->last_queue_full = 0;
1119
1120         sh->transportt = mptfc_transport_template;
1121         error = scsi_add_host (sh, &ioc->pcidev->dev);
1122         if(error) {
1123                 dprintk((KERN_ERR MYNAM
1124                   "scsi_add_host failed\n"));
1125                 goto out_mptfc_probe;
1126         }
1127
1128         /* initialize workqueue */
1129
1130         snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
1131                 sh->host_no);
1132         ioc->fc_rescan_work_q =
1133                 create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
1134         if (!ioc->fc_rescan_work_q)
1135                 goto out_mptfc_probe;
1136
1137         /*
1138          *  Pre-fetch FC port WWN and stuff...
1139          *  (FCPortPage0_t stuff)
1140          */
1141         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
1142                 (void) mptfc_GetFcPortPage0(ioc, ii);
1143         }
1144         mptfc_SetFcPortPage1_defaults(ioc);
1145
1146         /*
1147          * scan for rports -
1148          *      by doing it via the workqueue, some locking is eliminated
1149          */
1150
1151         queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
1152         flush_workqueue(ioc->fc_rescan_work_q);
1153
1154         return 0;
1155
1156 out_mptfc_probe:
1157
1158         mptscsih_remove(pdev);
1159         return error;
1160 }
1161
1162 static struct pci_driver mptfc_driver = {
1163         .name           = "mptfc",
1164         .id_table       = mptfc_pci_table,
1165         .probe          = mptfc_probe,
1166         .remove         = __devexit_p(mptfc_remove),
1167         .shutdown       = mptscsih_shutdown,
1168 #ifdef CONFIG_PM
1169         .suspend        = mptscsih_suspend,
1170         .resume         = mptscsih_resume,
1171 #endif
1172 };
1173
1174 static int
1175 mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
1176 {
1177         MPT_SCSI_HOST *hd;
1178         u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
1179         unsigned long flags;
1180         int rc=1;
1181
1182         devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
1183                         ioc->name, event));
1184
1185         if (ioc->sh == NULL ||
1186                 ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
1187                 return 1;
1188
1189         switch (event) {
1190         case MPI_EVENT_RESCAN:
1191                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1192                 if (ioc->fc_rescan_work_q) {
1193                         queue_work(ioc->fc_rescan_work_q,
1194                                    &ioc->fc_rescan_work);
1195                 }
1196                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1197                 break;
1198         default:
1199                 rc = mptscsih_event_process(ioc,pEvReply);
1200                 break;
1201         }
1202         return rc;
1203 }
1204
1205 static int
1206 mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
1207 {
1208         int             rc;
1209         unsigned long   flags;
1210
1211         rc = mptscsih_ioc_reset(ioc,reset_phase);
1212         if (rc == 0)
1213                 return rc;
1214
1215
1216         dtmprintk((KERN_WARNING MYNAM
1217                 ": IOC %s_reset routed to FC host driver!\n",
1218                 reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
1219                 reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
1220
1221         if (reset_phase == MPT_IOC_SETUP_RESET) {
1222                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1223                 if (ioc->fc_rescan_work_q) {
1224                         queue_work(ioc->fc_rescan_work_q,
1225                                    &ioc->fc_setup_reset_work);
1226                 }
1227                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1228         }
1229
1230         else if (reset_phase == MPT_IOC_PRE_RESET) {
1231         }
1232
1233         else {  /* MPT_IOC_POST_RESET */
1234                 mptfc_SetFcPortPage1_defaults(ioc);
1235                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1236                 if (ioc->fc_rescan_work_q) {
1237                         queue_work(ioc->fc_rescan_work_q,
1238                                    &ioc->fc_rescan_work);
1239                 }
1240                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1241         }
1242         return 1;
1243 }
1244
1245 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1246 /**
1247  *      mptfc_init - Register MPT adapter(s) as SCSI host(s) with
1248  *      linux scsi mid-layer.
1249  *
1250  *      Returns 0 for success, non-zero for failure.
1251  */
1252 static int __init
1253 mptfc_init(void)
1254 {
1255         int error;
1256
1257         show_mptmod_ver(my_NAME, my_VERSION);
1258
1259         /* sanity check module parameters */
1260         if (mptfc_dev_loss_tmo <= 0)
1261                 mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
1262
1263         mptfc_transport_template =
1264                 fc_attach_transport(&mptfc_transport_functions);
1265
1266         if (!mptfc_transport_template)
1267                 return -ENODEV;
1268
1269         mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
1270         mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
1271         mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
1272
1273         if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
1274                 devtverboseprintk((KERN_INFO MYNAM
1275                   ": Registered for IOC event notifications\n"));
1276         }
1277
1278         if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
1279                 dprintk((KERN_INFO MYNAM
1280                   ": Registered for IOC reset notifications\n"));
1281         }
1282
1283         error = pci_register_driver(&mptfc_driver);
1284         if (error)
1285                 fc_release_transport(mptfc_transport_template);
1286
1287         return error;
1288 }
1289
1290 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1291 /**
1292  *      mptfc_remove - Removed fc infrastructure for devices
1293  *      @pdev: Pointer to pci_dev structure
1294  *
1295  */
1296 static void __devexit
1297 mptfc_remove(struct pci_dev *pdev)
1298 {
1299         MPT_ADAPTER             *ioc = pci_get_drvdata(pdev);
1300         struct mptfc_rport_info *p, *n;
1301         struct workqueue_struct *work_q;
1302         unsigned long           flags;
1303         int                     ii;
1304
1305         /* destroy workqueue */
1306         if ((work_q=ioc->fc_rescan_work_q)) {
1307                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
1308                 ioc->fc_rescan_work_q = NULL;
1309                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
1310                 destroy_workqueue(work_q);
1311         }
1312
1313         fc_remove_host(ioc->sh);
1314
1315         list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
1316                 list_del(&p->list);
1317                 kfree(p);
1318         }
1319
1320         for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
1321                 if (ioc->fc_data.fc_port_page1[ii].data) {
1322                         pci_free_consistent(ioc->pcidev,
1323                                 ioc->fc_data.fc_port_page1[ii].pg_sz,
1324                                 (u8 *) ioc->fc_data.fc_port_page1[ii].data,
1325                                 ioc->fc_data.fc_port_page1[ii].dma);
1326                         ioc->fc_data.fc_port_page1[ii].data = NULL;
1327                 }
1328         }
1329
1330         mptscsih_remove(pdev);
1331 }
1332
1333 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1334 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1335 /**
1336  *      mptfc_exit - Unregisters MPT adapter(s)
1337  *
1338  */
1339 static void __exit
1340 mptfc_exit(void)
1341 {
1342         pci_unregister_driver(&mptfc_driver);
1343         fc_release_transport(mptfc_transport_template);
1344
1345         mpt_reset_deregister(mptfcDoneCtx);
1346         dprintk((KERN_INFO MYNAM
1347           ": Deregistered for IOC reset notifications\n"));
1348
1349         mpt_event_deregister(mptfcDoneCtx);
1350         dprintk((KERN_INFO MYNAM
1351           ": Deregistered for IOC event notifications\n"));
1352
1353         mpt_deregister(mptfcInternalCtx);
1354         mpt_deregister(mptfcTaskCtx);
1355         mpt_deregister(mptfcDoneCtx);
1356 }
1357
1358 module_init(mptfc_init);
1359 module_exit(mptfc_exit);