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