123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/rio.h>
#include <linux/rio_drv.h>
#include <linux/rio_ids.h>
#include <linux/rio_regs.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include "rio.h"
struct rio_pwrite {
struct list_head node;
int (*pwcback)(struct rio_mport *mport, void *context,
union rio_pw_msg *msg, int step);
void *context;
};
MODULE_DESCRIPTION("RapidIO Subsystem Core");
MODULE_AUTHOR("Matt Porter <mporter@kernel.crashing.org>");
MODULE_AUTHOR("Alexandre Bounine <alexandre.bounine@idt.com>");
MODULE_LICENSE("GPL");
static int hdid[RIO_MAX_MPORTS];
static int ids_num;
module_param_array(hdid, int, &ids_num, 0);
MODULE_PARM_DESC(hdid,
"Destination ID assignment to local RapidIO controllers");
static LIST_HEAD(rio_devices);
static LIST_HEAD(rio_nets);
static DEFINE_SPINLOCK(rio_global_list_lock);
static LIST_HEAD(rio_mports);
static LIST_HEAD(rio_scans);
static DEFINE_MUTEX(rio_mport_list_lock);
static unsigned char next_portid;
static DEFINE_SPINLOCK(rio_mmap_lock);
u16 rio_local_get_device_id(struct rio_mport *port)
{
u32 result;
rio_local_read_config_32(port, RIO_DID_CSR, &result);
return (RIO_GET_DID(port->sys_size, result));
}
EXPORT_SYMBOL_GPL(rio_local_get_device_id);
int rio_query_mport(struct rio_mport *port,
struct rio_mport_attr *mport_attr)
{
if (!port->ops->query_mport)
return -ENODATA;
return port->ops->query_mport(port, mport_attr);
}
EXPORT_SYMBOL(rio_query_mport);
struct rio_net *rio_alloc_net(struct rio_mport *mport)
{
struct rio_net *net = kzalloc_obj(*net);
if (net) {
INIT_LIST_HEAD(&net->node);
INIT_LIST_HEAD(&net->devices);
INIT_LIST_HEAD(&net->switches);
INIT_LIST_HEAD(&net->mports);
mport->net = net;
}
return net;
}
EXPORT_SYMBOL_GPL(rio_alloc_net);
int rio_add_net(struct rio_net *net)
{
int err;
err = device_register(&net->dev);
if (err)
return err;
spin_lock(&rio_global_list_lock);
list_add_tail(&net->node, &rio_nets);
spin_unlock(&rio_global_list_lock);
return 0;
}
EXPORT_SYMBOL_GPL(rio_add_net);
void rio_free_net(struct rio_net *net)
{
spin_lock(&rio_global_list_lock);
if (!list_empty(&net->node))
list_del(&net->node);
spin_unlock(&rio_global_list_lock);
if (net->release)
net->release(net);
device_unregister(&net->dev);
}
EXPORT_SYMBOL_GPL(rio_free_net);
void rio_local_set_device_id(struct rio_mport *port, u16 did)
{
rio_local_write_config_32(port, RIO_DID_CSR,
RIO_SET_DID(port->sys_size, did));
}
EXPORT_SYMBOL_GPL(rio_local_set_device_id);
int rio_add_device(struct rio_dev *rdev)
{
int err;
atomic_set(&rdev->state, RIO_DEVICE_RUNNING);
err = device_register(&rdev->dev);
if (err)
return err;
spin_lock(&rio_global_list_lock);
list_add_tail(&rdev->global_list, &rio_devices);
if (rdev->net) {
list_add_tail(&rdev->net_list, &rdev->net->devices);
if (rdev->pef & RIO_PEF_SWITCH)
list_add_tail(&rdev->rswitch->node,
&rdev->net->switches);
}
spin_unlock(&rio_global_list_lock);
return 0;
}
EXPORT_SYMBOL_GPL(rio_add_device);
void rio_del_device(struct rio_dev *rdev, enum rio_device_state state)
{
pr_debug("RIO: %s: removing %s\n", __func__, rio_name(rdev));
atomic_set(&rdev->state, state);
spin_lock(&rio_global_list_lock);
list_del(&rdev->global_list);
if (rdev->net) {
list_del(&rdev->net_list);
if (rdev->pef & RIO_PEF_SWITCH) {
list_del(&rdev->rswitch->node);
kfree(rdev->rswitch->route_table);
}
}
spin_unlock(&rio_global_list_lock);
device_unregister(&rdev->dev);
}
EXPORT_SYMBOL_GPL(rio_del_device);
int rio_request_inb_mbox(struct rio_mport *mport,
void *dev_id,
int mbox,
int entries,
void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
int slot))
{
int rc = -ENOSYS;
struct resource *res;
if (!mport->ops->open_inb_mbox)
goto out;
res = kzalloc_obj(*res);
if (res) {
rio_init_mbox_res(res, mbox, mbox);
rc = request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
res);
if (rc < 0) {
kfree(res);
goto out;
}
mport->inb_msg[mbox].res = res;
mport->inb_msg[mbox].mcback = minb;
rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
if (rc) {
mport->inb_msg[mbox].mcback = NULL;
mport->inb_msg[mbox].res = NULL;
release_resource(res);
kfree(res);
}
} else
rc = -ENOMEM;
out:
return rc;
}
EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
{
int rc;
if (!mport->ops->close_inb_mbox || !mport->inb_msg[mbox].res)
return -EINVAL;
mport->ops->close_inb_mbox(mport, mbox);
mport->inb_msg[mbox].mcback = NULL;
rc = release_resource(mport->inb_msg[mbox].res);
if (rc)
return rc;
kfree(mport->inb_msg[mbox].res);
mport->inb_msg[mbox].res = NULL;
return 0;
}
EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
int rio_request_outb_mbox(struct rio_mport *mport,
void *dev_id,
int mbox,
int entries,
void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
{
int rc = -ENOSYS;
struct resource *res;
if (!mport->ops->open_outb_mbox)
goto out;
res = kzalloc_obj(*res);
if (res) {
rio_init_mbox_res(res, mbox, mbox);
rc = request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
res);
if (rc < 0) {
kfree(res);
goto out;
}
mport->outb_msg[mbox].res = res;
mport->outb_msg[mbox].mcback = moutb;
rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
if (rc) {
mport->outb_msg[mbox].mcback = NULL;
mport->outb_msg[mbox].res = NULL;
release_resource(res);
kfree(res);
}
} else
rc = -ENOMEM;
out:
return rc;
}
EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
{
int rc;
if (!mport->ops->close_outb_mbox || !mport->outb_msg[mbox].res)
return -EINVAL;
mport->ops->close_outb_mbox(mport, mbox);
mport->outb_msg[mbox].mcback = NULL;
rc = release_resource(mport->outb_msg[mbox].res);
if (rc)
return rc;
kfree(mport->outb_msg[mbox].res);
mport->outb_msg[mbox].res = NULL;
return 0;
}
EXPORT_SYMBOL_GPL(rio_release_outb_mbox);
static int
rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
u16 info))
{
struct rio_dbell *dbell = kmalloc_obj(*dbell);
if (!dbell)
return -ENOMEM;
dbell->res = res;
dbell->dinb = dinb;
dbell->dev_id = dev_id;
mutex_lock(&mport->lock);
list_add_tail(&dbell->node, &mport->dbells);
mutex_unlock(&mport->lock);
return 0;
}
int rio_request_inb_dbell(struct rio_mport *mport,
void *dev_id,
u16 start,
u16 end,
void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
u16 dst, u16 info))
{
int rc;
struct resource *res = kzalloc_obj(*res);
if (res) {
rio_init_dbell_res(res, start, end);
rc = request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
res);
if (rc < 0) {
kfree(res);
goto out;
}
rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
} else
rc = -ENOMEM;
out:
return rc;
}
EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
{
int rc = 0, found = 0;
struct rio_dbell *dbell;
mutex_lock(&mport->lock);
list_for_each_entry(dbell, &mport->dbells, node) {
if ((dbell->res->start == start) && (dbell->res->end == end)) {
list_del(&dbell->node);
found = 1;
break;
}
}
mutex_unlock(&mport->lock);
if (!found) {
rc = -EINVAL;
goto out;
}
rc = release_resource(dbell->res);
kfree(dbell);
out:
return rc;
}
EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
u16 end)
{
struct resource *res = kzalloc_obj(struct resource);
if (res) {
rio_init_dbell_res(res, start, end);
if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
< 0) {
kfree(res);
res = NULL;
}
}
return res;
}
EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
{
int rc = release_resource(res);
kfree(res);
return rc;
}
EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
int rio_add_mport_pw_handler(struct rio_mport *mport, void *context,
int (*pwcback)(struct rio_mport *mport,
void *context, union rio_pw_msg *msg, int step))
{
struct rio_pwrite *pwrite = kzalloc_obj(*pwrite);
if (!pwrite)
return -ENOMEM;
pwrite->pwcback = pwcback;
pwrite->context = context;
mutex_lock(&mport->lock);
list_add_tail(&pwrite->node, &mport->pwrites);
mutex_unlock(&mport->lock);
return 0;
}
EXPORT_SYMBOL_GPL(rio_add_mport_pw_handler);
int rio_del_mport_pw_handler(struct rio_mport *mport, void *context,
int (*pwcback)(struct rio_mport *mport,
void *context, union rio_pw_msg *msg, int step))
{
int rc = -EINVAL;
struct rio_pwrite *pwrite;
mutex_lock(&mport->lock);
list_for_each_entry(pwrite, &mport->pwrites, node) {
if (pwrite->pwcback == pwcback && pwrite->context == context) {
list_del(&pwrite->node);
kfree(pwrite);
rc = 0;
break;
}
}
mutex_unlock(&mport->lock);
return rc;
}
EXPORT_SYMBOL_GPL(rio_del_mport_pw_handler);
int rio_request_inb_pwrite(struct rio_dev *rdev,
int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
{
int rc = 0;
spin_lock(&rio_global_list_lock);
if (rdev->pwcback)
rc = -ENOMEM;
else
rdev->pwcback = pwcback;
spin_unlock(&rio_global_list_lock);
return rc;
}
EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
int rio_release_inb_pwrite(struct rio_dev *rdev)
{
int rc = -ENOMEM;
spin_lock(&rio_global_list_lock);
if (rdev->pwcback) {
rdev->pwcback = NULL;
rc = 0;
}
spin_unlock(&rio_global_list_lock);
return rc;
}
EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
void rio_pw_enable(struct rio_mport *mport, int enable)
{
if (mport->ops->pwenable) {
mutex_lock(&mport->lock);
if ((enable && ++mport->pwe_refcnt == 1) ||
(!enable && mport->pwe_refcnt && --mport->pwe_refcnt == 0))
mport->ops->pwenable(mport, enable);
mutex_unlock(&mport->lock);
}
}
EXPORT_SYMBOL_GPL(rio_pw_enable);
int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
u64 rbase, u32 size, u32 rflags)
{
int rc;
unsigned long flags;
if (!mport->ops->map_inb)
return -1;
spin_lock_irqsave(&rio_mmap_lock, flags);
rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
spin_unlock_irqrestore(&rio_mmap_lock, flags);
return rc;
}
EXPORT_SYMBOL_GPL(rio_map_inb_region);
void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
{
unsigned long flags;
if (!mport->ops->unmap_inb)
return;
spin_lock_irqsave(&rio_mmap_lock, flags);
mport->ops->unmap_inb(mport, lstart);
spin_unlock_irqrestore(&rio_mmap_lock, flags);
}
EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
int rio_map_outb_region(struct rio_mport *mport, u16 destid, u64 rbase,
u32 size, u32 rflags, dma_addr_t *local)
{
int rc;
unsigned long flags;
if (!mport->ops->map_outb)
return -ENODEV;
spin_lock_irqsave(&rio_mmap_lock, flags);
rc = mport->ops->map_outb(mport, destid, rbase, size,
rflags, local);
spin_unlock_irqrestore(&rio_mmap_lock, flags);
return rc;
}
EXPORT_SYMBOL_GPL(rio_map_outb_region);
void rio_unmap_outb_region(struct rio_mport *mport, u16 destid, u64 rstart)
{
unsigned long flags;
if (!mport->ops->unmap_outb)
return;
spin_lock_irqsave(&rio_mmap_lock, flags);
mport->ops->unmap_outb(mport, destid, rstart);
spin_unlock_irqrestore(&rio_mmap_lock, flags);
}
EXPORT_SYMBOL_GPL(rio_unmap_outb_region);
u32
rio_mport_get_physefb(struct rio_mport *port, int local,
u16 destid, u8 hopcount, u32 *rmap)
{
u32 ext_ftr_ptr;
u32 ftr_header;
ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
while (ext_ftr_ptr) {
if (local)
rio_local_read_config_32(port, ext_ftr_ptr,
&ftr_header);
else
rio_mport_read_config_32(port, destid, hopcount,
ext_ftr_ptr, &ftr_header);
ftr_header = RIO_GET_BLOCK_ID(ftr_header);
switch (ftr_header) {
case RIO_EFB_SER_EP_ID:
case RIO_EFB_SER_EP_REC_ID:
case RIO_EFB_SER_EP_FREE_ID:
case RIO_EFB_SER_EP_M1_ID:
case RIO_EFB_SER_EP_SW_M1_ID:
case RIO_EFB_SER_EPF_M1_ID:
case RIO_EFB_SER_EPF_SW_M1_ID:
*rmap = 1;
return ext_ftr_ptr;
case RIO_EFB_SER_EP_M2_ID:
case RIO_EFB_SER_EP_SW_M2_ID:
case RIO_EFB_SER_EPF_M2_ID:
case RIO_EFB_SER_EPF_SW_M2_ID:
*rmap = 2;
return ext_ftr_ptr;
default:
break;
}
ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
hopcount, ext_ftr_ptr);
}
return ext_ftr_ptr;
}
EXPORT_SYMBOL_GPL(rio_mport_get_physefb);
struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
{
struct list_head *n;
struct rio_dev *rdev;
spin_lock(&rio_global_list_lock);
n = from ? from->global_list.next : rio_devices.next;
while (n && (n != &rio_devices)) {
rdev = rio_dev_g(n);
if (rdev->comp_tag == comp_tag)
goto exit;
n = n->next;
}
rdev = NULL;
exit:
spin_unlock(&rio_global_list_lock);
return rdev;
}
EXPORT_SYMBOL_GPL(rio_get_comptag);
int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
{
u32 regval;
rio_read_config_32(rdev,
RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
®val);
if (lock)
regval |= RIO_PORT_N_CTL_LOCKOUT;
else
regval &= ~RIO_PORT_N_CTL_LOCKOUT;
rio_write_config_32(rdev,
RIO_DEV_PORT_N_CTL_CSR(rdev, pnum),
regval);
return 0;
}
EXPORT_SYMBOL_GPL(rio_set_port_lockout);
int rio_enable_rx_tx_port(struct rio_mport *port,
int local, u16 destid,
u8 hopcount, u8 port_num)
{
#ifdef CONFIG_RAPIDIO_ENABLE_RX_TX_PORTS
u32 regval;
u32 ext_ftr_ptr;
u32 rmap;
pr_debug("rio_enable_rx_tx_port(local = %d, destid = %d, hopcount = "
"%d, port_num = %d)\n", local, destid, hopcount, port_num);
ext_ftr_ptr = rio_mport_get_physefb(port, local, destid,
hopcount, &rmap);
if (local) {
rio_local_read_config_32(port,
ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap),
®val);
} else {
if (rio_mport_read_config_32(port, destid, hopcount,
ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
®val) < 0)
return -EIO;
}
regval = regval | RIO_PORT_N_CTL_EN_RX | RIO_PORT_N_CTL_EN_TX;
if (local) {
rio_local_write_config_32(port,
ext_ftr_ptr + RIO_PORT_N_CTL_CSR(0, rmap), regval);
} else {
if (rio_mport_write_config_32(port, destid, hopcount,
ext_ftr_ptr + RIO_PORT_N_CTL_CSR(port_num, rmap),
regval) < 0)
return -EIO;
}
#endif
return 0;
}
EXPORT_SYMBOL_GPL(rio_enable_rx_tx_port);
static int
rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
{
u32 result;
int p_port, rc = -EIO;
struct rio_dev *prev = NULL;
while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
prev = rdev->prev;
break;
}
rdev = rdev->prev;
}
if (!prev)
goto err_out;
p_port = prev->rswitch->route_table[rdev->destid];
if (p_port != RIO_INVALID_ROUTE) {
pr_debug("RIO: link failed on [%s]-P%d\n",
rio_name(prev), p_port);
*nrdev = prev;
*npnum = p_port;
rc = 0;
} else
pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
err_out:
return rc;
}
int
rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
{
int i = 0;
u32 tmp;
while (rio_mport_read_config_32(mport, destid, hopcount,
RIO_DEV_ID_CAR, &tmp)) {
i++;
if (i == RIO_MAX_CHK_RETRY)
return -EIO;
mdelay(1);
}
return 0;
}
EXPORT_SYMBOL_GPL(rio_mport_chk_dev_access);
static int rio_chk_dev_access(struct rio_dev *rdev)
{
return rio_mport_chk_dev_access(rdev->net->hport,
rdev->destid, rdev->hopcount);
}
static int
rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
{
u32 regval;
int checkcount;
if (lnkresp) {
rio_read_config_32(rdev,
RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
®val);
udelay(50);
}
rio_write_config_32(rdev,
RIO_DEV_PORT_N_MNT_REQ_CSR(rdev, pnum),
RIO_MNT_REQ_CMD_IS);
if (!lnkresp)
return 0;
checkcount = 3;
while (checkcount--) {
udelay(50);
rio_read_config_32(rdev,
RIO_DEV_PORT_N_MNT_RSP_CSR(rdev, pnum),
®val);
if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
*lnkresp = regval;
return 0;
}
}
return -EIO;
}
static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
{
struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
u32 regval;
u32 far_ackid, far_linkstat, near_ackid;
if (err_status == 0)
rio_read_config_32(rdev,
RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
&err_status);
if (err_status & RIO_PORT_N_ERR_STS_OUT_ES) {
pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
if (rio_get_input_status(rdev, pnum, ®val)) {
pr_debug("RIO_EM: Input-status response timeout\n");
goto rd_err;
}
pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
pnum, regval);
far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
rio_read_config_32(rdev,
RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
®val);
pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
" near_ackID=0x%02x\n",
pnum, far_ackid, far_linkstat, near_ackid);
if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
(far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
rio_write_config_32(rdev,
RIO_DEV_PORT_N_ACK_STS_CSR(rdev, pnum),
(near_ackid << 24) |
(far_ackid << 8) | far_ackid);
far_ackid++;
if (!nextdev) {
pr_debug("RIO_EM: nextdev pointer == NULL\n");
goto rd_err;
}
rio_write_config_32(nextdev,
RIO_DEV_PORT_N_ACK_STS_CSR(nextdev,
RIO_GET_PORT_NUM(nextdev->swpinfo)),
(far_ackid << 24) |
(near_ackid << 8) | near_ackid);
}
rd_err:
rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
&err_status);
pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
}
if ((err_status & RIO_PORT_N_ERR_STS_INP_ES) && nextdev) {
pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
rio_get_input_status(nextdev,
RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
udelay(50);
rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, pnum),
&err_status);
pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
}
return (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
RIO_PORT_N_ERR_STS_INP_ES)) ? 1 : 0;
}
int rio_inb_pwrite_handler(struct rio_mport *mport, union rio_pw_msg *pw_msg)
{
struct rio_dev *rdev;
u32 err_status, em_perrdet, em_ltlerrdet;
int rc, portnum;
struct rio_pwrite *pwrite;
#ifdef DEBUG_PW
{
u32 i;
pr_debug("%s: PW to mport_%d:\n", __func__, mport->id);
for (i = 0; i < RIO_PW_MSG_SIZE / sizeof(u32); i = i + 4) {
pr_debug("0x%02x: %08x %08x %08x %08x\n",
i * 4, pw_msg->raw[i], pw_msg->raw[i + 1],
pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
}
}
#endif
rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
if (rdev) {
pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
} else {
pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
__func__, pw_msg->em.comptag);
}
if (rdev && rdev->pwcback) {
rc = rdev->pwcback(rdev, pw_msg, 0);
if (rc == 0)
return 0;
}
mutex_lock(&mport->lock);
list_for_each_entry(pwrite, &mport->pwrites, node)
pwrite->pwcback(mport, pwrite->context, pw_msg, 0);
mutex_unlock(&mport->lock);
if (!rdev)
return 0;
portnum = pw_msg->em.is_port & 0xFF;
if (rio_chk_dev_access(rdev)) {
pr_debug("RIO: device access failed - get link partner\n");
if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
pr_err("RIO: Route trace for %s failed\n",
rio_name(rdev));
return -EIO;
}
pw_msg = NULL;
}
if (!(rdev->pef & RIO_PEF_SWITCH))
return 0;
if (rdev->phys_efptr == 0) {
pr_err("RIO_PW: Bad switch initialization for %s\n",
rio_name(rdev));
return 0;
}
if (rdev->rswitch->ops && rdev->rswitch->ops->em_handle)
rdev->rswitch->ops->em_handle(rdev, portnum);
rio_read_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
&err_status);
pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
if (!(rdev->rswitch->port_ok & (1 << portnum))) {
rdev->rswitch->port_ok |= (1 << portnum);
rio_set_port_lockout(rdev, portnum, 0);
pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
rio_name(rdev), portnum);
}
if (err_status & (RIO_PORT_N_ERR_STS_OUT_ES |
RIO_PORT_N_ERR_STS_INP_ES)) {
if (rio_clr_err_stopped(rdev, portnum, err_status))
rio_clr_err_stopped(rdev, portnum, 0);
}
} else {
if (rdev->rswitch->port_ok & (1 << portnum)) {
rdev->rswitch->port_ok &= ~(1 << portnum);
rio_set_port_lockout(rdev, portnum, 1);
if (rdev->phys_rmap == 1) {
rio_write_config_32(rdev,
RIO_DEV_PORT_N_ACK_STS_CSR(rdev, portnum),
RIO_PORT_N_ACK_CLEAR);
} else {
rio_write_config_32(rdev,
RIO_DEV_PORT_N_OB_ACK_CSR(rdev, portnum),
RIO_PORT_N_OB_ACK_CLEAR);
rio_write_config_32(rdev,
RIO_DEV_PORT_N_IB_ACK_CSR(rdev, portnum),
0);
}
pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
rio_name(rdev), portnum);
}
}
rio_read_config_32(rdev,
rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
if (em_perrdet) {
pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
portnum, em_perrdet);
rio_write_config_32(rdev,
rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
}
rio_read_config_32(rdev,
rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
if (em_ltlerrdet) {
pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
em_ltlerrdet);
rio_write_config_32(rdev,
rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
}
rio_write_config_32(rdev, RIO_DEV_PORT_N_ERR_STS_CSR(rdev, portnum),
err_status);
return 0;
}
EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
u32
rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
u8 hopcount, u32 from)
{
u32 reg_val;
if (from == 0) {
if (local)
rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
®_val);
else
rio_mport_read_config_32(port, destid, hopcount,
RIO_ASM_INFO_CAR, ®_val);
return reg_val & RIO_EXT_FTR_PTR_MASK;
} else {
if (local)
rio_local_read_config_32(port, from, ®_val);
else
rio_mport_read_config_32(port, destid, hopcount,
from, ®_val);
return RIO_GET_BLOCK_ID(reg_val);
}
}
EXPORT_SYMBOL_GPL(rio_mport_get_efb);
u32
rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
u8 hopcount, int ftr)
{
u32 asm_info, ext_ftr_ptr, ftr_header;
if (local)
rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
else
rio_mport_read_config_32(port, destid, hopcount,
RIO_ASM_INFO_CAR, &asm_info);
ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
while (ext_ftr_ptr) {
if (local)
rio_local_read_config_32(port, ext_ftr_ptr,
&ftr_header);
else
rio_mport_read_config_32(port, destid, hopcount,
ext_ftr_ptr, &ftr_header);
if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
return ext_ftr_ptr;
ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header);
if (!ext_ftr_ptr)
break;
}
return 0;
}
EXPORT_SYMBOL_GPL(rio_mport_get_feature);
static int
rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
u16 table, u16 route_destid, u8 route_port)
{
if (table == RIO_GLOBAL_TABLE) {
rio_mport_write_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_DESTID_SEL_CSR,
(u32)route_destid);
rio_mport_write_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_PORT_SEL_CSR,
(u32)route_port);
}
udelay(10);
return 0;
}
static int
rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
u16 table, u16 route_destid, u8 *route_port)
{
u32 result;
if (table == RIO_GLOBAL_TABLE) {
rio_mport_write_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
rio_mport_read_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
*route_port = (u8)result;
}
return 0;
}
static int
rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
u16 table)
{
u32 max_destid = 0xff;
u32 i, pef, id_inc = 1, ext_cfg = 0;
u32 port_sel = RIO_INVALID_ROUTE;
if (table == RIO_GLOBAL_TABLE) {
rio_mport_read_config_32(mport, destid, hopcount,
RIO_PEF_CAR, &pef);
if (mport->sys_size) {
rio_mport_read_config_32(mport, destid, hopcount,
RIO_SWITCH_RT_LIMIT,
&max_destid);
max_destid &= RIO_RT_MAX_DESTID;
}
if (pef & RIO_PEF_EXT_RT) {
ext_cfg = 0x80000000;
id_inc = 4;
port_sel = (RIO_INVALID_ROUTE << 24) |
(RIO_INVALID_ROUTE << 16) |
(RIO_INVALID_ROUTE << 8) |
RIO_INVALID_ROUTE;
}
for (i = 0; i <= max_destid;) {
rio_mport_write_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_DESTID_SEL_CSR,
ext_cfg | i);
rio_mport_write_config_32(mport, destid, hopcount,
RIO_STD_RTE_CONF_PORT_SEL_CSR,
port_sel);
i += id_inc;
}
}
udelay(10);
return 0;
}
int rio_lock_device(struct rio_mport *port, u16 destid,
u8 hopcount, int wait_ms)
{
u32 result;
int tcnt = 0;
rio_mport_write_config_32(port, destid, hopcount,
RIO_HOST_DID_LOCK_CSR, port->host_deviceid);
rio_mport_read_config_32(port, destid, hopcount,
RIO_HOST_DID_LOCK_CSR, &result);
while (result != port->host_deviceid) {
if (wait_ms != 0 && tcnt == wait_ms) {
pr_debug("RIO: timeout when locking device %x:%x\n",
destid, hopcount);
return -EINVAL;
}
mdelay(1);
tcnt++;
rio_mport_write_config_32(port, destid,
hopcount,
RIO_HOST_DID_LOCK_CSR,
port->host_deviceid);
rio_mport_read_config_32(port, destid,
hopcount,
RIO_HOST_DID_LOCK_CSR, &result);
}
return 0;
}
EXPORT_SYMBOL_GPL(rio_lock_device);
int rio_unlock_device(struct rio_mport *port, u16 destid, u8 hopcount)
{
u32 result;
rio_mport_write_config_32(port, destid,
hopcount,
RIO_HOST_DID_LOCK_CSR,
port->host_deviceid);
rio_mport_read_config_32(port, destid, hopcount,
RIO_HOST_DID_LOCK_CSR, &result);
if ((result & 0xffff) != 0xffff) {
pr_debug("RIO: badness when releasing device lock %x:%x\n",
destid, hopcount);
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL_GPL(rio_unlock_device);
int rio_route_add_entry(struct rio_dev *rdev,
u16 table, u16 route_destid, u8 route_port, int lock)
{
int rc = -EINVAL;
struct rio_switch_ops *ops = rdev->rswitch->ops;
if (lock) {
rc = rio_lock_device(rdev->net->hport, rdev->destid,
rdev->hopcount, 1000);
if (rc)
return rc;
}
spin_lock(&rdev->rswitch->lock);
if (!ops || !ops->add_entry) {
rc = rio_std_route_add_entry(rdev->net->hport, rdev->destid,
rdev->hopcount, table,
route_destid, route_port);
} else if (try_module_get(ops->owner)) {
rc = ops->add_entry(rdev->net->hport, rdev->destid,
rdev->hopcount, table, route_destid,
route_port);
module_put(ops->owner);
}
spin_unlock(&rdev->rswitch->lock);
if (lock)
rio_unlock_device(rdev->net->hport, rdev->destid,
rdev->hopcount);
return rc;
}
EXPORT_SYMBOL_GPL(rio_route_add_entry);
int rio_route_get_entry(struct rio_dev *rdev, u16 table,
u16 route_destid, u8 *route_port, int lock)
{
int rc = -EINVAL;
struct rio_switch_ops *ops = rdev->rswitch->ops;
if (lock) {
rc = rio_lock_device(rdev->net->hport, rdev->destid,
rdev->hopcount, 1000);
if (rc)
return rc;
}
spin_lock(&rdev->rswitch->lock);
if (!ops || !ops->get_entry) {
rc = rio_std_route_get_entry(rdev->net->hport, rdev->destid,
rdev->hopcount, table,
route_destid, route_port);
} else if (try_module_get(ops->owner)) {
rc = ops->get_entry(rdev->net->hport, rdev->destid,
rdev->hopcount, table, route_destid,
route_port);
module_put(ops->owner);
}
spin_unlock(&rdev->rswitch->lock);
if (lock)
rio_unlock_device(rdev->net->hport, rdev->destid,
rdev->hopcount);
return rc;
}
EXPORT_SYMBOL_GPL(rio_route_get_entry);
int rio_route_clr_table(struct rio_dev *rdev, u16 table, int lock)
{
int rc = -EINVAL;
struct rio_switch_ops *ops = rdev->rswitch->ops;
if (lock) {
rc = rio_lock_device(rdev->net->hport, rdev->destid,
rdev->hopcount, 1000);
if (rc)
return rc;
}
spin_lock(&rdev->rswitch->lock);
if (!ops || !ops->clr_table) {
rc = rio_std_route_clr_table(rdev->net->hport, rdev->destid,
rdev->hopcount, table);
} else if (try_module_get(ops->owner)) {
rc = ops->clr_table(rdev->net->hport, rdev->destid,
rdev->hopcount, table);
module_put(ops->owner);
}
spin_unlock(&rdev->rswitch->lock);
if (lock)
rio_unlock_device(rdev->net->hport, rdev->destid,
rdev->hopcount);
return rc;
}
EXPORT_SYMBOL_GPL(rio_route_clr_table);
#ifdef CONFIG_RAPIDIO_DMA_ENGINE
static bool rio_chan_filter(struct dma_chan *chan, void *arg)
{
struct rio_mport *mport = arg;
return mport == container_of(chan->device, struct rio_mport, dma);
}
struct dma_chan *rio_request_mport_dma(struct rio_mport *mport)
{
dma_cap_mask_t mask;
dma_cap_zero(mask);
dma_cap_set(DMA_SLAVE, mask);
return dma_request_channel(mask, rio_chan_filter, mport);
}
EXPORT_SYMBOL_GPL(rio_request_mport_dma);
void rio_release_dma(struct dma_chan *dchan)
{
dma_release_channel(dchan);
}
EXPORT_SYMBOL_GPL(rio_release_dma);
struct dma_async_tx_descriptor *rio_dma_prep_xfer(struct dma_chan *dchan,
u16 destid, struct rio_dma_data *data,
enum dma_transfer_direction direction, unsigned long flags)
{
struct rio_dma_ext rio_ext;
if (!dchan->device->device_prep_slave_sg) {
pr_err("%s: prep_rio_sg == NULL\n", __func__);
return NULL;
}
rio_ext.destid = destid;
rio_ext.rio_addr_u = data->rio_addr_u;
rio_ext.rio_addr = data->rio_addr;
rio_ext.wr_type = data->wr_type;
return dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
direction, flags, &rio_ext);
}
EXPORT_SYMBOL_GPL(rio_dma_prep_xfer);
#endif /* CONFIG_RAPIDIO_DMA_ENGINE */
int rio_register_scan(int mport_id, struct rio_scan *scan_ops)
{
struct rio_mport *port;
struct rio_scan_node *scan;
int rc = 0;
pr_debug("RIO: %s for mport_id=%d\n", __func__, mport_id);
if ((mport_id != RIO_MPORT_ANY && mport_id >= RIO_MAX_MPORTS) ||
!scan_ops)
return -EINVAL;
mutex_lock(&rio_mport_list_lock);
list_for_each_entry(scan, &rio_scans, node) {
if (scan->mport_id == mport_id) {
rc = -EBUSY;
goto err_out;
}
}
scan = kzalloc_obj(*scan);
if (!scan) {
rc = -ENOMEM;
goto err_out;
}
scan->mport_id = mport_id;
scan->ops = scan_ops;
list_for_each_entry(port, &rio_mports, node) {
if (port->id == mport_id) {
port->nscan = scan_ops;
break;
} else if (mport_id == RIO_MPORT_ANY && !port->nscan)
port->nscan = scan_ops;
}
list_add_tail(&scan->node, &rio_scans);
err_out:
mutex_unlock(&rio_mport_list_lock);
return rc;
}
EXPORT_SYMBOL_GPL(rio_register_scan);
int rio_mport_scan(int mport_id)
{
struct rio_mport *port = NULL;
int rc;
mutex_lock(&rio_mport_list_lock);
list_for_each_entry(port, &rio_mports, node) {
if (port->id == mport_id)
goto found;
}
mutex_unlock(&rio_mport_list_lock);
return -ENODEV;
found:
if (!port->nscan) {
mutex_unlock(&rio_mport_list_lock);
return -EINVAL;
}
if (!try_module_get(port->nscan->owner)) {
mutex_unlock(&rio_mport_list_lock);
return -ENODEV;
}
mutex_unlock(&rio_mport_list_lock);
if (port->host_deviceid >= 0)
rc = port->nscan->enumerate(port, 0);
else
rc = port->nscan->discover(port, RIO_SCAN_ENUM_NO_WAIT);
module_put(port->nscan->owner);
return rc;
}
static struct workqueue_struct *rio_wq;
struct rio_disc_work {
struct work_struct work;
struct rio_mport *mport;
};
static void disc_work_handler(struct work_struct *_work)
{
struct rio_disc_work *work;
work = container_of(_work, struct rio_disc_work, work);
pr_debug("RIO: discovery work for mport %d %s\n",
work->mport->id, work->mport->name);
if (try_module_get(work->mport->nscan->owner)) {
work->mport->nscan->discover(work->mport, 0);
module_put(work->mport->nscan->owner);
}
}
int rio_init_mports(void)
{
struct rio_mport *port;
struct rio_disc_work *work;
int n = 0;
if (!next_portid)
return -ENODEV;
mutex_lock(&rio_mport_list_lock);
list_for_each_entry(port, &rio_mports, node) {
if (port->host_deviceid >= 0) {
if (port->nscan && try_module_get(port->nscan->owner)) {
port->nscan->enumerate(port, 0);
module_put(port->nscan->owner);
}
} else
n++;
}
mutex_unlock(&rio_mport_list_lock);
if (!n)
goto no_disc;
rio_wq = alloc_workqueue("riodisc", WQ_PERCPU, 0);
if (!rio_wq) {
pr_err("RIO: unable allocate rio_wq\n");
goto no_disc;
}
work = kzalloc_objs(*work, n);
if (!work) {
destroy_workqueue(rio_wq);
goto no_disc;
}
n = 0;
mutex_lock(&rio_mport_list_lock);
list_for_each_entry(port, &rio_mports, node) {
if (port->host_deviceid < 0 && port->nscan) {
work[n].mport = port;
INIT_WORK(&work[n].work, disc_work_handler);
queue_work(rio_wq, &work[n].work);
n++;
}
}
flush_workqueue(rio_wq);
mutex_unlock(&rio_mport_list_lock);
pr_debug("RIO: destroy discovery workqueue\n");
destroy_workqueue(rio_wq);
kfree(work);
no_disc:
return 0;
}
EXPORT_SYMBOL_GPL(rio_init_mports);
static int rio_get_hdid(int index)
{
if (ids_num == 0 || ids_num <= index || index >= RIO_MAX_MPORTS)
return -1;
return hdid[index];
}
int rio_mport_initialize(struct rio_mport *mport)
{
if (next_portid >= RIO_MAX_MPORTS) {
pr_err("RIO: reached specified max number of mports\n");
return -ENODEV;
}
atomic_set(&mport->state, RIO_DEVICE_INITIALIZING);
mport->id = next_portid++;
mport->host_deviceid = rio_get_hdid(mport->id);
mport->nscan = NULL;
mutex_init(&mport->lock);
mport->pwe_refcnt = 0;
INIT_LIST_HEAD(&mport->pwrites);
return 0;
}
EXPORT_SYMBOL_GPL(rio_mport_initialize);
int rio_register_mport(struct rio_mport *port)
{
struct rio_scan_node *scan = NULL;
int res = 0;
mutex_lock(&rio_mport_list_lock);
list_for_each_entry(scan, &rio_scans, node) {
if (port->id == scan->mport_id ||
scan->mport_id == RIO_MPORT_ANY) {
port->nscan = scan->ops;
if (port->id == scan->mport_id)
break;
}
}
list_add_tail(&port->node, &rio_mports);
mutex_unlock(&rio_mport_list_lock);
dev_set_name(&port->dev, "rapidio%d", port->id);
port->dev.class = &rio_mport_class;
atomic_set(&port->state, RIO_DEVICE_RUNNING);
res = device_register(&port->dev);
if (res) {
dev_err(&port->dev, "RIO: mport%d registration failed ERR=%d\n",
port->id, res);
mutex_lock(&rio_mport_list_lock);
list_del(&port->node);
mutex_unlock(&rio_mport_list_lock);
put_device(&port->dev);
} else {
dev_dbg(&port->dev, "RIO: registered mport%d\n", port->id);
}
return res;
}
EXPORT_SYMBOL_GPL(rio_register_mport);
static int rio_mport_cleanup_callback(struct device *dev, void *data)
{
struct rio_dev *rdev = to_rio_dev(dev);
if (dev->bus == &rio_bus_type)
rio_del_device(rdev, RIO_DEVICE_SHUTDOWN);
return 0;
}
static int rio_net_remove_children(struct rio_net *net)
{
device_for_each_child(&net->dev, NULL, rio_mport_cleanup_callback);
return 0;
}
int rio_unregister_mport(struct rio_mport *port)
{
pr_debug("RIO: %s %s id=%d\n", __func__, port->name, port->id);
if (atomic_cmpxchg(&port->state,
RIO_DEVICE_RUNNING,
RIO_DEVICE_SHUTDOWN) != RIO_DEVICE_RUNNING) {
pr_err("RIO: %s unexpected state transition for mport %s\n",
__func__, port->name);
}
if (port->net && port->net->hport == port) {
rio_net_remove_children(port->net);
rio_free_net(port->net);
}
mutex_lock(&rio_mport_list_lock);
list_del(&port->node);
mutex_unlock(&rio_mport_list_lock);
device_unregister(&port->dev);
return 0;
}
EXPORT_SYMBOL_GPL(rio_unregister_mport);