Path: blob/master/drivers/infiniband/hw/amso1100/c2_mm.c
15112 views
/*1* Copyright (c) 2005 Ammasso, Inc. All rights reserved.2* Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/32#include <linux/slab.h>3334#include "c2.h"35#include "c2_vq.h"3637#define PBL_VIRT 138#define PBL_PHYS 23940/*41* Send all the PBL messages to convey the remainder of the PBL42* Wait for the adapter's reply on the last one.43* This is indicated by setting the MEM_PBL_COMPLETE in the flags.44*45* NOTE: vq_req is _not_ freed by this function. The VQ Host46* Reply buffer _is_ freed by this function.47*/48static int49send_pbl_messages(struct c2_dev *c2dev, __be32 stag_index,50unsigned long va, u32 pbl_depth,51struct c2_vq_req *vq_req, int pbl_type)52{53u32 pbe_count; /* amt that fits in a PBL msg */54u32 count; /* amt in this PBL MSG. */55struct c2wr_nsmr_pbl_req *wr; /* PBL WR ptr */56struct c2wr_nsmr_pbl_rep *reply; /* reply ptr */57int err, pbl_virt, pbl_index, i;5859switch (pbl_type) {60case PBL_VIRT:61pbl_virt = 1;62break;63case PBL_PHYS:64pbl_virt = 0;65break;66default:67return -EINVAL;68break;69}7071pbe_count = (c2dev->req_vq.msg_size -72sizeof(struct c2wr_nsmr_pbl_req)) / sizeof(u64);73wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);74if (!wr) {75return -ENOMEM;76}77c2_wr_set_id(wr, CCWR_NSMR_PBL);7879/*80* Only the last PBL message will generate a reply from the verbs,81* so we set the context to 0 indicating there is no kernel verbs82* handler blocked awaiting this reply.83*/84wr->hdr.context = 0;85wr->rnic_handle = c2dev->adapter_handle;86wr->stag_index = stag_index; /* already swapped */87wr->flags = 0;88pbl_index = 0;89while (pbl_depth) {90count = min(pbe_count, pbl_depth);91wr->addrs_length = cpu_to_be32(count);9293/*94* If this is the last message, then reference the95* vq request struct cuz we're gonna wait for a reply.96* also make this PBL msg as the last one.97*/98if (count == pbl_depth) {99/*100* reference the request struct. dereferenced in the101* int handler.102*/103vq_req_get(c2dev, vq_req);104wr->flags = cpu_to_be32(MEM_PBL_COMPLETE);105106/*107* This is the last PBL message.108* Set the context to our VQ Request Object so we can109* wait for the reply.110*/111wr->hdr.context = (unsigned long) vq_req;112}113114/*115* If pbl_virt is set then va is a virtual address116* that describes a virtually contiguous memory117* allocation. The wr needs the start of each virtual page118* to be converted to the corresponding physical address119* of the page. If pbl_virt is not set then va is an array120* of physical addresses and there is no conversion to do.121* Just fill in the wr with what is in the array.122*/123for (i = 0; i < count; i++) {124if (pbl_virt) {125va += PAGE_SIZE;126} else {127wr->paddrs[i] =128cpu_to_be64(((u64 *)va)[pbl_index + i]);129}130}131132/*133* Send WR to adapter134*/135err = vq_send_wr(c2dev, (union c2wr *) wr);136if (err) {137if (count <= pbe_count) {138vq_req_put(c2dev, vq_req);139}140goto bail0;141}142pbl_depth -= count;143pbl_index += count;144}145146/*147* Now wait for the reply...148*/149err = vq_wait_for_reply(c2dev, vq_req);150if (err) {151goto bail0;152}153154/*155* Process reply156*/157reply = (struct c2wr_nsmr_pbl_rep *) (unsigned long) vq_req->reply_msg;158if (!reply) {159err = -ENOMEM;160goto bail0;161}162163err = c2_errno(reply);164165vq_repbuf_free(c2dev, reply);166bail0:167kfree(wr);168return err;169}170171#define C2_PBL_MAX_DEPTH 131072172int173c2_nsmr_register_phys_kern(struct c2_dev *c2dev, u64 *addr_list,174int page_size, int pbl_depth, u32 length,175u32 offset, u64 *va, enum c2_acf acf,176struct c2_mr *mr)177{178struct c2_vq_req *vq_req;179struct c2wr_nsmr_register_req *wr;180struct c2wr_nsmr_register_rep *reply;181u16 flags;182int i, pbe_count, count;183int err;184185if (!va || !length || !addr_list || !pbl_depth)186return -EINTR;187188/*189* Verify PBL depth is within rnic max190*/191if (pbl_depth > C2_PBL_MAX_DEPTH) {192return -EINTR;193}194195/*196* allocate verbs request object197*/198vq_req = vq_req_alloc(c2dev);199if (!vq_req)200return -ENOMEM;201202wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);203if (!wr) {204err = -ENOMEM;205goto bail0;206}207208/*209* build the WR210*/211c2_wr_set_id(wr, CCWR_NSMR_REGISTER);212wr->hdr.context = (unsigned long) vq_req;213wr->rnic_handle = c2dev->adapter_handle;214215flags = (acf | MEM_VA_BASED | MEM_REMOTE);216217/*218* compute how many pbes can fit in the message219*/220pbe_count = (c2dev->req_vq.msg_size -221sizeof(struct c2wr_nsmr_register_req)) / sizeof(u64);222223if (pbl_depth <= pbe_count) {224flags |= MEM_PBL_COMPLETE;225}226wr->flags = cpu_to_be16(flags);227wr->stag_key = 0; //stag_key;228wr->va = cpu_to_be64(*va);229wr->pd_id = mr->pd->pd_id;230wr->pbe_size = cpu_to_be32(page_size);231wr->length = cpu_to_be32(length);232wr->pbl_depth = cpu_to_be32(pbl_depth);233wr->fbo = cpu_to_be32(offset);234count = min(pbl_depth, pbe_count);235wr->addrs_length = cpu_to_be32(count);236237/*238* fill out the PBL for this message239*/240for (i = 0; i < count; i++) {241wr->paddrs[i] = cpu_to_be64(addr_list[i]);242}243244/*245* regerence the request struct246*/247vq_req_get(c2dev, vq_req);248249/*250* send the WR to the adapter251*/252err = vq_send_wr(c2dev, (union c2wr *) wr);253if (err) {254vq_req_put(c2dev, vq_req);255goto bail1;256}257258/*259* wait for reply from adapter260*/261err = vq_wait_for_reply(c2dev, vq_req);262if (err) {263goto bail1;264}265266/*267* process reply268*/269reply =270(struct c2wr_nsmr_register_rep *) (unsigned long) (vq_req->reply_msg);271if (!reply) {272err = -ENOMEM;273goto bail1;274}275if ((err = c2_errno(reply))) {276goto bail2;277}278//*p_pb_entries = be32_to_cpu(reply->pbl_depth);279mr->ibmr.lkey = mr->ibmr.rkey = be32_to_cpu(reply->stag_index);280vq_repbuf_free(c2dev, reply);281282/*283* if there are still more PBEs we need to send them to284* the adapter and wait for a reply on the final one.285* reuse vq_req for this purpose.286*/287pbl_depth -= count;288if (pbl_depth) {289290vq_req->reply_msg = (unsigned long) NULL;291atomic_set(&vq_req->reply_ready, 0);292err = send_pbl_messages(c2dev,293cpu_to_be32(mr->ibmr.lkey),294(unsigned long) &addr_list[i],295pbl_depth, vq_req, PBL_PHYS);296if (err) {297goto bail1;298}299}300301vq_req_free(c2dev, vq_req);302kfree(wr);303304return err;305306bail2:307vq_repbuf_free(c2dev, reply);308bail1:309kfree(wr);310bail0:311vq_req_free(c2dev, vq_req);312return err;313}314315int c2_stag_dealloc(struct c2_dev *c2dev, u32 stag_index)316{317struct c2_vq_req *vq_req; /* verbs request object */318struct c2wr_stag_dealloc_req wr; /* work request */319struct c2wr_stag_dealloc_rep *reply; /* WR reply */320int err;321322323/*324* allocate verbs request object325*/326vq_req = vq_req_alloc(c2dev);327if (!vq_req) {328return -ENOMEM;329}330331/*332* Build the WR333*/334c2_wr_set_id(&wr, CCWR_STAG_DEALLOC);335wr.hdr.context = (u64) (unsigned long) vq_req;336wr.rnic_handle = c2dev->adapter_handle;337wr.stag_index = cpu_to_be32(stag_index);338339/*340* reference the request struct. dereferenced in the int handler.341*/342vq_req_get(c2dev, vq_req);343344/*345* Send WR to adapter346*/347err = vq_send_wr(c2dev, (union c2wr *) & wr);348if (err) {349vq_req_put(c2dev, vq_req);350goto bail0;351}352353/*354* Wait for reply from adapter355*/356err = vq_wait_for_reply(c2dev, vq_req);357if (err) {358goto bail0;359}360361/*362* Process reply363*/364reply = (struct c2wr_stag_dealloc_rep *) (unsigned long) vq_req->reply_msg;365if (!reply) {366err = -ENOMEM;367goto bail0;368}369370err = c2_errno(reply);371372vq_repbuf_free(c2dev, reply);373bail0:374vq_req_free(c2dev, vq_req);375return err;376}377378379