Merge branch 'akpm'
[linux-2.6/next.git] / drivers / s390 / scsi / zfcp_qdio.c
blobdf9e69f5474204220d54f7498c724e56ae41f383
1 /*
2 * zfcp device driver
4 * Setup and helper functions to access QDIO.
6 * Copyright IBM Corporation 2002, 2010
7 */
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/slab.h>
13 #include "zfcp_ext.h"
14 #include "zfcp_qdio.h"
16 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
18 static bool enable_multibuffer;
19 module_param_named(datarouter, enable_multibuffer, bool, 0400);
20 MODULE_PARM_DESC(datarouter, "Enable hardware data router support");
22 static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
24 int pos;
26 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
27 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
28 if (!sbal[pos])
29 return -ENOMEM;
31 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
32 if (pos % QBUFF_PER_PAGE)
33 sbal[pos] = sbal[pos - 1] + 1;
34 return 0;
37 static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id,
38 unsigned int qdio_err)
40 struct zfcp_adapter *adapter = qdio->adapter;
42 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
44 if (qdio_err & QDIO_ERROR_SLSB_STATE) {
45 zfcp_qdio_siosl(adapter);
46 zfcp_erp_adapter_shutdown(adapter, 0, id);
47 return;
49 zfcp_erp_adapter_reopen(adapter,
50 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
51 ZFCP_STATUS_COMMON_ERP_FAILED, id);
54 static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
56 int i, sbal_idx;
58 for (i = first; i < first + cnt; i++) {
59 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
60 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
64 /* this needs to be called prior to updating the queue fill level */
65 static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
67 unsigned long long now, span;
68 int used;
70 now = get_clock_monotonic();
71 span = (now - qdio->req_q_time) >> 12;
72 used = QDIO_MAX_BUFFERS_PER_Q - atomic_read(&qdio->req_q_free);
73 qdio->req_q_util += used * span;
74 qdio->req_q_time = now;
77 static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
78 int queue_no, int idx, int count,
79 unsigned long parm)
81 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
83 if (unlikely(qdio_err)) {
84 zfcp_qdio_handler_error(qdio, "qdireq1", qdio_err);
85 return;
88 /* cleanup all SBALs being program-owned now */
89 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
91 spin_lock_irq(&qdio->stat_lock);
92 zfcp_qdio_account(qdio);
93 spin_unlock_irq(&qdio->stat_lock);
94 atomic_add(count, &qdio->req_q_free);
95 wake_up(&qdio->req_q_wq);
98 static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
99 int queue_no, int idx, int count,
100 unsigned long parm)
102 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
103 struct zfcp_adapter *adapter = qdio->adapter;
104 struct qdio_buffer_element *sbale;
105 int sbal_no, sbal_idx;
106 void *pl[ZFCP_QDIO_MAX_SBALS_PER_REQ + 1];
107 u64 req_id;
108 u8 scount;
110 if (unlikely(qdio_err)) {
111 memset(pl, 0, ZFCP_QDIO_MAX_SBALS_PER_REQ * sizeof(void *));
112 if (zfcp_adapter_multi_buffer_active(adapter)) {
113 sbale = qdio->res_q[idx]->element;
114 req_id = (u64) sbale->addr;
115 scount = sbale->scount + 1; /* incl. signaling SBAL */
117 for (sbal_no = 0; sbal_no < scount; sbal_no++) {
118 sbal_idx = (idx + sbal_no) %
119 QDIO_MAX_BUFFERS_PER_Q;
120 pl[sbal_no] = qdio->res_q[sbal_idx];
122 zfcp_dbf_hba_def_err(adapter, req_id, scount, pl);
124 zfcp_qdio_handler_error(qdio, "qdires1", qdio_err);
125 return;
129 * go through all SBALs from input queue currently
130 * returned by QDIO layer
132 for (sbal_no = 0; sbal_no < count; sbal_no++) {
133 sbal_idx = (idx + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
134 /* go through all SBALEs of SBAL */
135 zfcp_fsf_reqid_check(qdio, sbal_idx);
139 * put SBALs back to response queue
141 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, idx, count))
142 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdires2");
145 static struct qdio_buffer_element *
146 zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
148 struct qdio_buffer_element *sbale;
150 /* set last entry flag in current SBALE of current SBAL */
151 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
152 sbale->eflags |= SBAL_EFLAGS_LAST_ENTRY;
154 /* don't exceed last allowed SBAL */
155 if (q_req->sbal_last == q_req->sbal_limit)
156 return NULL;
158 /* set chaining flag in first SBALE of current SBAL */
159 sbale = zfcp_qdio_sbale_req(qdio, q_req);
160 sbale->sflags |= SBAL_SFLAGS0_MORE_SBALS;
162 /* calculate index of next SBAL */
163 q_req->sbal_last++;
164 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
166 /* keep this requests number of SBALs up-to-date */
167 q_req->sbal_number++;
168 BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
170 /* start at first SBALE of new SBAL */
171 q_req->sbale_curr = 0;
173 /* set storage-block type for new SBAL */
174 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
175 sbale->sflags |= q_req->sbtype;
177 return sbale;
180 static struct qdio_buffer_element *
181 zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
183 if (q_req->sbale_curr == qdio->max_sbale_per_sbal - 1)
184 return zfcp_qdio_sbal_chain(qdio, q_req);
185 q_req->sbale_curr++;
186 return zfcp_qdio_sbale_curr(qdio, q_req);
190 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
191 * @qdio: pointer to struct zfcp_qdio
192 * @q_req: pointer to struct zfcp_qdio_req
193 * @sg: scatter-gather list
194 * @max_sbals: upper bound for number of SBALs to be used
195 * Returns: zero or -EINVAL on error
197 int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
198 struct scatterlist *sg)
200 struct qdio_buffer_element *sbale;
202 /* set storage-block type for this request */
203 sbale = zfcp_qdio_sbale_req(qdio, q_req);
204 sbale->sflags |= q_req->sbtype;
206 for (; sg; sg = sg_next(sg)) {
207 sbale = zfcp_qdio_sbale_next(qdio, q_req);
208 if (!sbale) {
209 atomic_inc(&qdio->req_q_full);
210 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
211 q_req->sbal_number);
212 return -EINVAL;
214 sbale->addr = sg_virt(sg);
215 sbale->length = sg->length;
217 return 0;
220 static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
222 spin_lock_irq(&qdio->req_q_lock);
223 if (atomic_read(&qdio->req_q_free) ||
224 !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
225 return 1;
226 spin_unlock_irq(&qdio->req_q_lock);
227 return 0;
231 * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
232 * @qdio: pointer to struct zfcp_qdio
234 * The req_q_lock must be held by the caller of this function, and
235 * this function may only be called from process context; it will
236 * sleep when waiting for a free sbal.
238 * Returns: 0 on success, -EIO if there is no free sbal after waiting.
240 int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
242 long ret;
244 spin_unlock_irq(&qdio->req_q_lock);
245 ret = wait_event_interruptible_timeout(qdio->req_q_wq,
246 zfcp_qdio_sbal_check(qdio), 5 * HZ);
248 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
249 return -EIO;
251 if (ret > 0)
252 return 0;
254 if (!ret) {
255 atomic_inc(&qdio->req_q_full);
256 /* assume hanging outbound queue, try queue recovery */
257 zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1");
260 spin_lock_irq(&qdio->req_q_lock);
261 return -EIO;
265 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
266 * @qdio: pointer to struct zfcp_qdio
267 * @q_req: pointer to struct zfcp_qdio_req
268 * Returns: 0 on success, error otherwise
270 int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
272 int retval;
273 u8 sbal_number = q_req->sbal_number;
275 spin_lock(&qdio->stat_lock);
276 zfcp_qdio_account(qdio);
277 spin_unlock(&qdio->stat_lock);
279 retval = do_QDIO(qdio->adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0,
280 q_req->sbal_first, sbal_number);
282 if (unlikely(retval)) {
283 zfcp_qdio_zero_sbals(qdio->req_q, q_req->sbal_first,
284 sbal_number);
285 return retval;
288 /* account for transferred buffers */
289 atomic_sub(sbal_number, &qdio->req_q_free);
290 qdio->req_q_idx += sbal_number;
291 qdio->req_q_idx %= QDIO_MAX_BUFFERS_PER_Q;
293 return 0;
297 static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
298 struct zfcp_qdio *qdio)
300 memset(id, 0, sizeof(*id));
301 id->cdev = qdio->adapter->ccw_device;
302 id->q_format = QDIO_ZFCP_QFMT;
303 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
304 ASCEBC(id->adapter_name, 8);
305 id->qib_rflags = QIB_RFLAGS_ENABLE_DATA_DIV;
306 if (enable_multibuffer)
307 id->qdr_ac |= QDR_AC_MULTI_BUFFER_ENABLE;
308 id->no_input_qs = 1;
309 id->no_output_qs = 1;
310 id->input_handler = zfcp_qdio_int_resp;
311 id->output_handler = zfcp_qdio_int_req;
312 id->int_parm = (unsigned long) qdio;
313 id->input_sbal_addr_array = (void **) (qdio->res_q);
314 id->output_sbal_addr_array = (void **) (qdio->req_q);
315 id->scan_threshold =
316 QDIO_MAX_BUFFERS_PER_Q - ZFCP_QDIO_MAX_SBALS_PER_REQ * 2;
320 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
321 * @adapter: pointer to struct zfcp_adapter
322 * Returns: -ENOMEM on memory allocation error or return value from
323 * qdio_allocate
325 static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
327 struct qdio_initialize init_data;
329 if (zfcp_qdio_buffers_enqueue(qdio->req_q) ||
330 zfcp_qdio_buffers_enqueue(qdio->res_q))
331 return -ENOMEM;
333 zfcp_qdio_setup_init_data(&init_data, qdio);
334 init_waitqueue_head(&qdio->req_q_wq);
336 return qdio_allocate(&init_data);
340 * zfcp_close_qdio - close qdio queues for an adapter
341 * @qdio: pointer to structure zfcp_qdio
343 void zfcp_qdio_close(struct zfcp_qdio *qdio)
345 struct zfcp_adapter *adapter = qdio->adapter;
346 int idx, count;
348 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
349 return;
351 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
352 spin_lock_irq(&qdio->req_q_lock);
353 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
354 spin_unlock_irq(&qdio->req_q_lock);
356 wake_up(&qdio->req_q_wq);
358 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
360 /* cleanup used outbound sbals */
361 count = atomic_read(&qdio->req_q_free);
362 if (count < QDIO_MAX_BUFFERS_PER_Q) {
363 idx = (qdio->req_q_idx + count) % QDIO_MAX_BUFFERS_PER_Q;
364 count = QDIO_MAX_BUFFERS_PER_Q - count;
365 zfcp_qdio_zero_sbals(qdio->req_q, idx, count);
367 qdio->req_q_idx = 0;
368 atomic_set(&qdio->req_q_free, 0);
372 * zfcp_qdio_open - prepare and initialize response queue
373 * @qdio: pointer to struct zfcp_qdio
374 * Returns: 0 on success, otherwise -EIO
376 int zfcp_qdio_open(struct zfcp_qdio *qdio)
378 struct qdio_buffer_element *sbale;
379 struct qdio_initialize init_data;
380 struct zfcp_adapter *adapter = qdio->adapter;
381 struct ccw_device *cdev = adapter->ccw_device;
382 struct qdio_ssqd_desc ssqd;
383 int cc;
385 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
386 return -EIO;
388 atomic_clear_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
389 &qdio->adapter->status);
391 zfcp_qdio_setup_init_data(&init_data, qdio);
393 if (qdio_establish(&init_data))
394 goto failed_establish;
396 if (qdio_get_ssqd_desc(init_data.cdev, &ssqd))
397 goto failed_qdio;
399 if (ssqd.qdioac2 & CHSC_AC2_DATA_DIV_ENABLED)
400 atomic_set_mask(ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED,
401 &qdio->adapter->status);
403 if (ssqd.qdioac2 & CHSC_AC2_MULTI_BUFFER_ENABLED) {
404 atomic_set_mask(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
405 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER;
406 } else {
407 atomic_clear_mask(ZFCP_STATUS_ADAPTER_MB_ACT, &adapter->status);
408 qdio->max_sbale_per_sbal = QDIO_MAX_ELEMENTS_PER_BUFFER - 1;
411 qdio->max_sbale_per_req =
412 ZFCP_QDIO_MAX_SBALS_PER_REQ * qdio->max_sbale_per_sbal
413 - 2;
414 if (qdio_activate(cdev))
415 goto failed_qdio;
417 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
418 sbale = &(qdio->res_q[cc]->element[0]);
419 sbale->length = 0;
420 sbale->eflags = SBAL_EFLAGS_LAST_ENTRY;
421 sbale->sflags = 0;
422 sbale->addr = NULL;
425 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, QDIO_MAX_BUFFERS_PER_Q))
426 goto failed_qdio;
428 /* set index of first available SBALS / number of available SBALS */
429 qdio->req_q_idx = 0;
430 atomic_set(&qdio->req_q_free, QDIO_MAX_BUFFERS_PER_Q);
431 atomic_set_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
433 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;
438 return 0;
440 failed_qdio:
441 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
442 failed_establish:
443 dev_err(&cdev->dev,
444 "Setting up the QDIO connection to the FCP adapter failed\n");
445 return -EIO;
448 void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
450 int p;
452 if (!qdio)
453 return;
455 if (qdio->adapter->ccw_device)
456 qdio_free(qdio->adapter->ccw_device);
458 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
459 free_page((unsigned long) qdio->req_q[p]);
460 free_page((unsigned long) qdio->res_q[p]);
463 kfree(qdio);
466 int zfcp_qdio_setup(struct zfcp_adapter *adapter)
468 struct zfcp_qdio *qdio;
470 qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
471 if (!qdio)
472 return -ENOMEM;
474 qdio->adapter = adapter;
476 if (zfcp_qdio_allocate(qdio)) {
477 zfcp_qdio_destroy(qdio);
478 return -ENOMEM;
481 spin_lock_init(&qdio->req_q_lock);
482 spin_lock_init(&qdio->stat_lock);
484 adapter->qdio = qdio;
485 return 0;
489 * zfcp_qdio_siosl - Trigger logging in FCP channel
490 * @adapter: The zfcp_adapter where to trigger logging
492 * Call the cio siosl function to trigger hardware logging. This
493 * wrapper function sets a flag to ensure hardware logging is only
494 * triggered once before going through qdio shutdown.
496 * The triggers are always run from qdio tasklet context, so no
497 * additional synchronization is necessary.
499 void zfcp_qdio_siosl(struct zfcp_adapter *adapter)
501 int rc;
503 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_SIOSL_ISSUED)
504 return;
506 rc = ccw_device_siosl(adapter->ccw_device);
507 if (!rc)
508 atomic_set_mask(ZFCP_STATUS_ADAPTER_SIOSL_ISSUED,
509 &adapter->status);