drm/radeon/kms: enable use of unmappable VRAM V2
[pandora-kernel.git] / drivers / scsi / bfa / bfad_im.c
1 /*
2  * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17
18 /**
19  *  bfad_im.c Linux driver IM module.
20  */
21
22 #include "bfad_drv.h"
23 #include "bfad_im.h"
24 #include "bfad_trcmod.h"
25 #include "bfa_cb_ioim_macros.h"
26 #include <fcb/bfa_fcb_fcpim.h>
27
28 BFA_TRC_FILE(LDRV, IM);
29
30 DEFINE_IDR(bfad_im_port_index);
31 struct scsi_transport_template *bfad_im_scsi_transport_template;
32 static void bfad_im_itnim_work_handler(struct work_struct *work);
33 static int bfad_im_queuecommand(struct scsi_cmnd *cmnd,
34                 void (*done)(struct scsi_cmnd *));
35 static int bfad_im_slave_alloc(struct scsi_device *sdev);
36
37 void
38 bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
39                         enum bfi_ioim_status io_status, u8 scsi_status,
40                         int sns_len, u8 *sns_info, s32 residue)
41 {
42         struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
43         struct bfad_s         *bfad = drv;
44         struct bfad_itnim_data_s *itnim_data;
45         struct bfad_itnim_s *itnim;
46         u8         host_status = DID_OK;
47
48         switch (io_status) {
49         case BFI_IOIM_STS_OK:
50                 bfa_trc(bfad, scsi_status);
51                 scsi_set_resid(cmnd, 0);
52
53                 if (sns_len > 0) {
54                         bfa_trc(bfad, sns_len);
55                         if (sns_len > SCSI_SENSE_BUFFERSIZE)
56                                 sns_len = SCSI_SENSE_BUFFERSIZE;
57                         memcpy(cmnd->sense_buffer, sns_info, sns_len);
58                 }
59                 if (residue > 0) {
60                         bfa_trc(bfad, residue);
61                         scsi_set_resid(cmnd, residue);
62                         if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
63                                 (scsi_bufflen(cmnd) - residue) <
64                                         cmnd->underflow) {
65                                 bfa_trc(bfad, 0);
66                                 host_status = DID_ERROR;
67                         }
68                 }
69                 cmnd->result = ScsiResult(host_status, scsi_status);
70
71                 break;
72
73         case BFI_IOIM_STS_ABORTED:
74         case BFI_IOIM_STS_TIMEDOUT:
75         case BFI_IOIM_STS_PATHTOV:
76         default:
77                 cmnd->result = ScsiResult(DID_ERROR, 0);
78         }
79
80         /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
81         if (cmnd->device->host != NULL)
82                 scsi_dma_unmap(cmnd);
83
84         cmnd->host_scribble = NULL;
85         bfa_trc(bfad, cmnd->result);
86
87         itnim_data = cmnd->device->hostdata;
88         if (itnim_data) {
89                 itnim = itnim_data->itnim;
90                 if (!cmnd->result && itnim &&
91                          (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
92                         /* Queue depth adjustment for good status completion */
93                         bfad_os_ramp_up_qdepth(itnim, cmnd->device);
94                 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
95                         /* qfull handling */
96                         bfad_os_handle_qfull(itnim, cmnd->device);
97                 }
98         }
99
100         cmnd->scsi_done(cmnd);
101 }
102
103 void
104 bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
105 {
106         struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
107         struct bfad_itnim_data_s *itnim_data;
108         struct bfad_itnim_s *itnim;
109
110         cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
111
112         /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
113         if (cmnd->device->host != NULL)
114                 scsi_dma_unmap(cmnd);
115
116         cmnd->host_scribble = NULL;
117
118         /* Queue depth adjustment */
119         if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
120                 itnim_data = cmnd->device->hostdata;
121                 if (itnim_data) {
122                         itnim = itnim_data->itnim;
123                         if (itnim)
124                                 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
125                 }
126         }
127
128         cmnd->scsi_done(cmnd);
129 }
130
131 void
132 bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
133 {
134         struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
135         struct bfad_s         *bfad = drv;
136
137         cmnd->result = ScsiResult(DID_ERROR, 0);
138
139         /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
140         if (cmnd->device->host != NULL)
141                 scsi_dma_unmap(cmnd);
142
143         bfa_trc(bfad, cmnd->result);
144         cmnd->host_scribble = NULL;
145 }
146
147 void
148 bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
149                    enum bfi_tskim_status tsk_status)
150 {
151         struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
152         wait_queue_head_t *wq;
153
154         cmnd->SCp.Status |= tsk_status << 1;
155         set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
156         wq = (wait_queue_head_t *) cmnd->SCp.ptr;
157         cmnd->SCp.ptr = NULL;
158
159         if (wq)
160                 wake_up(wq);
161 }
162
163 void
164 bfa_cb_ioim_resfree(void *drv)
165 {
166 }
167
168 /**
169  *  Scsi_Host_template SCSI host template
170  */
171 /**
172  * Scsi_Host template entry, returns BFAD PCI info.
173  */
174 static const char *
175 bfad_im_info(struct Scsi_Host *shost)
176 {
177         static char     bfa_buf[256];
178         struct bfad_im_port_s *im_port =
179                         (struct bfad_im_port_s *) shost->hostdata[0];
180         struct bfad_s         *bfad = im_port->bfad;
181         char model[BFA_ADAPTER_MODEL_NAME_LEN];
182
183         bfa_get_adapter_model(&bfad->bfa, model);
184
185         memset(bfa_buf, 0, sizeof(bfa_buf));
186         snprintf(bfa_buf, sizeof(bfa_buf),
187                 "Brocade FC/FCOE Adapter, " "model: %s hwpath: %s driver: %s",
188                 model, bfad->pci_name, BFAD_DRIVER_VERSION);
189         return bfa_buf;
190 }
191
192 /**
193  * Scsi_Host template entry, aborts the specified SCSI command.
194  *
195  * Returns: SUCCESS or FAILED.
196  */
197 static int
198 bfad_im_abort_handler(struct scsi_cmnd *cmnd)
199 {
200         struct Scsi_Host *shost = cmnd->device->host;
201         struct bfad_im_port_s *im_port =
202                         (struct bfad_im_port_s *) shost->hostdata[0];
203         struct bfad_s         *bfad = im_port->bfad;
204         struct bfa_ioim_s *hal_io;
205         unsigned long   flags;
206         u32        timeout;
207         int             rc = FAILED;
208
209         spin_lock_irqsave(&bfad->bfad_lock, flags);
210         hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
211         if (!hal_io) {
212                 /* IO has been completed, retrun success */
213                 rc = SUCCESS;
214                 goto out;
215         }
216         if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
217                 rc = FAILED;
218                 goto out;
219         }
220
221         bfa_trc(bfad, hal_io->iotag);
222         bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT,
223                 im_port->shost->host_no, cmnd, hal_io->iotag);
224         bfa_ioim_abort(hal_io);
225         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
226
227         /* Need to wait until the command get aborted */
228         timeout = 10;
229         while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
230                 set_current_state(TASK_UNINTERRUPTIBLE);
231                 schedule_timeout(timeout);
232                 if (timeout < 4 * HZ)
233                         timeout *= 2;
234         }
235
236         cmnd->scsi_done(cmnd);
237         bfa_trc(bfad, hal_io->iotag);
238         bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT_COMP,
239                 im_port->shost->host_no, cmnd, hal_io->iotag);
240         return SUCCESS;
241 out:
242         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
243         return rc;
244 }
245
246 static bfa_status_t
247 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
248                      struct bfad_itnim_s *itnim)
249 {
250         struct bfa_tskim_s *tskim;
251         struct bfa_itnim_s *bfa_itnim;
252         bfa_status_t    rc = BFA_STATUS_OK;
253
254         bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
255         tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
256         if (!tskim) {
257                 BFA_DEV_PRINTF(bfad, BFA_ERR,
258                                "target reset, fail to allocate tskim\n");
259                 rc = BFA_STATUS_FAILED;
260                 goto out;
261         }
262
263         /*
264          * Set host_scribble to NULL to avoid aborting a task command if
265          * happens.
266          */
267         cmnd->host_scribble = NULL;
268         cmnd->SCp.Status = 0;
269         bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
270         bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
271                             FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
272 out:
273         return rc;
274 }
275
276 /**
277  * Scsi_Host template entry, resets a LUN and abort its all commands.
278  *
279  * Returns: SUCCESS or FAILED.
280  *
281  */
282 static int
283 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
284 {
285         struct Scsi_Host *shost = cmnd->device->host;
286         struct bfad_im_port_s *im_port =
287                         (struct bfad_im_port_s *) shost->hostdata[0];
288         struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
289         struct bfad_s         *bfad = im_port->bfad;
290         struct bfa_tskim_s *tskim;
291         struct bfad_itnim_s   *itnim;
292         struct bfa_itnim_s *bfa_itnim;
293         DECLARE_WAIT_QUEUE_HEAD(wq);
294         int             rc = SUCCESS;
295         unsigned long   flags;
296         enum bfi_tskim_status task_status;
297
298         spin_lock_irqsave(&bfad->bfad_lock, flags);
299         itnim = itnim_data->itnim;
300         if (!itnim) {
301                 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
302                 rc = FAILED;
303                 goto out;
304         }
305
306         tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
307         if (!tskim) {
308                 BFA_DEV_PRINTF(bfad, BFA_ERR,
309                                 "LUN reset, fail to allocate tskim");
310                 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
311                 rc = FAILED;
312                 goto out;
313         }
314
315         /**
316          * Set host_scribble to NULL to avoid aborting a task command
317          * if happens.
318          */
319         cmnd->host_scribble = NULL;
320         cmnd->SCp.ptr = (char *)&wq;
321         cmnd->SCp.Status = 0;
322         bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
323         bfa_tskim_start(tskim, bfa_itnim,
324                             bfad_int_to_lun(cmnd->device->lun),
325                             FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
326         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
327
328         wait_event(wq, test_bit(IO_DONE_BIT,
329                         (unsigned long *)&cmnd->SCp.Status));
330
331         task_status = cmnd->SCp.Status >> 1;
332         if (task_status != BFI_TSKIM_STS_OK) {
333                 BFA_DEV_PRINTF(bfad, BFA_ERR, "LUN reset failure, status: %d\n",
334                                task_status);
335                 rc = FAILED;
336         }
337
338 out:
339         return rc;
340 }
341
342 /**
343  * Scsi_Host template entry, resets the bus and abort all commands.
344  */
345 static int
346 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
347 {
348         struct Scsi_Host *shost = cmnd->device->host;
349         struct bfad_im_port_s *im_port =
350                                 (struct bfad_im_port_s *) shost->hostdata[0];
351         struct bfad_s         *bfad = im_port->bfad;
352         struct bfad_itnim_s   *itnim;
353         unsigned long   flags;
354         u32        i, rc, err_cnt = 0;
355         DECLARE_WAIT_QUEUE_HEAD(wq);
356         enum bfi_tskim_status task_status;
357
358         spin_lock_irqsave(&bfad->bfad_lock, flags);
359         for (i = 0; i < MAX_FCP_TARGET; i++) {
360                 itnim = bfad_os_get_itnim(im_port, i);
361                 if (itnim) {
362                         cmnd->SCp.ptr = (char *)&wq;
363                         rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
364                         if (rc != BFA_STATUS_OK) {
365                                 err_cnt++;
366                                 continue;
367                         }
368
369                         /* wait target reset to complete */
370                         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
371                         wait_event(wq, test_bit(IO_DONE_BIT,
372                                         (unsigned long *)&cmnd->SCp.Status));
373                         spin_lock_irqsave(&bfad->bfad_lock, flags);
374
375                         task_status = cmnd->SCp.Status >> 1;
376                         if (task_status != BFI_TSKIM_STS_OK) {
377                                 BFA_DEV_PRINTF(bfad, BFA_ERR,
378                                         "target reset failure,"
379                                         " status: %d\n", task_status);
380                                 err_cnt++;
381                         }
382                 }
383         }
384         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
385
386         if (err_cnt)
387                 return FAILED;
388
389         return SUCCESS;
390 }
391
392 /**
393  * Scsi_Host template entry slave_destroy.
394  */
395 static void
396 bfad_im_slave_destroy(struct scsi_device *sdev)
397 {
398         sdev->hostdata = NULL;
399         return;
400 }
401
402 /**
403  *  BFA FCS itnim callbacks
404  */
405
406 /**
407  * BFA FCS itnim alloc callback, after successful PRLI
408  * Context: Interrupt
409  */
410 void
411 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
412                     struct bfad_itnim_s **itnim_drv)
413 {
414         *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
415         if (*itnim_drv == NULL)
416                 return;
417
418         (*itnim_drv)->im = bfad->im;
419         *itnim = &(*itnim_drv)->fcs_itnim;
420         (*itnim_drv)->state = ITNIM_STATE_NONE;
421
422         /*
423          * Initiaze the itnim_work
424          */
425         INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
426         bfad->bfad_flags |= BFAD_RPORT_ONLINE;
427 }
428
429 /**
430  * BFA FCS itnim free callback.
431  * Context: Interrupt. bfad_lock is held
432  */
433 void
434 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
435 {
436         struct bfad_port_s    *port;
437         wwn_t wwpn;
438         u32 fcid;
439         char wwpn_str[32], fcid_str[16];
440
441         /* online to free state transtion should not happen */
442         bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
443
444         itnim_drv->queue_work = 1;
445         /* offline request is not yet done, use the same request to free */
446         if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
447                 itnim_drv->queue_work = 0;
448
449         itnim_drv->state = ITNIM_STATE_FREE;
450         port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
451         itnim_drv->im_port = port->im_port;
452         wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
453         fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
454         wwn2str(wwpn_str, wwpn);
455         fcid2str(fcid_str, fcid);
456         bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_FREE,
457                 port->im_port->shost->host_no,
458                 fcid_str, wwpn_str);
459         bfad_os_itnim_process(itnim_drv);
460 }
461
462 /**
463  * BFA FCS itnim online callback.
464  * Context: Interrupt. bfad_lock is held
465  */
466 void
467 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
468 {
469         struct bfad_port_s    *port;
470
471         itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
472         port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
473         itnim_drv->state = ITNIM_STATE_ONLINE;
474         itnim_drv->queue_work = 1;
475         itnim_drv->im_port = port->im_port;
476         bfad_os_itnim_process(itnim_drv);
477 }
478
479 /**
480  * BFA FCS itnim offline callback.
481  * Context: Interrupt. bfad_lock is held
482  */
483 void
484 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
485 {
486         struct bfad_port_s    *port;
487         struct bfad_s *bfad;
488
489         port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
490         bfad = port->bfad;
491         if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
492                  (port->flags & BFAD_PORT_DELETE)) {
493                 itnim_drv->state = ITNIM_STATE_OFFLINE;
494                 return;
495         }
496         itnim_drv->im_port = port->im_port;
497         itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
498         itnim_drv->queue_work = 1;
499         bfad_os_itnim_process(itnim_drv);
500 }
501
502 /**
503  * BFA FCS itnim timeout callback.
504  * Context: Interrupt. bfad_lock is held
505  */
506 void bfa_fcb_itnim_tov(struct bfad_itnim_s *itnim)
507 {
508         itnim->state = ITNIM_STATE_TIMEOUT;
509 }
510
511 /**
512  * Allocate a Scsi_Host for a port.
513  */
514 int
515 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
516 {
517         int error = 1;
518
519         if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
520                 printk(KERN_WARNING "idr_pre_get failure\n");
521                 goto out;
522         }
523
524         error = idr_get_new(&bfad_im_port_index, im_port,
525                                          &im_port->idr_id);
526         if (error) {
527                 printk(KERN_WARNING "idr_get_new failure\n");
528                 goto out;
529         }
530
531         im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
532         if (!im_port->shost) {
533                 error = 1;
534                 goto out_free_idr;
535         }
536
537         im_port->shost->hostdata[0] = (unsigned long)im_port;
538         im_port->shost->unique_id = im_port->idr_id;
539         im_port->shost->this_id = -1;
540         im_port->shost->max_id = MAX_FCP_TARGET;
541         im_port->shost->max_lun = MAX_FCP_LUN;
542         im_port->shost->max_cmd_len = 16;
543         im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
544         im_port->shost->transportt = bfad_im_scsi_transport_template;
545
546         error = bfad_os_scsi_add_host(im_port->shost, im_port, bfad);
547         if (error) {
548                 printk(KERN_WARNING "bfad_os_scsi_add_host failure %d\n",
549                                                         error);
550                 goto out_fc_rel;
551         }
552
553         /* setup host fixed attribute if the lk supports */
554         bfad_os_fc_host_init(im_port);
555
556         return 0;
557
558 out_fc_rel:
559         scsi_host_put(im_port->shost);
560 out_free_idr:
561         idr_remove(&bfad_im_port_index, im_port->idr_id);
562 out:
563         return error;
564 }
565
566 void
567 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
568 {
569         unsigned long flags;
570
571         bfa_trc(bfad, bfad->inst_no);
572         bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_HOST_FREE,
573                         im_port->shost->host_no);
574
575         fc_remove_host(im_port->shost);
576
577         scsi_remove_host(im_port->shost);
578         scsi_host_put(im_port->shost);
579
580         spin_lock_irqsave(&bfad->bfad_lock, flags);
581         idr_remove(&bfad_im_port_index, im_port->idr_id);
582         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
583 }
584
585 static void
586 bfad_im_port_delete_handler(struct work_struct *work)
587 {
588         struct bfad_im_port_s *im_port =
589                 container_of(work, struct bfad_im_port_s, port_delete_work);
590
591         bfad_im_scsi_host_free(im_port->bfad, im_port);
592         bfad_im_port_clean(im_port);
593         kfree(im_port);
594 }
595
596 bfa_status_t
597 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
598 {
599         int             rc = BFA_STATUS_OK;
600         struct bfad_im_port_s *im_port;
601
602         im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
603         if (im_port == NULL) {
604                 rc = BFA_STATUS_ENOMEM;
605                 goto ext;
606         }
607         port->im_port = im_port;
608         im_port->port = port;
609         im_port->bfad = bfad;
610
611         INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
612         INIT_LIST_HEAD(&im_port->itnim_mapped_list);
613         INIT_LIST_HEAD(&im_port->binding_list);
614
615 ext:
616         return rc;
617 }
618
619 void
620 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
621 {
622         struct bfad_im_port_s *im_port = port->im_port;
623
624         queue_work(bfad->im->drv_workq,
625                                 &im_port->port_delete_work);
626 }
627
628 void
629 bfad_im_port_clean(struct bfad_im_port_s *im_port)
630 {
631         struct bfad_fcp_binding *bp, *bp_new;
632         unsigned long flags;
633         struct bfad_s *bfad =  im_port->bfad;
634
635         spin_lock_irqsave(&bfad->bfad_lock, flags);
636         list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
637                                         list_entry) {
638                 list_del(&bp->list_entry);
639                 kfree(bp);
640         }
641
642         /* the itnim_mapped_list must be empty at this time */
643         bfa_assert(list_empty(&im_port->itnim_mapped_list));
644
645         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
646 }
647
648 void
649 bfad_im_port_online(struct bfad_s *bfad, struct bfad_port_s *port)
650 {
651 }
652
653 void
654 bfad_im_port_offline(struct bfad_s *bfad, struct bfad_port_s *port)
655 {
656 }
657
658 bfa_status_t
659 bfad_im_probe(struct bfad_s *bfad)
660 {
661         struct bfad_im_s      *im;
662         bfa_status_t    rc = BFA_STATUS_OK;
663
664         im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
665         if (im == NULL) {
666                 rc = BFA_STATUS_ENOMEM;
667                 goto ext;
668         }
669
670         bfad->im = im;
671         im->bfad = bfad;
672
673         if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
674                 kfree(im);
675                 rc = BFA_STATUS_FAILED;
676         }
677
678 ext:
679         return rc;
680 }
681
682 void
683 bfad_im_probe_undo(struct bfad_s *bfad)
684 {
685         if (bfad->im) {
686                 bfad_os_destroy_workq(bfad->im);
687                 kfree(bfad->im);
688                 bfad->im = NULL;
689         }
690 }
691
692
693
694
695 int
696 bfad_os_scsi_add_host(struct Scsi_Host *shost, struct bfad_im_port_s *im_port,
697                         struct bfad_s *bfad)
698 {
699     struct device *dev;
700
701     if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
702                 dev = &bfad->pcidev->dev;
703     else
704                 dev = &bfad->pport.im_port->shost->shost_gendev;
705
706     return scsi_add_host(shost, dev);
707 }
708
709 struct Scsi_Host *
710 bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
711 {
712         struct scsi_host_template *sht;
713
714         if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
715                 sht = &bfad_im_scsi_host_template;
716         else
717                 sht = &bfad_im_vport_template;
718
719         sht->sg_tablesize = bfad->cfg_data.io_max_sge;
720
721         return scsi_host_alloc(sht, sizeof(unsigned long));
722 }
723
724 void
725 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
726 {
727         flush_workqueue(bfad->im->drv_workq);
728         bfad_im_scsi_host_free(im_port->bfad, im_port);
729         bfad_im_port_clean(im_port);
730         kfree(im_port);
731 }
732
733 void
734 bfad_os_destroy_workq(struct bfad_im_s *im)
735 {
736         if (im && im->drv_workq) {
737                 destroy_workqueue(im->drv_workq);
738                 im->drv_workq = NULL;
739         }
740 }
741
742 bfa_status_t
743 bfad_os_thread_workq(struct bfad_s *bfad)
744 {
745         struct bfad_im_s      *im = bfad->im;
746
747         bfa_trc(bfad, 0);
748         snprintf(im->drv_workq_name, BFAD_KOBJ_NAME_LEN, "bfad_wq_%d",
749                  bfad->inst_no);
750         im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
751         if (!im->drv_workq)
752                 return BFA_STATUS_FAILED;
753
754         return BFA_STATUS_OK;
755 }
756
757 /**
758  * Scsi_Host template entry.
759  *
760  * Description:
761  * OS entry point to adjust the queue_depths on a per-device basis.
762  * Called once per device during the bus scan.
763  * Return non-zero if fails.
764  */
765 static int
766 bfad_im_slave_configure(struct scsi_device *sdev)
767 {
768         if (sdev->tagged_supported)
769                 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
770         else
771                 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
772
773         return 0;
774 }
775
776 struct scsi_host_template bfad_im_scsi_host_template = {
777         .module = THIS_MODULE,
778         .name = BFAD_DRIVER_NAME,
779         .info = bfad_im_info,
780         .queuecommand = bfad_im_queuecommand,
781         .eh_abort_handler = bfad_im_abort_handler,
782         .eh_device_reset_handler = bfad_im_reset_lun_handler,
783         .eh_bus_reset_handler = bfad_im_reset_bus_handler,
784
785         .slave_alloc = bfad_im_slave_alloc,
786         .slave_configure = bfad_im_slave_configure,
787         .slave_destroy = bfad_im_slave_destroy,
788
789         .this_id = -1,
790         .sg_tablesize = BFAD_IO_MAX_SGE,
791         .cmd_per_lun = 3,
792         .use_clustering = ENABLE_CLUSTERING,
793         .shost_attrs = bfad_im_host_attrs,
794         .max_sectors = 0xFFFF,
795 };
796
797 struct scsi_host_template bfad_im_vport_template = {
798         .module = THIS_MODULE,
799         .name = BFAD_DRIVER_NAME,
800         .info = bfad_im_info,
801         .queuecommand = bfad_im_queuecommand,
802         .eh_abort_handler = bfad_im_abort_handler,
803         .eh_device_reset_handler = bfad_im_reset_lun_handler,
804         .eh_bus_reset_handler = bfad_im_reset_bus_handler,
805
806         .slave_alloc = bfad_im_slave_alloc,
807         .slave_configure = bfad_im_slave_configure,
808         .slave_destroy = bfad_im_slave_destroy,
809
810         .this_id = -1,
811         .sg_tablesize = BFAD_IO_MAX_SGE,
812         .cmd_per_lun = 3,
813         .use_clustering = ENABLE_CLUSTERING,
814         .shost_attrs = bfad_im_vport_attrs,
815         .max_sectors = 0xFFFF,
816 };
817
818 void
819 bfad_im_probe_post(struct bfad_im_s *im)
820 {
821         flush_workqueue(im->drv_workq);
822 }
823
824 bfa_status_t
825 bfad_im_module_init(void)
826 {
827         bfad_im_scsi_transport_template =
828                 fc_attach_transport(&bfad_im_fc_function_template);
829         if (!bfad_im_scsi_transport_template)
830                 return BFA_STATUS_ENOMEM;
831
832         return BFA_STATUS_OK;
833 }
834
835 void
836 bfad_im_module_exit(void)
837 {
838         if (bfad_im_scsi_transport_template)
839                 fc_release_transport(bfad_im_scsi_transport_template);
840 }
841
842 void
843 bfad_os_itnim_process(struct bfad_itnim_s *itnim_drv)
844 {
845         struct bfad_im_s      *im = itnim_drv->im;
846
847         if (itnim_drv->queue_work)
848                 queue_work(im->drv_workq, &itnim_drv->itnim_work);
849 }
850
851 void
852 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
853 {
854         struct scsi_device *tmp_sdev;
855
856         if (((jiffies - itnim->last_ramp_up_time) >
857                 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
858                 ((jiffies - itnim->last_queue_full_time) >
859                 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
860                 shost_for_each_device(tmp_sdev, sdev->host) {
861                         if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
862                                 if (tmp_sdev->id != sdev->id)
863                                         continue;
864                                 if (tmp_sdev->ordered_tags)
865                                         scsi_adjust_queue_depth(tmp_sdev,
866                                                 MSG_ORDERED_TAG,
867                                                 tmp_sdev->queue_depth + 1);
868                                 else
869                                         scsi_adjust_queue_depth(tmp_sdev,
870                                                 MSG_SIMPLE_TAG,
871                                                 tmp_sdev->queue_depth + 1);
872
873                                 itnim->last_ramp_up_time = jiffies;
874                         }
875                 }
876         }
877 }
878
879 void
880 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
881 {
882         struct scsi_device *tmp_sdev;
883
884         itnim->last_queue_full_time = jiffies;
885
886         shost_for_each_device(tmp_sdev, sdev->host) {
887                 if (tmp_sdev->id != sdev->id)
888                         continue;
889                 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
890         }
891 }
892
893
894
895
896 struct bfad_itnim_s *
897 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
898 {
899         struct bfad_itnim_s   *itnim = NULL;
900
901         /* Search the mapped list for this target ID */
902         list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
903                 if (id == itnim->scsi_tgt_id)
904                         return itnim;
905         }
906
907         return NULL;
908 }
909
910 /**
911  * Scsi_Host template entry slave_alloc
912  */
913 static int
914 bfad_im_slave_alloc(struct scsi_device *sdev)
915 {
916         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
917
918         if (!rport || fc_remote_port_chkready(rport))
919                 return -ENXIO;
920
921         sdev->hostdata = rport->dd_data;
922
923         return 0;
924 }
925
926 void
927 bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
928 {
929         struct Scsi_Host *host = im_port->shost;
930         struct bfad_s         *bfad = im_port->bfad;
931         struct bfad_port_s    *port = im_port->port;
932         struct bfa_pport_attr_s pattr;
933         char model[BFA_ADAPTER_MODEL_NAME_LEN];
934         char fw_ver[BFA_VERSION_LEN];
935
936         fc_host_node_name(host) =
937                 bfa_os_htonll((bfa_fcs_port_get_nwwn(port->fcs_port)));
938         fc_host_port_name(host) =
939                 bfa_os_htonll((bfa_fcs_port_get_pwwn(port->fcs_port)));
940
941         fc_host_supported_classes(host) = FC_COS_CLASS3;
942
943         memset(fc_host_supported_fc4s(host), 0,
944                sizeof(fc_host_supported_fc4s(host)));
945         if (bfad_supported_fc4s & (BFA_PORT_ROLE_FCP_IM | BFA_PORT_ROLE_FCP_TM))
946                 /* For FCP type 0x08 */
947                 fc_host_supported_fc4s(host)[2] = 1;
948         if (bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IPFC)
949                 /* For LLC/SNAP type 0x05 */
950                 fc_host_supported_fc4s(host)[3] = 0x20;
951         /* For fibre channel services type 0x20 */
952         fc_host_supported_fc4s(host)[7] = 1;
953
954         bfa_get_adapter_model(&bfad->bfa, model);
955         bfa_get_adapter_fw_ver(&bfad->bfa, fw_ver);
956         sprintf(fc_host_symbolic_name(host), "Brocade %s FV%s DV%s",
957                 model, fw_ver, BFAD_DRIVER_VERSION);
958
959         fc_host_supported_speeds(host) = 0;
960         fc_host_supported_speeds(host) |=
961                 FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
962                 FC_PORTSPEED_1GBIT;
963
964         bfa_fcport_get_attr(&bfad->bfa, &pattr);
965         fc_host_maxframe_size(host) = pattr.pport_cfg.maxfrsize;
966 }
967
968 static void
969 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
970 {
971         struct fc_rport_identifiers rport_ids;
972         struct fc_rport *fc_rport;
973         struct bfad_itnim_data_s *itnim_data;
974
975         rport_ids.node_name =
976                 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
977         rport_ids.port_name =
978                 bfa_os_htonll(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
979         rport_ids.port_id =
980                 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
981         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
982
983         itnim->fc_rport = fc_rport =
984                 fc_remote_port_add(im_port->shost, 0, &rport_ids);
985
986         if (!fc_rport)
987                 return;
988
989         fc_rport->maxframe_size =
990                 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
991         fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
992
993         itnim_data = fc_rport->dd_data;
994         itnim_data->itnim = itnim;
995
996         rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
997
998         if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
999                 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1000
1001         if ((fc_rport->scsi_target_id != -1)
1002             && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1003                 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1004
1005         return;
1006 }
1007
1008 /**
1009  * Work queue handler using FC transport service
1010 * Context: kernel
1011  */
1012 static void
1013 bfad_im_itnim_work_handler(struct work_struct *work)
1014 {
1015         struct bfad_itnim_s   *itnim = container_of(work, struct bfad_itnim_s,
1016                                                         itnim_work);
1017         struct bfad_im_s      *im = itnim->im;
1018         struct bfad_s         *bfad = im->bfad;
1019         struct bfad_im_port_s *im_port;
1020         unsigned long   flags;
1021         struct fc_rport *fc_rport;
1022         wwn_t wwpn;
1023         u32 fcid;
1024         char wwpn_str[32], fcid_str[16];
1025
1026         spin_lock_irqsave(&bfad->bfad_lock, flags);
1027         im_port = itnim->im_port;
1028         bfa_trc(bfad, itnim->state);
1029         switch (itnim->state) {
1030         case ITNIM_STATE_ONLINE:
1031                 if (!itnim->fc_rport) {
1032                         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1033                         bfad_im_fc_rport_add(im_port, itnim);
1034                         spin_lock_irqsave(&bfad->bfad_lock, flags);
1035                         wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1036                         fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1037                         wwn2str(wwpn_str, wwpn);
1038                         fcid2str(fcid_str, fcid);
1039                         list_add_tail(&itnim->list_entry,
1040                                 &im_port->itnim_mapped_list);
1041                         bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_ONLINE,
1042                                 im_port->shost->host_no,
1043                                 itnim->scsi_tgt_id,
1044                                 fcid_str, wwpn_str);
1045                 } else {
1046                         printk(KERN_WARNING
1047                                 "%s: itnim %llx is already in online state\n",
1048                                 __func__,
1049                                 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1050                 }
1051
1052                 break;
1053         case ITNIM_STATE_OFFLINE_PENDING:
1054                 itnim->state = ITNIM_STATE_OFFLINE;
1055                 if (itnim->fc_rport) {
1056                         fc_rport = itnim->fc_rport;
1057                         ((struct bfad_itnim_data_s *)
1058                                 fc_rport->dd_data)->itnim = NULL;
1059                         itnim->fc_rport = NULL;
1060                         if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1061                                 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1062                                 fc_rport->dev_loss_tmo =
1063                                         bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1064                                 fc_remote_port_delete(fc_rport);
1065                                 spin_lock_irqsave(&bfad->bfad_lock, flags);
1066                         }
1067                         wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1068                         fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1069                         wwn2str(wwpn_str, wwpn);
1070                         fcid2str(fcid_str, fcid);
1071                         list_del(&itnim->list_entry);
1072                         bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_OFFLINE,
1073                                 im_port->shost->host_no,
1074                                 itnim->scsi_tgt_id,
1075                                 fcid_str, wwpn_str);
1076                 }
1077                 break;
1078         case ITNIM_STATE_FREE:
1079                 if (itnim->fc_rport) {
1080                         fc_rport = itnim->fc_rport;
1081                         ((struct bfad_itnim_data_s *)
1082                                 fc_rport->dd_data)->itnim = NULL;
1083                         itnim->fc_rport = NULL;
1084                         if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1085                                 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1086                                 fc_rport->dev_loss_tmo =
1087                                         bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1088                                 fc_remote_port_delete(fc_rport);
1089                                 spin_lock_irqsave(&bfad->bfad_lock, flags);
1090                         }
1091                         list_del(&itnim->list_entry);
1092                 }
1093
1094                 kfree(itnim);
1095                 break;
1096         default:
1097                 bfa_assert(0);
1098                 break;
1099         }
1100
1101         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1102 }
1103
1104 /**
1105  * Scsi_Host template entry, queue a SCSI command to the BFAD.
1106  */
1107 static int
1108 bfad_im_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1109 {
1110         struct bfad_im_port_s *im_port =
1111                 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1112         struct bfad_s         *bfad = im_port->bfad;
1113         struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1114         struct bfad_itnim_s   *itnim;
1115         struct bfa_ioim_s *hal_io;
1116         unsigned long   flags;
1117         int             rc;
1118         s16        sg_cnt = 0;
1119         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1120
1121         rc = fc_remote_port_chkready(rport);
1122         if (rc) {
1123                 cmnd->result = rc;
1124                 done(cmnd);
1125                 return 0;
1126         }
1127
1128         sg_cnt = scsi_dma_map(cmnd);
1129
1130         if (sg_cnt < 0)
1131                 return SCSI_MLQUEUE_HOST_BUSY;
1132
1133         cmnd->scsi_done = done;
1134
1135         spin_lock_irqsave(&bfad->bfad_lock, flags);
1136         if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1137                 printk(KERN_WARNING
1138                         "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1139                        bfad->inst_no, cmnd, cmnd->cmnd[0]);
1140                 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1141                 goto out_fail_cmd;
1142         }
1143
1144         itnim = itnim_data->itnim;
1145         if (!itnim) {
1146                 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1147                 goto out_fail_cmd;
1148         }
1149
1150         hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1151                                     itnim->bfa_itnim, sg_cnt);
1152         if (!hal_io) {
1153                 printk(KERN_WARNING "hal_io failure\n");
1154                 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1155                 scsi_dma_unmap(cmnd);
1156                 return SCSI_MLQUEUE_HOST_BUSY;
1157         }
1158
1159         cmnd->host_scribble = (char *)hal_io;
1160         bfa_trc_fp(bfad, hal_io->iotag);
1161         bfa_ioim_start(hal_io);
1162         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1163
1164         return 0;
1165
1166 out_fail_cmd:
1167         spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1168         scsi_dma_unmap(cmnd);
1169         if (done)
1170                 done(cmnd);
1171
1172         return 0;
1173 }
1174
1175 void
1176 bfad_os_rport_online_wait(struct bfad_s *bfad)
1177 {
1178         int i;
1179         int rport_delay = 10;
1180
1181         for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1182                  && i < bfa_linkup_delay; i++)
1183                 schedule_timeout_uninterruptible(HZ);
1184
1185         if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1186                 rport_delay = rport_delay < bfa_linkup_delay ?
1187                                  rport_delay : bfa_linkup_delay;
1188                 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1189                          && i < rport_delay; i++)
1190                         schedule_timeout_uninterruptible(HZ);
1191
1192                 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE))
1193                         schedule_timeout_uninterruptible(rport_delay * HZ);
1194         }
1195 }
1196
1197 int
1198 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1199 {
1200
1201         u8         nwwns = 0;
1202         wwn_t           *wwns;
1203         int             ldelay;
1204
1205         /*
1206          * Querying for the boot target port wwns
1207          * -- read from boot information in flash.
1208          * If nwwns > 0 => boot over SAN and set bfa_linkup_delay = 30
1209          * else => local boot machine set bfa_linkup_delay = 10
1210          */
1211
1212         bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, &wwns);
1213
1214         if (nwwns > 0) {
1215                 /* If boot over SAN; linkup_delay = 30sec */
1216                 ldelay = 30;
1217         } else {
1218                 /* If local boot; linkup_delay = 10sec */
1219                 ldelay = 0;
1220         }
1221
1222         return ldelay;
1223 }
1224
1225