blob: 4ab02e8d36f32ff4fe77a4e8fcbf74f9275ada65 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Swen Schillig00bab912008-06-10 18:20:57 +02003 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Swen Schillig00bab912008-06-10 18:20:57 +02005 * Setup and helper functions to access QDIO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02007 * Copyright IBM Corp. 2002, 2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Christof Schmittecf39d42008-12-25 13:39:53 +010010#define KMSG_COMPONENT "zfcp"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "zfcp_ext.h"
Christof Schmitt34c2b712010-02-17 11:18:59 +010016#include "zfcp_qdio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Benjamin Block51569342017-07-28 12:30:50 +020018static bool enable_multibuffer = true;
Swen Schillig86a96682011-08-15 14:40:32 +020019module_param_named(datarouter, enable_multibuffer, bool, 0400);
Steffen Maier909ee492013-08-22 17:49:34 +020020MODULE_PARM_DESC(datarouter, "Enable hardware data router support (default on)");
Swen Schillig86a96682011-08-15 14:40:32 +020021
Christof Schmitt339f4f42010-07-16 15:37:43 +020022static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
23 unsigned int qdio_err)
Swen Schillig00bab912008-06-10 18:20:57 +020024{
Swen Schillig564e1c82009-08-18 15:43:19 +020025 struct zfcp_adapter *adapter = qdio->adapter;
26
Christof Schmittff3b24f2008-10-01 12:42:15 +020027 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020028
Swen Schillig86a96682011-08-15 14:40:32 +020029 if (qdio_err & QDIO_ERROR_SLSB_STATE) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020030 zfcp_qdio_siosl(adapter);
Swen Schillig86a96682011-08-15 14:40:32 +020031 zfcp_erp_adapter_shutdown(adapter, 0, id);
32 return;
33 }
Swen Schillig00bab912008-06-10 18:20:57 +020034 zfcp_erp_adapter_reopen(adapter,
35 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
Swen Schilligea4a3a62010-12-02 15:16:16 +010036 ZFCP_STATUS_COMMON_ERP_FAILED, id);
Swen Schillig00bab912008-06-10 18:20:57 +020037}
38
Christof Schmitt5d4e2262008-07-02 10:56:34 +020039static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
40{
41 int i, sbal_idx;
42
43 for (i = first; i < first + cnt; i++) {
44 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
45 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
46 }
47}
48
Martin Peschke94506fd2009-03-02 13:08:56 +010049/* this needs to be called prior to updating the queue fill level */
Heiko Carstens41e05a12009-08-18 15:43:32 +020050static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010051{
Heiko Carstens41e05a12009-08-18 15:43:32 +020052 unsigned long long now, span;
Swen Schillig706eca42010-07-16 15:37:38 +020053 int used;
Martin Peschke94506fd2009-03-02 13:08:56 +010054
Heiko Carstens1aae0562013-01-30 09:49:40 +010055 now = get_tod_clock_monotonic();
Heiko Carstens41e05a12009-08-18 15:43:32 +020056 span = (now - qdio->req_q_time) >> 12;
Swen Schillig706eca42010-07-16 15:37:38 +020057 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020058 qdio->req_q_util += used * span;
59 qdio->req_q_time = now;
Martin Peschke94506fd2009-03-02 13:08:56 +010060}
61
Jan Glauber779e6e12008-07-17 17:16:48 +020062static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020063 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020064 unsigned long parm)
65{
Swen Schillig564e1c82009-08-18 15:43:19 +020066 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schillig00bab912008-06-10 18:20:57 +020067
Jan Glauber779e6e12008-07-17 17:16:48 +020068 if (unlikely(qdio_err)) {
Christof Schmitt339f4f42010-07-16 15:37:43 +020069 zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
Swen Schillig00bab912008-06-10 18:20:57 +020070 return;
71 }
72
73 /* cleanup all SBALs being program-owned now */
Swen Schillig706eca42010-07-16 15:37:38 +020074 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +020075
Christof Schmitt44a24cb2010-09-08 14:39:57 +020076 spin_lock_irq(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +020077 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +020078 spin_unlock_irq(&qdio->stat_lock);
Swen Schillig706eca42010-07-16 15:37:38 +020079 atomic_add(count, &qdio->req_q_free);
Swen Schillig564e1c82009-08-18 15:43:19 +020080 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +020081}
82
Jan Glauber779e6e12008-07-17 17:16:48 +020083static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
Swen Schillig706eca42010-07-16 15:37:38 +020084 int queue_no, int idx, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020085 unsigned long parm)
86{
Swen Schillig564e1c82009-08-18 15:43:19 +020087 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schillig86a96682011-08-15 14:40:32 +020088 struct zfcp_adapter *adapter = qdio->adapter;
Swen Schillig86a96682011-08-15 14:40:32 +020089 int sbal_no, sbal_idx;
Swen Schillig00bab912008-06-10 18:20:57 +020090
Jan Glauber779e6e12008-07-17 17:16:48 +020091 if (unlikely(qdio_err)) {
Swen Schillig86a96682011-08-15 14:40:32 +020092 if (zfcp_adapter_multi_buffer_active(adapter)) {
Steffen Maier01e60522012-09-04 15:23:31 +020093 void *pl[ZFCP_QDIO_MAX_SBALS_PER_REQ + 1];
94 struct qdio_buffer_element *sbale;
95 u64 req_id;
96 u8 scount;
97
98 memset(pl, 0,
99 ZFCP_QDIO_MAX_SBALS_PER_REQ * sizeof(void *));
Swen Schillig86a96682011-08-15 14:40:32 +0200100 sbale = qdio->res_q[idx]->element;
101 req_id = (u64) sbale->addr;
Steffen Maier01e60522012-09-04 15:23:31 +0200102 scount = min(sbale->scount + 1,
103 ZFCP_QDIO_MAX_SBALS_PER_REQ + 1);
104 /* incl. signaling SBAL */
Swen Schillig86a96682011-08-15 14:40:32 +0200105
106 for (sbal_no = 0; sbal_no < scount; sbal_no++) {
107 sbal_idx = (idx + sbal_no) %
108 QDIO_MAX_BUFFERS_PER_Q;
109 pl[sbal_no] = qdio->res_q[sbal_idx];
110 }
111 zfcp_dbf_hba_def_err(adapter, req_id, scount, pl);
112 }
Christof Schmitt339f4f42010-07-16 15:37:43 +0200113 zfcp_qdio_handler_error(qdio, "qdires1", qdio_err);
Swen Schillig00bab912008-06-10 18:20:57 +0200114 return;
115 }
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /*
118 * go through all SBALs from input queue currently
119 * returned by QDIO layer
120 */
Swen Schillig00bab912008-06-10 18:20:57 +0200121 for (sbal_no = 0; sbal_no < count; sbal_no++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200122 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200124 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 /*
Swen Schillig706eca42010-07-16 15:37:38 +0200128 * put SBALs back to response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 */
Swen Schillig706eca42010-07-16 15:37:38 +0200130 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
Swen Schilligea4a3a62010-12-02 15:16:16 +0100131 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Swen Schillig44cc76f2008-10-01 12:42:16 +0200134static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200135zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200137 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200140 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200141 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200144 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return NULL;
146
147 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200148 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200149 sbale->sflags |= SBAL_SFLAGS0_MORE_SBALS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200152 q_req->sbal_last++;
153 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200156 q_req->sbal_number++;
Swen Schillig01b04752010-07-16 15:37:37 +0200157 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200160 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200163 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200164 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return sbale;
167}
168
Swen Schillig44cc76f2008-10-01 12:42:16 +0200169static struct qdio_buffer_element *
Christof Schmitt1674b402010-04-30 18:09:34 +0200170zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Swen Schillig86a96682011-08-15 14:40:32 +0200172 if (q_req->sbale_curr == qdio->max_sbale_per_sbal - 1)
Christof Schmitt1674b402010-04-30 18:09:34 +0200173 return zfcp_qdio_sbal_chain(qdio, q_req);
Swen Schillig42428f72009-08-18 15:43:18 +0200174 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200175 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/**
179 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
Christof Schmitt1674b402010-04-30 18:09:34 +0200180 * @qdio: pointer to struct zfcp_qdio
181 * @q_req: pointer to struct zfcp_qdio_req
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig86a96682011-08-15 14:40:32 +0200184 * Returns: zero or -EINVAL on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100186int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
Swen Schillig01b04752010-07-16 15:37:37 +0200187 struct scatterlist *sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200189 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Swen Schillig00bab912008-06-10 18:20:57 +0200191 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200192 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Jan Glauber3ec908782011-06-06 14:14:40 +0200193 sbale->sflags |= q_req->sbtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Swen Schillig00bab912008-06-10 18:20:57 +0200195 for (; sg; sg = sg_next(sg)) {
Christof Schmitt1674b402010-04-30 18:09:34 +0200196 sbale = zfcp_qdio_sbale_next(qdio, q_req);
Christof Schmitt68322982010-04-30 18:09:33 +0200197 if (!sbale) {
198 atomic_inc(&qdio->req_q_full);
Swen Schillig706eca42010-07-16 15:37:38 +0200199 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
200 q_req->sbal_number);
Christof Schmitt68322982010-04-30 18:09:33 +0200201 return -EINVAL;
202 }
Christof Schmitt68322982010-04-30 18:09:33 +0200203 sbale->addr = sg_virt(sg);
204 sbale->length = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Swen Schillig86a96682011-08-15 14:40:32 +0200206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200209static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
210{
Swen Schillig706eca42010-07-16 15:37:38 +0200211 if (atomic_read(&qdio->req_q_free) ||
Christof Schmittc2af7542010-06-21 10:11:32 +0200212 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200213 return 1;
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200214 return 0;
215}
216
217/**
218 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
219 * @qdio: pointer to struct zfcp_qdio
220 *
221 * The req_q_lock must be held by the caller of this function, and
222 * this function may only be called from process context; it will
223 * sleep when waiting for a free sbal.
224 *
225 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
226 */
227int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
228{
229 long ret;
230
Martin Peschked79ff142013-08-22 17:45:36 +0200231 ret = wait_event_interruptible_lock_irq_timeout(qdio->req_q_wq,
232 zfcp_qdio_sbal_check(qdio), qdio->req_q_lock, 5 * HZ);
Christof Schmittc2af7542010-06-21 10:11:32 +0200233
234 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
235 return -EIO;
236
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200237 if (ret > 0)
238 return 0;
Christof Schmittc2af7542010-06-21 10:11:32 +0200239
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200240 if (!ret) {
241 atomic_inc(&qdio->req_q_full);
242 /* assume hanging outbound queue, try queue recovery */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100243 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200244 }
245
Christof Schmitt6b9e1522010-04-30 18:09:35 +0200246 return -EIO;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/**
Swen Schillig00bab912008-06-10 18:20:57 +0200250 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200251 * @qdio: pointer to struct zfcp_qdio
Christof Schmitt34c2b712010-02-17 11:18:59 +0100252 * @q_req: pointer to struct zfcp_qdio_req
Swen Schillig00bab912008-06-10 18:20:57 +0200253 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Christof Schmitt34c2b712010-02-17 11:18:59 +0100255int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100257 int retval;
Swen Schillig706eca42010-07-16 15:37:38 +0200258 u8 sbal_number = q_req->sbal_number;
Swen Schillig00bab912008-06-10 18:20:57 +0200259
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200260 spin_lock(&qdio->stat_lock);
Swen Schillig564e1c82009-08-18 15:43:19 +0200261 zfcp_qdio_account(qdio);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200262 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +0100263
Swen Schillig706eca42010-07-16 15:37:38 +0200264 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
265 q_req->sbal_first, sbal_number);
266
Swen Schillig00bab912008-06-10 18:20:57 +0200267 if (unlikely(retval)) {
Swen Schillig706eca42010-07-16 15:37:38 +0200268 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
269 sbal_number);
Swen Schillig00bab912008-06-10 18:20:57 +0200270 return retval;
271 }
272
273 /* account for transferred buffers */
Swen Schillig706eca42010-07-16 15:37:38 +0200274 atomic_sub(sbal_number, &qdio->req_q_free);
275 qdio->req_q_idx += sbal_number;
276 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
277
Swen Schillig00bab912008-06-10 18:20:57 +0200278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Swen Schillig564e1c82009-08-18 15:43:19 +0200281
282static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
283 struct zfcp_qdio *qdio)
284{
Jan Glauberd36deae2010-09-07 21:14:39 +0000285 memset(id, 0, sizeof(*id));
Swen Schillig564e1c82009-08-18 15:43:19 +0200286 id->cdev = qdio->adapter->ccw_device;
287 id->q_format = QDIO_ZFCP_QFMT;
288 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
289 ASCEBC(id->adapter_name, 8);
Christof Schmittdcc18f42010-07-16 15:37:41 +0200290 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
Swen Schillig86a96682011-08-15 14:40:32 +0200291 if (enable_multibuffer)
292 id->qdr_ac |= QDR_AC_MULTI_BUFFER_ENABLE;
Swen Schillig564e1c82009-08-18 15:43:19 +0200293 id->no_input_qs = 1;
294 id->no_output_qs = 1;
295 id->input_handler = zfcp_qdio_int_resp;
296 id->output_handler = zfcp_qdio_int_req;
297 id->int_parm = (unsigned long) qdio;
Swen Schillig706eca42010-07-16 15:37:38 +0200298 id->input_sbal_addr_array = (void **) (qdio->res_q);
299 id->output_sbal_addr_array = (void **) (qdio->req_q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +0100300 id->scan_threshold =
301 QDIO_MAX_BUFFERS_PER_Q - ZFCP_QDIO_MAX_SBALS_PER_REQ * 2;
Swen Schillig564e1c82009-08-18 15:43:19 +0200302}
Christof Schmittdcc18f42010-07-16 15:37:41 +0200303
Swen Schillig00bab912008-06-10 18:20:57 +0200304/**
Swen Schillig00bab912008-06-10 18:20:57 +0200305 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
306 * @adapter: pointer to struct zfcp_adapter
307 * Returns: -ENOMEM on memory allocation error or return value from
308 * qdio_allocate
309 */
Swen Schilligd5a282a2009-08-18 15:43:22 +0200310static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200311{
Swen Schillig564e1c82009-08-18 15:43:19 +0200312 struct qdio_initialize init_data;
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200313 int ret;
Swen Schillig00bab912008-06-10 18:20:57 +0200314
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200315 ret = qdio_alloc_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
316 if (ret)
Swen Schillig00bab912008-06-10 18:20:57 +0200317 return -ENOMEM;
318
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200319 ret = qdio_alloc_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
320 if (ret)
321 goto free_req_q;
322
Swen Schillig564e1c82009-08-18 15:43:19 +0200323 zfcp_qdio_setup_init_data(&init_data, qdio);
Christof Schmitt3d63d3b2010-12-02 15:16:17 +0100324 init_waitqueue_head(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +0200325
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200326 ret = qdio_allocate(&init_data);
327 if (ret)
328 goto free_res_q;
329
330 return 0;
331
332free_res_q:
333 qdio_free_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
334free_req_q:
335 qdio_free_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
336 return ret;
Swen Schillig00bab912008-06-10 18:20:57 +0200337}
338
339/**
340 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200341 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200342 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200343void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200344{
Swen Schillig706eca42010-07-16 15:37:38 +0200345 struct zfcp_adapter *adapter = qdio->adapter;
346 int idx, count;
Swen Schillig00bab912008-06-10 18:20:57 +0200347
Swen Schillig706eca42010-07-16 15:37:38 +0200348 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200349 return;
350
351 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200352 spin_lock_irq(&qdio->req_q_lock);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200353 atomic_andnot(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
Christof Schmitt44a24cb2010-09-08 14:39:57 +0200354 spin_unlock_irq(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200355
Christof Schmittc2af7542010-06-21 10:11:32 +0200356 wake_up(&qdio->req_q_wq);
357
Swen Schillig706eca42010-07-16 15:37:38 +0200358 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200359
360 /* cleanup used outbound sbals */
Swen Schillig706eca42010-07-16 15:37:38 +0200361 count = atomic_read(&qdio->req_q_free);
Swen Schillig00bab912008-06-10 18:20:57 +0200362 if (count < QDIO_MAX_BUFFERS_PER_Q) {
Swen Schillig706eca42010-07-16 15:37:38 +0200363 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200364 count = QDIO_MAX_BUFFERS_PER_Q - count;
Swen Schillig706eca42010-07-16 15:37:38 +0200365 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
Swen Schillig00bab912008-06-10 18:20:57 +0200366 }
Swen Schillig706eca42010-07-16 15:37:38 +0200367 qdio->req_q_idx = 0;
368 atomic_set(&qdio->req_q_free, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200369}
370
371/**
372 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200373 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200374 * Returns: 0 on success, otherwise -EIO
375 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200376int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200377{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200378 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200379 struct qdio_initialize init_data;
Swen Schillig706eca42010-07-16 15:37:38 +0200380 struct zfcp_adapter *adapter = qdio->adapter;
381 struct ccw_device *cdev = adapter->ccw_device;
Christof Schmittdcc18f42010-07-16 15:37:41 +0200382 struct qdio_ssqd_desc ssqd;
Swen Schillig00bab912008-06-10 18:20:57 +0200383 int cc;
384
Swen Schillig706eca42010-07-16 15:37:38 +0200385 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200386 return -EIO;
387
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200388 atomic_andnot(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
Christof Schmitt339f4f42010-07-16 15:37:43 +0200389 &qdio->adapter->status);
390
Swen Schillig564e1c82009-08-18 15:43:19 +0200391 zfcp_qdio_setup_init_data(&init_data, qdio);
392
393 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200394 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200395
Christof Schmittdcc18f42010-07-16 15:37:41 +0200396 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
397 goto failed_qdio;
398
399 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200400 atomic_or(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
Christof Schmittdcc18f42010-07-16 15:37:41 +0200401 &qdio->adapter->status);
402
Swen Schillig86a96682011-08-15 14:40:32 +0200403 if (ssqd.qdioac2 & CHSC_AC2_MULTI_BUFFER_ENABLED) {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200404 atomic_or(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
Swen Schillig86a96682011-08-15 14:40:32 +0200405 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER;
406 } else {
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200407 atomic_andnot(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
Swen Schillig86a96682011-08-15 14:40:32 +0200408 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER - 1;
409 }
410
411 qdio->max_sbale_per_req =
412 ZFCP_QDIO_MAX_SBALS_PER_REQ * qdio->max_sbale_per_sbal
413 - 2;
Swen Schillig564e1c82009-08-18 15:43:19 +0200414 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200415 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200416
417 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig706eca42010-07-16 15:37:38 +0200418 sbale = &(qdio->res_q[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200419 sbale->length = 0;
Jan Glauber3ec908782011-06-06 14:14:40 +0200420 sbale->eflags = SBAL_EFLAGS_LAST_ENTRY;
421 sbale->sflags = 0;
Swen Schillig00bab912008-06-10 18:20:57 +0200422 sbale->addr = NULL;
423 }
424
Swen Schillig706eca42010-07-16 15:37:38 +0200425 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200426 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200427
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300428 /* set index of first available SBALS / number of available SBALS */
Swen Schillig706eca42010-07-16 15:37:38 +0200429 qdio->req_q_idx = 0;
430 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200431 atomic_or(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
Swen Schillig00bab912008-06-10 18:20:57 +0200432
Swen Schillig86a96682011-08-15 14:40:32 +0200433 if (adapter->scsi_host) {
434 adapter->scsi_host->sg_tablesize = qdio->max_sbale_per_req;
435 adapter->scsi_host->max_sectors = qdio->max_sbale_per_req * 8;
436 }
437
Swen Schillig00bab912008-06-10 18:20:57 +0200438 return 0;
439
440failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200441 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200442failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200443 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200444 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200445 return -EIO;
446}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200447
448void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
449{
Swen Schilligd5a282a2009-08-18 15:43:22 +0200450 if (!qdio)
451 return;
452
453 if (qdio->adapter->ccw_device)
454 qdio_free(qdio->adapter->ccw_device);
455
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200456 qdio_free_buffers(qdio->req_q, QDIO_MAX_BUFFERS_PER_Q);
457 qdio_free_buffers(qdio->res_q, QDIO_MAX_BUFFERS_PER_Q);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200458 kfree(qdio);
459}
460
461int zfcp_qdio_setup(struct zfcp_adapter *adapter)
462{
463 struct zfcp_qdio *qdio;
464
465 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
466 if (!qdio)
467 return -ENOMEM;
468
469 qdio->adapter = adapter;
470
471 if (zfcp_qdio_allocate(qdio)) {
Sebastian Ott852eb1a2014-06-27 17:11:38 +0200472 kfree(qdio);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200473 return -ENOMEM;
474 }
475
476 spin_lock_init(&qdio->req_q_lock);
477 spin_lock_init(&qdio->stat_lock);
478
479 adapter->qdio = qdio;
480 return 0;
481}
482
Christof Schmitt339f4f42010-07-16 15:37:43 +0200483/**
484 * zfcp_qdio_siosl - Trigger logging in FCP channel
485 * @adapter: The zfcp_adapter where to trigger logging
486 *
487 * Call the cio siosl function to trigger hardware logging. This
488 * wrapper function sets a flag to ensure hardware logging is only
489 * triggered once before going through qdio shutdown.
490 *
491 * The triggers are always run from qdio tasklet context, so no
492 * additional synchronization is necessary.
493 */
494void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
495{
496 int rc;
497
498 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
499 return;
500
501 rc = ccw_device_siosl(adapter->ccw_device);
502 if (!rc)
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200503 atomic_or(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
Christof Schmitt339f4f42010-07-16 15:37:43 +0200504 &adapter->status);
505}