scsi.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * linux/drivers/acorn/scsi/scsi.h
  3. *
  4. * Copyright (C) 2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Commonly used scsi driver functions.
  11. */
  12. #include <linux/scatterlist.h>
  13. #define BELT_AND_BRACES
  14. /*
  15. * The scatter-gather list handling. This contains all
  16. * the yucky stuff that needs to be fixed properly.
  17. */
  18. /*
  19. * copy_SCp_to_sg() Assumes contiguous allocation at @sg of at-most @max
  20. * entries of uninitialized memory. SCp is from scsi-ml and has a valid
  21. * (possibly chained) sg-list
  22. */
  23. static inline int copy_SCp_to_sg(struct scatterlist *sg, struct scsi_pointer *SCp, int max)
  24. {
  25. int bufs = SCp->buffers_residual;
  26. /* FIXME: It should be easy for drivers to loop on copy_SCp_to_sg().
  27. * and to remove this BUG_ON. Use min() in-its-place
  28. */
  29. BUG_ON(bufs + 1 > max);
  30. sg_set_buf(sg, SCp->ptr, SCp->this_residual);
  31. if (bufs) {
  32. struct scatterlist *src_sg;
  33. unsigned i;
  34. for_each_sg(sg_next(SCp->buffer), src_sg, bufs, i)
  35. *(++sg) = *src_sg;
  36. sg_mark_end(sg);
  37. }
  38. return bufs + 1;
  39. }
  40. static inline int next_SCp(struct scsi_pointer *SCp)
  41. {
  42. int ret = SCp->buffers_residual;
  43. if (ret) {
  44. SCp->buffer = sg_next(SCp->buffer);
  45. SCp->buffers_residual--;
  46. SCp->ptr = sg_virt(SCp->buffer);
  47. SCp->this_residual = SCp->buffer->length;
  48. } else {
  49. SCp->ptr = NULL;
  50. SCp->this_residual = 0;
  51. }
  52. return ret;
  53. }
  54. static inline unsigned char get_next_SCp_byte(struct scsi_pointer *SCp)
  55. {
  56. char c = *SCp->ptr;
  57. SCp->ptr += 1;
  58. SCp->this_residual -= 1;
  59. return c;
  60. }
  61. static inline void put_next_SCp_byte(struct scsi_pointer *SCp, unsigned char c)
  62. {
  63. *SCp->ptr = c;
  64. SCp->ptr += 1;
  65. SCp->this_residual -= 1;
  66. }
  67. static inline void init_SCp(struct scsi_cmnd *SCpnt)
  68. {
  69. memset(&SCpnt->SCp, 0, sizeof(struct scsi_pointer));
  70. if (scsi_bufflen(SCpnt)) {
  71. unsigned long len = 0;
  72. SCpnt->SCp.buffer = scsi_sglist(SCpnt);
  73. SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
  74. SCpnt->SCp.ptr = sg_virt(SCpnt->SCp.buffer);
  75. SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
  76. SCpnt->SCp.phase = scsi_bufflen(SCpnt);
  77. #ifdef BELT_AND_BRACES
  78. { /*
  79. * Calculate correct buffer length. Some commands
  80. * come in with the wrong scsi_bufflen.
  81. */
  82. struct scatterlist *sg;
  83. unsigned i, sg_count = scsi_sg_count(SCpnt);
  84. scsi_for_each_sg(SCpnt, sg, sg_count, i)
  85. len += sg->length;
  86. if (scsi_bufflen(SCpnt) != len) {
  87. printk(KERN_WARNING
  88. "scsi%d.%c: bad request buffer "
  89. "length %d, should be %ld\n",
  90. SCpnt->device->host->host_no,
  91. '0' + SCpnt->device->id,
  92. scsi_bufflen(SCpnt), len);
  93. /*
  94. * FIXME: Totaly naive fixup. We should abort
  95. * with error
  96. */
  97. SCpnt->SCp.phase =
  98. min_t(unsigned long, len,
  99. scsi_bufflen(SCpnt));
  100. }
  101. }
  102. #endif
  103. } else {
  104. SCpnt->SCp.ptr = NULL;
  105. SCpnt->SCp.this_residual = 0;
  106. SCpnt->SCp.phase = 0;
  107. }
  108. }