Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/i2c-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 static int mpt_pq_filter = 0;
81 module_param(mpt_pq_filter, int, 0);
82 MODULE_PARM_DESC(mpt_pq_filter, " Enable peripheral qualifier filter: enable=1  (default=0)");
83
84 #define MPTFC_DEV_LOSS_TMO (60)
85 static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;     /* reasonable default */
86 module_param(mptfc_dev_loss_tmo, int, 0);
87 MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
88                                      " transport to wait for an rport to "
89                                      " return following a device loss event."
90                                      "  Default=60.");
91
92 static int      mptfcDoneCtx = -1;
93 static int      mptfcTaskCtx = -1;
94 static int      mptfcInternalCtx = -1; /* Used only for internal commands */
95
96 static int mptfc_target_alloc(struct scsi_target *starget);
97 static int mptfc_slave_alloc(struct scsi_device *sdev);
98 static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
99                       void (*done)(struct scsi_cmnd *));
100 static void mptfc_target_destroy(struct scsi_target *starget);
101 static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
102 static void __devexit mptfc_remove(struct pci_dev *pdev);
103
104 static struct scsi_host_template mptfc_driver_template = {
105         .module                         = THIS_MODULE,
106         .proc_name                      = "mptfc",
107         .proc_info                      = mptscsih_proc_info,
108         .name                           = "MPT FC Host",
109         .info                           = mptscsih_info,
110         .queuecommand                   = mptfc_qcmd,
111         .target_alloc                   = mptfc_target_alloc,
112         .slave_alloc                    = mptfc_slave_alloc,
113         .slave_configure                = mptscsih_slave_configure,
114         .target_destroy                 = mptfc_target_destroy,
115         .slave_destroy                  = mptscsih_slave_destroy,
116         .change_queue_depth             = mptscsih_change_queue_depth,
117         .eh_abort_handler               = mptscsih_abort,
118         .eh_device_reset_handler        = mptscsih_dev_reset,
119         .eh_bus_reset_handler           = mptscsih_bus_reset,
120         .eh_host_reset_handler          = mptscsih_host_reset,
121         .bios_param                     = mptscsih_bios_param,
122         .can_queue                      = MPT_FC_CAN_QUEUE,
123         .this_id                        = -1,
124         .sg_tablesize                   = MPT_SCSI_SG_DEPTH,
125         .max_sectors                    = 8192,
126         .cmd_per_lun                    = 7,
127         .use_clustering                 = ENABLE_CLUSTERING,
128 };
129
130 /****************************************************************************
131  * Supported hardware
132  */
133
134 static struct pci_device_id mptfc_pci_table[] = {
135         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC909,
136                 PCI_ANY_ID, PCI_ANY_ID },
137         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC919,
138                 PCI_ANY_ID, PCI_ANY_ID },
139         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC929,
140                 PCI_ANY_ID, PCI_ANY_ID },
141         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC919X,
142                 PCI_ANY_ID, PCI_ANY_ID },
143         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC929X,
144                 PCI_ANY_ID, PCI_ANY_ID },
145         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC939X,
146                 PCI_ANY_ID, PCI_ANY_ID },
147         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC949X,
148                 PCI_ANY_ID, PCI_ANY_ID },
149         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC949ES,
150                 PCI_ANY_ID, PCI_ANY_ID },
151         {0}     /* Terminating entry */
152 };
153 MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
154
155 static struct scsi_transport_template *mptfc_transport_template = NULL;
156
157 static struct fc_function_template mptfc_transport_functions = {
158         .dd_fcrport_size = 8,
159         .show_host_node_name = 1,
160         .show_host_port_name = 1,
161         .show_host_supported_classes = 1,
162         .show_host_port_id = 1,
163         .show_rport_supported_classes = 1,
164         .show_starget_node_name = 1,
165         .show_starget_port_name = 1,
166         .show_starget_port_id = 1,
167         .set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
168         .show_rport_dev_loss_tmo = 1,
169
170 };
171
172 /* FIXME! values controlling firmware RESCAN event
173  * need to be set low to allow dev_loss_tmo to
174  * work as expected.  Currently, firmware doesn't
175  * notify driver of RESCAN event until some number
176  * of seconds elapse.  This value can be set via
177  * lsiutil.
178  */
179 static void
180 mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
181 {
182         if (timeout > 0)
183                 rport->dev_loss_tmo = timeout;
184         else
185                 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
186 }
187
188 static int
189 mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
190 {
191         FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
192         FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
193
194         if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
195                 if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
196                         return 0;
197                 if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
198                         return -1;
199                 return 1;
200         }
201         if ((*aa)->CurrentBus < (*bb)->CurrentBus)
202                 return -1;
203         return 1;
204 }
205
206 static int
207 mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
208         void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
209 {
210         ConfigPageHeader_t       hdr;
211         CONFIGPARMS              cfg;
212         FCDevicePage0_t         *ppage0_alloc, *fc;
213         dma_addr_t               page0_dma;
214         int                      data_sz;
215         int                      ii;
216
217         FCDevicePage0_t         *p0_array=NULL, *p_p0;
218         FCDevicePage0_t         **pp0_array=NULL, **p_pp0;
219
220         int                      rc = -ENOMEM;
221         U32                      port_id = 0xffffff;
222         int                      num_targ = 0;
223         int                      max_bus = ioc->facts.MaxBuses;
224         int                      max_targ = ioc->facts.MaxDevices;
225
226         if (max_bus == 0 || max_targ == 0)
227                 goto out;
228
229         data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
230         p_p0 = p0_array =  kzalloc(data_sz, GFP_KERNEL);
231         if (!p0_array)
232                 goto out;
233
234         data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
235         p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
236         if (!pp0_array)
237                 goto out;
238
239         do {
240                 /* Get FC Device Page 0 header */
241                 hdr.PageVersion = 0;
242                 hdr.PageLength = 0;
243                 hdr.PageNumber = 0;
244                 hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
245                 cfg.cfghdr.hdr = &hdr;
246                 cfg.physAddr = -1;
247                 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
248                 cfg.dir = 0;
249                 cfg.pageAddr = port_id;
250                 cfg.timeout = 0;
251
252                 if ((rc = mpt_config(ioc, &cfg)) != 0)
253                         break;
254
255                 if (hdr.PageLength <= 0)
256                         break;
257
258                 data_sz = hdr.PageLength * 4;
259                 ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
260                                                         &page0_dma);
261                 rc = -ENOMEM;
262                 if (!ppage0_alloc)
263                         break;
264
265                 cfg.physAddr = page0_dma;
266                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
267
268                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
269                         ppage0_alloc->PortIdentifier =
270                                 le32_to_cpu(ppage0_alloc->PortIdentifier);
271
272                         ppage0_alloc->WWNN.Low =
273                                 le32_to_cpu(ppage0_alloc->WWNN.Low);
274
275                         ppage0_alloc->WWNN.High =
276                                 le32_to_cpu(ppage0_alloc->WWNN.High);
277
278                         ppage0_alloc->WWPN.Low =
279                                 le32_to_cpu(ppage0_alloc->WWPN.Low);
280
281                         ppage0_alloc->WWPN.High =
282                                 le32_to_cpu(ppage0_alloc->WWPN.High);
283
284                         ppage0_alloc->BBCredit =
285                                 le16_to_cpu(ppage0_alloc->BBCredit);
286
287                         ppage0_alloc->MaxRxFrameSize =
288                                 le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
289
290                         port_id = ppage0_alloc->PortIdentifier;
291                         num_targ++;
292                         *p_p0 = *ppage0_alloc;  /* save data */
293                         *p_pp0++ = p_p0++;      /* save addr */
294                 }
295                 pci_free_consistent(ioc->pcidev, data_sz,
296                                         (u8 *) ppage0_alloc, page0_dma);
297                 if (rc != 0)
298                         break;
299
300         } while (port_id <= 0xff0000);
301
302         if (num_targ) {
303                 /* sort array */
304                 if (num_targ > 1)
305                         sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
306                                 mptfc_FcDevPage0_cmp_func, NULL);
307                 /* call caller's func for each targ */
308                 for (ii = 0; ii < num_targ;  ii++) {
309                         fc = *(pp0_array+ii);
310                         func(ioc, ioc_port, fc);
311                 }
312         }
313
314  out:
315         if (pp0_array)
316                 kfree(pp0_array);
317         if (p0_array)
318                 kfree(p0_array);
319         return rc;
320 }
321
322 static int
323 mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
324 {
325         /* not currently usable */
326         if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
327                           MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
328                 return -1;
329
330         if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
331                 return -1;
332
333         if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
334                 return -1;
335
336         /*
337          * board data structure already normalized to platform endianness
338          * shifted to avoid unaligned access on 64 bit architecture
339          */
340         rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
341         rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
342         rid->port_id =   pg0->PortIdentifier;
343         rid->roles = FC_RPORT_ROLE_UNKNOWN;
344
345         return 0;
346 }
347
348 static void
349 mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
350 {
351         struct fc_rport_identifiers rport_ids;
352         struct fc_rport         *rport;
353         struct mptfc_rport_info *ri;
354         int                     new_ri = 1;
355         u64                     pn, nn;
356         VirtTarget              *vtarget;
357         u32                     roles = FC_RPORT_ROLE_UNKNOWN;
358
359         if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
360                 return;
361
362         roles |= FC_RPORT_ROLE_FCP_TARGET;
363         if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
364                 roles |= FC_RPORT_ROLE_FCP_INITIATOR;
365
366         /* scan list looking for a match */
367         list_for_each_entry(ri, &ioc->fc_rports, list) {
368                 pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
369                 if (pn == rport_ids.port_name) {        /* match */
370                         list_move_tail(&ri->list, &ioc->fc_rports);
371                         new_ri = 0;
372                         break;
373                 }
374         }
375         if (new_ri) {   /* allocate one */
376                 ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
377                 if (!ri)
378                         return;
379                 list_add_tail(&ri->list, &ioc->fc_rports);
380         }
381
382         ri->pg0 = *pg0; /* add/update pg0 data */
383         ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
384
385         /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
386         if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
387                 ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
388                 rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
389                 if (rport) {
390                         ri->rport = rport;
391                         if (new_ri) /* may have been reset by user */
392                                 rport->dev_loss_tmo = mptfc_dev_loss_tmo;
393                         /*
394                          * if already mapped, remap here.  If not mapped,
395                          * target_alloc will allocate vtarget and map,
396                          * slave_alloc will fill in vdev from vtarget.
397                          */
398                         if (ri->starget) {
399                                 vtarget = ri->starget->hostdata;
400                                 if (vtarget) {
401                                         vtarget->target_id = pg0->CurrentTargetID;
402                                         vtarget->bus_id = pg0->CurrentBus;
403                                 }
404                         }
405                         *((struct mptfc_rport_info **)rport->dd_data) = ri;
406                         /* scan will be scheduled once rport becomes a target */
407                         fc_remote_port_rolechg(rport,roles);
408
409                         pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
410                         nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
411                         dfcprintk ((MYIOC_s_INFO_FMT
412                                 "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
413                                 "rport tid %d, tmo %d\n",
414                                         ioc->name,
415                                         ioc->sh->host_no,
416                                         pg0->PortIdentifier,
417                                         (unsigned long long)nn,
418                                         (unsigned long long)pn,
419                                         pg0->CurrentTargetID,
420                                         ri->rport->scsi_target_id,
421                                         ri->rport->dev_loss_tmo));
422                 } else {
423                         list_del(&ri->list);
424                         kfree(ri);
425                         ri = NULL;
426                 }
427         }
428 }
429
430 /*
431  *      OS entry point to allow for host driver to free allocated memory
432  *      Called if no device present or device being unloaded
433  */
434 static void
435 mptfc_target_destroy(struct scsi_target *starget)
436 {
437         struct fc_rport         *rport;
438         struct mptfc_rport_info *ri;
439
440         rport = starget_to_rport(starget);
441         if (rport) {
442                 ri = *((struct mptfc_rport_info **)rport->dd_data);
443                 if (ri) /* better be! */
444                         ri->starget = NULL;
445         }
446         if (starget->hostdata)
447                 kfree(starget->hostdata);
448         starget->hostdata = NULL;
449 }
450
451 /*
452  *      OS entry point to allow host driver to alloc memory
453  *      for each scsi target. Called once per device the bus scan.
454  *      Return non-zero if allocation fails.
455  */
456 static int
457 mptfc_target_alloc(struct scsi_target *starget)
458 {
459         VirtTarget              *vtarget;
460         struct fc_rport         *rport;
461         struct mptfc_rport_info *ri;
462         int                     rc;
463
464         vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
465         if (!vtarget)
466                 return -ENOMEM;
467         starget->hostdata = vtarget;
468
469         rc = -ENODEV;
470         rport = starget_to_rport(starget);
471         if (rport) {
472                 ri = *((struct mptfc_rport_info **)rport->dd_data);
473                 if (ri) {       /* better be! */
474                         vtarget->target_id = ri->pg0.CurrentTargetID;
475                         vtarget->bus_id = ri->pg0.CurrentBus;
476                         ri->starget = starget;
477                         rc = 0;
478                 }
479         }
480         if (rc != 0) {
481                 kfree(vtarget);
482                 starget->hostdata = NULL;
483         }
484
485         return rc;
486 }
487
488 /*
489  *      OS entry point to allow host driver to alloc memory
490  *      for each scsi device. Called once per device the bus scan.
491  *      Return non-zero if allocation fails.
492  *      Init memory once per LUN.
493  */
494 static int
495 mptfc_slave_alloc(struct scsi_device *sdev)
496 {
497         MPT_SCSI_HOST           *hd;
498         VirtTarget              *vtarget;
499         VirtDevice              *vdev;
500         struct scsi_target      *starget;
501         struct fc_rport         *rport;
502
503
504         starget = scsi_target(sdev);
505         rport = starget_to_rport(starget);
506
507         if (!rport || fc_remote_port_chkready(rport))
508                 return -ENXIO;
509
510         hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
511
512         vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
513         if (!vdev) {
514                 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
515                                 hd->ioc->name, sizeof(VirtDevice));
516                 return -ENOMEM;
517         }
518
519
520         sdev->hostdata = vdev;
521         vtarget = starget->hostdata;
522
523         if (vtarget->num_luns == 0) {
524                 vtarget->ioc_id = hd->ioc->id;
525                 vtarget->tflags = MPT_TARGET_FLAGS_Q_YES |
526                                   MPT_TARGET_FLAGS_VALID_INQUIRY;
527                 hd->Targets[sdev->id] = vtarget;
528         }
529
530         vdev->vtarget = vtarget;
531         vdev->lun = sdev->lun;
532
533         vtarget->num_luns++;
534
535
536 #ifdef DMPT_DEBUG_FC
537         {
538         u64 nn, pn;
539         struct mptfc_rport_info *ri;
540         ri = *((struct mptfc_rport_info **)rport->dd_data);
541         pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
542         nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
543         dfcprintk ((MYIOC_s_INFO_FMT
544                 "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
545                 "CurrentTargetID %d, %x %llx %llx\n",
546                 hd->ioc->name,
547                 sdev->host->host_no,
548                 vtarget->num_luns,
549                 sdev->id, ri->pg0.CurrentTargetID,
550                 ri->pg0.PortIdentifier,
551                 (unsigned long long)pn,
552                 (unsigned long long)nn));
553         }
554 #endif
555
556         return 0;
557 }
558
559 static int
560 mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
561 {
562         struct mptfc_rport_info *ri;
563         struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
564         int             err;
565
566         err = fc_remote_port_chkready(rport);
567         if (unlikely(err)) {
568                 SCpnt->result = err;
569                 done(SCpnt);
570                 return 0;
571         }
572
573         /* dd_data is null until finished adding target */
574         ri = *((struct mptfc_rport_info **)rport->dd_data);
575         if (unlikely(!ri)) {
576                 dfcprintk ((MYIOC_s_INFO_FMT
577                         "mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
578                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
579                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
580                         SCpnt->device->id,SCpnt->device->lun));
581                 SCpnt->result = DID_IMM_RETRY << 16;
582                 done(SCpnt);
583                 return 0;
584         }
585
586         err = mptscsih_qcmd(SCpnt,done);
587 #ifdef DMPT_DEBUG_FC
588         if (unlikely(err)) {
589                 dfcprintk ((MYIOC_s_INFO_FMT
590                         "mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero.\n",
591                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
592                         ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
593                         SCpnt->device->id,SCpnt->device->lun));
594         }
595 #endif
596         return err;
597 }
598
599 static void
600 mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
601 {
602         unsigned class = 0, cos = 0;
603
604         /* don't know what to do as only one scsi (fc) host was allocated */
605         if (portnum != 0)
606                 return;
607
608         class = ioc->fc_port_page0[portnum].SupportedServiceClass;
609         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
610                 cos |= FC_COS_CLASS1;
611         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
612                 cos |= FC_COS_CLASS2;
613         if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
614                 cos |= FC_COS_CLASS3;
615
616         fc_host_node_name(ioc->sh) =
617                 (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
618                     | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
619
620         fc_host_port_name(ioc->sh) =
621                 (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
622                     | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
623
624         fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
625
626         fc_host_supported_classes(ioc->sh) = cos;
627
628         fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
629 }
630
631 static void
632 mptfc_rescan_devices(void *arg)
633 {
634         MPT_ADAPTER             *ioc = (MPT_ADAPTER *)arg;
635         int                     ii;
636         int                     work_to_do;
637         u64                     pn;
638         unsigned long           flags;
639         struct mptfc_rport_info *ri;
640
641         do {
642                 /* start by tagging all ports as missing */
643                 list_for_each_entry(ri, &ioc->fc_rports, list) {
644                         if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
645                                 ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
646                         }
647                 }
648
649                 /*
650                  * now rescan devices known to adapter,
651                  * will reregister existing rports
652                  */
653                 for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
654                         (void) mptbase_GetFcPortPage0(ioc, ii);
655                         mptfc_init_host_attr(ioc,ii);   /* refresh */
656                         mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
657                 }
658
659                 /* delete devices still missing */
660                 list_for_each_entry(ri, &ioc->fc_rports, list) {
661                         /* if newly missing, delete it */
662                         if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
663
664                                 ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
665                                                MPT_RPORT_INFO_FLAGS_MISSING);
666                                 fc_remote_port_delete(ri->rport);       /* won't sleep */
667                                 ri->rport = NULL;
668
669                                 pn = (u64)ri->pg0.WWPN.High << 32 |
670                                      (u64)ri->pg0.WWPN.Low;
671                                 dfcprintk ((MYIOC_s_INFO_FMT
672                                         "mptfc_rescan.%d: %llx deleted\n",
673                                         ioc->name,
674                                         ioc->sh->host_no,
675                                         (unsigned long long)pn));
676                         }
677                 }
678
679                 /*
680                  * allow multiple passes as target state
681                  * might have changed during scan
682                  */
683                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
684                 if (ioc->fc_rescan_work_count > 2)      /* only need one more */
685                         ioc->fc_rescan_work_count = 2;
686                 work_to_do = --ioc->fc_rescan_work_count;
687                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
688         } while (work_to_do);
689 }
690
691 static int
692 mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
693 {
694         struct Scsi_Host        *sh;
695         MPT_SCSI_HOST           *hd;
696         MPT_ADAPTER             *ioc;
697         unsigned long            flags;
698         int                      ii;
699         int                      numSGE = 0;
700         int                      scale;
701         int                      ioc_cap;
702         int                     error=0;
703         int                     r;
704
705         if ((r = mpt_attach(pdev,id)) != 0)
706                 return r;
707
708         ioc = pci_get_drvdata(pdev);
709         ioc->DoneCtx = mptfcDoneCtx;
710         ioc->TaskCtx = mptfcTaskCtx;
711         ioc->InternalCtx = mptfcInternalCtx;
712
713         /*  Added sanity check on readiness of the MPT adapter.
714          */
715         if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
716                 printk(MYIOC_s_WARN_FMT
717                   "Skipping because it's not operational!\n",
718                   ioc->name);
719                 error = -ENODEV;
720                 goto out_mptfc_probe;
721         }
722
723         if (!ioc->active) {
724                 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
725                   ioc->name);
726                 error = -ENODEV;
727                 goto out_mptfc_probe;
728         }
729
730         /*  Sanity check - ensure at least 1 port is INITIATOR capable
731          */
732         ioc_cap = 0;
733         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
734                 if (ioc->pfacts[ii].ProtocolFlags &
735                     MPI_PORTFACTS_PROTOCOL_INITIATOR)
736                         ioc_cap ++;
737         }
738
739         if (!ioc_cap) {
740                 printk(MYIOC_s_WARN_FMT
741                         "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
742                         ioc->name, ioc);
743                 return -ENODEV;
744         }
745
746         sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
747
748         if (!sh) {
749                 printk(MYIOC_s_WARN_FMT
750                         "Unable to register controller with SCSI subsystem\n",
751                         ioc->name);
752                 error = -1;
753                 goto out_mptfc_probe;
754         }
755
756         INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
757
758         spin_lock_irqsave(&ioc->FreeQlock, flags);
759
760         /* Attach the SCSI Host to the IOC structure
761          */
762         ioc->sh = sh;
763
764         sh->io_port = 0;
765         sh->n_io_port = 0;
766         sh->irq = 0;
767
768         /* set 16 byte cdb's */
769         sh->max_cmd_len = 16;
770
771         sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
772
773         sh->max_lun = MPT_LAST_LUN + 1;
774         sh->max_channel = 0;
775         sh->this_id = ioc->pfacts[0].PortSCSIID;
776
777         /* Required entry.
778          */
779         sh->unique_id = ioc->id;
780
781         /* Verify that we won't exceed the maximum
782          * number of chain buffers
783          * We can optimize:  ZZ = req_sz/sizeof(SGE)
784          * For 32bit SGE's:
785          *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
786          *               + (req_sz - 64)/sizeof(SGE)
787          * A slightly different algorithm is required for
788          * 64bit SGEs.
789          */
790         scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
791         if (sizeof(dma_addr_t) == sizeof(u64)) {
792                 numSGE = (scale - 1) *
793                   (ioc->facts.MaxChainDepth-1) + scale +
794                   (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
795                   sizeof(u32));
796         } else {
797                 numSGE = 1 + (scale - 1) *
798                   (ioc->facts.MaxChainDepth-1) + scale +
799                   (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
800                   sizeof(u32));
801         }
802
803         if (numSGE < sh->sg_tablesize) {
804                 /* Reset this value */
805                 dprintk((MYIOC_s_INFO_FMT
806                   "Resetting sg_tablesize to %d from %d\n",
807                   ioc->name, numSGE, sh->sg_tablesize));
808                 sh->sg_tablesize = numSGE;
809         }
810
811         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
812
813         hd = (MPT_SCSI_HOST *) sh->hostdata;
814         hd->ioc = ioc;
815
816         /* SCSI needs scsi_cmnd lookup table!
817          * (with size equal to req_depth*PtrSz!)
818          */
819         hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
820         if (!hd->ScsiLookup) {
821                 error = -ENOMEM;
822                 goto out_mptfc_probe;
823         }
824
825         dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
826                  ioc->name, hd->ScsiLookup));
827
828         /* Allocate memory for the device structures.
829          * A non-Null pointer at an offset
830          * indicates a device exists.
831          * max_id = 1 + maximum id (hosts.h)
832          */
833         hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
834         if (!hd->Targets) {
835                 error = -ENOMEM;
836                 goto out_mptfc_probe;
837         }
838
839         dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
840
841         /* Clear the TM flags
842          */
843         hd->tmPending = 0;
844         hd->tmState = TM_STATE_NONE;
845         hd->resetPending = 0;
846         hd->abortSCpnt = NULL;
847
848         /* Clear the pointer used to store
849          * single-threaded commands, i.e., those
850          * issued during a bus scan, dv and
851          * configuration pages.
852          */
853         hd->cmdPtr = NULL;
854
855         /* Initialize this SCSI Hosts' timers
856          * To use, set the timer expires field
857          * and add_timer
858          */
859         init_timer(&hd->timer);
860         hd->timer.data = (unsigned long) hd;
861         hd->timer.function = mptscsih_timer_expired;
862
863         hd->mpt_pq_filter = mpt_pq_filter;
864
865         ddvprintk((MYIOC_s_INFO_FMT
866                 "mpt_pq_filter %x\n",
867                 ioc->name, 
868                 mpt_pq_filter));
869
870         init_waitqueue_head(&hd->scandv_waitq);
871         hd->scandv_wait_done = 0;
872         hd->last_queue_full = 0;
873
874         sh->transportt = mptfc_transport_template;
875         error = scsi_add_host (sh, &ioc->pcidev->dev);
876         if(error) {
877                 dprintk((KERN_ERR MYNAM
878                   "scsi_add_host failed\n"));
879                 goto out_mptfc_probe;
880         }
881
882         /* initialize workqueue */
883
884         snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
885                 sh->host_no);
886         ioc->fc_rescan_work_q =
887                 create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
888         if (!ioc->fc_rescan_work_q)
889                 goto out_mptfc_probe;
890
891         /*
892          * scan for rports -
893          *      by doing it via the workqueue, some locking is eliminated
894          */
895
896         ioc->fc_rescan_work_count = 1;
897         queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
898         flush_workqueue(ioc->fc_rescan_work_q);
899
900         return 0;
901
902 out_mptfc_probe:
903
904         mptscsih_remove(pdev);
905         return error;
906 }
907
908 static struct pci_driver mptfc_driver = {
909         .name           = "mptfc",
910         .id_table       = mptfc_pci_table,
911         .probe          = mptfc_probe,
912         .remove         = __devexit_p(mptfc_remove),
913         .shutdown       = mptscsih_shutdown,
914 #ifdef CONFIG_PM
915         .suspend        = mptscsih_suspend,
916         .resume         = mptscsih_resume,
917 #endif
918 };
919
920 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
921 /**
922  *      mptfc_init - Register MPT adapter(s) as SCSI host(s) with
923  *      linux scsi mid-layer.
924  *
925  *      Returns 0 for success, non-zero for failure.
926  */
927 static int __init
928 mptfc_init(void)
929 {
930         int error;
931
932         show_mptmod_ver(my_NAME, my_VERSION);
933
934         /* sanity check module parameter */
935         if (mptfc_dev_loss_tmo == 0)
936                 mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
937
938         mptfc_transport_template =
939                 fc_attach_transport(&mptfc_transport_functions);
940
941         if (!mptfc_transport_template)
942                 return -ENODEV;
943
944         mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
945         mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
946         mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
947
948         if (mpt_event_register(mptfcDoneCtx, mptscsih_event_process) == 0) {
949                 devtverboseprintk((KERN_INFO MYNAM
950                   ": Registered for IOC event notifications\n"));
951         }
952
953         if (mpt_reset_register(mptfcDoneCtx, mptscsih_ioc_reset) == 0) {
954                 dprintk((KERN_INFO MYNAM
955                   ": Registered for IOC reset notifications\n"));
956         }
957
958         error = pci_register_driver(&mptfc_driver);
959         if (error)
960                 fc_release_transport(mptfc_transport_template);
961
962         return error;
963 }
964
965 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
966 /**
967  *      mptfc_remove - Removed fc infrastructure for devices
968  *      @pdev: Pointer to pci_dev structure
969  *
970  */
971 static void __devexit
972 mptfc_remove(struct pci_dev *pdev)
973 {
974         MPT_ADAPTER             *ioc = pci_get_drvdata(pdev);
975         struct mptfc_rport_info *p, *n;
976         struct workqueue_struct *work_q;
977         unsigned long           flags;
978
979         /* destroy workqueue */
980         if ((work_q=ioc->fc_rescan_work_q)) {
981                 spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
982                 ioc->fc_rescan_work_q = NULL;
983                 spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
984                 destroy_workqueue(work_q);
985         }
986
987         fc_remove_host(ioc->sh);
988
989         list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
990                 list_del(&p->list);
991                 kfree(p);
992         }
993
994         mptscsih_remove(pdev);
995 }
996
997 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
998 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
999 /**
1000  *      mptfc_exit - Unregisters MPT adapter(s)
1001  *
1002  */
1003 static void __exit
1004 mptfc_exit(void)
1005 {
1006         pci_unregister_driver(&mptfc_driver);
1007         fc_release_transport(mptfc_transport_template);
1008
1009         mpt_reset_deregister(mptfcDoneCtx);
1010         dprintk((KERN_INFO MYNAM
1011           ": Deregistered for IOC reset notifications\n"));
1012
1013         mpt_event_deregister(mptfcDoneCtx);
1014         dprintk((KERN_INFO MYNAM
1015           ": Deregistered for IOC event notifications\n"));
1016
1017         mpt_deregister(mptfcInternalCtx);
1018         mpt_deregister(mptfcTaskCtx);
1019         mpt_deregister(mptfcDoneCtx);
1020 }
1021
1022 module_init(mptfc_init);
1023 module_exit(mptfc_exit);