[SCSI] megaraid_sas: frame count optimization

This patch removes duplicated code in frame calculation & adds
megasas_get_frame_count() that also takes into account the number of frames
that can be contained in the Main frame.
FW uses the frame count to pull sufficient number of frames from host memory.

Signed-off-by: Sumant Patro <Sumant.Patro@lsil.com>
Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>

authored by Sumant Patro and committed by James Bottomley b1df99d9 e3bbff9f

+47 -25
+47 -25
drivers/scsi/megaraid/megaraid_sas.c
··· 495 495 return sge_count; 496 496 } 497 497 498 + /** 499 + * megasas_get_frame_count - Computes the number of frames 500 + * @sge_count : number of sg elements 501 + * 502 + * Returns the number of frames required for numnber of sge's (sge_count) 503 + */ 504 + 505 + u32 megasas_get_frame_count(u8 sge_count) 506 + { 507 + int num_cnt; 508 + int sge_bytes; 509 + u32 sge_sz; 510 + u32 frame_count=0; 511 + 512 + sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : 513 + sizeof(struct megasas_sge32); 514 + 515 + /* 516 + * Main frame can contain 2 SGEs for 64-bit SGLs and 517 + * 3 SGEs for 32-bit SGLs 518 + */ 519 + if (IS_DMA64) 520 + num_cnt = sge_count - 2; 521 + else 522 + num_cnt = sge_count - 3; 523 + 524 + if(num_cnt>0){ 525 + sge_bytes = sge_sz * num_cnt; 526 + 527 + frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + 528 + ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ; 529 + } 530 + /* Main frame */ 531 + frame_count +=1; 532 + 533 + if (frame_count > 7) 534 + frame_count = 8; 535 + return frame_count; 536 + } 537 + 498 538 /** 499 539 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command 500 540 * @instance: Adapter soft state ··· 548 508 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp, 549 509 struct megasas_cmd *cmd) 550 510 { 551 - u32 sge_sz; 552 - int sge_bytes; 553 511 u32 is_logical; 554 512 u32 device_id; 555 513 u16 flags = 0; ··· 582 544 /* 583 545 * Construct SGL 584 546 */ 585 - sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : 586 - sizeof(struct megasas_sge32); 587 - 588 547 if (IS_DMA64) { 589 548 pthru->flags |= MFI_FRAME_SGL64; 590 549 pthru->sge_count = megasas_make_sgl64(instance, scp, ··· 597 562 pthru->sense_buf_phys_addr_hi = 0; 598 563 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 599 564 600 - sge_bytes = sge_sz * pthru->sge_count; 601 - 602 565 /* 603 566 * Compute the total number of frames this command consumes. FW uses 604 567 * this number to pull sufficient number of frames from host memory. 605 568 */ 606 - cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + 607 - ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1; 608 - 609 - if (cmd->frame_count > 7) 610 - cmd->frame_count = 8; 569 + cmd->frame_count = megasas_get_frame_count(pthru->sge_count); 611 570 612 571 return cmd->frame_count; 613 572 } ··· 618 589 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp, 619 590 struct megasas_cmd *cmd) 620 591 { 621 - u32 sge_sz; 622 - int sge_bytes; 623 592 u32 device_id; 624 593 u8 sc = scp->cmnd[0]; 625 594 u16 flags = 0; ··· 632 605 flags = MFI_FRAME_DIR_READ; 633 606 634 607 /* 635 - * Preare the Logical IO frame: 2nd bit is zero for all read cmds 608 + * Prepare the Logical IO frame: 2nd bit is zero for all read cmds 636 609 */ 637 610 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ; 638 611 ldio->cmd_status = 0x0; ··· 701 674 /* 702 675 * Construct SGL 703 676 */ 704 - sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : 705 - sizeof(struct megasas_sge32); 706 - 707 677 if (IS_DMA64) { 708 678 ldio->flags |= MFI_FRAME_SGL64; 709 679 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl); ··· 714 690 ldio->sense_buf_phys_addr_hi = 0; 715 691 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 716 692 717 - sge_bytes = sge_sz * ldio->sge_count; 718 - 719 - cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + 720 - ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1; 721 - 722 - if (cmd->frame_count > 7) 723 - cmd->frame_count = 8; 693 + /* 694 + * Compute the total number of frames this command consumes. FW uses 695 + * this number to pull sufficient number of frames from host memory. 696 + */ 697 + cmd->frame_count = megasas_get_frame_count(ldio->sge_count); 724 698 725 699 return cmd->frame_count; 726 700 }