blob: d601ac543c52bd1f24b50a760c240ab2c374f62c [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18#include <linux/module.h>
19#include <linux/mempool.h>
20#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070022#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/pci.h>
25#include <linux/skbuff.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/workqueue.h>
Joe Eykholt78112e52009-11-03 11:49:22 -080029#include <linux/if_ether.h>
30#include <scsi/fc/fc_fip.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070031#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_fc.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/libfc.h>
36#include <scsi/fc_frame.h>
37
38#include "vnic_dev.h"
39#include "vnic_intr.h"
40#include "vnic_stats.h"
41#include "fnic_io.h"
42#include "fnic.h"
43
44#define PCI_DEVICE_ID_CISCO_FNIC 0x0045
45
46/* Timer to poll notification area for events. Used for MSI interrupts */
47#define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
48
49static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
50static struct kmem_cache *fnic_io_req_cache;
51LIST_HEAD(fnic_list);
52DEFINE_SPINLOCK(fnic_list_lock);
53
54/* Supported devices by fnic module */
55static struct pci_device_id fnic_id_table[] = {
56 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
57 { 0, }
58};
59
60MODULE_DESCRIPTION(DRV_DESCRIPTION);
61MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
62 "Joseph R. Eykholt <jeykholt@cisco.com>");
63MODULE_LICENSE("GPL v2");
64MODULE_VERSION(DRV_VERSION);
65MODULE_DEVICE_TABLE(pci, fnic_id_table);
66
67unsigned int fnic_log_level;
68module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
69MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
70
Hiral Patel4d7007b2013-02-12 17:01:02 -080071unsigned int fnic_trace_max_pages = 16;
72module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
73MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
74 "for fnic trace buffer");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070075
76static struct libfc_function_template fnic_transport_template = {
77 .frame_send = fnic_send,
Joe Eykholt78112e52009-11-03 11:49:22 -080078 .lport_set_port_id = fnic_set_port_id,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070079 .fcp_abort_io = fnic_empty_scsi_cleanup,
80 .fcp_cleanup = fnic_empty_scsi_cleanup,
81 .exch_mgr_reset = fnic_exch_mgr_reset
82};
83
84static int fnic_slave_alloc(struct scsi_device *sdev)
85{
86 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070087
88 sdev->tagged_supported = 1;
89
90 if (!rport || fc_remote_port_chkready(rport))
91 return -ENXIO;
92
93 scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070094 return 0;
95}
96
97static struct scsi_host_template fnic_host_template = {
98 .module = THIS_MODULE,
99 .name = DRV_NAME,
100 .queuecommand = fnic_queuecommand,
101 .eh_abort_handler = fnic_abort_cmd,
102 .eh_device_reset_handler = fnic_device_reset,
103 .eh_host_reset_handler = fnic_host_reset,
104 .slave_alloc = fnic_slave_alloc,
105 .change_queue_depth = fc_change_queue_depth,
106 .change_queue_type = fc_change_queue_type,
107 .this_id = -1,
108 .cmd_per_lun = 3,
109 .can_queue = FNIC_MAX_IO_REQ,
110 .use_clustering = ENABLE_CLUSTERING,
111 .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
112 .max_sectors = 0xffff,
113 .shost_attrs = fnic_attrs,
114};
115
Mike Christie8196a932010-08-06 03:02:40 -0500116static void
Mike Christie485868202010-09-15 16:52:28 -0500117fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
Mike Christie8196a932010-08-06 03:02:40 -0500118{
Mike Christie485868202010-09-15 16:52:28 -0500119 if (timeout)
120 rport->dev_loss_tmo = timeout;
121 else
122 rport->dev_loss_tmo = 1;
Mike Christie8196a932010-08-06 03:02:40 -0500123}
124
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700125static void fnic_get_host_speed(struct Scsi_Host *shost);
126static struct scsi_transport_template *fnic_fc_transport;
127static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
128
129static struct fc_function_template fnic_fc_functions = {
130
131 .show_host_node_name = 1,
132 .show_host_port_name = 1,
133 .show_host_supported_classes = 1,
134 .show_host_supported_fc4s = 1,
135 .show_host_active_fc4s = 1,
136 .show_host_maxframe_size = 1,
137 .show_host_port_id = 1,
138 .show_host_supported_speeds = 1,
139 .get_host_speed = fnic_get_host_speed,
140 .show_host_speed = 1,
141 .show_host_port_type = 1,
142 .get_host_port_state = fc_get_host_port_state,
143 .show_host_port_state = 1,
144 .show_host_symbolic_name = 1,
145 .show_rport_maxframe_size = 1,
146 .show_rport_supported_classes = 1,
147 .show_host_fabric_name = 1,
148 .show_starget_node_name = 1,
149 .show_starget_port_name = 1,
150 .show_starget_port_id = 1,
151 .show_rport_dev_loss_tmo = 1,
Mike Christie485868202010-09-15 16:52:28 -0500152 .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700153 .issue_fc_host_lip = fnic_reset,
154 .get_fc_host_stats = fnic_get_stats,
155 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
156 .terminate_rport_io = fnic_terminate_rport_io,
Joe Eykholt76d87372009-11-03 11:49:32 -0800157 .bsg_request = fc_lport_bsg_request,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700158};
159
160static void fnic_get_host_speed(struct Scsi_Host *shost)
161{
162 struct fc_lport *lp = shost_priv(shost);
163 struct fnic *fnic = lport_priv(lp);
164 u32 port_speed = vnic_dev_port_speed(fnic->vdev);
165
166 /* Add in other values as they get defined in fw */
167 switch (port_speed) {
168 case 10000:
169 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
170 break;
171 default:
172 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
173 break;
174 }
175}
176
177static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
178{
179 int ret;
180 struct fc_lport *lp = shost_priv(host);
181 struct fnic *fnic = lport_priv(lp);
182 struct fc_host_statistics *stats = &lp->host_stats;
183 struct vnic_stats *vs;
184 unsigned long flags;
185
186 if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
187 return stats;
188 fnic->stats_time = jiffies;
189
190 spin_lock_irqsave(&fnic->fnic_lock, flags);
191 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
192 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
193
194 if (ret) {
195 FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
196 "fnic: Get vnic stats failed"
197 " 0x%x", ret);
198 return stats;
199 }
200 vs = fnic->stats;
201 stats->tx_frames = vs->tx.tx_unicast_frames_ok;
202 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
203 stats->rx_frames = vs->rx.rx_unicast_frames_ok;
204 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
205 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
206 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
207 stats->invalid_crc_count = vs->rx.rx_crc_errors;
208 stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
209 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
210 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
211
212 return stats;
213}
214
215void fnic_log_q_error(struct fnic *fnic)
216{
217 unsigned int i;
218 u32 error_status;
219
220 for (i = 0; i < fnic->raw_wq_count; i++) {
221 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
222 if (error_status)
223 shost_printk(KERN_ERR, fnic->lport->host,
224 "WQ[%d] error_status"
225 " %d\n", i, error_status);
226 }
227
228 for (i = 0; i < fnic->rq_count; i++) {
229 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
230 if (error_status)
231 shost_printk(KERN_ERR, fnic->lport->host,
232 "RQ[%d] error_status"
233 " %d\n", i, error_status);
234 }
235
236 for (i = 0; i < fnic->wq_copy_count; i++) {
237 error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
238 if (error_status)
239 shost_printk(KERN_ERR, fnic->lport->host,
240 "CWQ[%d] error_status"
241 " %d\n", i, error_status);
242 }
243}
244
245void fnic_handle_link_event(struct fnic *fnic)
246{
247 unsigned long flags;
248
249 spin_lock_irqsave(&fnic->fnic_lock, flags);
250 if (fnic->stop_rx_link_events) {
251 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
252 return;
253 }
254 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
255
256 queue_work(fnic_event_queue, &fnic->link_work);
257
258}
259
260static int fnic_notify_set(struct fnic *fnic)
261{
262 int err;
263
264 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
265 case VNIC_DEV_INTR_MODE_INTX:
266 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
267 break;
268 case VNIC_DEV_INTR_MODE_MSI:
269 err = vnic_dev_notify_set(fnic->vdev, -1);
270 break;
271 case VNIC_DEV_INTR_MODE_MSIX:
272 err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
273 break;
274 default:
275 shost_printk(KERN_ERR, fnic->lport->host,
276 "Interrupt mode should be set up"
277 " before devcmd notify set %d\n",
278 vnic_dev_get_intr_mode(fnic->vdev));
279 err = -1;
280 break;
281 }
282
283 return err;
284}
285
286static void fnic_notify_timer(unsigned long data)
287{
288 struct fnic *fnic = (struct fnic *)data;
289
290 fnic_handle_link_event(fnic);
291 mod_timer(&fnic->notify_timer,
292 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
293}
294
295static void fnic_notify_timer_start(struct fnic *fnic)
296{
297 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
298 case VNIC_DEV_INTR_MODE_MSI:
299 /*
300 * Schedule first timeout immediately. The driver is
301 * initiatialized and ready to look for link up notification
302 */
303 mod_timer(&fnic->notify_timer, jiffies);
304 break;
305 default:
306 /* Using intr for notification for INTx/MSI-X */
307 break;
308 };
309}
310
311static int fnic_dev_wait(struct vnic_dev *vdev,
312 int (*start)(struct vnic_dev *, int),
313 int (*finished)(struct vnic_dev *, int *),
314 int arg)
315{
316 unsigned long time;
317 int done;
318 int err;
319
320 err = start(vdev, arg);
321 if (err)
322 return err;
323
324 /* Wait for func to complete...2 seconds max */
325 time = jiffies + (HZ * 2);
326 do {
327 err = finished(vdev, &done);
328 if (err)
329 return err;
330 if (done)
331 return 0;
332 schedule_timeout_uninterruptible(HZ / 10);
333 } while (time_after(time, jiffies));
334
335 return -ETIMEDOUT;
336}
337
338static int fnic_cleanup(struct fnic *fnic)
339{
340 unsigned int i;
341 int err;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700342
343 vnic_dev_disable(fnic->vdev);
344 for (i = 0; i < fnic->intr_count; i++)
345 vnic_intr_mask(&fnic->intr[i]);
346
347 for (i = 0; i < fnic->rq_count; i++) {
348 err = vnic_rq_disable(&fnic->rq[i]);
349 if (err)
350 return err;
351 }
352 for (i = 0; i < fnic->raw_wq_count; i++) {
353 err = vnic_wq_disable(&fnic->wq[i]);
354 if (err)
355 return err;
356 }
357 for (i = 0; i < fnic->wq_copy_count; i++) {
358 err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
359 if (err)
360 return err;
361 }
362
363 /* Clean up completed IOs and FCS frames */
364 fnic_wq_copy_cmpl_handler(fnic, -1);
365 fnic_wq_cmpl_handler(fnic, -1);
366 fnic_rq_cmpl_handler(fnic, -1);
367
368 /* Clean up the IOs and FCS frames that have not completed */
369 for (i = 0; i < fnic->raw_wq_count; i++)
370 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
371 for (i = 0; i < fnic->rq_count; i++)
372 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
373 for (i = 0; i < fnic->wq_copy_count; i++)
374 vnic_wq_copy_clean(&fnic->wq_copy[i],
375 fnic_wq_copy_cleanup_handler);
376
377 for (i = 0; i < fnic->cq_count; i++)
378 vnic_cq_clean(&fnic->cq[i]);
379 for (i = 0; i < fnic->intr_count; i++)
380 vnic_intr_clean(&fnic->intr[i]);
381
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700382 mempool_destroy(fnic->io_req_pool);
383 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
384 mempool_destroy(fnic->io_sgl_pool[i]);
385
386 return 0;
387}
388
389static void fnic_iounmap(struct fnic *fnic)
390{
391 if (fnic->bar0.vaddr)
392 iounmap(fnic->bar0.vaddr);
393}
394
Joe Eykholt78112e52009-11-03 11:49:22 -0800395/**
396 * fnic_get_mac() - get assigned data MAC address for FIP code.
397 * @lport: local port.
398 */
399static u8 *fnic_get_mac(struct fc_lport *lport)
400{
401 struct fnic *fnic = lport_priv(lport);
402
403 return fnic->data_src_addr;
404}
405
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800406static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700407{
408 struct Scsi_Host *host;
409 struct fc_lport *lp;
410 struct fnic *fnic;
411 mempool_t *pool;
412 int err;
413 int i;
414 unsigned long flags;
415
416 /*
417 * Allocate SCSI Host and set up association between host,
418 * local port, and fnic
419 */
Chris Leech86221962009-11-03 11:46:08 -0800420 lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
421 if (!lp) {
422 printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700423 err = -ENOMEM;
424 goto err_out;
425 }
Chris Leech86221962009-11-03 11:46:08 -0800426 host = lp->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700427 fnic = lport_priv(lp);
428 fnic->lport = lp;
Joe Eykholt78112e52009-11-03 11:49:22 -0800429 fnic->ctlr.lp = lp;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700430
431 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
432 host->host_no);
433
434 host->transportt = fnic_fc_transport;
435
436 err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
437 if (err) {
438 shost_printk(KERN_ERR, fnic->lport->host,
439 "Unable to alloc shared tag map\n");
440 goto err_out_free_hba;
441 }
442
443 /* Setup PCI resources */
444 pci_set_drvdata(pdev, fnic);
445
446 fnic->pdev = pdev;
447
448 err = pci_enable_device(pdev);
449 if (err) {
450 shost_printk(KERN_ERR, fnic->lport->host,
451 "Cannot enable PCI device, aborting.\n");
452 goto err_out_free_hba;
453 }
454
455 err = pci_request_regions(pdev, DRV_NAME);
456 if (err) {
457 shost_printk(KERN_ERR, fnic->lport->host,
458 "Cannot enable PCI resources, aborting\n");
459 goto err_out_disable_device;
460 }
461
462 pci_set_master(pdev);
463
464 /* Query PCI controller on system for DMA addressing
465 * limitation for the device. Try 40-bit first, and
466 * fail to 32-bit.
467 */
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700468 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700469 if (err) {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700470 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700471 if (err) {
472 shost_printk(KERN_ERR, fnic->lport->host,
473 "No usable DMA configuration "
474 "aborting\n");
475 goto err_out_release_regions;
476 }
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700477 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700478 if (err) {
479 shost_printk(KERN_ERR, fnic->lport->host,
480 "Unable to obtain 32-bit DMA "
481 "for consistent allocations, aborting.\n");
482 goto err_out_release_regions;
483 }
484 } else {
Abhijeet Joglekare3f47cc2009-06-24 07:42:25 -0700485 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700486 if (err) {
487 shost_printk(KERN_ERR, fnic->lport->host,
488 "Unable to obtain 40-bit DMA "
489 "for consistent allocations, aborting.\n");
490 goto err_out_release_regions;
491 }
492 }
493
494 /* Map vNIC resources from BAR0 */
495 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
496 shost_printk(KERN_ERR, fnic->lport->host,
497 "BAR0 not memory-map'able, aborting.\n");
498 err = -ENODEV;
499 goto err_out_release_regions;
500 }
501
502 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
503 fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
504 fnic->bar0.len = pci_resource_len(pdev, 0);
505
506 if (!fnic->bar0.vaddr) {
507 shost_printk(KERN_ERR, fnic->lport->host,
508 "Cannot memory-map BAR0 res hdr, "
509 "aborting.\n");
510 err = -ENODEV;
511 goto err_out_release_regions;
512 }
513
514 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
515 if (!fnic->vdev) {
516 shost_printk(KERN_ERR, fnic->lport->host,
517 "vNIC registration failed, "
518 "aborting.\n");
519 err = -ENODEV;
520 goto err_out_iounmap;
521 }
522
523 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
524 vnic_dev_open_done, 0);
525 if (err) {
526 shost_printk(KERN_ERR, fnic->lport->host,
527 "vNIC dev open failed, aborting.\n");
528 goto err_out_vnic_unregister;
529 }
530
531 err = vnic_dev_init(fnic->vdev, 0);
532 if (err) {
533 shost_printk(KERN_ERR, fnic->lport->host,
534 "vNIC dev init failed, aborting.\n");
535 goto err_out_dev_close;
536 }
537
Joe Eykholt78112e52009-11-03 11:49:22 -0800538 err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700539 if (err) {
540 shost_printk(KERN_ERR, fnic->lport->host,
541 "vNIC get MAC addr failed \n");
542 goto err_out_dev_close;
543 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800544 /* set data_src for point-to-point mode and to keep it non-zero */
545 memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700546
547 /* Get vNIC configuration */
548 err = fnic_get_vnic_config(fnic);
549 if (err) {
550 shost_printk(KERN_ERR, fnic->lport->host,
551 "Get vNIC configuration failed, "
552 "aborting.\n");
553 goto err_out_dev_close;
554 }
555 host->max_lun = fnic->config.luns_per_tgt;
556 host->max_id = FNIC_MAX_FCP_TARGET;
Vasu Devda87bfa2010-04-09 14:22:59 -0700557 host->max_cmd_len = FCOE_MAX_CMD_LEN;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700558
559 fnic_get_res_counts(fnic);
560
561 err = fnic_set_intr_mode(fnic);
562 if (err) {
563 shost_printk(KERN_ERR, fnic->lport->host,
564 "Failed to set intr mode, "
565 "aborting.\n");
566 goto err_out_dev_close;
567 }
568
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700569 err = fnic_alloc_vnic_resources(fnic);
570 if (err) {
571 shost_printk(KERN_ERR, fnic->lport->host,
572 "Failed to alloc vNIC resources, "
573 "aborting.\n");
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800574 goto err_out_clear_intr;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700575 }
576
577
578 /* initialize all fnic locks */
579 spin_lock_init(&fnic->fnic_lock);
580
581 for (i = 0; i < FNIC_WQ_MAX; i++)
582 spin_lock_init(&fnic->wq_lock[i]);
583
584 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
585 spin_lock_init(&fnic->wq_copy_lock[i]);
586 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
587 fnic->fw_ack_recd[i] = 0;
588 fnic->fw_ack_index[i] = -1;
589 }
590
591 for (i = 0; i < FNIC_IO_LOCKS; i++)
592 spin_lock_init(&fnic->io_req_lock[i]);
593
594 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
595 if (!fnic->io_req_pool)
596 goto err_out_free_resources;
597
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700598 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700599 if (!pool)
600 goto err_out_free_ioreq_pool;
601 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
602
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700603 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700604 if (!pool)
605 goto err_out_free_dflt_pool;
606 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
607
608 /* setup vlan config, hw inserts vlan header */
609 fnic->vlan_hw_insert = 1;
610 fnic->vlan_id = 0;
611
Joe Eykholt78112e52009-11-03 11:49:22 -0800612 /* Initialize the FIP fcoe_ctrl struct */
613 fnic->ctlr.send = fnic_eth_send;
614 fnic->ctlr.update_mac = fnic_update_mac;
615 fnic->ctlr.get_src_addr = fnic_get_mac;
Joe Eykholt78112e52009-11-03 11:49:22 -0800616 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
617 shost_printk(KERN_INFO, fnic->lport->host,
618 "firmware supports FIP\n");
Venkata Siva Vijayendra Bhamidipatiaaa5e562010-02-16 12:15:44 -0800619 /* enable directed and multicast */
620 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
Joe Eykholt78112e52009-11-03 11:49:22 -0800621 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
622 vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700623 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
Joe Eykholt78112e52009-11-03 11:49:22 -0800624 } else {
625 shost_printk(KERN_INFO, fnic->lport->host,
626 "firmware uses non-FIP mode\n");
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700627 fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
Joe Eykholt78112e52009-11-03 11:49:22 -0800628 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700629 fnic->state = FNIC_IN_FC_MODE;
630
Hiral Patel03298552013-02-12 17:00:58 -0800631 atomic_set(&fnic->in_flight, 0);
632 fnic->state_flags = FNIC_FLAGS_NONE;
633
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700634 /* Enable hardware stripping of vlan header on ingress */
635 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
636
637 /* Setup notification buffer area */
638 err = fnic_notify_set(fnic);
639 if (err) {
640 shost_printk(KERN_ERR, fnic->lport->host,
641 "Failed to alloc notify buffer, aborting.\n");
642 goto err_out_free_max_pool;
643 }
644
645 /* Setup notify timer when using MSI interrupts */
646 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
647 setup_timer(&fnic->notify_timer,
648 fnic_notify_timer, (unsigned long)fnic);
649
650 /* allocate RQ buffers and post them to RQ*/
651 for (i = 0; i < fnic->rq_count; i++) {
652 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
653 if (err) {
654 shost_printk(KERN_ERR, fnic->lport->host,
655 "fnic_alloc_rq_frame can't alloc "
656 "frame\n");
657 goto err_out_free_rq_buf;
658 }
659 }
660
661 /*
662 * Initialization done with PCI system, hardware, firmware.
663 * Add host to SCSI
664 */
665 err = scsi_add_host(lp->host, &pdev->dev);
666 if (err) {
667 shost_printk(KERN_ERR, fnic->lport->host,
668 "fnic: scsi_add_host failed...exiting\n");
669 goto err_out_free_rq_buf;
670 }
671
672 /* Start local port initiatialization */
673
674 lp->link_up = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700675
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700676 lp->max_retry_count = fnic->config.flogi_retries;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700677 lp->max_rport_retry_count = fnic->config.plogi_retries;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700678 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
679 FCP_SPPF_CONF_COMPL);
680 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
681 lp->service_params |= FCP_SPPF_RETRY;
682
683 lp->boot_time = jiffies;
684 lp->e_d_tov = fnic->config.ed_tov;
685 lp->r_a_tov = fnic->config.ra_tov;
686 lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
687 fc_set_wwnn(lp, fnic->config.node_wwn);
688 fc_set_wwpn(lp, fnic->config.port_wwn);
689
Joe Eykholte10f8c62010-07-20 15:20:30 -0700690 fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700691
Vasu Dev52ff8782009-07-29 17:05:10 -0700692 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
693 FCPIO_HOST_EXCH_RANGE_END, NULL)) {
694 err = -ENOMEM;
695 goto err_out_remove_scsi_host;
696 }
697
Venkata Siva Vijayendra Bhamidipatic693a712010-02-16 12:15:50 -0800698 fc_lport_init_stats(lp);
699
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700700 fc_lport_config(lp);
701
702 if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
703 sizeof(struct fc_frame_header))) {
704 err = -EINVAL;
705 goto err_out_free_exch_mgr;
706 }
707 fc_host_maxframe_size(lp->host) = lp->mfs;
Mike Christie485868202010-09-15 16:52:28 -0500708 fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700709
710 sprintf(fc_host_symbolic_name(lp->host),
711 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
712
713 spin_lock_irqsave(&fnic_list_lock, flags);
714 list_add_tail(&fnic->list, &fnic_list);
715 spin_unlock_irqrestore(&fnic_list_lock, flags);
716
717 INIT_WORK(&fnic->link_work, fnic_handle_link);
718 INIT_WORK(&fnic->frame_work, fnic_handle_frame);
719 skb_queue_head_init(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800720 skb_queue_head_init(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700721
722 /* Enable all queues */
723 for (i = 0; i < fnic->raw_wq_count; i++)
724 vnic_wq_enable(&fnic->wq[i]);
725 for (i = 0; i < fnic->rq_count; i++)
726 vnic_rq_enable(&fnic->rq[i]);
727 for (i = 0; i < fnic->wq_copy_count; i++)
728 vnic_wq_copy_enable(&fnic->wq_copy[i]);
729
730 fc_fabric_login(lp);
731
732 vnic_dev_enable(fnic->vdev);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800733
734 err = fnic_request_intr(fnic);
735 if (err) {
736 shost_printk(KERN_ERR, fnic->lport->host,
737 "Unable to request irq.\n");
738 goto err_out_free_exch_mgr;
739 }
740
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700741 for (i = 0; i < fnic->intr_count; i++)
742 vnic_intr_unmask(&fnic->intr[i]);
743
744 fnic_notify_timer_start(fnic);
745
746 return 0;
747
748err_out_free_exch_mgr:
Vasu Dev52ff8782009-07-29 17:05:10 -0700749 fc_exch_mgr_free(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700750err_out_remove_scsi_host:
Joe Eykholt78112e52009-11-03 11:49:22 -0800751 fc_remove_host(lp->host);
752 scsi_remove_host(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700753err_out_free_rq_buf:
754 for (i = 0; i < fnic->rq_count; i++)
755 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
756 vnic_dev_notify_unset(fnic->vdev);
757err_out_free_max_pool:
758 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
759err_out_free_dflt_pool:
760 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
761err_out_free_ioreq_pool:
762 mempool_destroy(fnic->io_req_pool);
763err_out_free_resources:
764 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700765err_out_clear_intr:
766 fnic_clear_intr_mode(fnic);
767err_out_dev_close:
768 vnic_dev_close(fnic->vdev);
769err_out_vnic_unregister:
770 vnic_dev_unregister(fnic->vdev);
771err_out_iounmap:
772 fnic_iounmap(fnic);
773err_out_release_regions:
774 pci_release_regions(pdev);
775err_out_disable_device:
776 pci_disable_device(pdev);
777err_out_free_hba:
778 scsi_host_put(lp->host);
779err_out:
780 return err;
781}
782
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800783static void fnic_remove(struct pci_dev *pdev)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700784{
785 struct fnic *fnic = pci_get_drvdata(pdev);
Joe Eykholt78112e52009-11-03 11:49:22 -0800786 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700787 unsigned long flags;
788
789 /*
790 * Mark state so that the workqueue thread stops forwarding
791 * received frames and link events to the local port. ISR and
792 * other threads that can queue work items will also stop
793 * creating work items on the fnic workqueue
794 */
795 spin_lock_irqsave(&fnic->fnic_lock, flags);
796 fnic->stop_rx_link_events = 1;
797 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
798
799 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
800 del_timer_sync(&fnic->notify_timer);
801
802 /*
803 * Flush the fnic event queue. After this call, there should
804 * be no event queued for this fnic device in the workqueue
805 */
806 flush_workqueue(fnic_event_queue);
807 skb_queue_purge(&fnic->frame_queue);
Joe Eykholt78112e52009-11-03 11:49:22 -0800808 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700809
810 /*
811 * Log off the fabric. This stops all remote ports, dns port,
812 * logs off the fabric. This flushes all rport, disc, lport work
813 * before returning
814 */
815 fc_fabric_logoff(fnic->lport);
816
817 spin_lock_irqsave(&fnic->fnic_lock, flags);
818 fnic->in_remove = 1;
819 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
820
Joe Eykholt78112e52009-11-03 11:49:22 -0800821 fcoe_ctlr_destroy(&fnic->ctlr);
822 fc_lport_destroy(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700823
824 /*
825 * This stops the fnic device, masks all interrupts. Completed
826 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
827 * cleaned up
828 */
829 fnic_cleanup(fnic);
830
831 BUG_ON(!skb_queue_empty(&fnic->frame_queue));
Joe Eykholt78112e52009-11-03 11:49:22 -0800832 BUG_ON(!skb_queue_empty(&fnic->tx_queue));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700833
834 spin_lock_irqsave(&fnic_list_lock, flags);
835 list_del(&fnic->list);
836 spin_unlock_irqrestore(&fnic_list_lock, flags);
837
838 fc_remove_host(fnic->lport->host);
839 scsi_remove_host(fnic->lport->host);
Vasu Dev52ff8782009-07-29 17:05:10 -0700840 fc_exch_mgr_free(fnic->lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700841 vnic_dev_notify_unset(fnic->vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700842 fnic_free_intr(fnic);
Abhijeet Joglekar2e76f762009-11-03 11:45:37 -0800843 fnic_free_vnic_resources(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700844 fnic_clear_intr_mode(fnic);
845 vnic_dev_close(fnic->vdev);
846 vnic_dev_unregister(fnic->vdev);
847 fnic_iounmap(fnic);
848 pci_release_regions(pdev);
849 pci_disable_device(pdev);
850 pci_set_drvdata(pdev, NULL);
Joe Eykholt78112e52009-11-03 11:49:22 -0800851 scsi_host_put(lp->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700852}
853
854static struct pci_driver fnic_driver = {
855 .name = DRV_NAME,
856 .id_table = fnic_id_table,
857 .probe = fnic_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800858 .remove = fnic_remove,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700859};
860
861static int __init fnic_init_module(void)
862{
863 size_t len;
864 int err = 0;
865
866 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
867
Hiral Patel4d7007b2013-02-12 17:01:02 -0800868 /* Allocate memory for trace buffer */
869 err = fnic_trace_buf_init();
870 if (err < 0) {
871 printk(KERN_ERR PFX "Trace buffer initialization Failed "
872 "Fnic Tracing utility is disabled\n");
873 fnic_trace_free();
874 }
875
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700876 /* Create a cache for allocation of default size sgls */
877 len = sizeof(struct fnic_dflt_sgl_list);
878 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
879 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700880 SLAB_HWCACHE_ALIGN,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700881 NULL);
882 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
883 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
884 err = -ENOMEM;
885 goto err_create_fnic_sgl_slab_dflt;
886 }
887
888 /* Create a cache for allocation of max size sgls*/
889 len = sizeof(struct fnic_sgl_list);
890 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
891 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700892 SLAB_HWCACHE_ALIGN,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700893 NULL);
894 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
895 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
896 err = -ENOMEM;
897 goto err_create_fnic_sgl_slab_max;
898 }
899
900 /* Create a cache of io_req structs for use via mempool */
901 fnic_io_req_cache = kmem_cache_create("fnic_io_req",
902 sizeof(struct fnic_io_req),
903 0, SLAB_HWCACHE_ALIGN, NULL);
904 if (!fnic_io_req_cache) {
905 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
906 err = -ENOMEM;
907 goto err_create_fnic_ioreq_slab;
908 }
909
910 fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
911 if (!fnic_event_queue) {
912 printk(KERN_ERR PFX "fnic work queue create failed\n");
913 err = -ENOMEM;
914 goto err_create_fnic_workq;
915 }
916
917 spin_lock_init(&fnic_list_lock);
918 INIT_LIST_HEAD(&fnic_list);
919
920 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
921 if (!fnic_fc_transport) {
922 printk(KERN_ERR PFX "fc_attach_transport error\n");
923 err = -ENOMEM;
924 goto err_fc_transport;
925 }
926
927 /* register the driver with PCI system */
928 err = pci_register_driver(&fnic_driver);
929 if (err < 0) {
930 printk(KERN_ERR PFX "pci register error\n");
931 goto err_pci_register;
932 }
933 return err;
934
935err_pci_register:
936 fc_release_transport(fnic_fc_transport);
937err_fc_transport:
938 destroy_workqueue(fnic_event_queue);
939err_create_fnic_workq:
940 kmem_cache_destroy(fnic_io_req_cache);
941err_create_fnic_ioreq_slab:
942 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
943err_create_fnic_sgl_slab_max:
944 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
945err_create_fnic_sgl_slab_dflt:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800946 fnic_trace_free();
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700947 return err;
948}
949
950static void __exit fnic_cleanup_module(void)
951{
952 pci_unregister_driver(&fnic_driver);
953 destroy_workqueue(fnic_event_queue);
954 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
955 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
956 kmem_cache_destroy(fnic_io_req_cache);
957 fc_release_transport(fnic_fc_transport);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800958 fnic_trace_free();
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700959}
960
961module_init(fnic_init_module);
962module_exit(fnic_cleanup_module);
963