Advertisement
Guest User

Untitled

a guest
May 30th, 2011
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 111.29 KB | None | 0 0
  1. /*
  2.  *      sd.c Copyright (C) 1992 Drew Eckhardt
  3.  *           Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4.  *
  5.  *      Linux scsi disk driver
  6.  *              Initial versions: Drew Eckhardt
  7.  *              Subsequent revisions: Eric Youngdale
  8.  *  Modification history:
  9.  *       - Drew Eckhardt <drew@colorado.edu> original
  10.  *       - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
  11.  *         outstanding request, and other enhancements.
  12.  *         Support loadable low-level scsi drivers.
  13.  *       - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
  14.  *         eight major numbers.
  15.  *       - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
  16.  *   - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
  17.  *     sd_init and cleanups.
  18.  *   - Alex Davis <letmein@erols.com> Fix problem where partition info
  19.  *     not being read in sd_open. Fix problem where removable media
  20.  *     could be ejected after sd_open.
  21.  *   - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
  22.  *   - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
  23.  *     <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
  24.  *     Support 32k/1M disks.
  25.  *
  26.  *  Logging policy (needs CONFIG_SCSI_LOGGING defined):
  27.  *   - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
  28.  *   - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
  29.  *   - entering sd_ioctl: SCSI_LOG_IOCTL level 1
  30.  *   - entering other commands: SCSI_LOG_HLQUEUE level 3
  31.  *  Note: when the logging level is set by the user, it must be greater
  32.  *  than the level indicated above to trigger output.  
  33.  */
  34.  
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/kernel.h>
  38. #include <linux/mm.h>
  39. #include <linux/bio.h>
  40. #include <linux/genhd.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/errno.h>
  43. #include <linux/idr.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/init.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/blkpg.h>
  48. #include <linux/delay.h>
  49. #include <linux/mutex.h>
  50. #include <linux/string_helpers.h>
  51. #include <linux/async.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/unaligned.h>
  54.  
  55. #include <scsi/scsi.h>
  56. #include <scsi/scsi_cmnd.h>
  57. #include <scsi/scsi_dbg.h>
  58. #include <scsi/scsi_device.h>
  59. #include <scsi/scsi_driver.h>
  60. #include <scsi/scsi_eh.h>
  61. #include <scsi/scsi_host.h>
  62. #include <scsi/scsi_ioctl.h>
  63. #include <scsi/scsicam.h>
  64.  
  65. #include "sd.h"
  66. #include "scsi_logging.h"
  67. //Patch by QNAP: usb and sata device mapping
  68. #ifdef CONFIG_MACH_QNAPTS
  69. #include <linux/proc_fs.h>
  70. #include <linux/usb.h>
  71. #include <qnap/pic.h>
  72. int usbdev_exist[MAX_USB_ENTRY];
  73. int usbdev_status_changed[MAX_USB_ENTRY];
  74. int usbdev_speedtype[MAX_USB_ENTRY];
  75. EXPORT_SYMBOL(usbdev_exist);
  76. EXPORT_SYMBOL(usbdev_status_changed);
  77. EXPORT_SYMBOL(usbdev_speedtype);
  78. int QNAP_disk_index(struct scsi_device *sdp);
  79. static int sd_dev_arr[26];
  80. //Patch by QNAP: add x59 derivative model
  81. extern int get_model_revision(void);
  82. extern int is_x59_proII_model(void);
  83. //////////////////////////////////
  84. #endif
  85. #ifdef NAS_VIRTUAL
  86. #define ISCSI_DEV_START_INDEX 598
  87. #define MAX_ISCSI_DISK 26
  88. #define Is_iSCSI_Index(index) ((index >= ISCSI_DEV_START_INDEX && index <= (ISCSI_DEV_START_INDEX+MAX_ISCSI_DISK-1))? 1 : 0)
  89. #define Get_iSCSI_Index(c1, c2) (26*(c1-'a'+1)+c2-'a')
  90. static int iscsi_dev_arr[MAX_ISCSI_DISK];
  91. static struct device * iscsi_dev_ptr[MAX_ISCSI_DISK];
  92. #endif
  93. ////////////////////////////////////
  94. //Patch by QNAP: Port Multiplier
  95. #define ESATA_BUS1_START_INDEX 0x28a //sdya
  96. #define ESATA_BUS2_START_INDEX 0x2a4 //sdza
  97. #define MAX_ESATA_DISKS_BY_BUS 8
  98. #define Is_ESATA_Bus1_Index(index) ((index >= ESATA_BUS1_START_INDEX && index < (ESATA_BUS1_START_INDEX + MAX_ESATA_DISKS_BY_BUS)) ? 1 : 0)
  99. #define Is_ESATA_Bus2_Index(index) ((index >= ESATA_BUS2_START_INDEX && index < (ESATA_BUS2_START_INDEX + MAX_ESATA_DISKS_BY_BUS)) ? 1 : 0)
  100. static int esata_bus1_dev_arr[MAX_ESATA_DISKS_BY_BUS];
  101. static int esata_bus2_dev_arr[MAX_ESATA_DISKS_BY_BUS];
  102. //
  103. //Patch by QNAP: USB Port Multiplier
  104. #define USB_FRONT_PORT_START_INDEX 0xea //sdia
  105. #define USB_BACK_PORT_1_START_INDEX 0x1EE //sdsa
  106. #define USB_BACK_PORT_2_START_INDEX 0x208 //sdta
  107. #define USB_BACK_PORT_3_START_INDEX 0x222 //sdua
  108. #define USB_BACK_PORT_4_START_INDEX 0x23c //sdva
  109. #define USB_BACK_PORT_5_START_INDEX 0x1ba //sdqa
  110. #define USB_BACK_PORT_6_START_INDEX 0x1d4 //sdra
  111. #define MAX_USB_DISKS 26
  112. #define Is_USB_Front_Port_Index(index) ((index >= USB_FRONT_PORT_START_INDEX && index < (USB_FRONT_PORT_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  113. #define Is_USB_Back_Port1_Index(index) ((index >= USB_BACK_PORT_1_START_INDEX && index < (USB_BACK_PORT_1_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  114. #define Is_USB_Back_Port2_Index(index) ((index >= USB_BACK_PORT_2_START_INDEX && index < (USB_BACK_PORT_2_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  115. #define Is_USB_Back_Port3_Index(index) ((index >= USB_BACK_PORT_3_START_INDEX && index < (USB_BACK_PORT_3_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  116. #define Is_USB_Back_Port4_Index(index) ((index >= USB_BACK_PORT_4_START_INDEX && index < (USB_BACK_PORT_4_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  117. #define Is_USB_Back_Port5_Index(index) ((index >= USB_BACK_PORT_5_START_INDEX && index < (USB_BACK_PORT_5_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  118. #define Is_USB_Back_Port6_Index(index) ((index >= USB_BACK_PORT_6_START_INDEX && index < (USB_BACK_PORT_6_START_INDEX + MAX_USB_DISKS)) ? 1 : 0)
  119. static int usb_front_port_dev_arr[MAX_USB_DISKS];
  120. static int usb_back_port1_dev_arr[MAX_USB_DISKS];
  121. static int usb_back_port2_dev_arr[MAX_USB_DISKS];
  122. static int usb_back_port3_dev_arr[MAX_USB_DISKS];
  123. static int usb_back_port4_dev_arr[MAX_USB_DISKS];
  124. static int usb_back_port5_dev_arr[MAX_USB_DISKS];
  125. static int usb_back_port6_dev_arr[MAX_USB_DISKS];
  126. ////////////////////////////////
  127. MODULE_AUTHOR("Eric Youngdale");
  128. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  129. MODULE_LICENSE("GPL");
  130.  
  131. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  132. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  133. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  134. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  135. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  136. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  137. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  138. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  139. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  140. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  141. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  142. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  143. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  144. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  145. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  146. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  147. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  148. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  149. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  150.  
  151. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  152. #define SD_MINORS   16
  153. #else
  154. #define SD_MINORS   0
  155. #endif
  156.  
  157. static int  sd_revalidate_disk(struct gendisk *);
  158. static int  sd_probe(struct device *);
  159. static int  sd_remove(struct device *);
  160. static void sd_shutdown(struct device *);
  161. static int sd_suspend(struct device *, pm_message_t state);
  162. static int sd_resume(struct device *);
  163. static void sd_rescan(struct device *);
  164. static int sd_done(struct scsi_cmnd *);
  165. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  166. static void scsi_disk_release(struct device *cdev);
  167. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  168. static void sd_print_result(struct scsi_disk *, int);
  169. //Patch by QNAP: usb and sata device mapping
  170. #ifndef CONFIG_MACH_QNAPTS
  171. static DEFINE_IDA(sd_index_ida);
  172. #endif
  173. #ifdef NAS_VIRTUAL
  174. #define QNAP_DISK_NODE "scsi/qnap_disk_node"
  175. #define MAX_DISK_NODE_LEN       16
  176.  
  177. static char qnap_disk_node[MAX_DISK_NODE_LEN];
  178. static struct proc_dir_entry *qnap_disk_node_proc_entry;
  179.  
  180. #ifdef NAS_VIRTUAL_EX
  181. #define QNAP_IQN_NODE "scsi/qnap_iqn_node"
  182. #define MAX_IQN_NODE_LEN       260
  183. #define QNAP_SN_VPD_NODE "scsi/qnap_sn_vpd_node"
  184.  
  185. static char *iscsi_iqn_arr[MAX_ISCSI_DISK];
  186. static unsigned char *iscsi_sn_vpd_arr[MAX_ISCSI_DISK];
  187.  
  188. static char qnap_iqn_node[MAX_IQN_NODE_LEN];
  189. static struct proc_dir_entry *qnap_iqn_node_proc_entry;
  190. #endif
  191. static ssize_t qnap_disk_node_write( struct file *filp, const char __user *buff, unsigned long len, void *data )
  192. {
  193.   if (len > MAX_DISK_NODE_LEN) {
  194.     printk(KERN_INFO "qnap_disk_node: only can write %d bytes!\n", MAX_DISK_NODE_LEN);
  195.     return -ENOSPC;
  196.   }
  197.   if (copy_from_user(qnap_disk_node, buff, len )) {
  198.     return -EFAULT;
  199.   }
  200.   return len;
  201. }
  202. static int qnap_disk_node_read( char *page, char **start, off_t off, int count, int *eof, void *data )
  203. {
  204.         *eof = 1;
  205.         return snprintf(page, count, "%s\n", qnap_disk_node);
  206. }
  207. #ifdef NAS_VIRTUAL_EX
  208. static ssize_t qnap_iqn_node_write( struct file *filp, const char __user *buff, unsigned long len, void *data )
  209. {
  210. printk("qnap_iqn_node_write(%p, %ul, %p)\n", buff, len, data);
  211.   memset(qnap_iqn_node, 0, sizeof(qnap_iqn_node));
  212.   if (copy_from_user(qnap_iqn_node, buff, MAX_IQN_NODE_LEN-1)) {
  213.     return -EFAULT;
  214.   }
  215.   int copy_len = 0;
  216.   qnap_iqn_node[MAX_IQN_NODE_LEN-1] = 0;
  217.   if (*qnap_iqn_node) {
  218.     char *ptr = strchr(qnap_iqn_node, '\n');
  219.     if (ptr) *ptr = 0;
  220.     copy_len = strlen(qnap_iqn_node);
  221. printk("qnap_iqn_node_write copy_len=%d, get %s\n", copy_len, qnap_iqn_node);
  222.   }
  223.   return (copy_len > len)?copy_len:len;
  224. }
  225. static int qnap_iqn_node_read( char *page, char **start, off_t off, int count, int *eof, void *data )
  226. {
  227.     int i, total_count = 0;
  228.     *eof = 1;
  229.     for (i = 0; i < MAX_ISCSI_DISK; i ++) {
  230.         if (iscsi_dev_arr[i] && iscsi_iqn_arr[i]) {
  231.             int read_count = snprintf(page+total_count, count, "%4d %s #%s\n", i, iscsi_iqn_arr[i], (iscsi_sn_vpd_arr[i])?iscsi_sn_vpd_arr[i]+4:"");
  232.             total_count += read_count;
  233.             if ((count -= read_count) <= 0)
  234.                 break;
  235.         }
  236.     }
  237.     if (count) {
  238.         int read_count = snprintf(page+total_count, count, "END\n");
  239.         total_count += read_count;
  240.     }
  241.     return total_count;
  242. }
  243. #endif
  244. void create_qnap_disk_node_proc(void)
  245. {
  246.         qnap_disk_node_proc_entry = create_proc_entry( QNAP_DISK_NODE, 0644, NULL );
  247.         if (!qnap_disk_node_proc_entry) return ;
  248.         qnap_disk_node_proc_entry->read_proc = qnap_disk_node_read;
  249.         qnap_disk_node_proc_entry->write_proc = qnap_disk_node_write;
  250. #ifdef NAS_VIRTUAL_EX
  251.         qnap_iqn_node_proc_entry = create_proc_entry( QNAP_IQN_NODE, 0644, NULL );
  252.         if (!qnap_iqn_node_proc_entry) return ;
  253.         qnap_iqn_node_proc_entry->read_proc = qnap_iqn_node_read;
  254.         qnap_iqn_node_proc_entry->write_proc = qnap_iqn_node_write;
  255. #endif
  256. }
  257. void remove_qnap_disk_node_proc(void)
  258. {
  259.         remove_proc_entry(QNAP_DISK_NODE, NULL);
  260. #ifdef NAS_VIRTUAL_EX
  261.         remove_proc_entry(QNAP_IQN_NODE, NULL);
  262. #endif
  263. }
  264. #endif
  265. ///////////////////////////////////////
  266.  
  267. static DEFINE_SPINLOCK(sd_index_lock);
  268.  
  269. /* This semaphore is used to mediate the 0->1 reference get in the
  270.  * face of object destruction (i.e. we can't allow a get on an
  271.  * object after last put) */
  272. static DEFINE_MUTEX(sd_ref_mutex);
  273.  
  274. struct kmem_cache *sd_cdb_cache;
  275. mempool_t *sd_cdb_pool;
  276.  
  277. static const char *sd_cache_types[] = {
  278.     "write through", "none", "write back",
  279.     "write back, no read (daft)"
  280. };
  281.  
  282. static ssize_t
  283. sd_store_cache_type(struct device *dev, struct device_attribute *attr,
  284.             const char *buf, size_t count)
  285. {
  286.     int i, ct = -1, rcd, wce, sp;
  287.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  288.     struct scsi_device *sdp = sdkp->device;
  289.     char buffer[64];
  290.     char *buffer_data;
  291.     struct scsi_mode_data data;
  292.     struct scsi_sense_hdr sshdr;
  293.     int len;
  294.  
  295.     if (sdp->type != TYPE_DISK)
  296.         /* no cache control on RBC devices; theoretically they
  297.          * can do it, but there's probably so many exceptions
  298.          * it's not worth the risk */
  299.         return -EINVAL;
  300.  
  301.     for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  302.         const int len = strlen(sd_cache_types[i]);
  303.         if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  304.             buf[len] == '\n') {
  305.             ct = i;
  306.             break;
  307.         }
  308.     }
  309.     if (ct < 0)
  310.         return -EINVAL;
  311.     rcd = ct & 0x01 ? 1 : 0;
  312.     wce = ct & 0x02 ? 1 : 0;
  313.     if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  314.                 SD_MAX_RETRIES, &data, NULL))
  315.         return -EINVAL;
  316.     len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  317.           data.block_descriptor_length);
  318.     buffer_data = buffer + data.header_length +
  319.         data.block_descriptor_length;
  320.     buffer_data[2] &= ~0x05;
  321.     buffer_data[2] |= wce << 2 | rcd;
  322.     sp = buffer_data[0] & 0x80 ? 1 : 0;
  323.  
  324.     if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  325.                  SD_MAX_RETRIES, &data, &sshdr)) {
  326.         if (scsi_sense_valid(&sshdr))
  327.             sd_print_sense_hdr(sdkp, &sshdr);
  328.         return -EINVAL;
  329.     }
  330.     revalidate_disk(sdkp->disk);
  331.     return count;
  332. }
  333.  
  334. static ssize_t
  335. sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
  336.                const char *buf, size_t count)
  337. {
  338.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  339.     struct scsi_device *sdp = sdkp->device;
  340.  
  341.     if (!capable(CAP_SYS_ADMIN))
  342.         return -EACCES;
  343.  
  344.     sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  345.  
  346.     return count;
  347. }
  348.  
  349. static ssize_t
  350. sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
  351.                const char *buf, size_t count)
  352. {
  353.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  354.     struct scsi_device *sdp = sdkp->device;
  355.  
  356.     if (!capable(CAP_SYS_ADMIN))
  357.         return -EACCES;
  358.  
  359.     if (sdp->type != TYPE_DISK)
  360.         return -EINVAL;
  361.  
  362.     sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  363.  
  364.     return count;
  365. }
  366.  
  367. static ssize_t
  368. sd_show_cache_type(struct device *dev, struct device_attribute *attr,
  369.            char *buf)
  370. {
  371.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  372.     int ct = sdkp->RCD + 2*sdkp->WCE;
  373.  
  374.     return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  375. }
  376.  
  377. static ssize_t
  378. sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
  379. {
  380.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  381.  
  382.     return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  383. }
  384.  
  385. static ssize_t
  386. sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
  387.               char *buf)
  388. {
  389.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  390.     struct scsi_device *sdp = sdkp->device;
  391.  
  392.     return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  393. }
  394.  
  395. static ssize_t
  396. sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
  397.               char *buf)
  398. {
  399.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  400.  
  401.     return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  402. }
  403.  
  404. static ssize_t
  405. sd_show_protection_type(struct device *dev, struct device_attribute *attr,
  406.             char *buf)
  407. {
  408.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  409.  
  410.     return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  411. }
  412.  
  413. static ssize_t
  414. sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
  415.             char *buf)
  416. {
  417.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  418.  
  419.     return snprintf(buf, 20, "%u\n", sdkp->ATO);
  420. }
  421.  
  422. static ssize_t
  423. sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
  424.               char *buf)
  425. {
  426.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  427.  
  428.     return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
  429. }
  430.  
  431. //Patch by QNAP:fix SSD SMART issue
  432. #ifdef CONFIG_MACH_QNAPTS
  433. //bit 0->SSD medium(ps.some SSD not support)
  434. //bit 1->SMART feature support
  435. //bit 2->SMART self-test support
  436. //bit 3->SMART error log support
  437. static ssize_t sd_show_smart_capability(struct device *dev, struct device_attribute *attr,
  438.             char *buf)
  439. {
  440.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  441.     unsigned char *inquiry = sdkp->device->inquiry;
  442.     return snprintf(buf, 40, "%x\n", inquiry[36]);
  443. }
  444. #endif
  445. ////////////////////////
  446.  
  447. static struct device_attribute sd_disk_attrs[] = {
  448.     __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  449.            sd_store_cache_type),
  450.     __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  451.     __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  452.            sd_store_allow_restart),
  453.     __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
  454.            sd_store_manage_start_stop),
  455.     __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
  456.     __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
  457.     __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
  458. //Patch by QNAP:fix SSD SMART issue
  459. #ifdef CONFIG_MACH_QNAPTS
  460.     __ATTR(smart_capability, S_IRUGO|S_IWUSR, sd_show_smart_capability,NULL),
  461. #endif
  462. /////////////////////////////
  463.     __ATTR_NULL,
  464. };
  465.  
  466. static struct class sd_disk_class = {
  467.     .name       = "scsi_disk",
  468.     .owner      = THIS_MODULE,
  469.     .dev_release    = scsi_disk_release,
  470.     .dev_attrs  = sd_disk_attrs,
  471. };
  472.  
  473. static struct scsi_driver sd_template = {
  474.     .owner          = THIS_MODULE,
  475.     .gendrv = {
  476.         .name       = "sd",
  477.         .probe      = sd_probe,
  478.         .remove     = sd_remove,
  479.         .suspend    = sd_suspend,
  480.         .resume     = sd_resume,
  481.         .shutdown   = sd_shutdown,
  482.     },
  483.     .rescan         = sd_rescan,
  484.     .done           = sd_done,
  485. };
  486.  
  487. /*
  488.  * Device no to disk mapping:
  489.  *
  490.  *       major         disc2     disc  p1
  491.  *   |............|.............|....|....| <- dev_t
  492.  *    31        20 19          8 7  4 3  0
  493.  *
  494.  * Inside a major, we have 16k disks, however mapped non-
  495.  * contiguously. The first 16 disks are for major0, the next
  496.  * ones with major1, ... Disk 256 is for major0 again, disk 272
  497.  * for major1, ...
  498.  * As we stay compatible with our numbering scheme, we can reuse
  499.  * the well-know SCSI majors 8, 65--71, 136--143.
  500.  */
  501. static int sd_major(int major_idx)
  502. {
  503.     switch (major_idx) {
  504.     case 0:
  505.         return SCSI_DISK0_MAJOR;
  506.     case 1 ... 7:
  507.         return SCSI_DISK1_MAJOR + major_idx - 1;
  508.     case 8 ... 15:
  509.         return SCSI_DISK8_MAJOR + major_idx - 8;
  510.     default:
  511.         BUG();
  512.         return 0;   /* shut up gcc */
  513.     }
  514. }
  515.  
  516. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  517. {
  518.     struct scsi_disk *sdkp = NULL;
  519.  
  520.     if (disk->private_data) {
  521.         sdkp = scsi_disk(disk);
  522.         if (scsi_device_get(sdkp->device) == 0)
  523.             get_device(&sdkp->dev);
  524.         else
  525.             sdkp = NULL;
  526.     }
  527.     return sdkp;
  528. }
  529.  
  530. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  531. {
  532.     struct scsi_disk *sdkp;
  533.  
  534.     mutex_lock(&sd_ref_mutex);
  535.     sdkp = __scsi_disk_get(disk);
  536.     mutex_unlock(&sd_ref_mutex);
  537.     return sdkp;
  538. }
  539.  
  540. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  541. {
  542.     struct scsi_disk *sdkp;
  543.  
  544.     mutex_lock(&sd_ref_mutex);
  545.     sdkp = dev_get_drvdata(dev);
  546.     if (sdkp)
  547.         sdkp = __scsi_disk_get(sdkp->disk);
  548.     mutex_unlock(&sd_ref_mutex);
  549.     return sdkp;
  550. }
  551.  
  552. static void scsi_disk_put(struct scsi_disk *sdkp)
  553. {
  554.     struct scsi_device *sdev = sdkp->device;
  555.  
  556.     mutex_lock(&sd_ref_mutex);
  557.     put_device(&sdkp->dev);
  558.     scsi_device_put(sdev);
  559.     mutex_unlock(&sd_ref_mutex);
  560. }
  561.  
  562. static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
  563. {
  564.     unsigned int prot_op = SCSI_PROT_NORMAL;
  565.     unsigned int dix = scsi_prot_sg_count(scmd);
  566.  
  567.     if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
  568.         if (dif && dix)
  569.             prot_op = SCSI_PROT_READ_PASS;
  570.         else if (dif && !dix)
  571.             prot_op = SCSI_PROT_READ_STRIP;
  572.         else if (!dif && dix)
  573.             prot_op = SCSI_PROT_READ_INSERT;
  574.     } else {
  575.         if (dif && dix)
  576.             prot_op = SCSI_PROT_WRITE_PASS;
  577.         else if (dif && !dix)
  578.             prot_op = SCSI_PROT_WRITE_INSERT;
  579.         else if (!dif && dix)
  580.             prot_op = SCSI_PROT_WRITE_STRIP;
  581.     }
  582.  
  583.     scsi_set_prot_op(scmd, prot_op);
  584.     scsi_set_prot_type(scmd, dif);
  585. }
  586.  
  587. /**
  588.  * sd_prepare_discard - unmap blocks on thinly provisioned device
  589.  * @rq: Request to prepare
  590.  *
  591.  * Will issue either UNMAP or WRITE SAME(16) depending on preference
  592.  * indicated by target device.
  593.  **/
  594. static int sd_prepare_discard(struct request *rq)
  595. {
  596.     struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  597.     struct bio *bio = rq->bio;
  598.     sector_t sector = bio->bi_sector;
  599.     unsigned int num = bio_sectors(bio);
  600.  
  601.     if (sdkp->device->sector_size == 4096) {
  602.         sector >>= 3;
  603.         num >>= 3;
  604.     }
  605.  
  606.     rq->cmd_type = REQ_TYPE_BLOCK_PC;
  607.     rq->timeout = SD_TIMEOUT;
  608.  
  609.     memset(rq->cmd, 0, rq->cmd_len);
  610.  
  611.     if (sdkp->unmap) {
  612.         char *buf = kmap_atomic(bio_page(bio), KM_USER0);
  613.  
  614.         rq->cmd[0] = UNMAP;
  615.         rq->cmd[8] = 24;
  616.         rq->cmd_len = 10;
  617.  
  618.         /* Ensure that data length matches payload */
  619.         rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
  620.  
  621.         put_unaligned_be16(6 + 16, &buf[0]);
  622.         put_unaligned_be16(16, &buf[2]);
  623.         put_unaligned_be64(sector, &buf[8]);
  624.         put_unaligned_be32(num, &buf[16]);
  625.  
  626.         kunmap_atomic(buf, KM_USER0);
  627.     } else {
  628.         rq->cmd[0] = WRITE_SAME_16;
  629.         rq->cmd[1] = 0x8; /* UNMAP */
  630.         put_unaligned_be64(sector, &rq->cmd[2]);
  631.         put_unaligned_be32(num, &rq->cmd[10]);
  632.         rq->cmd_len = 16;
  633.     }
  634.  
  635.     return BLKPREP_OK;
  636. }
  637.  
  638. /**
  639.  *  sd_init_command - build a scsi (read or write) command from
  640.  *  information in the request structure.
  641.  *  @SCpnt: pointer to mid-level's per scsi command structure that
  642.  *  contains request and into which the scsi command is written
  643.  *
  644.  *  Returns 1 if successful and 0 if error (or cannot be done now).
  645.  **/
  646. static int sd_prep_fn(struct request_queue *q, struct request *rq)
  647. {
  648.     struct scsi_cmnd *SCpnt;
  649.     struct scsi_device *sdp = q->queuedata;
  650.     struct gendisk *disk = rq->rq_disk;
  651.     struct scsi_disk *sdkp;
  652.     sector_t block = blk_rq_pos(rq);
  653.     sector_t threshold;
  654.     unsigned int this_count = blk_rq_sectors(rq);
  655.     int ret, host_dif;
  656.     unsigned char protect;
  657.  
  658.     /*
  659.      * Discard request come in as REQ_TYPE_FS but we turn them into
  660.      * block PC requests to make life easier.
  661.      */
  662.     if (blk_discard_rq(rq))
  663.         ret = sd_prepare_discard(rq);
  664.  
  665.     if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  666.         ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  667.         goto out;
  668.     } else if (rq->cmd_type != REQ_TYPE_FS) {
  669.         ret = BLKPREP_KILL;
  670.         goto out;
  671.     }
  672.     ret = scsi_setup_fs_cmnd(sdp, rq);
  673.     if (ret != BLKPREP_OK)
  674.         goto out;
  675.     SCpnt = rq->special;
  676.     sdkp = scsi_disk(disk);
  677.  
  678.     /* from here on until we're complete, any goto out
  679.      * is used for a killable error condition */
  680.     ret = BLKPREP_KILL;
  681.  
  682.     SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
  683.                     "sd_init_command: block=%llu, "
  684.                     "count=%d\n",
  685.                     (unsigned long long)block,
  686.                     this_count));
  687.  
  688.     if (!sdp || !scsi_device_online(sdp) ||
  689.         block + blk_rq_sectors(rq) > get_capacity(disk)) {
  690.         SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  691.                         "Finishing %u sectors\n",
  692.                         blk_rq_sectors(rq)));
  693.         SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  694.                         "Retry with 0x%p\n", SCpnt));
  695.         goto out;
  696.     }
  697.  
  698.     if (sdp->changed) {
  699.         /*
  700.          * quietly refuse to do anything to a changed disc until
  701.          * the changed bit has been reset
  702.          */
  703.         /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  704.         goto out;
  705.     }
  706.  
  707.     /*
  708.      * Some SD card readers can't handle multi-sector accesses which touch
  709.      * the last one or two hardware sectors.  Split accesses as needed.
  710.      */
  711.     threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  712.         (sdp->sector_size / 512);
  713.  
  714.     if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  715.         if (block < threshold) {
  716.             /* Access up to the threshold but not beyond */
  717.             this_count = threshold - block;
  718.         } else {
  719.             /* Access only a single hardware sector */
  720.             this_count = sdp->sector_size / 512;
  721.         }
  722.     }
  723.  
  724.     SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  725.                     (unsigned long long)block));
  726.  
  727.     /*
  728.      * If we have a 1K hardware sectorsize, prevent access to single
  729.      * 512 byte sectors.  In theory we could handle this - in fact
  730.      * the scsi cdrom driver must be able to handle this because
  731.      * we typically use 1K blocksizes, and cdroms typically have
  732.      * 2K hardware sectorsizes.  Of course, things are simpler
  733.      * with the cdrom, since it is read-only.  For performance
  734.      * reasons, the filesystems should be able to handle this
  735.      * and not force the scsi disk driver to use bounce buffers
  736.      * for this.
  737.      */
  738.     if (sdp->sector_size == 1024) {
  739.         if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  740.             scmd_printk(KERN_ERR, SCpnt,
  741.                     "Bad block number requested\n");
  742.             goto out;
  743.         } else {
  744.             block = block >> 1;
  745.             this_count = this_count >> 1;
  746.         }
  747.     }
  748.     if (sdp->sector_size == 2048) {
  749.         if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  750.             scmd_printk(KERN_ERR, SCpnt,
  751.                     "Bad block number requested\n");
  752.             goto out;
  753.         } else {
  754.             block = block >> 2;
  755.             this_count = this_count >> 2;
  756.         }
  757.     }
  758.     if (sdp->sector_size == 4096) {
  759.         if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  760.             scmd_printk(KERN_ERR, SCpnt,
  761.                     "Bad block number requested\n");
  762.             goto out;
  763.         } else {
  764.             block = block >> 3;
  765.             this_count = this_count >> 3;
  766.         }
  767.     }
  768.     if (rq_data_dir(rq) == WRITE) {
  769.         if (!sdp->writeable) {
  770.             goto out;
  771.         }
  772.         SCpnt->cmnd[0] = WRITE_6;
  773.         SCpnt->sc_data_direction = DMA_TO_DEVICE;
  774.  
  775.         if (blk_integrity_rq(rq) &&
  776.             sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
  777.             goto out;
  778.  
  779.     } else if (rq_data_dir(rq) == READ) {
  780.         SCpnt->cmnd[0] = READ_6;
  781.         SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  782.     } else {
  783.         scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  784.         goto out;
  785.     }
  786.  
  787.     SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  788.                     "%s %d/%u 512 byte blocks.\n",
  789.                     (rq_data_dir(rq) == WRITE) ?
  790.                     "writing" : "reading", this_count,
  791.                     blk_rq_sectors(rq)));
  792.  
  793.     /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
  794.     host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  795.     if (host_dif)
  796.         protect = 1 << 5;
  797.     else
  798.         protect = 0;
  799.  
  800.     if (host_dif == SD_DIF_TYPE2_PROTECTION) {
  801.         SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  802.  
  803.         if (unlikely(SCpnt->cmnd == NULL)) {
  804.             ret = BLKPREP_DEFER;
  805.             goto out;
  806.         }
  807.  
  808.         SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  809.         memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  810.         SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  811.         SCpnt->cmnd[7] = 0x18;
  812.         SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  813.         SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  814.  
  815.         /* LBA */
  816.         SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  817.         SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  818.         SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  819.         SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  820.         SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  821.         SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  822.         SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  823.         SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  824.  
  825.         /* Expected Indirect LBA */
  826.         SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  827.         SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  828.         SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  829.         SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  830.  
  831.         /* Transfer length */
  832.         SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  833.         SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  834.         SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  835.         SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  836.     } else if (block > 0xffffffff) {
  837.         SCpnt->cmnd[0] += READ_16 - READ_6;
  838.         SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  839.         SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  840.         SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  841.         SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  842.         SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  843.         SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  844.         SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  845.         SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  846.         SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  847.         SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  848.         SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  849.         SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  850.         SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  851.         SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  852.     } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  853.            scsi_device_protection(SCpnt->device) ||
  854.            SCpnt->device->use_10_for_rw) {
  855.         if (this_count > 0xffff)
  856.             this_count = 0xffff;
  857.  
  858.         SCpnt->cmnd[0] += READ_10 - READ_6;
  859.         SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  860.         SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  861.         SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  862.         SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  863.         SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  864.         SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  865.         SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  866.         SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  867.     } else {
  868.         if (unlikely(blk_fua_rq(rq))) {
  869.             /*
  870.              * This happens only if this drive failed
  871.              * 10byte rw command with ILLEGAL_REQUEST
  872.              * during operation and thus turned off
  873.              * use_10_for_rw.
  874.              */
  875.             scmd_printk(KERN_ERR, SCpnt,
  876.                     "FUA write on READ/WRITE(6) drive\n");
  877.             goto out;
  878.         }
  879.  
  880.         SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  881.         SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  882.         SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  883.         SCpnt->cmnd[4] = (unsigned char) this_count;
  884.         SCpnt->cmnd[5] = 0;
  885.     }
  886.     SCpnt->sdb.length = this_count * sdp->sector_size;
  887.  
  888.     /* If DIF or DIX is enabled, tell HBA how to handle request */
  889.     if (host_dif || scsi_prot_sg_count(SCpnt))
  890.         sd_prot_op(SCpnt, host_dif);
  891.  
  892.     /*
  893.      * We shouldn't disconnect in the middle of a sector, so with a dumb
  894.      * host adapter, it's safe to assume that we can at least transfer
  895.      * this many bytes between each connect / disconnect.
  896.      */
  897.     SCpnt->transfersize = sdp->sector_size;
  898.     SCpnt->underflow = this_count << 9;
  899.     SCpnt->allowed = SD_MAX_RETRIES;
  900.  
  901.     /*
  902.      * This indicates that the command is ready from our end to be
  903.      * queued.
  904.      */
  905.     ret = BLKPREP_OK;
  906.  out:
  907.     return scsi_prep_return(q, rq, ret);
  908. }
  909.  
  910. /**
  911.  *  sd_open - open a scsi disk device
  912.  *  @inode: only i_rdev member may be used
  913.  *  @filp: only f_mode and f_flags may be used
  914.  *
  915.  *  Returns 0 if successful. Returns a negated errno value in case
  916.  *  of error.
  917.  *
  918.  *  Note: This can be called from a user context (e.g. fsck(1) )
  919.  *  or from within the kernel (e.g. as a result of a mount(1) ).
  920.  *  In the latter case @inode and @filp carry an abridged amount
  921.  *  of information as noted above.
  922.  **/
  923. static int sd_open(struct block_device *bdev, fmode_t mode)
  924. {
  925.     struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  926.     struct scsi_device *sdev;
  927.     int retval;
  928.  
  929.     if (!sdkp)
  930.         return -ENXIO;
  931.  
  932.     SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  933.  
  934.     sdev = sdkp->device;
  935.  
  936.     /*
  937.      * If the device is in error recovery, wait until it is done.
  938.      * If the device is offline, then disallow any access to it.
  939.      */
  940.     retval = -ENXIO;
  941.     if (!scsi_block_when_processing_errors(sdev))
  942.         goto error_out;
  943.  
  944.     if (sdev->removable || sdkp->write_prot)
  945.         check_disk_change(bdev);
  946.  
  947.     /*
  948.      * If the drive is empty, just let the open fail.
  949.      */
  950.     retval = -ENOMEDIUM;
  951.     if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  952.         goto error_out;
  953.  
  954.     /*
  955.      * If the device has the write protect tab set, have the open fail
  956.      * if the user expects to be able to write to the thing.
  957.      */
  958.     retval = -EROFS;
  959.     if (sdkp->write_prot && (mode & FMODE_WRITE))
  960.         goto error_out;
  961.  
  962.     /*
  963.      * It is possible that the disk changing stuff resulted in
  964.      * the device being taken offline.  If this is the case,
  965.      * report this to the user, and don't pretend that the
  966.      * open actually succeeded.
  967.      */
  968.     retval = -ENXIO;
  969.     if (!scsi_device_online(sdev))
  970.         goto error_out;
  971.  
  972.     if (!sdkp->openers++ && sdev->removable) {
  973.         if (scsi_block_when_processing_errors(sdev))
  974.             scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  975.     }
  976.  
  977.     return 0;
  978.  
  979. error_out:
  980.     scsi_disk_put(sdkp);
  981.     return retval; 
  982. }
  983.  
  984. /**
  985.  *  sd_release - invoked when the (last) close(2) is called on this
  986.  *  scsi disk.
  987.  *  @inode: only i_rdev member may be used
  988.  *  @filp: only f_mode and f_flags may be used
  989.  *
  990.  *  Returns 0.
  991.  *
  992.  *  Note: may block (uninterruptible) if error recovery is underway
  993.  *  on this disk.
  994.  **/
  995. static int sd_release(struct gendisk *disk, fmode_t mode)
  996. {
  997.     struct scsi_disk *sdkp = scsi_disk(disk);
  998.     struct scsi_device *sdev = sdkp->device;
  999.  
  1000.     SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  1001.  
  1002.     if (!--sdkp->openers && sdev->removable) {
  1003.         if (scsi_block_when_processing_errors(sdev))
  1004.             scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  1005.     }
  1006.  
  1007.     /*
  1008.      * XXX and what if there are packets in flight and this close()
  1009.      * XXX is followed by a "rmmod sd_mod"?
  1010.      */
  1011.     scsi_disk_put(sdkp);
  1012.     return 0;
  1013. }
  1014.  
  1015. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1016. {
  1017.     struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  1018.     struct scsi_device *sdp = sdkp->device;
  1019.     struct Scsi_Host *host = sdp->host;
  1020.     int diskinfo[4];
  1021.  
  1022.     /* default to most commonly used values */
  1023.         diskinfo[0] = 0x40; /* 1 << 6 */
  1024.         diskinfo[1] = 0x20; /* 1 << 5 */
  1025.         diskinfo[2] = sdkp->capacity >> 11;
  1026.    
  1027.     /* override with calculated, extended default, or driver values */
  1028.     if (host->hostt->bios_param)
  1029.         host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  1030.     else
  1031.         scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  1032.  
  1033.     geo->heads = diskinfo[0];
  1034.     geo->sectors = diskinfo[1];
  1035.     geo->cylinders = diskinfo[2];
  1036.     return 0;
  1037. }
  1038.  
  1039. /**
  1040.  *  sd_ioctl - process an ioctl
  1041.  *  @inode: only i_rdev/i_bdev members may be used
  1042.  *  @filp: only f_mode and f_flags may be used
  1043.  *  @cmd: ioctl command number
  1044.  *  @arg: this is third argument given to ioctl(2) system call.
  1045.  *  Often contains a pointer.
  1046.  *
  1047.  *  Returns 0 if successful (some ioctls return postive numbers on
  1048.  *  success as well). Returns a negated errno value in case of error.
  1049.  *
  1050.  *  Note: most ioctls are forward onto the block subsystem or further
  1051.  *  down in the scsi subsystem.
  1052.  **/
  1053. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  1054.             unsigned int cmd, unsigned long arg)
  1055. {
  1056.     struct gendisk *disk = bdev->bd_disk;
  1057.     struct scsi_device *sdp = scsi_disk(disk)->device;
  1058.     void __user *p = (void __user *)arg;
  1059.     int error;
  1060.    
  1061.     SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  1062.                         disk->disk_name, cmd));
  1063.  
  1064.     /*
  1065.      * If we are in the middle of error recovery, don't let anyone
  1066.      * else try and use this device.  Also, if error recovery fails, it
  1067.      * may try and take the device offline, in which case all further
  1068.      * access to the device is prohibited.
  1069.      */
  1070.     error = scsi_nonblockable_ioctl(sdp, cmd, p,
  1071.                     (mode & FMODE_NDELAY) != 0);
  1072.     if (!scsi_block_when_processing_errors(sdp) || !error)
  1073.         return error;
  1074.  
  1075.     /*
  1076.      * Send SCSI addressing ioctls directly to mid level, send other
  1077.      * ioctls to block level and then onto mid level if they can't be
  1078.      * resolved.
  1079.      */
  1080.     switch (cmd) {
  1081.         case SCSI_IOCTL_GET_IDLUN:
  1082.         case SCSI_IOCTL_GET_BUS_NUMBER:
  1083.             return scsi_ioctl(sdp, cmd, p);
  1084.         default:
  1085.             error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
  1086.             if (error != -ENOTTY)
  1087.                 return error;
  1088.     }
  1089.     return scsi_ioctl(sdp, cmd, p);
  1090. }
  1091.  
  1092. static void set_media_not_present(struct scsi_disk *sdkp)
  1093. {
  1094.     sdkp->media_present = 0;
  1095.     sdkp->capacity = 0;
  1096.     sdkp->device->changed = 1;
  1097. }
  1098.  
  1099. /**
  1100.  *  sd_media_changed - check if our medium changed
  1101.  *  @disk: kernel device descriptor
  1102.  *
  1103.  *  Returns 0 if not applicable or no change; 1 if change
  1104.  *
  1105.  *  Note: this function is invoked from the block subsystem.
  1106.  **/
  1107. static int sd_media_changed(struct gendisk *disk)
  1108. {
  1109.     struct scsi_disk *sdkp = scsi_disk(disk);
  1110.     struct scsi_device *sdp = sdkp->device;
  1111.     struct scsi_sense_hdr *sshdr = NULL;
  1112.     int retval;
  1113.  
  1114.     SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
  1115.  
  1116.     if (!sdp->removable)
  1117.         return 0;
  1118.  
  1119.     /*
  1120.      * If the device is offline, don't send any commands - just pretend as
  1121.      * if the command failed.  If the device ever comes back online, we
  1122.      * can deal with it then.  It is only because of unrecoverable errors
  1123.      * that we would ever take a device offline in the first place.
  1124.      */
  1125.     if (!scsi_device_online(sdp)) {
  1126.         set_media_not_present(sdkp);
  1127.         retval = 1;
  1128.         goto out;
  1129.     }
  1130.  
  1131.     /*
  1132.      * Using TEST_UNIT_READY enables differentiation between drive with
  1133.      * no cartridge loaded - NOT READY, drive with changed cartridge -
  1134.      * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  1135.      *
  1136.      * Drives that auto spin down. eg iomega jaz 1G, will be started
  1137.      * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  1138.      * sd_revalidate() is called.
  1139.      */
  1140.     retval = -ENODEV;
  1141.  
  1142.     if (scsi_block_when_processing_errors(sdp)) {
  1143.         sshdr  = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1144.         retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  1145.                           sshdr);
  1146.     }
  1147.  
  1148.     /*
  1149.      * Unable to test, unit probably not ready.   This usually
  1150.      * means there is no disc in the drive.  Mark as changed,
  1151.      * and we will figure it out later once the drive is
  1152.      * available again.
  1153.      */
  1154.     if (retval || (scsi_sense_valid(sshdr) &&
  1155.                /* 0x3a is medium not present */
  1156.                sshdr->asc == 0x3a)) {
  1157.         set_media_not_present(sdkp);
  1158.         retval = 1;
  1159.         goto out;
  1160.     }
  1161.  
  1162.     /*
  1163.      * For removable scsi disk we have to recognise the presence
  1164.      * of a disk in the drive. This is kept in the struct scsi_disk
  1165.      * struct and tested at open !  Daniel Roche (dan@lectra.fr)
  1166.      */
  1167.     sdkp->media_present = 1;
  1168.  
  1169.     retval = sdp->changed;
  1170.     sdp->changed = 0;
  1171. out:
  1172.     if (retval != sdkp->previous_state)
  1173.         sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
  1174.     sdkp->previous_state = retval;
  1175.     kfree(sshdr);
  1176.     return retval;
  1177. }
  1178.  
  1179. static int sd_sync_cache(struct scsi_disk *sdkp)
  1180. {
  1181.     int retries, res;
  1182.     struct scsi_device *sdp = sdkp->device;
  1183.     struct scsi_sense_hdr sshdr;
  1184.  
  1185.     if (!scsi_device_online(sdp))
  1186.         return -ENODEV;
  1187.  
  1188.  
  1189.     for (retries = 3; retries > 0; --retries) {
  1190.         unsigned char cmd[10] = { 0 };
  1191.  
  1192.         cmd[0] = SYNCHRONIZE_CACHE;
  1193.         /*
  1194.          * Leave the rest of the command zero to indicate
  1195.          * flush everything.
  1196.          */
  1197.         res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  1198.                        SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1199.         if (res == 0)
  1200.             break;
  1201.     }
  1202.  
  1203.     if (res) {
  1204.         sd_print_result(sdkp, res);
  1205.         if (driver_byte(res) & DRIVER_SENSE)
  1206.             sd_print_sense_hdr(sdkp, &sshdr);
  1207.     }
  1208.  
  1209.     if (res)
  1210.         return -EIO;
  1211.     return 0;
  1212. }
  1213.  
  1214. static void sd_prepare_flush(struct request_queue *q, struct request *rq)
  1215. {
  1216.     rq->cmd_type = REQ_TYPE_BLOCK_PC;
  1217.     rq->timeout = SD_TIMEOUT;
  1218.     rq->cmd[0] = SYNCHRONIZE_CACHE;
  1219.     rq->cmd_len = 10;
  1220. }
  1221.  
  1222. static void sd_rescan(struct device *dev)
  1223. {
  1224.     struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1225.  
  1226.     if (sdkp) {
  1227.         revalidate_disk(sdkp->disk);
  1228.         scsi_disk_put(sdkp);
  1229.     }
  1230. }
  1231.  
  1232.  
  1233. #ifdef CONFIG_COMPAT
  1234. /*
  1235.  * This gets directly called from VFS. When the ioctl
  1236.  * is not recognized we go back to the other translation paths.
  1237.  */
  1238. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1239.                unsigned int cmd, unsigned long arg)
  1240. {
  1241.     struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1242.  
  1243.     /*
  1244.      * If we are in the middle of error recovery, don't let anyone
  1245.      * else try and use this device.  Also, if error recovery fails, it
  1246.      * may try and take the device offline, in which case all further
  1247.      * access to the device is prohibited.
  1248.      */
  1249.     if (!scsi_block_when_processing_errors(sdev))
  1250.         return -ENODEV;
  1251.            
  1252.     if (sdev->host->hostt->compat_ioctl) {
  1253.         int ret;
  1254.  
  1255.         ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  1256.  
  1257.         return ret;
  1258.     }
  1259.  
  1260.     /*
  1261.      * Let the static ioctl translation table take care of it.
  1262.      */
  1263.     return -ENOIOCTLCMD;
  1264. }
  1265. #endif
  1266.  
  1267. static const struct block_device_operations sd_fops = {
  1268.     .owner          = THIS_MODULE,
  1269.     .open           = sd_open,
  1270.     .release        = sd_release,
  1271.     .locked_ioctl       = sd_ioctl,
  1272.     .getgeo         = sd_getgeo,
  1273. #ifdef CONFIG_COMPAT
  1274.     .compat_ioctl       = sd_compat_ioctl,
  1275. #endif
  1276.     .media_changed      = sd_media_changed,
  1277.     .revalidate_disk    = sd_revalidate_disk,
  1278. };
  1279.  
  1280. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  1281. {
  1282.     u64 start_lba = blk_rq_pos(scmd->request);
  1283.     u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  1284.     u64 bad_lba;
  1285.     int info_valid;
  1286.  
  1287.     if (!blk_fs_request(scmd->request))
  1288.         return 0;
  1289.  
  1290.     info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  1291.                          SCSI_SENSE_BUFFERSIZE,
  1292.                          &bad_lba);
  1293.     if (!info_valid)
  1294.         return 0;
  1295.  
  1296.     if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  1297.         return 0;
  1298.  
  1299.     if (scmd->device->sector_size < 512) {
  1300.         /* only legitimate sector_size here is 256 */
  1301.         start_lba <<= 1;
  1302.         end_lba <<= 1;
  1303.     } else {
  1304.         /* be careful ... don't want any overflows */
  1305.         u64 factor = scmd->device->sector_size / 512;
  1306.         do_div(start_lba, factor);
  1307.         do_div(end_lba, factor);
  1308.     }
  1309.  
  1310.     /* The bad lba was reported incorrectly, we have no idea where
  1311.      * the error is.
  1312.      */
  1313.     if (bad_lba < start_lba  || bad_lba >= end_lba)
  1314.         return 0;
  1315.  
  1316.     /* This computation should always be done in terms of
  1317.      * the resolution of the device's medium.
  1318.      */
  1319.     return (bad_lba - start_lba) * scmd->device->sector_size;
  1320. }
  1321.  
  1322. /**
  1323.  *  sd_done - bottom half handler: called when the lower level
  1324.  *  driver has completed (successfully or otherwise) a scsi command.
  1325.  *  @SCpnt: mid-level's per command structure.
  1326.  *
  1327.  *  Note: potentially run from within an ISR. Must not block.
  1328.  **/
  1329. static int sd_done(struct scsi_cmnd *SCpnt)
  1330. {
  1331.     int result = SCpnt->result;
  1332.     unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1333.     struct scsi_sense_hdr sshdr;
  1334.     struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1335.     int sense_valid = 0;
  1336.     int sense_deferred = 0;
  1337.  
  1338.     if (result) {
  1339.         sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1340.         if (sense_valid)
  1341.             sense_deferred = scsi_sense_is_deferred(&sshdr);
  1342.     }
  1343. #ifdef CONFIG_SCSI_LOGGING
  1344.     SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
  1345.     if (sense_valid) {
  1346.         SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1347.                            "sd_done: sb[respc,sk,asc,"
  1348.                            "ascq]=%x,%x,%x,%x\n",
  1349.                            sshdr.response_code,
  1350.                            sshdr.sense_key, sshdr.asc,
  1351.                            sshdr.ascq));
  1352.     }
  1353. #endif
  1354.     if (driver_byte(result) != DRIVER_SENSE &&
  1355.         (!sense_valid || sense_deferred))
  1356.         goto out;
  1357.  
  1358.     switch (sshdr.sense_key) {
  1359.     case HARDWARE_ERROR:
  1360.     case MEDIUM_ERROR:
  1361.         good_bytes = sd_completed_bytes(SCpnt);
  1362.         break;
  1363.     case RECOVERED_ERROR:
  1364.         good_bytes = scsi_bufflen(SCpnt);
  1365.         break;
  1366.     case NO_SENSE:
  1367.         /* This indicates a false check condition, so ignore it.  An
  1368.          * unknown amount of data was transferred so treat it as an
  1369.          * error.
  1370.          */
  1371.         scsi_print_sense("sd", SCpnt);
  1372.         SCpnt->result = 0;
  1373.         memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1374.         break;
  1375.     case ABORTED_COMMAND:
  1376.         if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
  1377.             scsi_print_result(SCpnt);
  1378.             scsi_print_sense("sd", SCpnt);
  1379.             good_bytes = sd_completed_bytes(SCpnt);
  1380.         }
  1381.         break;
  1382.     case ILLEGAL_REQUEST:
  1383.         if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
  1384.             scsi_print_result(SCpnt);
  1385.             scsi_print_sense("sd", SCpnt);
  1386.             good_bytes = sd_completed_bytes(SCpnt);
  1387.         }
  1388.         break;
  1389.     default:
  1390.         break;
  1391.     }
  1392.  out:
  1393.     if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1394.         sd_dif_complete(SCpnt, good_bytes);
  1395.  
  1396.     if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
  1397.         == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
  1398.         mempool_free(SCpnt->cmnd, sd_cdb_pool);
  1399.  
  1400.     return good_bytes;
  1401. }
  1402.  
  1403. static int media_not_present(struct scsi_disk *sdkp,
  1404.                  struct scsi_sense_hdr *sshdr)
  1405. {
  1406.  
  1407.     if (!scsi_sense_valid(sshdr))
  1408.         return 0;
  1409.     /* not invoked for commands that could return deferred errors */
  1410.     if (sshdr->sense_key != NOT_READY &&
  1411.         sshdr->sense_key != UNIT_ATTENTION)
  1412.         return 0;
  1413.     if (sshdr->asc != 0x3A) /* medium not present */
  1414.         return 0;
  1415.  
  1416.     set_media_not_present(sdkp);
  1417.     return 1;
  1418. }
  1419.  
  1420. /*
  1421.  * spinup disk - called only in sd_revalidate_disk()
  1422.  */
  1423. static void
  1424. sd_spinup_disk(struct scsi_disk *sdkp)
  1425. {
  1426.     unsigned char cmd[10];
  1427.     unsigned long spintime_expire = 0;
  1428.     int retries, spintime;
  1429.     unsigned int the_result;
  1430.     struct scsi_sense_hdr sshdr;
  1431.     int sense_valid = 0;
  1432.  
  1433.     spintime = 0;
  1434.  
  1435.     /* Spin up drives, as required.  Only do this at boot time */
  1436.     /* Spinup needs to be done for module loads too. */
  1437.     do {
  1438.         retries = 0;
  1439.  
  1440.         do {
  1441.             cmd[0] = TEST_UNIT_READY;
  1442.             memset((void *) &cmd[1], 0, 9);
  1443.  
  1444.             the_result = scsi_execute_req(sdkp->device, cmd,
  1445.                               DMA_NONE, NULL, 0,
  1446.                               &sshdr, SD_TIMEOUT,
  1447.                               SD_MAX_RETRIES, NULL);
  1448.  
  1449.             /*
  1450.              * If the drive has indicated to us that it
  1451.              * doesn't have any media in it, don't bother
  1452.              * with any more polling.
  1453.              */
  1454.             if (media_not_present(sdkp, &sshdr))
  1455.                 return;
  1456.  
  1457.             if (the_result)
  1458.                 sense_valid = scsi_sense_valid(&sshdr);
  1459.             retries++;
  1460.         } while (retries < 3 &&
  1461.              (!scsi_status_is_good(the_result) ||
  1462.               ((driver_byte(the_result) & DRIVER_SENSE) &&
  1463.               sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1464.  
  1465.         if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1466.             /* no sense, TUR either succeeded or failed
  1467.              * with a status error */
  1468.             if(!spintime && !scsi_status_is_good(the_result)) {
  1469.                 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1470.                 sd_print_result(sdkp, the_result);
  1471.             }
  1472.             break;
  1473.         }
  1474.                    
  1475.         /*
  1476.          * The device does not want the automatic start to be issued.
  1477.          */
  1478.         if (sdkp->device->no_start_on_add)
  1479.             break;
  1480.  
  1481.         if (sense_valid && sshdr.sense_key == NOT_READY) {
  1482.             if (sshdr.asc == 4 && sshdr.ascq == 3)
  1483.                 break;  /* manual intervention required */
  1484.             if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1485.                 break;  /* standby */
  1486.             if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1487.                 break;  /* unavailable */
  1488.             /*
  1489.              * Issue command to spin up drive when not ready
  1490.              */
  1491.             if (!spintime) {
  1492.                 sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1493.                 cmd[0] = START_STOP;
  1494.                 cmd[1] = 1; /* Return immediately */
  1495.                 memset((void *) &cmd[2], 0, 8);
  1496.                 cmd[4] = 1; /* Start spin cycle */
  1497.                 if (sdkp->device->start_stop_pwr_cond)
  1498.                     cmd[4] |= 1 << 4;
  1499.                 scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1500.                          NULL, 0, &sshdr,
  1501.                          SD_TIMEOUT, SD_MAX_RETRIES,
  1502.                          NULL);
  1503.                 spintime_expire = jiffies + 100 * HZ;
  1504.                 spintime = 1;
  1505.             }
  1506.             /* Wait 1 second for next try */
  1507.             msleep(1000);
  1508.             printk(".");
  1509.  
  1510.         /*
  1511.          * Wait for USB flash devices with slow firmware.
  1512.          * Yes, this sense key/ASC combination shouldn't
  1513.          * occur here.  It's characteristic of these devices.
  1514.          */
  1515.         } else if (sense_valid &&
  1516.                 sshdr.sense_key == UNIT_ATTENTION &&
  1517.                 sshdr.asc == 0x28) {
  1518.             if (!spintime) {
  1519.                 spintime_expire = jiffies + 5 * HZ;
  1520.                 spintime = 1;
  1521.             }
  1522.             /* Wait 1 second for next try */
  1523.             msleep(1000);
  1524.         } else {
  1525.             /* we don't understand the sense code, so it's
  1526.              * probably pointless to loop */
  1527.             if(!spintime) {
  1528.                 sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1529.                 sd_print_sense_hdr(sdkp, &sshdr);
  1530.             }
  1531.             break;
  1532.         }
  1533.                
  1534.     } while (spintime && time_before_eq(jiffies, spintime_expire));
  1535.  
  1536.     if (spintime) {
  1537.         if (scsi_status_is_good(the_result))
  1538.             printk("ready\n");
  1539.         else
  1540.             printk("not responding...\n");
  1541.     }
  1542. }
  1543.  
  1544.  
  1545. /*
  1546.  * Determine whether disk supports Data Integrity Field.
  1547.  */
  1548. void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1549. {
  1550.     struct scsi_device *sdp = sdkp->device;
  1551.     u8 type;
  1552.  
  1553.     if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1554.         return;
  1555.  
  1556.     type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1557.  
  1558.     if (type == sdkp->protection_type || !sdkp->first_scan)
  1559.         return;
  1560.  
  1561.     sdkp->protection_type = type;
  1562.  
  1563.     if (type > SD_DIF_TYPE3_PROTECTION) {
  1564.         sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
  1565.               "protection type %u. Disabling disk!\n", type);
  1566.         sdkp->capacity = 0;
  1567.         return;
  1568.     }
  1569.  
  1570.     if (scsi_host_dif_capable(sdp->host, type))
  1571.         sd_printk(KERN_NOTICE, sdkp,
  1572.               "Enabling DIF Type %u protection\n", type);
  1573.     else
  1574.         sd_printk(KERN_NOTICE, sdkp,
  1575.               "Disabling DIF Type %u protection\n", type);
  1576. }
  1577.  
  1578. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1579.             struct scsi_sense_hdr *sshdr, int sense_valid,
  1580.             int the_result)
  1581. {
  1582.     sd_print_result(sdkp, the_result);
  1583.     if (driver_byte(the_result) & DRIVER_SENSE)
  1584.         sd_print_sense_hdr(sdkp, sshdr);
  1585.     else
  1586.         sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1587.  
  1588.     /*
  1589.      * Set dirty bit for removable devices if not ready -
  1590.      * sometimes drives will not report this properly.
  1591.      */
  1592.     if (sdp->removable &&
  1593.         sense_valid && sshdr->sense_key == NOT_READY)
  1594.         sdp->changed = 1;
  1595.  
  1596.     /*
  1597.      * We used to set media_present to 0 here to indicate no media
  1598.      * in the drive, but some drives fail read capacity even with
  1599.      * media present, so we can't do that.
  1600.      */
  1601.     sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1602. }
  1603.  
  1604. #define RC16_LEN 32
  1605. #if RC16_LEN > SD_BUF_SIZE
  1606. #error RC16_LEN must not be more than SD_BUF_SIZE
  1607. #endif
  1608.  
  1609. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1610.                         unsigned char *buffer)
  1611. {
  1612.     unsigned char cmd[16];
  1613.     struct scsi_sense_hdr sshdr;
  1614.     int sense_valid = 0;
  1615.     int the_result;
  1616.     int retries = 3;
  1617.     unsigned int alignment;
  1618.     unsigned long long lba;
  1619.     unsigned sector_size;
  1620.  
  1621.     do {
  1622.         memset(cmd, 0, 16);
  1623.         cmd[0] = SERVICE_ACTION_IN;
  1624.         cmd[1] = SAI_READ_CAPACITY_16;
  1625.         cmd[13] = RC16_LEN;
  1626.         memset(buffer, 0, RC16_LEN);
  1627.  
  1628.         the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1629.                     buffer, RC16_LEN, &sshdr,
  1630.                     SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1631.  
  1632.         if (media_not_present(sdkp, &sshdr))
  1633.             return -ENODEV;
  1634.  
  1635.         if (the_result) {
  1636.             sense_valid = scsi_sense_valid(&sshdr);
  1637.             if (sense_valid &&
  1638.                 sshdr.sense_key == ILLEGAL_REQUEST &&
  1639.                 (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1640.                 sshdr.ascq == 0x00)
  1641.                 /* Invalid Command Operation Code or
  1642.                  * Invalid Field in CDB, just retry
  1643.                  * silently with RC10 */
  1644.                 return -EINVAL;
  1645.         }
  1646.         retries--;
  1647.  
  1648.     } while (the_result && retries);
  1649.  
  1650.     if (the_result) {
  1651.         sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1652.         read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1653.         return -EINVAL;
  1654.     }
  1655.  
  1656.     sector_size = get_unaligned_be32(&buffer[8]);
  1657.     lba = get_unaligned_be64(&buffer[0]);
  1658.  
  1659.     sd_read_protection_type(sdkp, buffer);
  1660.  
  1661.     if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
  1662.         sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1663.             "kernel compiled with support for large block "
  1664.             "devices.\n");
  1665.         sdkp->capacity = 0;
  1666.         return -EOVERFLOW;
  1667.     }
  1668.  
  1669.     /* Logical blocks per physical block exponent */
  1670.     sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1671.  
  1672.     /* Lowest aligned logical block */
  1673.     alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1674.     blk_queue_alignment_offset(sdp->request_queue, alignment);
  1675.     if (alignment && sdkp->first_scan)
  1676.         sd_printk(KERN_NOTICE, sdkp,
  1677.               "physical block alignment offset: %u\n", alignment);
  1678.  
  1679.     if (buffer[14] & 0x80) { /* TPE */
  1680.         struct request_queue *q = sdp->request_queue;
  1681.  
  1682.         sdkp->thin_provisioning = 1;
  1683.         q->limits.discard_granularity = sdkp->hw_sector_size;
  1684.         q->limits.max_discard_sectors = 0xffffffff;
  1685.  
  1686.         if (buffer[14] & 0x40) /* TPRZ */
  1687.             q->limits.discard_zeroes_data = 1;
  1688.  
  1689.         queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1690.     }
  1691.  
  1692.     sdkp->capacity = lba + 1;
  1693.     return sector_size;
  1694. }
  1695.  
  1696. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1697.                         unsigned char *buffer)
  1698. {
  1699.     unsigned char cmd[16];
  1700.     struct scsi_sense_hdr sshdr;
  1701.     int sense_valid = 0;
  1702.     int the_result;
  1703.     int retries = 3;
  1704.     sector_t lba;
  1705.     unsigned sector_size;
  1706.  
  1707.     do {
  1708.         cmd[0] = READ_CAPACITY;
  1709.         memset(&cmd[1], 0, 9);
  1710.         memset(buffer, 0, 8);
  1711.  
  1712.         the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1713.                     buffer, 8, &sshdr,
  1714.                     SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1715.  
  1716.         if (media_not_present(sdkp, &sshdr))
  1717.             return -ENODEV;
  1718.  
  1719.         if (the_result)
  1720.             sense_valid = scsi_sense_valid(&sshdr);
  1721.         retries--;
  1722.  
  1723.     } while (the_result && retries);
  1724.  
  1725.     if (the_result) {
  1726.         sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1727.         read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1728.         return -EINVAL;
  1729.     }
  1730.  
  1731.     sector_size = get_unaligned_be32(&buffer[4]);
  1732.     lba = get_unaligned_be32(&buffer[0]);
  1733.  
  1734.     if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
  1735.         sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1736.             "kernel compiled with support for large block "
  1737.             "devices.\n");
  1738.         sdkp->capacity = 0;
  1739.         return -EOVERFLOW;
  1740.     }
  1741.  
  1742.     sdkp->capacity = lba + 1;
  1743.     sdkp->hw_sector_size = sector_size;
  1744.     return sector_size;
  1745. }
  1746.  
  1747. static int sd_try_rc16_first(struct scsi_device *sdp)
  1748. {
  1749.     if (sdp->scsi_level > SCSI_SPC_2)
  1750.         return 1;
  1751.     if (scsi_device_protection(sdp))
  1752.         return 1;
  1753.     return 0;
  1754. }
  1755.  
  1756. /*
  1757.  * read disk capacity
  1758.  */
  1759. static void
  1760. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1761. {
  1762.     int sector_size;
  1763.     struct scsi_device *sdp = sdkp->device;
  1764.     sector_t old_capacity = sdkp->capacity;
  1765.  
  1766.     if (sd_try_rc16_first(sdp)) {
  1767.         sector_size = read_capacity_16(sdkp, sdp, buffer);
  1768.         if (sector_size == -EOVERFLOW)
  1769.             goto got_data;
  1770.         if (sector_size == -ENODEV)
  1771.             return;
  1772.         if (sector_size < 0)
  1773.             sector_size = read_capacity_10(sdkp, sdp, buffer);
  1774.         if (sector_size < 0)
  1775.             return;
  1776.     } else {
  1777.         sector_size = read_capacity_10(sdkp, sdp, buffer);
  1778.         if (sector_size == -EOVERFLOW)
  1779.             goto got_data;
  1780.         if (sector_size < 0)
  1781.             return;
  1782.         if ((sizeof(sdkp->capacity) > 4) &&
  1783.             (sdkp->capacity > 0xffffffffULL)) {
  1784.             int old_sector_size = sector_size;
  1785.             sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1786.                     "Trying to use READ CAPACITY(16).\n");
  1787.             sector_size = read_capacity_16(sdkp, sdp, buffer);
  1788.             if (sector_size < 0) {
  1789.                 sd_printk(KERN_NOTICE, sdkp,
  1790.                     "Using 0xffffffff as device size\n");
  1791.                 sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1792.                 sector_size = old_sector_size;
  1793.                 goto got_data;
  1794.             }
  1795.         }
  1796.     }
  1797.  
  1798.     /* Some devices are known to return the total number of blocks,
  1799.      * not the highest block number.  Some devices have versions
  1800.      * which do this and others which do not.  Some devices we might
  1801.      * suspect of doing this but we don't know for certain.
  1802.      *
  1803.      * If we know the reported capacity is wrong, decrement it.  If
  1804.      * we can only guess, then assume the number of blocks is even
  1805.      * (usually true but not always) and err on the side of lowering
  1806.      * the capacity.
  1807.      */
  1808.     if (sdp->fix_capacity ||
  1809.         (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1810.         sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1811.                 "from its reported value: %llu\n",
  1812.                 (unsigned long long) sdkp->capacity);
  1813.         --sdkp->capacity;
  1814.     }
  1815.  
  1816. got_data:
  1817.     if (sector_size == 0) {
  1818.         sector_size = 512;
  1819.         sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1820.               "assuming 512.\n");
  1821.     }
  1822.  
  1823.     if (sector_size != 512 &&
  1824.         sector_size != 1024 &&
  1825.         sector_size != 2048 &&
  1826.         sector_size != 4096 &&
  1827.         sector_size != 256) {
  1828.         sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1829.               sector_size);
  1830.         /*
  1831.          * The user might want to re-format the drive with
  1832.          * a supported sectorsize.  Once this happens, it
  1833.          * would be relatively trivial to set the thing up.
  1834.          * For this reason, we leave the thing in the table.
  1835.          */
  1836.         sdkp->capacity = 0;
  1837.         /*
  1838.          * set a bogus sector size so the normal read/write
  1839.          * logic in the block layer will eventually refuse any
  1840.          * request on this device without tripping over power
  1841.          * of two sector size assumptions
  1842.          */
  1843.         sector_size = 512;
  1844.     }
  1845.     blk_queue_logical_block_size(sdp->request_queue, sector_size);
  1846.  
  1847.     {
  1848.         char cap_str_2[10], cap_str_10[10];
  1849.         u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
  1850.  
  1851.         string_get_size(sz, STRING_UNITS_2, cap_str_2,
  1852.                 sizeof(cap_str_2));
  1853.         string_get_size(sz, STRING_UNITS_10, cap_str_10,
  1854.                 sizeof(cap_str_10));
  1855.  
  1856.         if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  1857.             sd_printk(KERN_NOTICE, sdkp,
  1858.                   "%llu %d-byte logical blocks: (%s/%s)\n",
  1859.                   (unsigned long long)sdkp->capacity,
  1860.                   sector_size, cap_str_10, cap_str_2);
  1861.  
  1862.             if (sdkp->hw_sector_size != sector_size)
  1863.                 sd_printk(KERN_NOTICE, sdkp,
  1864.                       "%u-byte physical blocks\n",
  1865.                       sdkp->hw_sector_size);
  1866.         }
  1867.     }
  1868.  
  1869.     /* Rescale capacity to 512-byte units */
  1870.     if (sector_size == 4096)
  1871.         sdkp->capacity <<= 3;
  1872.     else if (sector_size == 2048)
  1873.         sdkp->capacity <<= 2;
  1874.     else if (sector_size == 1024)
  1875.         sdkp->capacity <<= 1;
  1876.     else if (sector_size == 256)
  1877.         sdkp->capacity >>= 1;
  1878.  
  1879.     blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
  1880.     sdkp->device->sector_size = sector_size;
  1881. }
  1882.  
  1883. /* called with buffer of length 512 */
  1884. static inline int
  1885. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1886.          unsigned char *buffer, int len, struct scsi_mode_data *data,
  1887.          struct scsi_sense_hdr *sshdr)
  1888. {
  1889.     return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1890.                    SD_TIMEOUT, SD_MAX_RETRIES, data,
  1891.                    sshdr);
  1892. }
  1893.  
  1894. /*
  1895.  * read write protect setting, if possible - called only in sd_revalidate_disk()
  1896.  * called with buffer of length SD_BUF_SIZE
  1897.  */
  1898. static void
  1899. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1900. {
  1901.     int res;
  1902.     struct scsi_device *sdp = sdkp->device;
  1903.     struct scsi_mode_data data;
  1904.     int old_wp = sdkp->write_prot;
  1905.  
  1906.     set_disk_ro(sdkp->disk, 0);
  1907.     if (sdp->skip_ms_page_3f) {
  1908.         sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1909.         return;
  1910.     }
  1911.  
  1912.     if (sdp->use_192_bytes_for_3f) {
  1913.         res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1914.     } else {
  1915.         /*
  1916.          * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1917.          * We have to start carefully: some devices hang if we ask
  1918.          * for more than is available.
  1919.          */
  1920.         res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1921.  
  1922.         /*
  1923.          * Second attempt: ask for page 0 When only page 0 is
  1924.          * implemented, a request for page 3F may return Sense Key
  1925.          * 5: Illegal Request, Sense Code 24: Invalid field in
  1926.          * CDB.
  1927.          */
  1928.         if (!scsi_status_is_good(res))
  1929.             res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1930.  
  1931.         /*
  1932.          * Third attempt: ask 255 bytes, as we did earlier.
  1933.          */
  1934.         if (!scsi_status_is_good(res))
  1935.             res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1936.                            &data, NULL);
  1937.     }
  1938.  
  1939.     if (!scsi_status_is_good(res)) {
  1940.         sd_printk(KERN_WARNING, sdkp,
  1941.               "Test WP failed, assume Write Enabled\n");
  1942.     } else {
  1943.         sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1944.         set_disk_ro(sdkp->disk, sdkp->write_prot);
  1945.         if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  1946.             sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1947.                   sdkp->write_prot ? "on" : "off");
  1948.             sd_printk(KERN_DEBUG, sdkp,
  1949.                   "Mode Sense: %02x %02x %02x %02x\n",
  1950.                   buffer[0], buffer[1], buffer[2], buffer[3]);
  1951.         }
  1952.     }
  1953. }
  1954.  
  1955. /*
  1956.  * sd_read_cache_type - called only from sd_revalidate_disk()
  1957.  * called with buffer of length SD_BUF_SIZE
  1958.  */
  1959. static void
  1960. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1961. {
  1962.     int len = 0, res;
  1963.     struct scsi_device *sdp = sdkp->device;
  1964.  
  1965.     int dbd;
  1966.     int modepage;
  1967.     struct scsi_mode_data data;
  1968.     struct scsi_sense_hdr sshdr;
  1969.     int old_wce = sdkp->WCE;
  1970.     int old_rcd = sdkp->RCD;
  1971.     int old_dpofua = sdkp->DPOFUA;
  1972.  
  1973.     if (sdp->skip_ms_page_8)
  1974.         goto defaults;
  1975.  
  1976.     if (sdp->type == TYPE_RBC) {
  1977.         modepage = 6;
  1978.         dbd = 8;
  1979.     } else {
  1980.         modepage = 8;
  1981.         dbd = 0;
  1982.     }
  1983.  
  1984.     /* cautiously ask */
  1985.     res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1986.  
  1987.     if (!scsi_status_is_good(res))
  1988.         goto bad_sense;
  1989.  
  1990.     if (!data.header_length) {
  1991.         modepage = 6;
  1992.         sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1993.     }
  1994.  
  1995.     /* that went OK, now ask for the proper length */
  1996.     len = data.length;
  1997.  
  1998.     /*
  1999.      * We're only interested in the first three bytes, actually.
  2000.      * But the data cache page is defined for the first 20.
  2001.      */
  2002.     if (len < 3)
  2003.         goto bad_sense;
  2004.     if (len > 20)
  2005.         len = 20;
  2006.  
  2007.     /* Take headers and block descriptors into account */
  2008.     len += data.header_length + data.block_descriptor_length;
  2009.     if (len > SD_BUF_SIZE)
  2010.         goto bad_sense;
  2011.  
  2012.     /* Get the data */
  2013.     res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  2014.  
  2015.     if (scsi_status_is_good(res)) {
  2016.         int offset = data.header_length + data.block_descriptor_length;
  2017.  
  2018.         if (offset >= SD_BUF_SIZE - 2) {
  2019.             sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
  2020.             goto defaults;
  2021.         }
  2022.  
  2023.         if ((buffer[offset] & 0x3f) != modepage) {
  2024.             sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  2025.             goto defaults;
  2026.         }
  2027.  
  2028.         if (modepage == 8) {
  2029.             sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  2030.             sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  2031.         } else {
  2032.             sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  2033.             sdkp->RCD = 0;
  2034.         }
  2035.  
  2036.         sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  2037.         if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  2038.             sd_printk(KERN_NOTICE, sdkp,
  2039.                   "Uses READ/WRITE(6), disabling FUA\n");
  2040.             sdkp->DPOFUA = 0;
  2041.         }
  2042.  
  2043.         if (sdkp->first_scan || old_wce != sdkp->WCE ||
  2044.             old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  2045.             sd_printk(KERN_NOTICE, sdkp,
  2046.                   "Write cache: %s, read cache: %s, %s\n",
  2047.                   sdkp->WCE ? "enabled" : "disabled",
  2048.                   sdkp->RCD ? "disabled" : "enabled",
  2049.                   sdkp->DPOFUA ? "supports DPO and FUA"
  2050.                   : "doesn't support DPO or FUA");
  2051.  
  2052.         return;
  2053.     }
  2054.  
  2055. bad_sense:
  2056.     if (scsi_sense_valid(&sshdr) &&
  2057.         sshdr.sense_key == ILLEGAL_REQUEST &&
  2058.         sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  2059.         /* Invalid field in CDB */
  2060.         sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  2061.     else
  2062.         sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  2063.  
  2064. defaults:
  2065.     sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  2066.     sdkp->WCE = 0;
  2067.     sdkp->RCD = 0;
  2068.     sdkp->DPOFUA = 0;
  2069. }
  2070.  
  2071. /*
  2072.  * The ATO bit indicates whether the DIF application tag is available
  2073.  * for use by the operating system.
  2074.  */
  2075. void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  2076. {
  2077.     int res, offset;
  2078.     struct scsi_device *sdp = sdkp->device;
  2079.     struct scsi_mode_data data;
  2080.     struct scsi_sense_hdr sshdr;
  2081.  
  2082.     if (sdp->type != TYPE_DISK)
  2083.         return;
  2084.  
  2085.     if (sdkp->protection_type == 0)
  2086.         return;
  2087.  
  2088.     res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  2089.                   SD_MAX_RETRIES, &data, &sshdr);
  2090.  
  2091.     if (!scsi_status_is_good(res) || !data.header_length ||
  2092.         data.length < 6) {
  2093.         sd_printk(KERN_WARNING, sdkp,
  2094.               "getting Control mode page failed, assume no ATO\n");
  2095.  
  2096.         if (scsi_sense_valid(&sshdr))
  2097.             sd_print_sense_hdr(sdkp, &sshdr);
  2098.  
  2099.         return;
  2100.     }
  2101.  
  2102.     offset = data.header_length + data.block_descriptor_length;
  2103.  
  2104.     if ((buffer[offset] & 0x3f) != 0x0a) {
  2105.         sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  2106.         return;
  2107.     }
  2108.  
  2109.     if ((buffer[offset + 5] & 0x80) == 0)
  2110.         return;
  2111.  
  2112.     sdkp->ATO = 1;
  2113.  
  2114.     return;
  2115. }
  2116.  
  2117. /**
  2118.  * sd_read_block_limits - Query disk device for preferred I/O sizes.
  2119.  * @disk: disk to query
  2120.  */
  2121. static void sd_read_block_limits(struct scsi_disk *sdkp)
  2122. {
  2123.     struct request_queue *q = sdkp->disk->queue;
  2124.     unsigned int sector_sz = sdkp->device->sector_size;
  2125.     char *buffer;
  2126.  
  2127.     /* Block Limits VPD */
  2128.     buffer = scsi_get_vpd_page(sdkp->device, 0xb0);
  2129.  
  2130.     if (buffer == NULL)
  2131.         return;
  2132.  
  2133.     blk_queue_io_min(sdkp->disk->queue,
  2134.              get_unaligned_be16(&buffer[6]) * sector_sz);
  2135.     blk_queue_io_opt(sdkp->disk->queue,
  2136.              get_unaligned_be32(&buffer[12]) * sector_sz);
  2137.  
  2138.     /* Thin provisioning enabled and page length indicates TP support */
  2139.     if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
  2140.         unsigned int lba_count, desc_count, granularity;
  2141.  
  2142.         lba_count = get_unaligned_be32(&buffer[20]);
  2143.         desc_count = get_unaligned_be32(&buffer[24]);
  2144.  
  2145.         if (lba_count) {
  2146.             q->limits.max_discard_sectors =
  2147.                 lba_count * sector_sz >> 9;
  2148.  
  2149.             if (desc_count)
  2150.                 sdkp->unmap = 1;
  2151.         }
  2152.  
  2153.         granularity = get_unaligned_be32(&buffer[28]);
  2154.  
  2155.         if (granularity)
  2156.             q->limits.discard_granularity = granularity * sector_sz;
  2157.  
  2158.         if (buffer[32] & 0x80)
  2159.             q->limits.discard_alignment =
  2160.                 get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  2161.     }
  2162.  
  2163.     kfree(buffer);
  2164. }
  2165.  
  2166. /**
  2167.  * sd_read_block_characteristics - Query block dev. characteristics
  2168.  * @disk: disk to query
  2169.  */
  2170. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  2171. {
  2172.     char *buffer;
  2173.     u16 rot;
  2174.  
  2175.     /* Block Device Characteristics VPD */
  2176.     buffer = scsi_get_vpd_page(sdkp->device, 0xb1);
  2177.  
  2178.     if (buffer == NULL)
  2179.         return;
  2180.  
  2181.     rot = get_unaligned_be16(&buffer[4]);
  2182.  
  2183.     if (rot == 1)
  2184.         queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  2185.  
  2186.     kfree(buffer);
  2187. }
  2188.  
  2189. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  2190. {
  2191.     /*
  2192.      * Although VPD inquiries can go to SCSI-2 type devices,
  2193.      * some USB ones crash on receiving them, and the pages
  2194.      * we currently ask for are for SPC-3 and beyond
  2195.      */
  2196.     if (sdp->scsi_level > SCSI_SPC_2)
  2197.         return 1;
  2198.     return 0;
  2199. }
  2200.  
  2201. /**
  2202.  *  sd_revalidate_disk - called the first time a new disk is seen,
  2203.  *  performs disk spin up, read_capacity, etc.
  2204.  *  @disk: struct gendisk we care about
  2205.  **/
  2206. static int sd_revalidate_disk(struct gendisk *disk)
  2207. {
  2208.     struct scsi_disk *sdkp = scsi_disk(disk);
  2209.     struct scsi_device *sdp = sdkp->device;
  2210.     unsigned char *buffer;
  2211.     unsigned ordered;
  2212.  
  2213.     SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  2214.                       "sd_revalidate_disk\n"));
  2215.  
  2216.     /*
  2217.      * If the device is offline, don't try and read capacity or any
  2218.      * of the other niceties.
  2219.      */
  2220.     if (!scsi_device_online(sdp))
  2221.         goto out;
  2222.  
  2223.     buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  2224.     if (!buffer) {
  2225.         sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  2226.               "allocation failure.\n");
  2227.         goto out;
  2228.     }
  2229.  
  2230.     sd_spinup_disk(sdkp);
  2231.  
  2232.     /*
  2233.      * Without media there is no reason to ask; moreover, some devices
  2234.      * react badly if we do.
  2235.      */
  2236.     if (sdkp->media_present) {
  2237.         sd_read_capacity(sdkp, buffer);
  2238.  
  2239.         if (sd_try_extended_inquiry(sdp)) {
  2240.             sd_read_block_limits(sdkp);
  2241.             sd_read_block_characteristics(sdkp);
  2242.         }
  2243.  
  2244.         sd_read_write_protect_flag(sdkp, buffer);
  2245.         sd_read_cache_type(sdkp, buffer);
  2246.         sd_read_app_tag_own(sdkp, buffer);
  2247.     }
  2248.  
  2249.     sdkp->first_scan = 0;
  2250.  
  2251.     /*
  2252.      * We now have all cache related info, determine how we deal
  2253.      * with ordered requests.  Note that as the current SCSI
  2254.      * dispatch function can alter request order, we cannot use
  2255.      * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  2256.      */
  2257.     if (sdkp->WCE)
  2258.         ordered = sdkp->DPOFUA
  2259.             ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  2260.     else
  2261.         ordered = QUEUE_ORDERED_DRAIN;
  2262.  
  2263.     blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  2264.  
  2265.     set_capacity(disk, sdkp->capacity);
  2266.     kfree(buffer);
  2267.  
  2268.  out:
  2269.     return 0;
  2270. }
  2271. //Patch by QNAP: usb and sata device mapping
  2272. #ifdef CONFIG_MACH_QNAPTS
  2273. #define SATA_TYPE_6145 1
  2274. #define SATA_TYPE_7042 2
  2275. //Patch by QNAP: Port Multiplier
  2276. static int bus_map_index(int adapter_type,unsigned short host_no,int channel,int id,int lun);
  2277. int disk_slot_is_occupy(unsigned short host_no,int channel,int id,int lun);
  2278. int QNAP_disk_index(struct scsi_device *sdp);
  2279.  
  2280. /**
  2281.  * bus_map_index - called by disk_slot_is_occupy() and QNAP_disk_index()
  2282.  * adapter_type only use for TS509
  2283.  * Return disk_index if found or 0xff if fail.
  2284.  **/
  2285. static int bus_map_index(int adapter_type,unsigned short host_no,int channel,int id,int lun)
  2286. {
  2287.     int index = 0xff;
  2288.     if(channel < 0 || channel > MAX_ESATA_DISKS_BY_BUS)
  2289.         channel = 0;
  2290.  
  2291. //  printk(" host_no=%d, channel=%d, id=%d, lun=%d\n", host_no, channel, id, lun); // roylin test
  2292. #if defined(TS509)
  2293.     if(host_no == 0 && id == 0)
  2294.         index = 23;
  2295.     else if(adapter_type == SATA_TYPE_6145){
  2296.         if(host_no >= 2 && host_no <=3){
  2297.             switch(id){
  2298.             case 0:
  2299.                 index = 0;
  2300.                 break;
  2301.             case 5:
  2302.                 index = 1;
  2303.                 break;
  2304.             case 10:
  2305.                 index = 2;
  2306.                 break;
  2307.             case 15:
  2308.                 index = 3;
  2309.                 break;
  2310.             default:
  2311.                 printk("Unexpect id!!! Please check it.\n");
  2312.                 goto return_out;
  2313.             }
  2314.             index += (host_no-2)*4;
  2315.             if(host_no == 3 && id == 15)
  2316.                 index = ESATA_BUS2_START_INDEX + channel; //sdza
  2317.         }
  2318.     }
  2319.     else if(adapter_type == SATA_TYPE_7042){
  2320.         switch(host_no){
  2321.         case 2://sdd
  2322.             index = 3;
  2323.             break;
  2324.         case 3://sdc
  2325.             index = 2;
  2326.             break;
  2327.         case 4://sdb
  2328.             index = 1;
  2329.             break;
  2330.         case 5://sda
  2331.             index = 0;
  2332.             break;
  2333.         case 6://sdz
  2334.             index = ESATA_BUS2_START_INDEX + channel; // sdza
  2335.             break;
  2336.         case 9://sde
  2337.             index = 4;
  2338.             break;
  2339.         default:
  2340.             printk("Unexpect id!!! Please check it.\n");
  2341.             goto return_out;
  2342.         }
  2343.     }
  2344. #elif defined(TS609)
  2345.     if(host_no == 0 && id == 0)
  2346.         index = 23;
  2347.     else{
  2348.         switch(host_no){
  2349.             case 2:
  2350.             case 3:
  2351.             case 4:
  2352.             case 5:
  2353.             case 6:
  2354.             case 7:
  2355.                 index = host_no-2;
  2356.                 break;
  2357.             case 8:
  2358.                 index = ESATA_BUS2_START_INDEX + channel; // sdza
  2359.                 break;
  2360.             default:
  2361.                 printk("Unexpect id!!! Please check it.\n");
  2362.                 goto return_out;
  2363.         }
  2364.     }
  2365. #elif defined(TS809) || defined (TS809U)
  2366.     if(host_no == 0 && id == 0)
  2367.         index = 23;
  2368.     else{
  2369.         switch(host_no){
  2370.         case 2://sdd
  2371.             index = 3;
  2372.             break;
  2373.         case 3://sdc
  2374.             index = 2;
  2375.             break;
  2376.         case 4://sdb
  2377.             index = 1;
  2378.             break;
  2379.         case 5://sda
  2380.             index = 0;
  2381.             break;
  2382.         case 6://sdh
  2383.             index = 7;
  2384.             break;
  2385.         case 7://sdg
  2386.             index = 6;
  2387.             break;
  2388.         case 8://sdf
  2389.             index = 5;
  2390.             break;
  2391.         case 9://sde
  2392.             index = 4;
  2393.             break;
  2394.         default:
  2395.             printk("Unexpect id!!! Please check it.\n");
  2396.             goto return_out;
  2397.         }
  2398.     }
  2399. //Patch by QNAP: Port Multiplier
  2400. #elif defined(TS259)
  2401.     //Patch by QNAP: add x59 derivative model
  2402.     if(is_x59_proII_model()){
  2403.         if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2404.             index = ESATA_BUS1_START_INDEX + channel;
  2405.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2406.             index = ESATA_BUS2_START_INDEX + channel;      
  2407.         else if(host_no>=6 && host_no<=7)
  2408.             index = host_no - 6;
  2409.     }
  2410.     else{
  2411.     if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2412.         index = ESATA_BUS1_START_INDEX + channel;
  2413.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2414.         index = ESATA_BUS2_START_INDEX + channel;
  2415.     else if(host_no>=2 && host_no<=3)
  2416.         index = 3 - host_no;
  2417.     }
  2418. #elif defined(TS459)
  2419.     //Patch by QNAP: add x59 derivative model
  2420.     if(is_x59_proII_model()){
  2421.         if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2422.             index = ESATA_BUS1_START_INDEX + channel;
  2423.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2424.             index = ESATA_BUS2_START_INDEX + channel;      
  2425.         else if(host_no>=6 && host_no<=9)
  2426.             index = host_no - 6;
  2427.     }
  2428.     else{
  2429.         if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2430.             index = ESATA_BUS1_START_INDEX + channel;
  2431.         else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2432.             index = ESATA_BUS2_START_INDEX + channel;
  2433.         else if(host_no>=2 && host_no<=5)
  2434.             index = 5 - host_no;
  2435.     }
  2436.     /////////////////////////////////////
  2437. #elif defined(TS459U)
  2438.     //Patch by QNAP: add x59 derivative model
  2439.     if(is_x59_proII_model()){
  2440.         if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2441.             index = ESATA_BUS1_START_INDEX + channel;
  2442.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2443.             index = ESATA_BUS2_START_INDEX + channel;      
  2444.         else if(host_no>=6 && host_no<=9)
  2445.             index = host_no - 6;
  2446.     }
  2447.     else{
  2448.         if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2449.             index = ESATA_BUS1_START_INDEX + channel;
  2450.         else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2451.             index = ESATA_BUS2_START_INDEX + channel;
  2452.         else if(host_no>=2 && host_no<=5)
  2453.             index = host_no - 2;
  2454.     }            
  2455. #elif defined(TS559)
  2456.     //Patch by QNAP: add x59 derivative model
  2457.     if(is_x59_proII_model() ){
  2458.         if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2459.             index = ESATA_BUS1_START_INDEX + channel;
  2460.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2461.             index = ESATA_BUS2_START_INDEX + channel;
  2462.         else if(host_no>=6 && host_no<=10)
  2463.             index = host_no - 6;
  2464.     }
  2465.     else{
  2466.         if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2467.             index = ESATA_BUS1_START_INDEX + channel;
  2468.         else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2469.             index = ESATA_BUS2_START_INDEX + channel;  
  2470.         else if(host_no>=0 && host_no<=5)
  2471.             index = 5 - host_no;
  2472.     }            
  2473.     ///////////////////////////////////////////    
  2474. #elif defined(TS659)
  2475.     //Patch by QNAP: add x59 derivative model
  2476.     if(is_x59_proII_model()){
  2477.         if(host_no == 4 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2478.             index = ESATA_BUS1_START_INDEX + channel;
  2479.         else if(host_no == 5 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2480.             index = ESATA_BUS2_START_INDEX + channel;
  2481.         else if(host_no>=6 && host_no<=11)
  2482.             index = host_no - 6;
  2483.     }
  2484.     else{
  2485.         if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2486.             index = ESATA_BUS1_START_INDEX + channel;
  2487.         else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2488.             index = ESATA_BUS2_START_INDEX + channel;
  2489.         else if(host_no>=0 && host_no<=5)
  2490.             index = 5 - host_no;
  2491.     }
  2492.     ///////////////////////////////////////////    
  2493. #elif defined (TS859)
  2494.     if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2495.         index = ESATA_BUS1_START_INDEX + channel;
  2496.     else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2497.         index = ESATA_BUS2_START_INDEX + channel;
  2498.     else if(host_no>=2 && host_no<=5)
  2499.         index = 5 - host_no;
  2500.     else if(host_no>=8 && host_no<=11)
  2501.         index = host_no-4;
  2502. #elif defined (TS859U)
  2503.     if(host_no == 6 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2504.         index = ESATA_BUS1_START_INDEX + channel;
  2505.     else if(host_no == 7 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2506.         index = ESATA_BUS2_START_INDEX + channel;
  2507.     else if(host_no>=2 && host_no<=5)
  2508.         index = host_no + 2;
  2509.     else if(host_no>=8 && host_no<=11)
  2510.         index = 3 - (host_no-8);
  2511. #elif defined(TS239)
  2512.     if(host_no == 4 && id == 0)
  2513.         index = 23;
  2514. #if 0
  2515.     else if(host_no >= 8 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1 // roylin test
  2516.         index = ESATA_BUS1_START_INDEX + channel + id;
  2517. #else
  2518.     else if(host_no == 8 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2519.         index = ESATA_BUS1_START_INDEX + channel + id;
  2520.     else if(host_no == 9 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2521.         index = ESATA_BUS2_START_INDEX + channel + id;
  2522. #endif
  2523.     else if(host_no == 6 && id == 0)
  2524.         index = 0; //sda
  2525.     else if(host_no == 7 && id == 0)
  2526.         index = 1; //sdb
  2527.     else
  2528.         printk("Unexpect id!!! Please check it.\n");
  2529. #elif defined(TS439) || defined(TS639) || defined (TS439U)
  2530.     if(host_no == 4 && id == 0)
  2531.         index = 23;
  2532.     else if(host_no == 0 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2533.         index = ESATA_BUS1_START_INDEX + channel;
  2534.     else if(host_no == 2 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2535.         index = ESATA_BUS2_START_INDEX + channel;
  2536.     else if(host_no >= 6 && host_no <= 11)
  2537.         index = host_no - 6;
  2538. #elif defined(SS439)
  2539.     if(host_no == 4 && id == 0)
  2540.         index = 23;
  2541.     else if(host_no == 0 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2542.         index = ESATA_BUS1_START_INDEX + channel;
  2543.     else if(host_no == 2 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2544.         index = ESATA_BUS2_START_INDEX + channel;
  2545.     else if(host_no >= 6 && host_no <= 9)
  2546.         index = 3 - (host_no - 6);
  2547. #elif defined(TS839)
  2548.     if(host_no == 4 && id == 0)
  2549.         index = 23;
  2550.     else if(host_no == 0 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2551.         index = ESATA_BUS1_START_INDEX + channel;
  2552.     else if(host_no == 2 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2553.         index = ESATA_BUS2_START_INDEX + channel;
  2554.     else{
  2555.         switch(host_no){
  2556.             case 13:
  2557.                 index = 0;
  2558.                 break;
  2559.             case 12:
  2560.                 index = 1;
  2561.                 break;
  2562.             case 11:
  2563.                 index = 2;
  2564.                 break;
  2565.             case 10:
  2566.                 index = 3;
  2567.                 break;
  2568.             case 6:
  2569.                 index = 4;
  2570.                 break;
  2571.             case 7:
  2572.                 index = 5;
  2573.                 break;
  2574.             case 8:
  2575.                 index = 6;
  2576.                 break;
  2577.             case 9:
  2578.                 index = 7;
  2579.                 break;
  2580.         }
  2581.     }
  2582. #elif defined(SS839)
  2583.     if(host_no == 4 && id == 0)
  2584.         index = 23;
  2585.     else if(host_no == 0 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2586.         index = ESATA_BUS1_START_INDEX + channel;
  2587.     else if(host_no == 2 && channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2588.         index = ESATA_BUS2_START_INDEX + channel;
  2589.     else{
  2590.         switch(host_no){
  2591.             case 10:
  2592.                 index = 0;
  2593.                 break;
  2594.             case 11:
  2595.                 index = 1;
  2596.                 break;
  2597.             case 12:
  2598.                 index = 2;
  2599.                 break;
  2600.             case 13:
  2601.                 index = 3;
  2602.                 break;
  2603.             case 9:
  2604.                 index = 4;
  2605.                 break;
  2606.             case 8:
  2607.                 index = 5;
  2608.                 break;
  2609.             case 7:
  2610.                 index = 6;
  2611.                 break;
  2612.             case 6:
  2613.                 index = 7;
  2614.                 break;
  2615.         }
  2616.     }
  2617. #elif defined(TS879) || defined(TS1079)
  2618.     switch(host_no){
  2619.         case 10:
  2620.             index = 0;
  2621.             break;
  2622.         case 11:
  2623.             index = 1;
  2624.             break;
  2625.         case 8:
  2626.             index = 2;
  2627.             break;
  2628.         case 9:
  2629.             index = 3;
  2630.             break;
  2631.         case 6:
  2632.             index = 4;
  2633.             break;
  2634.         case 7:
  2635.             index = 5;
  2636.             break;
  2637.         case 1:
  2638.             index = 6;
  2639.             break;
  2640.         case 0:
  2641.             index = 7;
  2642.             break;
  2643.         case 12:
  2644.             index = 8;
  2645.             break;
  2646.         case 13:
  2647.             index = 9;
  2648.             break;
  2649.         case 4:
  2650.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2651.                 index = ESATA_BUS1_START_INDEX + channel;
  2652.             break;
  2653.         case 5:
  2654.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2655.                 index = ESATA_BUS2_START_INDEX + channel;
  2656.             break;
  2657.     }
  2658. #elif defined(TS879U)
  2659.     switch(host_no){
  2660.         case 6:
  2661.             index = 0;
  2662.             break;
  2663.         case 7:
  2664.             index = 1;
  2665.             break;
  2666.         case 1:
  2667.             index = 2;
  2668.             break;
  2669.         case 0:
  2670.             index = 3;
  2671.             break;
  2672.         case 8:
  2673.             index = 4;
  2674.             break;
  2675.         case 9:
  2676.             index = 5;
  2677.             break;
  2678.         case 10:
  2679.             index = 6;
  2680.             break;
  2681.         case 11:
  2682.             index = 7;
  2683.             break;
  2684.         case 4:
  2685.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2686.                 index = ESATA_BUS1_START_INDEX + channel;
  2687.             break;
  2688.         case 5:
  2689.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2690.                 index = ESATA_BUS2_START_INDEX + channel;
  2691.             break;
  2692.     }
  2693. #elif defined(TS1259U)
  2694.     switch(host_no){
  2695.         case 3:
  2696.             index = 0;
  2697.             break;
  2698.         case 2:
  2699.             index = 1;
  2700.             break;
  2701.         case 1:
  2702.             index = 2;
  2703.             break;
  2704.         case 0:
  2705.             index = 3;
  2706.             break;
  2707.         case 6:
  2708.             index = 4;
  2709.             break;
  2710.         case 9:
  2711.             index = 5;
  2712.             break;
  2713.         case 10:
  2714.             index = 6;
  2715.             break;
  2716.         case 13:
  2717.             index = 7;
  2718.             break;
  2719.         case 7:
  2720.             index = 8;
  2721.             break;
  2722.         case 8:
  2723.             index = 9;
  2724.             break;
  2725.         case 11:
  2726.             index = 10;
  2727.             break;
  2728.         case 12:
  2729.             index = 11;
  2730.             break;
  2731.         case 4:
  2732.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2733.                 index = ESATA_BUS1_START_INDEX + channel;
  2734.             break;
  2735.         case 5:
  2736.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2737.                 index = ESATA_BUS2_START_INDEX + channel;
  2738.             break;
  2739.     }
  2740. #elif defined(TS1279U)
  2741.     switch(host_no){
  2742.         case 6:
  2743.             index = 0;
  2744.             break;
  2745.         case 7:
  2746.             index = 1;
  2747.             break;
  2748.         case 1:
  2749.             index = 2;
  2750.             break;
  2751.         case 0:
  2752.             index = 3;
  2753.             break;
  2754.         case 10:
  2755.             index = 4;
  2756.             break;
  2757.         case 11:
  2758.             index = 5;
  2759.             break;
  2760.         case 12:
  2761.             index = 6;
  2762.             break;
  2763.         case 13:
  2764.             index = 7;
  2765.             break;
  2766.         case 8:
  2767.             index = 8;
  2768.             break;
  2769.         case 9:
  2770.             index = 9;
  2771.             break;
  2772.         case 14:
  2773.             index = 10;
  2774.             break;
  2775.         case 15:
  2776.             index = 11;
  2777.             break;
  2778.         case 4:
  2779.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-1
  2780.                 index = ESATA_BUS1_START_INDEX + channel;
  2781.             break;
  2782.         case 5:
  2783.             if(channel < MAX_ESATA_DISKS_BY_BUS)//ESATA-2
  2784.                 index = ESATA_BUS2_START_INDEX + channel;
  2785.             break;
  2786.     }
  2787. #endif
  2788. return_out:
  2789.     return index;
  2790. }
  2791.  
  2792. /**
  2793.  * disk_slot_is_occupy - called by sata driver to detect slot is free or not
  2794.  * Return 1 if occupy or 0 if empty
  2795.  **/
  2796. int disk_slot_is_occupy(unsigned short host_no,int channel,int id,int lun)
  2797. {
  2798.     int index = bus_map_index(0,host_no,channel,id,lun);
  2799.     int occupy = 0;
  2800.     if(Is_ESATA_Bus1_Index(index)){ //sdya ~
  2801.         occupy = esata_bus1_dev_arr[index - ESATA_BUS1_START_INDEX];
  2802.     }
  2803.     else if(Is_ESATA_Bus2_Index(index)){ //sdza ~
  2804.         occupy = esata_bus2_dev_arr[index - ESATA_BUS2_START_INDEX];
  2805.     }
  2806.     else if(index == 0xff){
  2807.         for(index=0;index < 8;index++){
  2808.             if(!sd_dev_arr[index])
  2809.                 break;
  2810.         }
  2811.         if(index == 8)
  2812.             occupy = 1;
  2813.         else
  2814.             occupy = 0;
  2815.     }
  2816.     else
  2817.         occupy = sd_dev_arr[index];
  2818.     return occupy;
  2819. }
  2820. EXPORT_SYMBOL(disk_slot_is_occupy);
  2821. #ifdef NAS_VIRTUAL_EX
  2822. static int QNAP_get_iscsi_free_slot(const char *iqn)
  2823. {
  2824.     int i;
  2825.     if (!iqn || !*iqn)
  2826.         return -1;
  2827.     for (i = 0; i < MAX_ISCSI_DISK; i ++) {
  2828.         if (iscsi_dev_arr[i] == 0) {
  2829.             return i;
  2830.         }
  2831.     }
  2832.     return -1;
  2833. }
  2834. #endif
  2835. /**
  2836.  * QNAP_disk_index - called by sd_probe in sd.c and sg_alloc in sg.c
  2837.  * Return disk_index if found or 0xff
  2838.  **/
  2839. int QNAP_disk_index(struct scsi_device *sdp)
  2840. {
  2841.     unsigned short host_no = sdp->host->host_no;
  2842.     unsigned int channel = sdp->channel;
  2843.     unsigned int id = sdp->id;
  2844.     unsigned int lun = sdp->lun;
  2845.     int adapter_type = 0;
  2846. #if defined(TS509)
  2847.     struct scsi_host_template *hostt = sdp->host->hostt;
  2848. #endif
  2849. #ifdef NAS_VIRTUAL
  2850.     int index = 0xff;
  2851.     struct scsi_host_template *iscsi_hostt = sdp->host->hostt;
  2852.     printk("Check proc_name[%s].\n", iscsi_hostt->proc_name);
  2853.     if (!strcmp(iscsi_hostt->proc_name, "iscsi_tcp")) {
  2854. #ifdef NAS_VIRTUAL_EX
  2855.         int idx;
  2856.         if ((idx = QNAP_get_iscsi_free_slot(qnap_iqn_node)) >= 0) {
  2857.             index = Get_iSCSI_Index('w', ('a'+idx));    //sdwX
  2858.             printk("qnap_iqn_node=%s.\n", qnap_iqn_node);
  2859.         }
  2860.         else {
  2861. #endif
  2862.             if (strlen(qnap_disk_node) >= 4 && strncmp(qnap_disk_node, "sd", 2) == 0)
  2863.                 index = Get_iSCSI_Index(qnap_disk_node[2], qnap_disk_node[3]);
  2864. #ifdef NAS_VIRTUAL_EX
  2865.         }
  2866. #endif
  2867.         if (!Is_iSCSI_Index(index)) {
  2868.             return -1;
  2869.         }
  2870.         printk("Get sd index %d.\n", index);
  2871.         return index;
  2872.     }
  2873. #endif
  2874. #if defined(TS509)
  2875.     if(!strcmp(hostt->proc_name,"mvSata")){
  2876.         //MV7042
  2877.         adapter_type = SATA_TYPE_7042;
  2878.     }
  2879.     else if(!strcmp(hostt->proc_name,"mv61xx")){
  2880.         //MV6145
  2881.         adapter_type = SATA_TYPE_6145;
  2882.     }
  2883.     printk("TS509 SATA TYPE is %d\n",adapter_type);
  2884. #endif
  2885.     return bus_map_index(adapter_type,host_no,channel,id,lun);
  2886. }
  2887.  
  2888. #endif
  2889. ///////////////////////////////////////
  2890.  
  2891. /**
  2892.  *  sd_format_disk_name - format disk name
  2893.  *  @prefix: name prefix - ie. "sd" for SCSI disks
  2894.  *  @index: index of the disk to format name for
  2895.  *  @buf: output buffer
  2896.  *  @buflen: length of the output buffer
  2897.  *
  2898.  *  SCSI disk names starts at sda.  The 26th device is sdz and the
  2899.  *  27th is sdaa.  The last one for two lettered suffix is sdzz
  2900.  *  which is followed by sdaaa.
  2901.  *
  2902.  *  This is basically 26 base counting with one extra 'nil' entry
  2903.  *  at the beggining from the second digit on and can be
  2904.  *  determined using similar method as 26 base conversion with the
  2905.  *  index shifted -1 after each digit is computed.
  2906.  *
  2907.  *  CONTEXT:
  2908.  *  Don't care.
  2909.  *
  2910.  *  RETURNS:
  2911.  *  0 on success, -errno on failure.
  2912.  */
  2913. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  2914. {
  2915.     const int base = 'z' - 'a' + 1;
  2916.     char *begin = buf + strlen(prefix);
  2917.     char *end = buf + buflen;
  2918.     char *p;
  2919.     int unit;
  2920.  
  2921.     p = end - 1;
  2922.     *p = '\0';
  2923.     unit = base;
  2924.     do {
  2925.         if (p == begin)
  2926.             return -EINVAL;
  2927.         *--p = 'a' + (index % unit);
  2928.         index = (index / unit) - 1;
  2929.     } while (index >= 0);
  2930.  
  2931.     memmove(begin, p, end - p);
  2932.     memcpy(buf, prefix, strlen(prefix));
  2933.  
  2934.     return 0;
  2935. }
  2936. #ifdef CONFIG_MACH_QNAPTS
  2937. //Patch by QNAP: USB port
  2938. static void qnap_set_usb_hub_table(int mainport, int hub_port,int havehub,int speed)
  2939. {
  2940.     if(havehub){
  2941.         int usb_hub_port;
  2942.         usb_hub_port = 1<<(4+hub_port);
  2943.         usbdev_exist[mainport] |= usb_hub_port;
  2944.         usbdev_status_changed[mainport] |= usb_hub_port;
  2945.         if(speed==USB_HIGH_SPEED)
  2946.             usbdev_speedtype[mainport] |= usb_hub_port;
  2947.     }
  2948.     else{
  2949.         usbdev_exist[mainport]=USB_PORT;
  2950.         usbdev_status_changed[mainport]=USB_PORT;
  2951.         if(speed==USB_HIGH_SPEED)
  2952.             usbdev_speedtype[mainport] |= USB_PORT;
  2953.     }  
  2954. }
  2955. ////////////////////////////////////////////////
  2956. #endif
  2957. /*
  2958.  * The asynchronous part of sd_probe
  2959.  */
  2960. static void sd_probe_async(void *data, async_cookie_t cookie)
  2961. {
  2962.     struct scsi_disk *sdkp = data;
  2963.     struct scsi_device *sdp;
  2964.     struct gendisk *gd;
  2965.     u32 index;
  2966.     struct device *dev;
  2967. //Patch by QNAP: usb and sata device mapping
  2968. #ifdef CONFIG_MACH_QNAPTS
  2969.     int usb_port=0, havehub=0;
  2970.     int hub_port=0, speed=0, mainport=0;
  2971. #endif 
  2972.     sdp = sdkp->device;
  2973. //Patch by QNAP: usb and sata device mapping
  2974. #ifdef CONFIG_MACH_QNAPTS
  2975.     usb_port = usb_get_port(sdp->host->shost_gendev.parent);
  2976.     havehub = usb_have_hub(sdp->host->shost_gendev.parent);
  2977.     speed = usb_get_speed(sdp->host->shost_gendev.parent);
  2978. #endif
  2979. ////////////////////////////////////////////////////////
  2980.     gd = sdkp->disk;
  2981.     index = sdkp->index;
  2982.     dev = &sdp->sdev_gendev;
  2983.  
  2984.     if (index < SD_MAX_DISKS) {
  2985.         gd->major = sd_major((index & 0xf0) >> 4);
  2986.         gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  2987.         gd->minors = SD_MINORS;
  2988.     }
  2989.     gd->fops = &sd_fops;
  2990.     gd->private_data = &sdkp->driver;
  2991.     gd->queue = sdkp->device->request_queue;
  2992.  
  2993.     /* defaults, until the device tells us otherwise */
  2994.     sdp->sector_size = 512;
  2995.     sdkp->capacity = 0;
  2996.     sdkp->media_present = 1;
  2997.     sdkp->write_prot = 0;
  2998.     sdkp->WCE = 0;
  2999.     sdkp->RCD = 0;
  3000.     sdkp->ATO = 0;
  3001.     sdkp->first_scan = 1;
  3002.  
  3003.     sd_revalidate_disk(gd);
  3004.  
  3005.     blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
  3006.  
  3007.     gd->driverfs_dev = &sdp->sdev_gendev;
  3008.     gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
  3009.     if (sdp->removable)
  3010.         gd->flags |= GENHD_FL_REMOVABLE;
  3011.  
  3012.     dev_set_drvdata(dev, sdkp);
  3013.     add_disk(gd);
  3014.     sd_dif_config_host(sdkp);
  3015.  
  3016.     sd_revalidate_disk(gd);
  3017.  
  3018.     sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  3019.           sdp->removable ? "removable " : "");
  3020.          
  3021.     put_device(&sdkp->dev);
  3022. //Patch by QNAP: usb and sata device mapping
  3023. //add by avery
  3024. //printk("sd_probe_async :speed = %d\n",speed);
  3025. //printk("sd_probe_async :major = %d\n",gd->major);
  3026. //printk("sd_probe_async :first_minor = %d\n",gd->first_minor);
  3027. //////////////////
  3028. #ifdef CONFIG_MACH_QNAPTS
  3029.     switch(index){
  3030. #if defined(X86_SANDYBRIDGE) || defined(TS1259U)
  3031.         case 0 ... 15:
  3032.             send_message_to_app(QNAP_IOCTL_SATA_UP + index);        
  3033.             break;
  3034. #else
  3035.         case 0:
  3036.             send_message_to_app(QNAP_SATA_UP);
  3037.             break;
  3038.         case 1:
  3039.             send_message_to_app(QNAP_SATA2_UP);
  3040.             break;
  3041.         case 2:
  3042.             send_message_to_app(QNAP_SATA3_UP);
  3043.             break;
  3044.         case 3:
  3045.             send_message_to_app(QNAP_SATA4_UP);
  3046.             break;
  3047.         case 4:
  3048.             send_message_to_app(QNAP_SATA5_UP);
  3049.             break;
  3050.         case 5:
  3051.             send_message_to_app(QNAP_SATA6);
  3052.             break;
  3053.         case 6:
  3054.             send_message_to_app(QNAP_SATA7);
  3055.             break;
  3056.         case 7:
  3057.             send_message_to_app(QNAP_SATA8_UP);
  3058.             break;
  3059.         case 8:
  3060.         case 9:
  3061.         case 10:
  3062.         case 11:
  3063.         case 12:
  3064.         case 13:
  3065.         case 14:
  3066.         case 15:
  3067.             mainport=PORT1;
  3068.             if(havehub){
  3069.                 hub_port = usb_port-USB_FRONT_PORT;
  3070.                 if(hub_port==0){
  3071.                     usbdev_exist[mainport] |= USB_HUB_PORT1;
  3072.                     usbdev_status_changed[mainport] |= USB_HUB_PORT1;
  3073.                     if(speed==USB_HIGH_SPEED)
  3074.                         usbdev_speedtype[mainport] |= USB_HUB_PORT1;
  3075.                 }
  3076.                 else if(hub_port==1){
  3077.                     usbdev_exist[mainport]|=USB_HUB_PORT2;
  3078.                     usbdev_status_changed[mainport] |= USB_HUB_PORT2;
  3079.                     if(speed==USB_HIGH_SPEED)
  3080.                         usbdev_speedtype[mainport] |= USB_HUB_PORT2;
  3081.                 }
  3082.                 else if(hub_port==2){
  3083.                     usbdev_exist[mainport]|=USB_HUB_PORT3;
  3084.                     usbdev_status_changed[mainport] |= USB_HUB_PORT3;
  3085.                     if(speed==USB_HIGH_SPEED)
  3086.                         usbdev_speedtype[mainport] |= USB_HUB_PORT3;
  3087.                 }
  3088.                 else if(hub_port==3){
  3089.                     usbdev_exist[mainport]|=USB_HUB_PORT4;
  3090.                     usbdev_status_changed[mainport] |= USB_HUB_PORT4;
  3091.                     if(speed==USB_HIGH_SPEED)
  3092.                         usbdev_speedtype[mainport] |= USB_HUB_PORT4;
  3093.                 }
  3094.                 else if(hub_port==4){
  3095.                     usbdev_exist[mainport]|=USB_HUB_PORT5;
  3096.                     usbdev_status_changed[mainport] |= USB_HUB_PORT5;
  3097.                     if(speed==USB_HIGH_SPEED)
  3098.                         usbdev_speedtype[mainport] |= USB_HUB_PORT5;
  3099.                 }
  3100.                 else if(hub_port==5){
  3101.                     usbdev_exist[mainport]|=USB_HUB_PORT6;
  3102.                     usbdev_status_changed[mainport] |= USB_HUB_PORT6;
  3103.                     if(speed==USB_HIGH_SPEED)
  3104.                         usbdev_speedtype[mainport] |= USB_HUB_PORT6;
  3105.                 }
  3106.                 else if(hub_port==6){
  3107.                     usbdev_exist[mainport]|=USB_HUB_PORT7;
  3108.                     usbdev_status_changed[mainport] |= USB_HUB_PORT7;
  3109.                     if(speed==USB_HIGH_SPEED)
  3110.                         usbdev_speedtype[mainport] |= USB_HUB_PORT7;
  3111.                 }
  3112.                 else if(hub_port==7){
  3113.                     usbdev_exist[mainport]|=USB_HUB_PORT8;
  3114.                     usbdev_status_changed[mainport] |= USB_HUB_PORT8;
  3115.                     if(speed==USB_HIGH_SPEED)
  3116.                         usbdev_speedtype[mainport] |= USB_HUB_PORT8;
  3117.                 }
  3118.                 else{
  3119.                     printk("Send TS over limited\n");
  3120.                 }
  3121.             }
  3122.             else{
  3123.                 usbdev_exist[mainport]=USB_PORT;
  3124.                 usbdev_status_changed[mainport]=USB_PORT;
  3125.                 if(speed==USB_HIGH_SPEED)
  3126.                     usbdev_speedtype[mainport] |= USB_PORT;
  3127.             }
  3128.             send_message_to_app(QNAP_USB_FRONT_UP);
  3129.             break;
  3130. #endif         
  3131. //Patch by QNAP: add x59 derivative model
  3132.         case 16:
  3133.             mainport=PORT6;
  3134.             usbdev_exist[mainport]=USB_PORT;
  3135.             usbdev_status_changed[mainport]=USB_PORT;
  3136.             if(speed==USB_HIGH_SPEED)
  3137.                 usbdev_speedtype[mainport] |= USB_PORT;        
  3138.             send_message_to_app(QNAP_USB_BACK5_UP);
  3139.             break;
  3140.         case 17:
  3141.             mainport=PORT7;
  3142.             usbdev_exist[mainport]=USB_PORT;
  3143.             usbdev_status_changed[mainport]=USB_PORT;
  3144.             if(speed==USB_HIGH_SPEED)
  3145.                 usbdev_speedtype[mainport] |= USB_PORT;        
  3146.             send_message_to_app(QNAP_USB_BACK6_UP);
  3147.             break;
  3148. /////////////////////////////////////            
  3149.         case 18:
  3150.                         //Patch by QNAP: USB Port Multiplier
  3151.             mainport=PORT2;
  3152.             usbdev_exist[mainport]=USB_PORT;
  3153.             usbdev_status_changed[mainport]=USB_PORT;
  3154.             if(speed==USB_HIGH_SPEED)
  3155.                 usbdev_speedtype[mainport] |= USB_PORT;        
  3156.             send_message_to_app(QNAP_USB_BACK1_UP);
  3157.             break;
  3158.         case 19:
  3159.                         //Patch by QNAP: USB Port Multiplier
  3160.             mainport=PORT3;
  3161.             usbdev_exist[mainport]=USB_PORT;
  3162.             usbdev_status_changed[mainport]=USB_PORT;
  3163.             if(speed==USB_HIGH_SPEED)
  3164.                 usbdev_speedtype[mainport] |= USB_PORT;            
  3165.             send_message_to_app(QNAP_USB_BACK2_UP);
  3166.             break;
  3167.         case 20:   
  3168.                         //Patch by QNAP: USB Port Multiplier   
  3169.             mainport=PORT4;
  3170.             usbdev_exist[mainport]=USB_PORT;
  3171.             usbdev_status_changed[mainport]=USB_PORT;
  3172.             if(speed==USB_HIGH_SPEED)
  3173.                 usbdev_speedtype[mainport] |= USB_PORT;            
  3174.             send_message_to_app(QNAP_USB_BACK3_UP);
  3175.             break;
  3176.         case 21:
  3177.                         //Patch by QNAP: USB Port Multiplier
  3178.             mainport=PORT5;
  3179.             usbdev_exist[mainport]=USB_PORT;
  3180.             usbdev_status_changed[mainport]=USB_PORT;
  3181.             if(speed==USB_HIGH_SPEED)
  3182.                 usbdev_speedtype[mainport] |= USB_PORT;            
  3183.             send_message_to_app(QNAP_USB_BACK4_UP);
  3184.             break;
  3185.         case 23:
  3186.             //For X86_PINEVIEW
  3187.             break;
  3188.         case 24:
  3189.             send_message_to_app(QNAP_ESATA_UP);
  3190.             break;
  3191.         case 25:
  3192.             send_message_to_app(QNAP_ESATA2_UP);
  3193.             break;
  3194.         default:
  3195. #ifdef NAS_VIRTUAL
  3196.             if (Is_iSCSI_Index(index)) {
  3197.                 send_message_to_app(QNAP_ISCSI_UP);
  3198.                 break;
  3199.             }
  3200.             else
  3201. #endif
  3202. //Patch by QNAP: Port Multiplier
  3203.             if (Is_USB_Front_Port_Index(index)){
  3204.                 hub_port = index -USB_FRONT_PORT_START_INDEX;
  3205.                 qnap_set_usb_hub_table(PORT1,hub_port,havehub,speed);
  3206.                 //do not sent message in hub case
  3207.                 //send_message_to_app(QNAP_USB_FRONT_UP);
  3208.             }else if (Is_USB_Back_Port1_Index(index)){
  3209.                 hub_port = index -USB_BACK_PORT_1_START_INDEX;
  3210.                 qnap_set_usb_hub_table(PORT2,hub_port,havehub,speed);
  3211.                 //do not sent message in hub case
  3212.                 //send_message_to_app(QNAP_USB_BACK1_UP);
  3213.                 break;
  3214.             }else if (Is_USB_Back_Port2_Index(index)){
  3215.                 hub_port = index -USB_BACK_PORT_2_START_INDEX;
  3216.                 qnap_set_usb_hub_table(PORT3,hub_port,havehub,speed);          
  3217.                 //do not sent message in hub case
  3218.                 //send_message_to_app(QNAP_USB_BACK2_UP);
  3219.                 break; 
  3220.             }else if (Is_USB_Back_Port3_Index(index)){
  3221.                 hub_port = index -USB_BACK_PORT_3_START_INDEX;
  3222.                 qnap_set_usb_hub_table(PORT4,hub_port,havehub,speed);              
  3223.                 //do not sent message in hub case
  3224.                 //send_message_to_app(QNAP_USB_BACK3_UP);
  3225.                 break;
  3226.             }else if (Is_USB_Back_Port4_Index(index)){
  3227.                 hub_port = index -USB_BACK_PORT_4_START_INDEX;
  3228.                 qnap_set_usb_hub_table(PORT5,hub_port,havehub,speed);              
  3229.                 //do not sent message in hub case
  3230.                 //send_message_to_app(QNAP_USB_BACK4_UP);
  3231.                 break;
  3232.             }else if (Is_USB_Back_Port5_Index(index)){
  3233.                 hub_port = index -USB_BACK_PORT_5_START_INDEX;
  3234.                 qnap_set_usb_hub_table(PORT6,hub_port,havehub,speed);              
  3235.                 //do not sent message in hub case
  3236.                 //send_message_to_app(QNAP_USB_BACK5_UP);
  3237.                 break;
  3238.             }else if (Is_USB_Back_Port6_Index(index)){
  3239.                 hub_port = index -USB_BACK_PORT_6_START_INDEX;
  3240.                 qnap_set_usb_hub_table(PORT7,hub_port,havehub,speed);              
  3241.                 //do not sent message in hub case
  3242.                 //send_message_to_app(QNAP_USB_BACK6_UP);
  3243.                 break;
  3244.             }else  
  3245. /////////////////
  3246. //Patch by QNAP: Port Multiplier
  3247.             if(Is_ESATA_Bus1_Index(index)){ //sdya ~
  3248.                 send_message_to_app(QNAP_IOCTL_ESATA_UP + (index - ESATA_BUS1_START_INDEX));
  3249.                 break;
  3250.             }
  3251.             else if(Is_ESATA_Bus2_Index(index)){ //sdza ~
  3252.                 send_message_to_app(QNAP_IOCTL_ESATA_UP + 0x10 + (index - ESATA_BUS2_START_INDEX));
  3253.                 break;
  3254.             }
  3255.             else
  3256. /////////////////
  3257.                 printk("sd: Unknown port.\n");
  3258.             break;
  3259.     }
  3260.  
  3261. #endif
  3262.     return;
  3263.  
  3264. }
  3265.  
  3266. /**
  3267.  *  sd_probe - called during driver initialization and whenever a
  3268.  *  new scsi device is attached to the system. It is called once
  3269.  *  for each scsi device (not just disks) present.
  3270.  *  @dev: pointer to device object
  3271.  *
  3272.  *  Returns 0 if successful (or not interested in this scsi device
  3273.  *  (e.g. scanner)); 1 when there is an error.
  3274.  *
  3275.  *  Note: this function is invoked from the scsi mid-level.
  3276.  *  This function sets up the mapping between a given
  3277.  *  <host,channel,id,lun> (found in sdp) and new device name
  3278.  *  (e.g. /dev/sda). More precisely it is the block device major
  3279.  *  and minor number that is chosen here.
  3280.  *
  3281.  *  Assume sd_attach is not re-entrant (for time being)
  3282.  *  Also think about sd_attach() and sd_remove() running coincidentally.
  3283.  **/
  3284. static int sd_probe(struct device *dev)
  3285. {
  3286.     struct scsi_device *sdp = to_scsi_device(dev);
  3287.     struct scsi_disk *sdkp;
  3288.     struct gendisk *gd;
  3289.     u32 index;
  3290.     int error;
  3291. //Patch by QNAP: usb and sata device mapping
  3292. #ifdef CONFIG_MACH_QNAPTS
  3293.     int usb_port=0;
  3294.     usb_port = usb_get_port(sdp->host->shost_gendev.parent);
  3295. #endif
  3296. /////////////////////////////////////////////////
  3297.     error = -ENODEV;
  3298.     if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  3299.         goto out;
  3300.  
  3301.     SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  3302.                     "sd_attach\n"));
  3303.  
  3304.     error = -ENOMEM;
  3305.     sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  3306.     if (!sdkp)
  3307.         goto out;
  3308.  
  3309.     gd = alloc_disk(SD_MINORS);
  3310.     if (!gd)
  3311.         goto out_free;
  3312. //Patch by QNAP: usb and sata device mapping
  3313. #ifdef CONFIG_MACH_QNAPTS
  3314.     spin_lock(&sd_index_lock);
  3315.     if(usb_port > 0){
  3316.         index = usb_port;
  3317.         if(index >= USB_FRONT_PORT){
  3318.             for(;index<26;index++){
  3319.                 if(!sd_dev_arr[index]){ // Need to skip E-SATA
  3320.                     break;
  3321.                 }
  3322.             }
  3323.             if(usb_port >= 8 && usb_port <=17){
  3324.                 if(index > 17){ //Out of usb range of front port.
  3325.                     index = 0xff;
  3326.                     //printk("Over limitation! ==================================================== index = [%d], usb_port = [%d]\n", index, usb_port);
  3327.                     send_message_to_app(QNAP_USB_OVER_LIMIT);
  3328.                 }  
  3329.             }
  3330.         }
  3331.         else
  3332.             index = 0xff;
  3333.     }
  3334.     else{   /* Sata HD here, Marvell driver will fix host number. */
  3335.         index = QNAP_disk_index(sdp);
  3336.     }
  3337. #ifdef NAS_VIRTUAL
  3338. #ifdef NAS_VIRTUAL_EX
  3339.     if (index == -1) {
  3340.         error = -ENODEV;
  3341.     }
  3342.     else
  3343. //  if (index >= ISCSI_DEV_START_INDEX) { //sdwa ~ sdwz
  3344.     if (Is_iSCSI_Index(index)) { //sdwa ~ sdwz
  3345.             int ip = index - ISCSI_DEV_START_INDEX;
  3346.             error = -EBUSY;
  3347.             printk("index of iscsi_dev_arr is %d\n", ip);
  3348.             if (!iscsi_dev_arr[ip]) {
  3349.                     iscsi_dev_arr[ip] = 1;
  3350.                     if (iscsi_iqn_arr[ip])
  3351.                         kfree(iscsi_iqn_arr[ip]);
  3352.                     iscsi_iqn_arr[ip] = kstrdup(qnap_iqn_node, GFP_KERNEL);
  3353.                     if (iscsi_sn_vpd_arr[ip])
  3354.                         kfree(iscsi_sn_vpd_arr[ip]);
  3355.                     iscsi_sn_vpd_arr[ip] = scsi_get_vpd_page(sdp, 0x80);
  3356.                     int vpd_len = iscsi_sn_vpd_arr[ip][3]+4;
  3357.                     iscsi_sn_vpd_arr[ip][vpd_len] = '\0';
  3358. //                  iscsi_dev_ptr[ip] = dev;
  3359.                     error = 0;
  3360.             }
  3361.     }
  3362.     else
  3363. #else
  3364.     if ((index >= ISCSI_DEV_START_INDEX) && (index < (ISCSI_DEV_START_INDEX+26))) { //sdwa ~ sdwz
  3365.         int ip = index - ISCSI_DEV_START_INDEX;
  3366.         error = -EBUSY;
  3367.         if (!iscsi_dev_arr[ip]) {
  3368.             iscsi_dev_arr[ip] = 1;
  3369.             error = 0;
  3370.         }
  3371.     }
  3372.     else
  3373. #endif
  3374. #endif
  3375. //Patch by QNAP: USB Port Multiplier
  3376.     if(Is_USB_Front_Port_Index(index)){ //sdia ~
  3377.         printk("usb_front_port_dev_arr[%d] = %d \n",index - USB_FRONT_PORT_START_INDEX,usb_front_port_dev_arr[index - USB_FRONT_PORT_START_INDEX]);
  3378.         if(usb_front_port_dev_arr[index - USB_FRONT_PORT_START_INDEX]){
  3379.             error = -EBUSY;
  3380.         }
  3381.         else{
  3382.             usb_front_port_dev_arr[index - USB_FRONT_PORT_START_INDEX] = 1;
  3383.             error = 0;    
  3384.         }
  3385.     }
  3386.     else if(Is_USB_Back_Port1_Index(index)){ //sdsa ~
  3387.         printk("usb_back_port1_dev_arr[%d] = %d \n",index - USB_BACK_PORT_1_START_INDEX,usb_back_port1_dev_arr[index - USB_BACK_PORT_1_START_INDEX]);
  3388.         if(usb_back_port1_dev_arr[index - USB_BACK_PORT_1_START_INDEX]){
  3389.             error = -EBUSY;
  3390.         }
  3391.         else{
  3392.             usb_back_port1_dev_arr[index - USB_BACK_PORT_1_START_INDEX] = 1;
  3393.             error = 0;  
  3394.         }
  3395.     }
  3396.     else if(Is_USB_Back_Port2_Index(index)){ //sdta ~
  3397.         printk("usb_back_port2_dev_arr[%d] = %d \n",index - USB_BACK_PORT_2_START_INDEX,usb_back_port2_dev_arr[index - USB_BACK_PORT_2_START_INDEX]);
  3398.         if(usb_back_port2_dev_arr[index - USB_BACK_PORT_2_START_INDEX]){
  3399.             error = -EBUSY;
  3400.         }
  3401.         else{
  3402.             usb_back_port2_dev_arr[index - USB_BACK_PORT_2_START_INDEX] = 1;
  3403.             error = 0;
  3404.         }
  3405.     }
  3406.     else if(Is_USB_Back_Port3_Index(index)){ //sdua ~
  3407.         if(usb_back_port3_dev_arr[index - USB_BACK_PORT_3_START_INDEX]){
  3408.             error = -EBUSY;
  3409.         }
  3410.         else{
  3411.             usb_back_port3_dev_arr[index - USB_BACK_PORT_3_START_INDEX] = 1;
  3412.             error = 0;    
  3413.         }
  3414.     }
  3415.     else if(Is_USB_Back_Port4_Index(index)){ //sdva ~
  3416.         if(usb_back_port4_dev_arr[index - USB_BACK_PORT_4_START_INDEX]){
  3417.             error = -EBUSY;
  3418.         }
  3419.         else{
  3420.             usb_back_port4_dev_arr[index - USB_BACK_PORT_4_START_INDEX] = 1;
  3421.             error = 0;  
  3422.         }
  3423.     }
  3424.     else if(Is_USB_Back_Port5_Index(index)){ //sdqa ~
  3425.         if(usb_back_port5_dev_arr[index - USB_BACK_PORT_5_START_INDEX]){
  3426.             error = -EBUSY;
  3427.         }
  3428.         else{
  3429.             usb_back_port5_dev_arr[index - USB_BACK_PORT_5_START_INDEX] = 1;
  3430.             error = 0;  
  3431.         }
  3432.     }
  3433.     else if(Is_USB_Back_Port6_Index(index)){ //sdra ~
  3434.         if(usb_back_port6_dev_arr[index - USB_BACK_PORT_6_START_INDEX]){
  3435.             error = -EBUSY;
  3436.         }
  3437.         else{
  3438.             usb_back_port6_dev_arr[index - USB_BACK_PORT_6_START_INDEX] = 1;
  3439.             error = 0;  
  3440.         }
  3441.     }
  3442.     else
  3443. ///////////////////////
  3444.         //Patch by QNAP: Port Multiplier
  3445.         if(Is_ESATA_Bus1_Index(index)){ //sdya ~
  3446.             if(esata_bus1_dev_arr[index - ESATA_BUS1_START_INDEX]){
  3447.                 printk("SCSI sd slot %d is occupy,Please check it.\n",index);
  3448.                 error = -EBUSY;
  3449.             }
  3450.             else{
  3451.                 esata_bus1_dev_arr[index - ESATA_BUS1_START_INDEX] = 1;
  3452.                 error = 0;    
  3453.             }
  3454.         }
  3455.         else if(Is_ESATA_Bus2_Index(index)){ //sdza ~
  3456.             if(esata_bus2_dev_arr[index - ESATA_BUS2_START_INDEX]){
  3457.                 printk("SCSI sd slot %d is occupy,Please check it.\n",index);
  3458.                 error = -EBUSY;
  3459.             }
  3460.             else{
  3461.                 esata_bus2_dev_arr[index - ESATA_BUS2_START_INDEX] = 1;
  3462.                 error = 0;    
  3463.             }
  3464.         }
  3465.         else{
  3466.             ///////////////
  3467.             if(index == 0xff){
  3468.                 int i;
  3469.                 for(i=0;i<8;i++){
  3470.                     if(!sd_dev_arr[i]){
  3471.                         index = i;
  3472.                         sd_dev_arr[index] = 1;
  3473.                         error = 0;
  3474.                         break;
  3475.                     }
  3476.                 }
  3477.             }
  3478.             else if(sd_dev_arr[index]){
  3479.                 printk("SCSI sd slot %d is occupy,Please check it.\n",index);
  3480.                 error = -EBUSY;
  3481.             }
  3482.             else{
  3483.                 sd_dev_arr[index] = 1;
  3484.                 error = 0;
  3485.             }
  3486.         }
  3487.  
  3488.     spin_unlock(&sd_index_lock);
  3489. #else // ------------------------------------------------
  3490.     do {
  3491.         if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  3492.             goto out_put;
  3493.  
  3494.         spin_lock(&sd_index_lock);
  3495.         error = ida_get_new(&sd_index_ida, &index);
  3496.         spin_unlock(&sd_index_lock);
  3497.     } while (error == -EAGAIN);
  3498.  
  3499. #endif // CONFIG_MACH_QNAPTS
  3500.     /////////////////////////////////////////////////////
  3501.     if (error)
  3502.         goto out_put;
  3503.  
  3504.     error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  3505.     if (error)
  3506.         goto out_free_index;
  3507.  
  3508.     sdkp->device = sdp;
  3509.     sdkp->driver = &sd_template;
  3510.     sdkp->disk = gd;
  3511.     sdkp->index = index;
  3512.     sdkp->openers = 0;
  3513.     sdkp->previous_state = 1;
  3514.  
  3515.     if (!sdp->request_queue->rq_timeout) {
  3516.         if (sdp->type != TYPE_MOD)
  3517.             blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  3518.         else
  3519.             blk_queue_rq_timeout(sdp->request_queue,
  3520.                     SD_MOD_TIMEOUT);
  3521.     }
  3522.  
  3523.     device_initialize(&sdkp->dev);
  3524.     sdkp->dev.parent = &sdp->sdev_gendev;
  3525.     sdkp->dev.class = &sd_disk_class;
  3526.     dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
  3527.  
  3528.     if (device_add(&sdkp->dev))
  3529.         goto out_free_index;
  3530.  
  3531.     get_device(&sdp->sdev_gendev);
  3532.  
  3533.     get_device(&sdkp->dev); /* prevent release before async_schedule */
  3534.     async_schedule(sd_probe_async, sdkp);
  3535.  
  3536.     return 0;
  3537.  
  3538. out_free_index:
  3539.     spin_lock(&sd_index_lock);
  3540.     //Patch by QNAP: usb and sata device mapping
  3541. #ifdef CONFIG_MACH_QNAPTS
  3542. //Patch by QNAP: USB Port Multiplier
  3543.     if(Is_USB_Front_Port_Index(index))  //sdia
  3544.         usb_front_port_dev_arr[index - USB_FRONT_PORT_START_INDEX] = 0;
  3545.     else if(Is_USB_Back_Port1_Index(index))//sdsa ~
  3546.         usb_back_port1_dev_arr[index - USB_BACK_PORT_1_START_INDEX] = 0;
  3547.     else if(Is_USB_Back_Port2_Index(index)) //sdta ~
  3548.         usb_back_port2_dev_arr[index - USB_BACK_PORT_2_START_INDEX] = 0;
  3549.     else if(Is_USB_Back_Port3_Index(index)) //sdua ~
  3550.         usb_back_port3_dev_arr[index - USB_BACK_PORT_3_START_INDEX] = 0;
  3551.     else if(Is_USB_Back_Port4_Index(index)) //sdva ~
  3552.         usb_back_port4_dev_arr[index - USB_BACK_PORT_4_START_INDEX] = 0;
  3553.     else if(Is_USB_Back_Port5_Index(index)) //sdqa ~a
  3554.         usb_back_port5_dev_arr[index - USB_BACK_PORT_5_START_INDEX] = 0;
  3555.     else if(Is_USB_Back_Port6_Index(index)) //sdra ~
  3556.         usb_back_port6_dev_arr[index - USB_BACK_PORT_6_START_INDEX] = 0;
  3557.     else
  3558. ////////////////////////
  3559.     if(Is_ESATA_Bus1_Index(index)){ //sdya ~
  3560.         esata_bus1_dev_arr[index - ESATA_BUS1_START_INDEX] = 0;
  3561.     }
  3562.     else if(Is_ESATA_Bus2_Index(index)){ //sdza ~
  3563.         esata_bus2_dev_arr[index - ESATA_BUS2_START_INDEX] = 0;
  3564.     }
  3565.     else
  3566.         sd_dev_arr[index] = 0;
  3567. #else    
  3568.     ida_remove(&sd_index_ida, index);
  3569. #endif
  3570.     ///////////////////////////////////////////
  3571.     spin_unlock(&sd_index_lock);
  3572. out_put:
  3573.     put_disk(gd);
  3574. out_free:
  3575.     kfree(sdkp);
  3576. out:
  3577.     return error;
  3578. }
  3579.  
  3580. /**
  3581.  *  sd_remove - called whenever a scsi disk (previously recognized by
  3582.  *  sd_probe) is detached from the system. It is called (potentially
  3583.  *  multiple times) during sd module unload.
  3584.  *  @sdp: pointer to mid level scsi device object
  3585.  *
  3586.  *  Note: this function is invoked from the scsi mid-level.
  3587.  *  This function potentially frees up a device name (e.g. /dev/sdc)
  3588.  *  that could be re-used by a subsequent sd_probe().
  3589.  *  This function is not called when the built-in sd driver is "exit-ed".
  3590.  **/
  3591. static int sd_remove(struct device *dev)
  3592. {
  3593.     struct scsi_disk *sdkp;
  3594.  
  3595.     async_synchronize_full();
  3596.     sdkp = dev_get_drvdata(dev);
  3597.     blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
  3598.     device_del(&sdkp->dev);
  3599.     del_gendisk(sdkp->disk);
  3600.     sd_shutdown(dev);
  3601.  
  3602.     mutex_lock(&sd_ref_mutex);
  3603.     dev_set_drvdata(dev, NULL);
  3604.     put_device(&sdkp->dev);
  3605.     mutex_unlock(&sd_ref_mutex);
  3606. //Patch by QNAP: usb and sata device mapping
  3607. #ifdef CONFIG_MACH_QNAPTS
  3608.     switch(sdkp->index){
  3609. #if defined(X86_SANDYBRIDGE) || defined(TS1259U)
  3610.         case 0 ... 15:
  3611.             send_message_to_app(QNAP_IOCTL_SATA_DOWN + sdkp->index);        
  3612.             break;
  3613. #else
  3614.         case 0:
  3615.             send_message_to_app(QNAP_SATA_DOWN);
  3616.             break;
  3617.         case 1:
  3618.             send_message_to_app(QNAP_SATA2_DOWN);
  3619.             break;
  3620.         case 2:
  3621.             send_message_to_app(QNAP_SATA3_DOWN);
  3622.             break;
  3623.         case 3:
  3624.             send_message_to_app(QNAP_SATA4_DOWN);
  3625.             break;
  3626.         case 4:
  3627.             send_message_to_app(QNAP_SATA5_DOWN);
  3628.             break;
  3629.         case 5:
  3630.             send_message_to_app(QNAP_SATA6);
  3631.             break;
  3632.         case 6:
  3633.             send_message_to_app(QNAP_SATA7);
  3634.             break;
  3635.         case 7:
  3636.             send_message_to_app(QNAP_SATA8_DOWN);
  3637.             break;
  3638. #endif         
  3639.         case 24:
  3640.             send_message_to_app(QNAP_ESATA_DOWN);
  3641.             break;
  3642.         case 25:
  3643.             send_message_to_app(QNAP_ESATA2_DOWN);
  3644.             break;
  3645. #ifdef NAS_VIRTUAL
  3646.         default:
  3647.             if (Is_iSCSI_Index(sdkp->index)) {
  3648.                 send_message_to_app(QNAP_ISCSI_DOWN);
  3649.                 break;
  3650.             }
  3651.             else
  3652. #endif
  3653.             if(Is_ESATA_Bus1_Index(sdkp->index)){ //sdya ~
  3654.                 send_message_to_app(QNAP_IOCTL_ESATA_DOWN + (sdkp->index - ESATA_BUS1_START_INDEX));
  3655.                 break;
  3656.             }
  3657.             else if(Is_ESATA_Bus2_Index(sdkp->index)){ //sdza ~
  3658.                 send_message_to_app(QNAP_IOCTL_ESATA_DOWN + 0x10 + (sdkp->index - ESATA_BUS2_START_INDEX));
  3659.                 break;
  3660.             }
  3661.     }
  3662. #endif
  3663.     return 0;
  3664. }
  3665.  
  3666. /**
  3667.  *  scsi_disk_release - Called to free the scsi_disk structure
  3668.  *  @dev: pointer to embedded class device
  3669.  *
  3670.  *  sd_ref_mutex must be held entering this routine.  Because it is
  3671.  *  called on last put, you should always use the scsi_disk_get()
  3672.  *  scsi_disk_put() helpers which manipulate the semaphore directly
  3673.  *  and never do a direct put_device.
  3674.  **/
  3675. static void scsi_disk_release(struct device *dev)
  3676. {
  3677.     struct scsi_disk *sdkp = to_scsi_disk(dev);
  3678.     struct gendisk *disk = sdkp->disk;
  3679. //Patch by QNAP: usb and sata device mapping
  3680. #ifdef CONFIG_MACH_QNAPTS
  3681.     int index = sdkp->index;
  3682. #endif
  3683. /////////////////////////////////////////  
  3684.     spin_lock(&sd_index_lock);
  3685. //Patch by QNAP: usb and sata device mapping
  3686. #ifdef CONFIG_MACH_QNAPTS
  3687. #ifdef NAS_VIRTUAL
  3688.     if (Is_iSCSI_Index(index)) {
  3689.         index -= ISCSI_DEV_START_INDEX;
  3690.         iscsi_dev_arr[index]=0;
  3691. #ifdef NAS_VIRTUAL_EX
  3692.         printk("iSCSI LUN %d released\n", index);
  3693.         if (iscsi_iqn_arr[index]) {
  3694.             kfree(iscsi_iqn_arr[index]);
  3695.             iscsi_iqn_arr[index]=NULL;
  3696.         }
  3697.         if (iscsi_sn_vpd_arr[index]) {
  3698.             kfree(iscsi_sn_vpd_arr[index]);
  3699.             iscsi_sn_vpd_arr[index]=NULL;
  3700.         }
  3701. #endif
  3702.     }
  3703. #endif
  3704. //Patch by QNAP: USB Port Multiplier
  3705.     if(Is_USB_Front_Port_Index(index))  //sdia
  3706.         usb_front_port_dev_arr[index - USB_FRONT_PORT_START_INDEX] = 0;
  3707.     else if(Is_USB_Back_Port1_Index(index))//sdsa ~
  3708.         usb_back_port1_dev_arr[index - USB_BACK_PORT_1_START_INDEX] = 0;
  3709.     else if(Is_USB_Back_Port2_Index(index)) //sdta ~
  3710.         usb_back_port2_dev_arr[index - USB_BACK_PORT_2_START_INDEX] = 0;
  3711.     else if(Is_USB_Back_Port3_Index(index)) //sdua ~
  3712.         usb_back_port3_dev_arr[index - USB_BACK_PORT_3_START_INDEX] = 0;
  3713.     else if(Is_USB_Back_Port4_Index(index)) //sdva ~
  3714.         usb_back_port4_dev_arr[index - USB_BACK_PORT_4_START_INDEX] = 0;
  3715.     else if(Is_USB_Back_Port5_Index(index)) //sdqa ~
  3716.         usb_back_port5_dev_arr[index - USB_BACK_PORT_5_START_INDEX] = 0;
  3717.     else if(Is_USB_Back_Port6_Index(index)) //sdra ~
  3718.         usb_back_port6_dev_arr[index - USB_BACK_PORT_6_START_INDEX] = 0;   
  3719. /////////////////////
  3720.     if(Is_ESATA_Bus1_Index(index)){ //sdya ~
  3721.         esata_bus1_dev_arr[index - ESATA_BUS1_START_INDEX] = 0;
  3722.     }
  3723.     else if(Is_ESATA_Bus2_Index(index)){ //sdza ~
  3724.         esata_bus2_dev_arr[index - ESATA_BUS2_START_INDEX] = 0;
  3725.     }
  3726.     else sd_dev_arr[index]=0;
  3727. #else
  3728.     ida_remove(&sd_index_ida, sdkp->index);
  3729. #endif
  3730.     /////////////////////////////////////////
  3731.     spin_unlock(&sd_index_lock);
  3732.  
  3733.     disk->private_data = NULL;
  3734.     put_disk(disk);
  3735.     put_device(&sdkp->device->sdev_gendev);
  3736.  
  3737.     kfree(sdkp);
  3738. }
  3739.  
  3740. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  3741. {
  3742.     unsigned char cmd[6] = { START_STOP };  /* START_VALID */
  3743.     struct scsi_sense_hdr sshdr;
  3744.     struct scsi_device *sdp = sdkp->device;
  3745.     int res;
  3746.  
  3747.     if (start)
  3748.         cmd[4] |= 1;    /* START */
  3749.  
  3750.     if (sdp->start_stop_pwr_cond)
  3751.         cmd[4] |= start ? 1 << 4 : 3 << 4;  /* Active or Standby */
  3752.  
  3753.     if (!scsi_device_online(sdp))
  3754.         return -ENODEV;
  3755.  
  3756.     res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  3757.                    SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  3758.     if (res) {
  3759.         sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
  3760.         sd_print_result(sdkp, res);
  3761.         if (driver_byte(res) & DRIVER_SENSE)
  3762.             sd_print_sense_hdr(sdkp, &sshdr);
  3763.     }
  3764.  
  3765.     return res;
  3766. }
  3767.  
  3768. /*
  3769.  * Send a SYNCHRONIZE CACHE instruction down to the device through
  3770.  * the normal SCSI command structure.  Wait for the command to
  3771.  * complete.
  3772.  */
  3773. static void sd_shutdown(struct device *dev)
  3774. {
  3775.     struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  3776.     if (!sdkp)
  3777.         return;         /* this can happen */
  3778.     if (sdkp->WCE) {
  3779.         sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  3780.         sd_sync_cache(sdkp);
  3781.     }
  3782.  
  3783.     if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  3784.         sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  3785.         sd_start_stop_device(sdkp, 0);
  3786.     }
  3787.  
  3788.     scsi_disk_put(sdkp);
  3789. }
  3790.  
  3791. static int sd_suspend(struct device *dev, pm_message_t mesg)
  3792. {
  3793.     struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  3794.     int ret = 0;
  3795.  
  3796.     if (!sdkp)
  3797.         return 0;   /* this can happen */
  3798.  
  3799.     if (sdkp->WCE) {
  3800.         sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  3801.         ret = sd_sync_cache(sdkp);
  3802.         if (ret)
  3803.             goto done;
  3804.     }
  3805.  
  3806.     if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
  3807.         sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  3808.         ret = sd_start_stop_device(sdkp, 0);
  3809.     }
  3810.  
  3811. done:
  3812.     scsi_disk_put(sdkp);
  3813.     return ret;
  3814. }
  3815.  
  3816. static int sd_resume(struct device *dev)
  3817. {
  3818.     struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  3819.     int ret = 0;
  3820.  
  3821.     if (!sdkp->device->manage_start_stop)
  3822.         goto done;
  3823.  
  3824.     sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  3825.     ret = sd_start_stop_device(sdkp, 1);
  3826.  
  3827. done:
  3828.     scsi_disk_put(sdkp);
  3829.     return ret;
  3830. }
  3831.  
  3832. /**
  3833.  *  init_sd - entry point for this driver (both when built in or when
  3834.  *  a module).
  3835.  *
  3836.  *  Note: this function registers this driver with the scsi mid-level.
  3837.  **/
  3838. static int __init init_sd(void)
  3839. {
  3840.     int majors = 0, i, err;
  3841.  
  3842.     SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  3843.  
  3844.     for (i = 0; i < SD_MAJORS; i++)
  3845.         if (register_blkdev(sd_major(i), "sd") == 0)
  3846.             majors++;
  3847.  
  3848.     if (!majors)
  3849.         return -ENODEV;
  3850.  
  3851.     err = class_register(&sd_disk_class);
  3852.     if (err)
  3853.         goto err_out;
  3854.  
  3855.     err = scsi_register_driver(&sd_template.gendrv);
  3856.     if (err)
  3857.         goto err_out_class;
  3858.  
  3859.     sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  3860.                      0, 0, NULL);
  3861.     if (!sd_cdb_cache) {
  3862.         printk(KERN_ERR "sd: can't init extended cdb cache\n");
  3863.         goto err_out_class;
  3864.     }
  3865.  
  3866.     sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  3867.     if (!sd_cdb_pool) {
  3868.         printk(KERN_ERR "sd: can't init extended cdb pool\n");
  3869.         goto err_out_cache;
  3870.     }
  3871.  
  3872. #ifdef NAS_VIRTUAL
  3873. #ifdef NAS_VIRTUAL_EX
  3874.     memset(iscsi_dev_arr, 0, sizeof(iscsi_dev_arr));
  3875.     memset(iscsi_iqn_arr, 0, sizeof(iscsi_iqn_arr));
  3876.     memset(iscsi_sn_vpd_arr, 0, sizeof(iscsi_sn_vpd_arr));
  3877.     memset(qnap_disk_node, 0, sizeof(qnap_disk_node));
  3878.     memset(qnap_iqn_node, 0, sizeof(qnap_iqn_node));
  3879. #endif
  3880.     create_qnap_disk_node_proc();
  3881. #endif
  3882.     return 0;
  3883.  
  3884. err_out_cache:
  3885.     kmem_cache_destroy(sd_cdb_cache);
  3886.  
  3887. err_out_class:
  3888.     class_unregister(&sd_disk_class);
  3889. err_out:
  3890.     for (i = 0; i < SD_MAJORS; i++)
  3891.         unregister_blkdev(sd_major(i), "sd");
  3892.     return err;
  3893. }
  3894.  
  3895. /**
  3896.  *  exit_sd - exit point for this driver (when it is a module).
  3897.  *
  3898.  *  Note: this function unregisters this driver from the scsi mid-level.
  3899.  **/
  3900. static void __exit exit_sd(void)
  3901. {
  3902.     int i;
  3903.  
  3904.     SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  3905.  
  3906.     mempool_destroy(sd_cdb_pool);
  3907.     kmem_cache_destroy(sd_cdb_cache);
  3908.  
  3909.     scsi_unregister_driver(&sd_template.gendrv);
  3910.     class_unregister(&sd_disk_class);
  3911.  
  3912.     for (i = 0; i < SD_MAJORS; i++)
  3913.         unregister_blkdev(sd_major(i), "sd");
  3914. }
  3915.  
  3916. module_init(init_sd);
  3917. module_exit(exit_sd);
  3918.  
  3919. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  3920.                    struct scsi_sense_hdr *sshdr)
  3921. {
  3922.     sd_printk(KERN_INFO, sdkp, "");
  3923.     scsi_show_sense_hdr(sshdr);
  3924.     sd_printk(KERN_INFO, sdkp, "");
  3925.     scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  3926. }
  3927.  
  3928. static void sd_print_result(struct scsi_disk *sdkp, int result)
  3929. {
  3930.     sd_printk(KERN_INFO, sdkp, "");
  3931.     scsi_show_result(result);
  3932. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement