+/**************************************************************************/
+/* Locking */
+/**************************************************************************/
+/**
+ * The underlying function that controls access to the sata core
+ *
+ * @return non-zero indicates that you have acquired exclusive access to the
+ * sata core.
+ */
+static int __acquire_sata_core(
+ int port_no,
+ ox820sata_isr_callback_t callback,
+ unsigned long arg,
+ int may_sleep,
+ int timeout_jiffies,
+ int hw_access,
+ void *uid,
+ int locker_type)
+{
+ unsigned long end = jiffies + timeout_jiffies;
+ int acquired = 0;
+ unsigned long flags;
+ int timed_out = 0;
+ DEFINE_WAIT(wait);
+
+ spin_lock_irqsave(&oxsacs_lock, flags);
+
+ DPRINTK("Entered uid %p, port %d, h/w count %d, d count %d, callback %p, "
+ "hw_access %d, core_locked %d, reentrant_port_no %d, ox820sata_isr_callback %p\n",
+ uid, port_no, hw_lock_count, direct_lock_count, callback, hw_access,
+ core_locked, reentrant_port_no, ox820sata_isr_callback);
+
+ while (!timed_out) {
+ if (core_locked || (!hw_access && scsi_nonblocking_attempts)) {
+ /* Can only allow access if from SCSI/SATA stack and if
+ reentrant access is allowed and this access is to the same
+ port for which the lock is current held */
+ if (hw_access && (port_no == reentrant_port_no)) {
+ BUG_ON(!hw_lock_count);
+ ++hw_lock_count;
+
+ DPRINTK("Allow SCSI/SATA re-entrant access to uid %p port %d\n", uid, port_no);
+ acquired = 1;
+ break;
+ } else if (!hw_access) {
+ if ((locker_type == SATA_READER) && (current_locker_type == SATA_READER)) {
+ WARN(1,
+ "Already locked by reader, uid %p, locker_uid %p, port %d, "
+ "h/w count %d, d count %d, hw_access %d\n", uid, locker_uid,
+ port_no, hw_lock_count, direct_lock_count, hw_access);
+ goto check_uid;
+ }
+
+ if ((locker_type != SATA_READER) && (locker_type != SATA_WRITER)) {
+ goto wait_for_lock;
+ }
+
+check_uid:
+ WARN(uid == locker_uid, "Attempt to lock by locker type %d "
+ "uid %p, already locked by locker type %d with "
+ "locker_uid %p, port %d, h/w count %d, d count %d, "
+ "hw_access %d\n", locker_type, uid, current_locker_type,
+ locker_uid, port_no, hw_lock_count, direct_lock_count, hw_access);
+ }
+ } else {
+ WARN(hw_lock_count || direct_lock_count, "Core unlocked but counts "
+ "non-zero: uid %p, locker_uid %p, port %d, h/w count %d, "
+ "d count %d, hw_access %d\n", uid, locker_uid, port_no,
+ hw_lock_count, direct_lock_count, hw_access);
+
+ BUG_ON(current_locker_type != SATA_UNLOCKED);
+
+ WARN(locker_uid, "Attempt to lock uid %p when locker_uid %p is "
+ "non-zero, port %d, h/w count %d, d count %d, hw_access %d\n",
+ uid, locker_uid, port_no, hw_lock_count, direct_lock_count,
+ hw_access);
+
+ if (!hw_access) {
+ /* Direct access attempting to acquire non-contented lock */
+ BUG_ON(!callback); // Must have callback for direct access
+ BUG_ON(reentrant_port_no != -1); // Sanity check lock state
+
+ ox820sata_isr_callback = callback;
+ ox820sata_isr_arg = arg;
+ ++direct_lock_count;
+
+ current_locker_type = locker_type;
+ } else {
+ /* SCSI/SATA attempting to acquire non-contented lock */
+ BUG_ON(callback); // No callbacks for SCSI/SATA access
+ BUG_ON(arg); // No callback args for SCSI/SATA access
+
+ BUG_ON(ox820sata_isr_callback); // Sanity check lock state
+ BUG_ON(ox820sata_isr_arg); // Sanity check lock state
+
+ ++hw_lock_count;
+ reentrant_port_no = port_no;
+
+ current_locker_type = SATA_SCSI_STACK;
+ }
+
+ core_locked = 1;
+ acquired = 1;
+ locker_uid = uid;
+ break;
+ }
+
+wait_for_lock:
+ if (!may_sleep) {
+ DPRINTK("Denying for uid %p locker_type %d, hw_access %d, port %d, "
+ "current_locker_type %d as cannot sleep\n", uid, locker_type,
+ hw_access, port_no, current_locker_type);
+
+ if (hw_access) {
+ ++scsi_nonblocking_attempts;
+ }
+ break;
+ }
+
+ // Core is locked and we're allowed to sleep, so wait to be awoken when
+ // the core is unlocked
+ for (;;) {
+ prepare_to_wait(hw_access ? &scsi_wait_queue : &fast_wait_queue,
+ &wait, TASK_UNINTERRUPTIBLE);
+ if (!core_locked && !(!hw_access && scsi_nonblocking_attempts)) {
+ // We're going to use variables that will have been changed by
+ // the waker prior to clearing core_locked so we need to ensure
+ // we see changes to all those variables
+ smp_rmb();
+ break;
+ }
+ if (time_after(jiffies, end)) {
+ printk("__acquire_sata_core() uid %p failing for port %d timed out, "
+ "locker_uid %p, h/w count %d, d count %d, callback %p, hw_access %d, "
+ "core_locked %d, reentrant_port_no %d, ox820sata_isr_callback %p, "
+ "ox820sata_isr_arg %p\n", uid, port_no, locker_uid,
+ hw_lock_count, direct_lock_count, callback, hw_access,
+ core_locked, reentrant_port_no, ox820sata_isr_callback,
+ (void*)ox820sata_isr_arg);
+ timed_out = 1;
+ break;
+ }
+ spin_unlock_irqrestore(&oxsacs_lock, flags);
+ if (!schedule_timeout(4*HZ)) {
+ printk(KERN_INFO "__acquire_sata_core() uid %p, locker_uid %p, "
+ "timed-out of schedule(), checking overall timeout\n",
+ uid, locker_uid);
+ }
+ spin_lock_irqsave(&oxsacs_lock, flags);
+ }
+ finish_wait(hw_access ? &scsi_wait_queue : &fast_wait_queue, &wait);
+ }
+
+ if (hw_access && acquired) {
+ if (scsi_nonblocking_attempts) {
+ scsi_nonblocking_attempts = 0;
+ }
+
+ // Wake any other SCSI/SATA waiters so they can get reentrant access to
+ // the same port if appropriate. This is because if the SATA core is
+ // locked by fast access, or SCSI/SATA access to other port, then can
+ // have >1 SCSI/SATA waiters on the wait list so want to give reentrant
+ // accessors a chance to get access ASAP
+ if (!list_empty(&scsi_wait_queue.task_list)) {
+ wake_up(&scsi_wait_queue);
+ }
+ }
+
+ DPRINTK("Leaving uid %p with acquired = %d, port %d, callback %p\n", uid, acquired, port_no, callback);
+
+ spin_unlock_irqrestore(&oxsacs_lock, flags);
+
+ return acquired;
+}
+
+int sata_core_has_fast_waiters(void)
+{
+ int has_waiters;
+ unsigned long flags;
+
+ spin_lock_irqsave(&oxsacs_lock, flags);
+ has_waiters = !list_empty(&fast_wait_queue.task_list);
+ spin_unlock_irqrestore(&oxsacs_lock, flags);
+
+ return has_waiters;
+}
+EXPORT_SYMBOL(sata_core_has_fast_waiters);
+
+int sata_core_has_scsi_waiters(void)
+{
+ int has_waiters;
+ unsigned long flags;
+
+ spin_lock_irqsave(&oxsacs_lock, flags);
+ has_waiters = scsi_nonblocking_attempts || !list_empty(&scsi_wait_queue.task_list);
+ spin_unlock_irqrestore(&oxsacs_lock, flags);
+
+ return has_waiters;
+}
+EXPORT_SYMBOL(sata_core_has_scsi_waiters);
+
+/*
+ * ata_port operation to gain ownership of the SATA hardware prior to issuing
+ * a command against a SATA host. Allows any number of users of the port against
+ * which the lock was first acquired, thus enforcing that only one SATA core
+ * port may be operated on at once.
+ */
+static int sata_oxnas_acquire_hw(
+ int port_no,
+ int may_sleep,
+ int timeout_jiffies)
+{
+ return __acquire_sata_core(port_no, NULL, 0, may_sleep, timeout_jiffies, 1, (void*)HW_LOCKER_UID, SATA_SCSI_STACK);
+}
+
+/*
+ * operation to release ownership of the SATA hardware
+ */
+static void sata_oxnas_release_hw(unsigned int port_no)
+{
+ unsigned long flags;
+ int released = 0;
+
+ spin_lock_irqsave(&oxsacs_lock, flags);
+
+ DPRINTK("Entered port_no = %d, h/w count %d, d count %d, core locked = %d, "
+ "reentrant_port_no = %d, ox820sata_isr_callback %p\n", port_no,
+ hw_lock_count, direct_lock_count, core_locked, reentrant_port_no, ox820sata_isr_callback);
+
+ if (!core_locked) {
+ /* Nobody holds the SATA lock */
+ printk(KERN_WARNING "Nobody holds SATA lock, port_no %d\n", port_no);
+ released = 1;
+ } else if (!hw_lock_count) {
+ /* SCSI/SATA has released without holding the lock */
+ printk(KERN_WARNING "SCSI/SATA does not hold SATA lock, port_no %d\n", port_no);
+ } else {
+ /* Trap incorrect usage */
+ BUG_ON(reentrant_port_no == -1);
+ BUG_ON(port_no != reentrant_port_no);
+ BUG_ON(direct_lock_count);
+ BUG_ON(current_locker_type != SATA_SCSI_STACK);
+
+ WARN(!locker_uid || (locker_uid != HW_LOCKER_UID), "Invalid locker "
+ "uid %p, h/w count %d, d count %d, reentrant_port_no %d, "
+ "core_locked %d, ox820sata_isr_callback %p\n", locker_uid,
+ hw_lock_count, direct_lock_count, reentrant_port_no, core_locked,
+ ox820sata_isr_callback);
+
+ if (--hw_lock_count) {
+ DPRINTK("Still nested port_no %d\n", port_no);
+ } else {
+ DPRINTK("Release port_no %d\n", port_no);
+ reentrant_port_no = -1;
+ ox820sata_isr_callback = NULL;
+ current_locker_type = SATA_UNLOCKED;
+ locker_uid = 0;
+ core_locked = 0;
+ released = 1;
+ wake_up(!list_empty(&scsi_wait_queue.task_list) ? &scsi_wait_queue : &fast_wait_queue);
+ }
+ }
+
+ DPRINTK("Leaving, port_no %d, count %d\n", port_no, hw_lock_count);
+
+ spin_unlock_irqrestore(&oxsacs_lock, flags);
+
+ /* CONFIG_SATA_OX820_DIRECT_HWRAID */
+ /* if (released)
+ ox820hwraid_restart_queue();
+ } */
+}
+