blob: b9fffc8d94a76daaf6b7faadbcd85194cb555094 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Swen Schillig00bab912008-06-10 18:20:57 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Swen Schillig00bab912008-06-10 18:20:57 +02004 * Setup and helper functions to access QDIO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02006 * Copyright IBM Corp. 2002, 2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "zfcp_ext.h"
Christof Schmitt34c2b712010-02-17 11:18:59 +010015#include "zfcp_qdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Christof Schmitt5d4e2262008-07-02 10:56:34 +020017#define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Swen Schillig86a96682011-08-15 14:40:32 +020019static bool enable_multibuffer;
20module_param_named(datarouter, enable_multibuffer, bool, 0400);
21MODULE_PARM_DESC(datarouter, "Enable hardware data router support");
22
Swen Schillig00bab912008-06-10 18:20:57 +020023static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
Swen Schilligb4e44592007-07-18 10:55:13 +020024{
25 int pos;
26
27 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
Swen Schillig00bab912008-06-10 18:20:57 +020028 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
29 if (!sbal[pos])
Swen Schilligb4e44592007-07-18 10:55:13 +020030 return -ENOMEM;
Swen Schilligb4e44592007-07-18 10:55:13 +020031 }
32 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
33 if (pos % QBUFF_PER_PAGE)
Swen Schillig00bab912008-06-10 18:20:57 +020034 sbal[pos] = sbal[pos - 1] + 1;
Swen Schilligb4e44592007-07-18 10:55:13 +020035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Christof Schmitt339f4f42010-07-16 15:37:43 +020038static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
39 unsigned int qdio_err)
Swen Schillig00bab912008-06-10 18:20:57 +020040{
Swen Schillig564e1c82009-08-18 15:43:19 +020041 struct zfcp_adapter *adapter = qdio->adapter;
42
Christof Schmittff3b24f2008-10-01 12:42:15 +020043 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020044
Swen Schillig86a96682011-08-15 14:40:32 +020045 if (qdio_err & QDIO_ERROR_SLSB_STATE) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020046 zfcp_qdio_siosl(adapter);
Swen Schillig86a96682011-08-15 14:40:32 +020047 zfcp_erp_adapter_shutdown(adapter, 0, id);
48 return;
49 }
Swen Schillig00bab912008-06-10 18:20:57 +020050 zfcp_erp_adapter_reopen(adapter,
51 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
Swen Schilligea4a3a62010-12-02 15:16:16 +010052 ZFCP_STATUS_COMMON_ERP_FAILED, id);
Swen Schillig00bab912008-06-10 18:20:57 +020053}
54
Christof Schmitt5d4e2262008-07-02 10:56:34 +020055static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
56{
57 int i, sbal_idx;
58
59 for (i = first; i < first + cnt; i++) {
60 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
61 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
62 }
63}
64
Martin Peschke94506fd2009-03-02 13:08:56 +010065/* this needs to be called prior to updating the queue fill level */
Heiko Carstens41e05a12009-08-18 15:43:32 +020066static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010067{
Heiko Carstens41e05a12009-08-18 15:43:32 +020068 unsigned long long now, span;
Swen Schillig706eca42010-07-16 15:37:38 +020069 int used;
Martin Peschke94506fd2009-03-02 13:08:56 +010070
Heiko Carstens41e05a12009-08-18 15:43:32 +020071 now = get_clock_monotonic();
72 span = (now - qdio->req_q_time) >> 12;
Swen Schillig706eca42010-07-16 15:37:38 +020073 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020074 qdio->req_q_util += used * span;
75 qdio->req_q_time = now;
Martin Peschke94506fd2009-03-02 13:08:56 +010076}
77
Jan Glauber779e6e12008-07-17 17:16:48 +020078static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020079 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020080 unsigned long parm)
81{
Swen Schillig564e1c82009-08-18 15:43:19 +020082 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schillig00bab912008-06-10 18:20:57 +020083
Jan Glauber779e6e12008-07-17 17:16:48 +020084 if (unlikely(qdio_err)) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020085 zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
Swen Schillig00bab912008-06-10 18:20:57 +020086 return;
87 }
88
89 /* cleanup all SBALs being program-owned now */
Swen Schillig706eca42010-07-16 15:37:38 +020090 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +020091
Christof Schmitt44a24cb2010-09-08 14:39:57 +020092 spin_lock_irq(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +020093 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +020094 spin_unlock_irq(&qdio->stat_lock);
Swen Schillig706eca42010-07-16 15:37:38 +020095 atomic_add(count, &qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020096 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020097}
98
Jan Glauber779e6e12008-07-17 17:16:48 +020099static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +0200100 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +0200101 unsigned long parm)
102{
Swen Schillig564e1c82009-08-18 15:43:19 +0200103 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schillig86a96682011-08-15 14:40:32 +0200104 struct zfcp_adapter *adapter = qdio->adapter;
105 struct qdio_buffer_element *sbale;
106 int sbal_no, sbal_idx;
107 void *pl[ZFCP_QDIO_MAX_SBALS_PER_REQ + 1];
108 u64 req_id;
109 u8 scount;
Swen Schillig00bab912008-06-10 18:20:57 +0200110
Jan Glauber779e6e12008-07-17 17:16:48 +0200111 if (unlikely(qdio_err)) {
Swen Schillig86a96682011-08-15 14:40:32 +0200112 memset(pl, 0, ZFCP_QDIO_MAX_SBALS_PER_REQ * sizeof(void *));
113 if (zfcp_adapter_multi_buffer_active(adapter)) {
114 sbale = qdio->res_q[idx]->element;
115 req_id = (u64) sbale->addr;
116 scount = sbale->scount + 1; /* incl. signaling SBAL */
117
118 for (sbal_no = 0; sbal_no < scount; sbal_no++) {
119 sbal_idx = (idx + sbal_no) %
120 QDIO_MAX_BUFFERS_PER_Q;
121 pl[sbal_no] = qdio->res_q[sbal_idx];
122 }
123 zfcp_dbf_hba_def_err(adapter, req_id, scount, pl);
124 }
Christof Schmitt339f4f42010-07-16 15:37:43 +0200125 zfcp_qdio_handler_error(qdio, "qdires1", qdio_err);
Swen Schillig00bab912008-06-10 18:20:57 +0200126 return;
127 }
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /*
130 * go through all SBALs from input queue currently
131 * returned by QDIO layer
132 */
Swen Schillig00bab912008-06-10 18:20:57 +0200133 for (sbal_no = 0; sbal_no < count; sbal_no++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200134 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200136 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
139 /*
Swen Schillig706eca42010-07-16 15:37:38 +0200140 * put SBALs back to response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
Swen Schillig706eca42010-07-16 15:37:38 +0200142 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
Swen Schilligea4a3a62010-12-02 15:16:16 +0100143 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Swen Schillig44cc76f2008-10-01 12:42:16 +0200146static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200147zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200149 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200152 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200153 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200156 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return NULL;
158
159 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200160 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200161 sbale->sflags |= SBAL_SFLAGS0_MORE_SBALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200164 q_req->sbal_last++;
165 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200168 q_req->sbal_number++;
Swen Schillig01b04752010-07-16 15:37:37 +0200169 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200172 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200175 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200176 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 return sbale;
179}
180
Swen Schillig44cc76f2008-10-01 12:42:16 +0200181static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200182zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Swen Schillig86a96682011-08-15 14:40:32 +0200184 if (q_req->sbale_curr == qdio->max_sbale_per_sbal - 1)
Christof Schmitt1674b402010-04-30 18:09:34 +0200185 return zfcp_qdio_sbal_chain(qdio, q_req);
Swen Schillig42428f72009-08-18 15:43:18 +0200186 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200187 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**
191 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
Christof Schmitt1674b402010-04-30 18:09:34 +0200192 * @qdio: pointer to struct zfcp_qdio
193 * @q_req: pointer to struct zfcp_qdio_req
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig86a96682011-08-15 14:40:32 +0200196 * Returns: zero or -EINVAL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100198int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig01b04752010-07-16 15:37:37 +0200199 struct scatterlist *sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200201 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Swen Schillig00bab912008-06-10 18:20:57 +0200203 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200204 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200205 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Swen Schillig00bab912008-06-10 18:20:57 +0200207 for (; sg; sg = sg_next(sg)) {
Christof Schmitt1674b402010-04-30 18:09:34 +0200208 sbale = zfcp_qdio_sbale_next(qdio, q_req);
Christof Schmitt68322982010-04-30 18:09:33 +0200209 if (!sbale) {
210 atomic_inc(&qdio->req_q_full);
Swen Schillig706eca42010-07-16 15:37:38 +0200211 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
212 q_req->sbal_number);
Christof Schmitt68322982010-04-30 18:09:33 +0200213 return -EINVAL;
214 }
Christof Schmitt68322982010-04-30 18:09:33 +0200215 sbale->addr = sg_virt(sg);
216 sbale->length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Swen Schillig86a96682011-08-15 14:40:32 +0200218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200221static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
222{
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200223 spin_lock_irq(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200224 if (atomic_read(&qdio->req_q_free) ||
Christof Schmittc2af7542010-06-21 10:11:32 +0200225 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200226 return 1;
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200227 spin_unlock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200228 return 0;
229}
230
231/**
232 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
233 * @qdio: pointer to struct zfcp_qdio
234 *
235 * The req_q_lock must be held by the caller of this function, and
236 * this function may only be called from process context; it will
237 * sleep when waiting for a free sbal.
238 *
239 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
240 */
241int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
242{
243 long ret;
244
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200245 spin_unlock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200246 ret = wait_event_interruptible_timeout(qdio->req_q_wq,
247 zfcp_qdio_sbal_check(qdio), 5 * HZ);
Christof Schmittc2af7542010-06-21 10:11:32 +0200248
249 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
250 return -EIO;
251
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200252 if (ret > 0)
253 return 0;
Christof Schmittc2af7542010-06-21 10:11:32 +0200254
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200255 if (!ret) {
256 atomic_inc(&qdio->req_q_full);
257 /* assume hanging outbound queue, try queue recovery */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100258 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200259 }
260
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200261 spin_lock_irq(&qdio->req_q_lock);
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200262 return -EIO;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
Swen Schillig00bab912008-06-10 18:20:57 +0200266 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200267 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100268 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200269 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100271int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100273 int retval;
Swen Schillig706eca42010-07-16 15:37:38 +0200274 u8 sbal_number = q_req->sbal_number;
Swen Schillig00bab912008-06-10 18:20:57 +0200275
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200276 spin_lock(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +0200277 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200278 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +0100279
Swen Schillig706eca42010-07-16 15:37:38 +0200280 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
281 q_req->sbal_first, sbal_number);
282
Swen Schillig00bab912008-06-10 18:20:57 +0200283 if (unlikely(retval)) {
Swen Schillig706eca42010-07-16 15:37:38 +0200284 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
285 sbal_number);
Swen Schillig00bab912008-06-10 18:20:57 +0200286 return retval;
287 }
288
289 /* account for transferred buffers */
Swen Schillig706eca42010-07-16 15:37:38 +0200290 atomic_sub(sbal_number, &qdio->req_q_free);
291 qdio->req_q_idx += sbal_number;
292 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
293
Swen Schillig00bab912008-06-10 18:20:57 +0200294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Swen Schillig564e1c82009-08-18 15:43:19 +0200297
298static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
299 struct zfcp_qdio *qdio)
300{
Jan Glauberd36deae2010-09-07 21:14:39 +0000301 memset(id, 0, sizeof(*id));
Swen Schillig564e1c82009-08-18 15:43:19 +0200302 id->cdev = qdio->adapter->ccw_device;
303 id->q_format = QDIO_ZFCP_QFMT;
304 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
305 ASCEBC(id->adapter_name, 8);
Christof Schmittdcc18f42010-07-16 15:37:41 +0200306 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
Swen Schillig86a96682011-08-15 14:40:32 +0200307 if (enable_multibuffer)
308 id->qdr_ac |= QDR_AC_MULTI_BUFFER_ENABLE;
Swen Schillig564e1c82009-08-18 15:43:19 +0200309 id->no_input_qs = 1;
310 id->no_output_qs = 1;
311 id->input_handler = zfcp_qdio_int_resp;
312 id->output_handler = zfcp_qdio_int_req;
313 id->int_parm = (unsigned long) qdio;
Swen Schillig706eca42010-07-16 15:37:38 +0200314 id->input_sbal_addr_array = (void **) (qdio->res_q);
315 id->output_sbal_addr_array = (void **) (qdio->req_q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +0100316 id->scan_threshold =
317 QDIO_MAX_BUFFERS_PER_Q - ZFCP_QDIO_MAX_SBALS_PER_REQ * 2;
Swen Schillig564e1c82009-08-18 15:43:19 +0200318}
Christof Schmittdcc18f42010-07-16 15:37:41 +0200319
Swen Schillig00bab912008-06-10 18:20:57 +0200320/**
Swen Schillig00bab912008-06-10 18:20:57 +0200321 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
322 * @adapter: pointer to struct zfcp_adapter
323 * Returns: -ENOMEM on memory allocation error or return value from
324 * qdio_allocate
325 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200326static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200327{
Swen Schillig564e1c82009-08-18 15:43:19 +0200328 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200329
Swen Schillig706eca42010-07-16 15:37:38 +0200330 if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
331 zfcp_qdio_buffers_enqueue(qdio->res_q))
Swen Schillig00bab912008-06-10 18:20:57 +0200332 return -ENOMEM;
333
Swen Schillig564e1c82009-08-18 15:43:19 +0200334 zfcp_qdio_setup_init_data(&init_data, qdio);
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100335 init_waitqueue_head(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +0200336
Swen Schillig564e1c82009-08-18 15:43:19 +0200337 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200338}
339
340/**
341 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200342 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200343 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200344void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200345{
Swen Schillig706eca42010-07-16 15:37:38 +0200346 struct zfcp_adapter *adapter = qdio->adapter;
347 int idx, count;
Swen Schillig00bab912008-06-10 18:20:57 +0200348
Swen Schillig706eca42010-07-16 15:37:38 +0200349 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200350 return;
351
352 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200353 spin_lock_irq(&qdio->req_q_lock);
Swen Schillig706eca42010-07-16 15:37:38 +0200354 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200355 spin_unlock_irq(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200356
Christof Schmittc2af7542010-06-21 10:11:32 +0200357 wake_up(&qdio->req_q_wq);
358
Swen Schillig706eca42010-07-16 15:37:38 +0200359 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200360
361 /* cleanup used outbound sbals */
Swen Schillig706eca42010-07-16 15:37:38 +0200362 count = atomic_read(&qdio->req_q_free);
Swen Schillig00bab912008-06-10 18:20:57 +0200363 if (count < QDIO_MAX_BUFFERS_PER_Q) {
Swen Schillig706eca42010-07-16 15:37:38 +0200364 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200365 count = QDIO_MAX_BUFFERS_PER_Q - count;
Swen Schillig706eca42010-07-16 15:37:38 +0200366 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +0200367 }
Swen Schillig706eca42010-07-16 15:37:38 +0200368 qdio->req_q_idx = 0;
369 atomic_set(&qdio->req_q_free, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200370}
371
372/**
373 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200374 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200375 * Returns: 0 on success, otherwise -EIO
376 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200377int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200378{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200379 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200380 struct qdio_initialize init_data;
Swen Schillig706eca42010-07-16 15:37:38 +0200381 struct zfcp_adapter *adapter = qdio->adapter;
382 struct ccw_device *cdev = adapter->ccw_device;
Christof Schmittdcc18f42010-07-16 15:37:41 +0200383 struct qdio_ssqd_desc ssqd;
Swen Schillig00bab912008-06-10 18:20:57 +0200384 int cc;
385
Swen Schillig706eca42010-07-16 15:37:38 +0200386 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200387 return -EIO;
388
Christof Schmitt339f4f42010-07-16 15:37:43 +0200389 atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
390 &qdio->adapter->status);
391
Swen Schillig564e1c82009-08-18 15:43:19 +0200392 zfcp_qdio_setup_init_data(&init_data, qdio);
393
394 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200395 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200396
Christof Schmittdcc18f42010-07-16 15:37:41 +0200397 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
398 goto failed_qdio;
399
400 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
401 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
402 &qdio->adapter->status);
403
Swen Schillig86a96682011-08-15 14:40:32 +0200404 if (ssqd.qdioac2 & CHSC_AC2_MULTI_BUFFER_ENABLED) {
405 atomic_set_mask(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
406 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER;
407 } else {
408 atomic_clear_mask(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
409 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER - 1;
410 }
411
412 qdio->max_sbale_per_req =
413 ZFCP_QDIO_MAX_SBALS_PER_REQ * qdio->max_sbale_per_sbal
414 - 2;
Swen Schillig564e1c82009-08-18 15:43:19 +0200415 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200416 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200417
418 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200419 sbale = &(qdio->res_q[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200420 sbale->length = 0;
Jan Glauber3ec908782011-06-06 14:14:40 +0200421 sbale->eflags = SBAL_EFLAGS_LAST_ENTRY;
422 sbale->sflags = 0;
Swen Schillig00bab912008-06-10 18:20:57 +0200423 sbale->addr = NULL;
424 }
425
Swen Schillig706eca42010-07-16 15:37:38 +0200426 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200427 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200428
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 /* set index of first available SBALS / number of available SBALS */
Swen Schillig706eca42010-07-16 15:37:38 +0200430 qdio->req_q_idx = 0;
431 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100432 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
Swen Schillig00bab912008-06-10 18:20:57 +0200433
Swen Schillig86a96682011-08-15 14:40:32 +0200434 if (adapter->scsi_host) {
435 adapter->scsi_host->sg_tablesize = qdio->max_sbale_per_req;
436 adapter->scsi_host->max_sectors = qdio->max_sbale_per_req * 8;
437 }
438
Swen Schillig00bab912008-06-10 18:20:57 +0200439 return 0;
440
441failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200442 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200443failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200444 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200445 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200446 return -EIO;
447}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200448
449void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
450{
Swen Schilligd5a282a2009-08-18 15:43:22 +0200451 int p;
452
453 if (!qdio)
454 return;
455
456 if (qdio->adapter->ccw_device)
457 qdio_free(qdio->adapter->ccw_device);
458
Swen Schilligd5a282a2009-08-18 15:43:22 +0200459 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
Swen Schillig706eca42010-07-16 15:37:38 +0200460 free_page((unsigned long) qdio->req_q[p]);
461 free_page((unsigned long) qdio->res_q[p]);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200462 }
463
464 kfree(qdio);
465}
466
467int zfcp_qdio_setup(struct zfcp_adapter *adapter)
468{
469 struct zfcp_qdio *qdio;
470
471 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
472 if (!qdio)
473 return -ENOMEM;
474
475 qdio->adapter = adapter;
476
477 if (zfcp_qdio_allocate(qdio)) {
478 zfcp_qdio_destroy(qdio);
479 return -ENOMEM;
480 }
481
482 spin_lock_init(&qdio->req_q_lock);
483 spin_lock_init(&qdio->stat_lock);
484
485 adapter->qdio = qdio;
486 return 0;
487}
488
Christof Schmitt339f4f42010-07-16 15:37:43 +0200489/**
490 * zfcp_qdio_siosl - Trigger logging in FCP channel
491 * @adapter: The zfcp_adapter where to trigger logging
492 *
493 * Call the cio siosl function to trigger hardware logging. This
494 * wrapper function sets a flag to ensure hardware logging is only
495 * triggered once before going through qdio shutdown.
496 *
497 * The triggers are always run from qdio tasklet context, so no
498 * additional synchronization is necessary.
499 */
500void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
501{
502 int rc;
503
504 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
505 return;
506
507 rc = ccw_device_siosl(adapter->ccw_device);
508 if (!rc)
509 atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
510 &adapter->status);
511}