1 /*
2 * Copyright 2014 Cisco Systems, Inc. All rights reserved.
3 *
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15 * SOFTWARE.
16 */
17
18 #include <linux/errno.h>
19 #include <linux/mempool.h>
20
21 #include <scsi/scsi_tcq.h>
22
23 #include "snic_disc.h"
24 #include "snic.h"
25 #include "snic_io.h"
26
27
28 /* snic target types */
29 static const char * const snic_tgt_type_str[] = {
30 [SNIC_TGT_DAS] = "DAS",
31 [SNIC_TGT_SAN] = "SAN",
32 };
33
34 static inline const char *
snic_tgt_type_to_str(int typ)35 snic_tgt_type_to_str(int typ)
36 {
37 return ((typ > SNIC_TGT_NONE && typ <= SNIC_TGT_SAN) ?
38 snic_tgt_type_str[typ] : "Unknown");
39 }
40
41 static const char * const snic_tgt_state_str[] = {
42 [SNIC_TGT_STAT_INIT] = "INIT",
43 [SNIC_TGT_STAT_ONLINE] = "ONLINE",
44 [SNIC_TGT_STAT_OFFLINE] = "OFFLINE",
45 [SNIC_TGT_STAT_DEL] = "DELETION IN PROGRESS",
46 };
47
48 const char *
snic_tgt_state_to_str(int state)49 snic_tgt_state_to_str(int state)
50 {
51 return ((state >= SNIC_TGT_STAT_INIT && state <= SNIC_TGT_STAT_DEL) ?
52 snic_tgt_state_str[state] : "UNKNOWN");
53 }
54
55 /*
56 * Initiate report_tgt req desc
57 */
58 static void
snic_report_tgt_init(struct snic_host_req * req,u32 hid,u8 * buf,u32 len,dma_addr_t rsp_buf_pa,ulong ctx)59 snic_report_tgt_init(struct snic_host_req *req, u32 hid, u8 *buf, u32 len,
60 dma_addr_t rsp_buf_pa, ulong ctx)
61 {
62 struct snic_sg_desc *sgd = NULL;
63
64
65 snic_io_hdr_enc(&req->hdr, SNIC_REQ_REPORT_TGTS, 0, SCSI_NO_TAG, hid,
66 1, ctx);
67
68 req->u.rpt_tgts.sg_cnt = cpu_to_le16(1);
69 sgd = req_to_sgl(req);
70 sgd[0].addr = cpu_to_le64(rsp_buf_pa);
71 sgd[0].len = cpu_to_le32(len);
72 sgd[0]._resvd = 0;
73 req->u.rpt_tgts.sg_addr = cpu_to_le64((ulong)sgd);
74 }
75
76 /*
77 * snic_queue_report_tgt_req: Queues report target request.
78 */
79 static int
snic_queue_report_tgt_req(struct snic * snic)80 snic_queue_report_tgt_req(struct snic *snic)
81 {
82 struct snic_req_info *rqi = NULL;
83 u32 ntgts, buf_len = 0;
84 u8 *buf = NULL;
85 dma_addr_t pa = 0;
86 int ret = 0;
87
88 rqi = snic_req_init(snic, 1);
89 if (!rqi) {
90 ret = -ENOMEM;
91 goto error;
92 }
93
94 if (snic->fwinfo.max_tgts)
95 ntgts = min_t(u32, snic->fwinfo.max_tgts, snic->shost->max_id);
96 else
97 ntgts = snic->shost->max_id;
98
99 /* Allocate Response Buffer */
100 SNIC_BUG_ON(ntgts == 0);
101 buf_len = ntgts * sizeof(struct snic_tgt_id) + SNIC_SG_DESC_ALIGN;
102
103 buf = kzalloc(buf_len, GFP_KERNEL|GFP_DMA);
104 if (!buf) {
105 snic_req_free(snic, rqi);
106 SNIC_HOST_ERR(snic->shost, "Resp Buf Alloc Failed.\n");
107
108 ret = -ENOMEM;
109 goto error;
110 }
111
112 SNIC_BUG_ON((((unsigned long)buf) % SNIC_SG_DESC_ALIGN) != 0);
113
114 pa = dma_map_single(&snic->pdev->dev, buf, buf_len, DMA_FROM_DEVICE);
115 if (dma_mapping_error(&snic->pdev->dev, pa)) {
116 SNIC_HOST_ERR(snic->shost,
117 "Rpt-tgt rspbuf %p: PCI DMA Mapping Failed\n",
118 buf);
119 kfree(buf);
120 snic_req_free(snic, rqi);
121 ret = -EINVAL;
122
123 goto error;
124 }
125
126
127 SNIC_BUG_ON(pa == 0);
128 rqi->sge_va = (ulong) buf;
129
130 snic_report_tgt_init(rqi->req,
131 snic->config.hid,
132 buf,
133 buf_len,
134 pa,
135 (ulong)rqi);
136
137 snic_handle_untagged_req(snic, rqi);
138
139 ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
140 if (ret) {
141 dma_unmap_single(&snic->pdev->dev, pa, buf_len,
142 DMA_FROM_DEVICE);
143 kfree(buf);
144 rqi->sge_va = 0;
145 snic_release_untagged_req(snic, rqi);
146 SNIC_HOST_ERR(snic->shost, "Queuing Report Tgts Failed.\n");
147
148 goto error;
149 }
150
151 SNIC_DISC_DBG(snic->shost, "Report Targets Issued.\n");
152
153 return ret;
154
155 error:
156 SNIC_HOST_ERR(snic->shost,
157 "Queuing Report Targets Failed, err = %d\n",
158 ret);
159 return ret;
160 } /* end of snic_queue_report_tgt_req */
161
162 /* call into SML */
163 static void
snic_scsi_scan_tgt(struct work_struct * work)164 snic_scsi_scan_tgt(struct work_struct *work)
165 {
166 struct snic_tgt *tgt = container_of(work, struct snic_tgt, scan_work);
167 struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
168 unsigned long flags;
169
170 SNIC_HOST_INFO(shost, "Scanning Target id 0x%x\n", tgt->id);
171 scsi_scan_target(&tgt->dev,
172 tgt->channel,
173 tgt->scsi_tgt_id,
174 SCAN_WILD_CARD,
175 SCSI_SCAN_RESCAN);
176
177 spin_lock_irqsave(shost->host_lock, flags);
178 tgt->flags &= ~SNIC_TGT_SCAN_PENDING;
179 spin_unlock_irqrestore(shost->host_lock, flags);
180 } /* end of snic_scsi_scan_tgt */
181
182 /*
183 * snic_tgt_lookup :
184 */
185 static struct snic_tgt *
snic_tgt_lookup(struct snic * snic,struct snic_tgt_id * tgtid)186 snic_tgt_lookup(struct snic *snic, struct snic_tgt_id *tgtid)
187 {
188 struct list_head *cur, *nxt;
189 struct snic_tgt *tgt = NULL;
190
191 list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
192 tgt = list_entry(cur, struct snic_tgt, list);
193 if (tgt->id == le32_to_cpu(tgtid->tgt_id))
194 return tgt;
195 tgt = NULL;
196 }
197
198 return tgt;
199 } /* end of snic_tgt_lookup */
200
201 /*
202 * snic_tgt_dev_release : Called on dropping last ref for snic_tgt object
203 */
204 void
snic_tgt_dev_release(struct device * dev)205 snic_tgt_dev_release(struct device *dev)
206 {
207 struct snic_tgt *tgt = dev_to_tgt(dev);
208
209 SNIC_HOST_INFO(snic_tgt_to_shost(tgt),
210 "Target Device ID %d (%s) Permanently Deleted.\n",
211 tgt->id,
212 dev_name(dev));
213
214 SNIC_BUG_ON(!list_empty(&tgt->list));
215 kfree(tgt);
216 }
217
218 /*
219 * snic_tgt_del : work function to delete snic_tgt
220 */
221 static void
snic_tgt_del(struct work_struct * work)222 snic_tgt_del(struct work_struct *work)
223 {
224 struct snic_tgt *tgt = container_of(work, struct snic_tgt, del_work);
225 struct Scsi_Host *shost = snic_tgt_to_shost(tgt);
226
227 if (tgt->flags & SNIC_TGT_SCAN_PENDING)
228 scsi_flush_work(shost);
229
230 /* Block IOs on child devices, stops new IOs */
231 scsi_target_block(&tgt->dev);
232
233 /* Cleanup IOs */
234 snic_tgt_scsi_abort_io(tgt);
235
236 /* Unblock IOs now, to flush if there are any. */
237 scsi_target_unblock(&tgt->dev, SDEV_TRANSPORT_OFFLINE);
238
239 /* Delete SCSI Target and sdevs */
240 scsi_remove_target(&tgt->dev); /* ?? */
241 device_del(&tgt->dev);
242 put_device(&tgt->dev);
243 } /* end of snic_tgt_del */
244
245 /* snic_tgt_create: checks for existence of snic_tgt, if it doesn't
246 * it creates one.
247 */
248 static struct snic_tgt *
snic_tgt_create(struct snic * snic,struct snic_tgt_id * tgtid)249 snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)
250 {
251 struct snic_tgt *tgt = NULL;
252 unsigned long flags;
253 int ret;
254
255 tgt = snic_tgt_lookup(snic, tgtid);
256 if (tgt) {
257 /* update the information if required */
258 return tgt;
259 }
260
261 tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
262 if (!tgt) {
263 SNIC_HOST_ERR(snic->shost, "Failure to allocate snic_tgt.\n");
264 ret = -ENOMEM;
265
266 return tgt;
267 }
268
269 INIT_LIST_HEAD(&tgt->list);
270 tgt->id = le32_to_cpu(tgtid->tgt_id);
271 tgt->channel = 0;
272
273 SNIC_BUG_ON(le16_to_cpu(tgtid->tgt_type) > SNIC_TGT_SAN);
274 tgt->tdata.typ = le16_to_cpu(tgtid->tgt_type);
275
276 /*
277 * Plugging into SML Device Tree
278 */
279 tgt->tdata.disc_id = 0;
280 tgt->state = SNIC_TGT_STAT_INIT;
281 device_initialize(&tgt->dev);
282 tgt->dev.parent = get_device(&snic->shost->shost_gendev);
283 tgt->dev.release = snic_tgt_dev_release;
284 INIT_WORK(&tgt->scan_work, snic_scsi_scan_tgt);
285 INIT_WORK(&tgt->del_work, snic_tgt_del);
286 switch (tgt->tdata.typ) {
287 case SNIC_TGT_DAS:
288 dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
289 snic->shost->host_no, tgt->channel, tgt->id);
290 break;
291
292 case SNIC_TGT_SAN:
293 dev_set_name(&tgt->dev, "snic_san_tgt:%d:%d-%d",
294 snic->shost->host_no, tgt->channel, tgt->id);
295 break;
296
297 default:
298 SNIC_HOST_INFO(snic->shost, "Target type Unknown Detected.\n");
299 dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
300 snic->shost->host_no, tgt->channel, tgt->id);
301 break;
302 }
303
304 spin_lock_irqsave(snic->shost->host_lock, flags);
305 list_add_tail(&tgt->list, &snic->disc.tgt_list);
306 tgt->scsi_tgt_id = snic->disc.nxt_tgt_id++;
307 tgt->state = SNIC_TGT_STAT_ONLINE;
308 spin_unlock_irqrestore(snic->shost->host_lock, flags);
309
310 SNIC_HOST_INFO(snic->shost,
311 "Tgt %d, type = %s detected. Adding..\n",
312 tgt->id, snic_tgt_type_to_str(tgt->tdata.typ));
313
314 ret = device_add(&tgt->dev);
315 if (ret) {
316 SNIC_HOST_ERR(snic->shost,
317 "Snic Tgt: device_add, with err = %d\n",
318 ret);
319
320 put_device(&snic->shost->shost_gendev);
321 spin_lock_irqsave(snic->shost->host_lock, flags);
322 list_del(&tgt->list);
323 spin_unlock_irqrestore(snic->shost->host_lock, flags);
324 kfree(tgt);
325 tgt = NULL;
326
327 return tgt;
328 }
329
330 SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));
331
332 scsi_queue_work(snic->shost, &tgt->scan_work);
333
334 return tgt;
335 } /* end of snic_tgt_create */
336
337 /* Handler for discovery */
338 void
snic_handle_tgt_disc(struct work_struct * work)339 snic_handle_tgt_disc(struct work_struct *work)
340 {
341 struct snic *snic = container_of(work, struct snic, tgt_work);
342 struct snic_tgt_id *tgtid = NULL;
343 struct snic_tgt *tgt = NULL;
344 unsigned long flags;
345 int i;
346
347 spin_lock_irqsave(&snic->snic_lock, flags);
348 if (snic->in_remove) {
349 spin_unlock_irqrestore(&snic->snic_lock, flags);
350 kfree(snic->disc.rtgt_info);
351
352 return;
353 }
354 spin_unlock_irqrestore(&snic->snic_lock, flags);
355
356 mutex_lock(&snic->disc.mutex);
357 /* Discover triggered during disc in progress */
358 if (snic->disc.req_cnt) {
359 snic->disc.state = SNIC_DISC_DONE;
360 snic->disc.req_cnt = 0;
361 mutex_unlock(&snic->disc.mutex);
362 kfree(snic->disc.rtgt_info);
363 snic->disc.rtgt_info = NULL;
364
365 SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");
366 /* Start Discovery Again */
367 snic_disc_start(snic);
368
369 return;
370 }
371
372 tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;
373
374 SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);
375
376 for (i = 0; i < snic->disc.rtgt_cnt; i++) {
377 tgt = snic_tgt_create(snic, &tgtid[i]);
378 if (!tgt) {
379 int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);
380
381 SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");
382 snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);
383 break;
384 }
385 }
386
387 snic->disc.rtgt_info = NULL;
388 snic->disc.state = SNIC_DISC_DONE;
389 mutex_unlock(&snic->disc.mutex);
390
391 SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");
392
393 kfree(tgtid);
394 } /* end of snic_handle_tgt_disc */
395
396
397 int
snic_report_tgt_cmpl_handler(struct snic * snic,struct snic_fw_req * fwreq)398 snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
399 {
400
401 u8 typ, cmpl_stat;
402 u32 cmnd_id, hid, tgt_cnt = 0;
403 ulong ctx;
404 struct snic_req_info *rqi = NULL;
405 struct snic_tgt_id *tgtid;
406 int i, ret = 0;
407
408 snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);
409 rqi = (struct snic_req_info *) ctx;
410 tgtid = (struct snic_tgt_id *) rqi->sge_va;
411
412 tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);
413 if (tgt_cnt == 0) {
414 SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");
415 ret = 1;
416
417 goto end;
418 }
419
420 /* printing list of targets here */
421 SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);
422
423 SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);
424
425 for (i = 0; i < tgt_cnt; i++)
426 SNIC_HOST_INFO(snic->shost,
427 "Tgt id = 0x%x\n",
428 le32_to_cpu(tgtid[i].tgt_id));
429
430 /*
431 * Queue work for further processing,
432 * Response Buffer Memory is freed after creating targets
433 */
434 snic->disc.rtgt_cnt = tgt_cnt;
435 snic->disc.rtgt_info = (u8 *) tgtid;
436 queue_work(snic_glob->event_q, &snic->tgt_work);
437 ret = 0;
438
439 end:
440 /* Unmap Response Buffer */
441 snic_pci_unmap_rsp_buf(snic, rqi);
442 if (ret)
443 kfree(tgtid);
444
445 rqi->sge_va = 0;
446 snic_release_untagged_req(snic, rqi);
447
448 return ret;
449 } /* end of snic_report_tgt_cmpl_handler */
450
451 /* Discovery init fn */
452 void
snic_disc_init(struct snic_disc * disc)453 snic_disc_init(struct snic_disc *disc)
454 {
455 INIT_LIST_HEAD(&disc->tgt_list);
456 mutex_init(&disc->mutex);
457 disc->disc_id = 0;
458 disc->nxt_tgt_id = 0;
459 disc->state = SNIC_DISC_INIT;
460 disc->req_cnt = 0;
461 disc->rtgt_cnt = 0;
462 disc->rtgt_info = NULL;
463 disc->cb = NULL;
464 } /* end of snic_disc_init */
465
466 /* Discovery, uninit fn */
467 void
snic_disc_term(struct snic * snic)468 snic_disc_term(struct snic *snic)
469 {
470 struct snic_disc *disc = &snic->disc;
471
472 mutex_lock(&disc->mutex);
473 if (disc->req_cnt) {
474 disc->req_cnt = 0;
475 SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");
476 }
477 mutex_unlock(&disc->mutex);
478 }
479
480 /*
481 * snic_disc_start: Discovery Start ...
482 */
483 int
snic_disc_start(struct snic * snic)484 snic_disc_start(struct snic *snic)
485 {
486 struct snic_disc *disc = &snic->disc;
487 unsigned long flags;
488 int ret = 0;
489
490 SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");
491
492 spin_lock_irqsave(&snic->snic_lock, flags);
493 if (snic->in_remove) {
494 spin_unlock_irqrestore(&snic->snic_lock, flags);
495 SNIC_ERR("snic driver removal in progress ...\n");
496 ret = 0;
497
498 return ret;
499 }
500 spin_unlock_irqrestore(&snic->snic_lock, flags);
501
502 mutex_lock(&disc->mutex);
503 if (disc->state == SNIC_DISC_PENDING) {
504 disc->req_cnt++;
505 mutex_unlock(&disc->mutex);
506
507 return ret;
508 }
509 disc->state = SNIC_DISC_PENDING;
510 mutex_unlock(&disc->mutex);
511
512 ret = snic_queue_report_tgt_req(snic);
513 if (ret)
514 SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);
515
516 return ret;
517 } /* end of snic_disc_start */
518
519 /*
520 * snic_disc_work :
521 */
522 void
snic_handle_disc(struct work_struct * work)523 snic_handle_disc(struct work_struct *work)
524 {
525 struct snic *snic = container_of(work, struct snic, disc_work);
526 int ret = 0;
527
528 SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");
529
530 ret = snic_disc_start(snic);
531 if (ret)
532 goto disc_err;
533
534 disc_err:
535 SNIC_HOST_ERR(snic->shost,
536 "disc_work: Discovery Failed w/ err = %d\n",
537 ret);
538 } /* end of snic_disc_work */
539
540 /*
541 * snic_tgt_del_all : cleanup all snic targets
542 * Called on unbinding the interface
543 */
544 void
snic_tgt_del_all(struct snic * snic)545 snic_tgt_del_all(struct snic *snic)
546 {
547 struct snic_tgt *tgt = NULL;
548 struct list_head *cur, *nxt;
549 unsigned long flags;
550
551 scsi_flush_work(snic->shost);
552
553 mutex_lock(&snic->disc.mutex);
554 spin_lock_irqsave(snic->shost->host_lock, flags);
555
556 list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
557 tgt = list_entry(cur, struct snic_tgt, list);
558 tgt->state = SNIC_TGT_STAT_DEL;
559 list_del_init(&tgt->list);
560 SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);
561 queue_work(snic_glob->event_q, &tgt->del_work);
562 tgt = NULL;
563 }
564 spin_unlock_irqrestore(snic->shost->host_lock, flags);
565 mutex_unlock(&snic->disc.mutex);
566
567 flush_workqueue(snic_glob->event_q);
568 } /* end of snic_tgt_del_all */
569