aoecmd.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/ata.h>
  7. #include <linux/slab.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/genhd.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/kthread.h>
  16. #include <net/net_namespace.h>
  17. #include <asm/unaligned.h>
  18. #include <linux/uio.h>
  19. #include "aoe.h"
  20. #define MAXIOC (8192) /* default meant to avoid most soft lockups */
  21. static void ktcomplete(struct frame *, struct sk_buff *);
  22. static struct buf *nextbuf(struct aoedev *);
  23. static int aoe_deadsecs = 60 * 3;
  24. module_param(aoe_deadsecs, int, 0644);
  25. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  26. static int aoe_maxout = 16;
  27. module_param(aoe_maxout, int, 0644);
  28. MODULE_PARM_DESC(aoe_maxout,
  29. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  30. static wait_queue_head_t ktiowq;
  31. static struct ktstate kts;
  32. /* io completion queue */
  33. static struct {
  34. struct list_head head;
  35. spinlock_t lock;
  36. } iocq;
  37. static struct sk_buff *
  38. new_skb(ulong len)
  39. {
  40. struct sk_buff *skb;
  41. skb = alloc_skb(len, GFP_ATOMIC);
  42. if (skb) {
  43. skb_reset_mac_header(skb);
  44. skb_reset_network_header(skb);
  45. skb->protocol = __constant_htons(ETH_P_AOE);
  46. skb_checksum_none_assert(skb);
  47. }
  48. return skb;
  49. }
  50. static struct frame *
  51. getframe(struct aoedev *d, u32 tag)
  52. {
  53. struct frame *f;
  54. struct list_head *head, *pos, *nx;
  55. u32 n;
  56. n = tag % NFACTIVE;
  57. head = &d->factive[n];
  58. list_for_each_safe(pos, nx, head) {
  59. f = list_entry(pos, struct frame, head);
  60. if (f->tag == tag) {
  61. list_del(pos);
  62. return f;
  63. }
  64. }
  65. return NULL;
  66. }
  67. /*
  68. * Leave the top bit clear so we have tagspace for userland.
  69. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  70. * This driver reserves tag -1 to mean "unused frame."
  71. */
  72. static int
  73. newtag(struct aoedev *d)
  74. {
  75. register ulong n;
  76. n = jiffies & 0xffff;
  77. return n |= (++d->lasttag & 0x7fff) << 16;
  78. }
  79. static u32
  80. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  81. {
  82. u32 host_tag = newtag(d);
  83. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  84. memcpy(h->dst, t->addr, sizeof h->dst);
  85. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  86. h->verfl = AOE_HVER;
  87. h->major = cpu_to_be16(d->aoemajor);
  88. h->minor = d->aoeminor;
  89. h->cmd = AOECMD_ATA;
  90. h->tag = cpu_to_be32(host_tag);
  91. return host_tag;
  92. }
  93. static inline void
  94. put_lba(struct aoe_atahdr *ah, sector_t lba)
  95. {
  96. ah->lba0 = lba;
  97. ah->lba1 = lba >>= 8;
  98. ah->lba2 = lba >>= 8;
  99. ah->lba3 = lba >>= 8;
  100. ah->lba4 = lba >>= 8;
  101. ah->lba5 = lba >>= 8;
  102. }
  103. static struct aoeif *
  104. ifrotate(struct aoetgt *t)
  105. {
  106. struct aoeif *ifp;
  107. ifp = t->ifp;
  108. ifp++;
  109. if (ifp >= &t->ifs[NAOEIFS] || ifp->nd == NULL)
  110. ifp = t->ifs;
  111. if (ifp->nd == NULL)
  112. return NULL;
  113. return t->ifp = ifp;
  114. }
  115. static void
  116. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  117. {
  118. __skb_queue_tail(&d->skbpool, skb);
  119. }
  120. static struct sk_buff *
  121. skb_pool_get(struct aoedev *d)
  122. {
  123. struct sk_buff *skb = skb_peek(&d->skbpool);
  124. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  125. __skb_unlink(skb, &d->skbpool);
  126. return skb;
  127. }
  128. if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
  129. (skb = new_skb(ETH_ZLEN)))
  130. return skb;
  131. return NULL;
  132. }
  133. void
  134. aoe_freetframe(struct frame *f)
  135. {
  136. struct aoetgt *t;
  137. t = f->t;
  138. f->buf = NULL;
  139. f->bv = NULL;
  140. f->r_skb = NULL;
  141. list_add(&f->head, &t->ffree);
  142. }
  143. static struct frame *
  144. newtframe(struct aoedev *d, struct aoetgt *t)
  145. {
  146. struct frame *f;
  147. struct sk_buff *skb;
  148. struct list_head *pos;
  149. if (list_empty(&t->ffree)) {
  150. if (t->falloc >= NSKBPOOLMAX*2)
  151. return NULL;
  152. f = kcalloc(1, sizeof(*f), GFP_ATOMIC);
  153. if (f == NULL)
  154. return NULL;
  155. t->falloc++;
  156. f->t = t;
  157. } else {
  158. pos = t->ffree.next;
  159. list_del(pos);
  160. f = list_entry(pos, struct frame, head);
  161. }
  162. skb = f->skb;
  163. if (skb == NULL) {
  164. f->skb = skb = new_skb(ETH_ZLEN);
  165. if (!skb) {
  166. bail: aoe_freetframe(f);
  167. return NULL;
  168. }
  169. }
  170. if (atomic_read(&skb_shinfo(skb)->dataref) != 1) {
  171. skb = skb_pool_get(d);
  172. if (skb == NULL)
  173. goto bail;
  174. skb_pool_put(d, f->skb);
  175. f->skb = skb;
  176. }
  177. skb->truesize -= skb->data_len;
  178. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  179. skb_trim(skb, 0);
  180. return f;
  181. }
  182. static struct frame *
  183. newframe(struct aoedev *d)
  184. {
  185. struct frame *f;
  186. struct aoetgt *t, **tt;
  187. int totout = 0;
  188. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  189. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  190. return NULL;
  191. }
  192. tt = d->tgt; /* last used target */
  193. for (;;) {
  194. tt++;
  195. if (tt >= &d->targets[NTARGETS] || !*tt)
  196. tt = d->targets;
  197. t = *tt;
  198. totout += t->nout;
  199. if (t->nout < t->maxout
  200. && t != d->htgt
  201. && t->ifp->nd) {
  202. f = newtframe(d, t);
  203. if (f) {
  204. ifrotate(t);
  205. d->tgt = tt;
  206. return f;
  207. }
  208. }
  209. if (tt == d->tgt) /* we've looped and found nada */
  210. break;
  211. }
  212. if (totout == 0) {
  213. d->kicked++;
  214. d->flags |= DEVFL_KICKME;
  215. }
  216. return NULL;
  217. }
  218. static void
  219. skb_fillup(struct sk_buff *skb, struct bio_vec *bv, ulong off, ulong cnt)
  220. {
  221. int frag = 0;
  222. ulong fcnt;
  223. loop:
  224. fcnt = bv->bv_len - (off - bv->bv_offset);
  225. if (fcnt > cnt)
  226. fcnt = cnt;
  227. skb_fill_page_desc(skb, frag++, bv->bv_page, off, fcnt);
  228. cnt -= fcnt;
  229. if (cnt <= 0)
  230. return;
  231. bv++;
  232. off = bv->bv_offset;
  233. goto loop;
  234. }
  235. static void
  236. fhash(struct frame *f)
  237. {
  238. struct aoedev *d = f->t->d;
  239. u32 n;
  240. n = f->tag % NFACTIVE;
  241. list_add_tail(&f->head, &d->factive[n]);
  242. }
  243. static int
  244. aoecmd_ata_rw(struct aoedev *d)
  245. {
  246. struct frame *f;
  247. struct aoe_hdr *h;
  248. struct aoe_atahdr *ah;
  249. struct buf *buf;
  250. struct aoetgt *t;
  251. struct sk_buff *skb;
  252. struct sk_buff_head queue;
  253. ulong bcnt, fbcnt;
  254. char writebit, extbit;
  255. writebit = 0x10;
  256. extbit = 0x4;
  257. buf = nextbuf(d);
  258. if (buf == NULL)
  259. return 0;
  260. f = newframe(d);
  261. if (f == NULL)
  262. return 0;
  263. t = *d->tgt;
  264. bcnt = d->maxbcnt;
  265. if (bcnt == 0)
  266. bcnt = DEFAULTBCNT;
  267. if (bcnt > buf->resid)
  268. bcnt = buf->resid;
  269. fbcnt = bcnt;
  270. f->bv = buf->bv;
  271. f->bv_off = f->bv->bv_offset + (f->bv->bv_len - buf->bv_resid);
  272. do {
  273. if (fbcnt < buf->bv_resid) {
  274. buf->bv_resid -= fbcnt;
  275. buf->resid -= fbcnt;
  276. break;
  277. }
  278. fbcnt -= buf->bv_resid;
  279. buf->resid -= buf->bv_resid;
  280. if (buf->resid == 0) {
  281. d->ip.buf = NULL;
  282. break;
  283. }
  284. buf->bv++;
  285. buf->bv_resid = buf->bv->bv_len;
  286. WARN_ON(buf->bv_resid == 0);
  287. } while (fbcnt);
  288. /* initialize the headers & frame */
  289. skb = f->skb;
  290. h = (struct aoe_hdr *) skb_mac_header(skb);
  291. ah = (struct aoe_atahdr *) (h+1);
  292. skb_put(skb, sizeof *h + sizeof *ah);
  293. memset(h, 0, skb->len);
  294. f->tag = aoehdr_atainit(d, t, h);
  295. fhash(f);
  296. t->nout++;
  297. f->waited = 0;
  298. f->buf = buf;
  299. f->bcnt = bcnt;
  300. f->lba = buf->sector;
  301. /* set up ata header */
  302. ah->scnt = bcnt >> 9;
  303. put_lba(ah, buf->sector);
  304. if (d->flags & DEVFL_EXT) {
  305. ah->aflags |= AOEAFL_EXT;
  306. } else {
  307. extbit = 0;
  308. ah->lba3 &= 0x0f;
  309. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  310. }
  311. if (bio_data_dir(buf->bio) == WRITE) {
  312. skb_fillup(skb, f->bv, f->bv_off, bcnt);
  313. ah->aflags |= AOEAFL_WRITE;
  314. skb->len += bcnt;
  315. skb->data_len = bcnt;
  316. skb->truesize += bcnt;
  317. t->wpkts++;
  318. } else {
  319. t->rpkts++;
  320. writebit = 0;
  321. }
  322. ah->cmdstat = ATA_CMD_PIO_READ | writebit | extbit;
  323. /* mark all tracking fields and load out */
  324. buf->nframesout += 1;
  325. buf->sector += bcnt >> 9;
  326. skb->dev = t->ifp->nd;
  327. skb = skb_clone(skb, GFP_ATOMIC);
  328. if (skb) {
  329. __skb_queue_head_init(&queue);
  330. __skb_queue_tail(&queue, skb);
  331. aoenet_xmit(&queue);
  332. }
  333. return 1;
  334. }
  335. /* some callers cannot sleep, and they can call this function,
  336. * transmitting the packets later, when interrupts are on
  337. */
  338. static void
  339. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
  340. {
  341. struct aoe_hdr *h;
  342. struct aoe_cfghdr *ch;
  343. struct sk_buff *skb;
  344. struct net_device *ifp;
  345. rcu_read_lock();
  346. for_each_netdev_rcu(&init_net, ifp) {
  347. dev_hold(ifp);
  348. if (!is_aoe_netif(ifp))
  349. goto cont;
  350. skb = new_skb(sizeof *h + sizeof *ch);
  351. if (skb == NULL) {
  352. printk(KERN_INFO "aoe: skb alloc failure\n");
  353. goto cont;
  354. }
  355. skb_put(skb, sizeof *h + sizeof *ch);
  356. skb->dev = ifp;
  357. __skb_queue_tail(queue, skb);
  358. h = (struct aoe_hdr *) skb_mac_header(skb);
  359. memset(h, 0, sizeof *h + sizeof *ch);
  360. memset(h->dst, 0xff, sizeof h->dst);
  361. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  362. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  363. h->verfl = AOE_HVER;
  364. h->major = cpu_to_be16(aoemajor);
  365. h->minor = aoeminor;
  366. h->cmd = AOECMD_CFG;
  367. cont:
  368. dev_put(ifp);
  369. }
  370. rcu_read_unlock();
  371. }
  372. static void
  373. resend(struct aoedev *d, struct frame *f)
  374. {
  375. struct sk_buff *skb;
  376. struct sk_buff_head queue;
  377. struct aoe_hdr *h;
  378. struct aoe_atahdr *ah;
  379. struct aoetgt *t;
  380. char buf[128];
  381. u32 n;
  382. t = f->t;
  383. n = newtag(d);
  384. skb = f->skb;
  385. if (ifrotate(t) == NULL) {
  386. /* probably can't happen, but set it up to fail anyway */
  387. pr_info("aoe: resend: no interfaces to rotate to.\n");
  388. ktcomplete(f, NULL);
  389. return;
  390. }
  391. h = (struct aoe_hdr *) skb_mac_header(skb);
  392. ah = (struct aoe_atahdr *) (h+1);
  393. snprintf(buf, sizeof buf,
  394. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x s=%pm d=%pm nout=%d\n",
  395. "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
  396. h->src, h->dst, t->nout);
  397. aoechr_error(buf);
  398. f->tag = n;
  399. fhash(f);
  400. h->tag = cpu_to_be32(n);
  401. memcpy(h->dst, t->addr, sizeof h->dst);
  402. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  403. skb->dev = t->ifp->nd;
  404. skb = skb_clone(skb, GFP_ATOMIC);
  405. if (skb == NULL)
  406. return;
  407. __skb_queue_head_init(&queue);
  408. __skb_queue_tail(&queue, skb);
  409. aoenet_xmit(&queue);
  410. }
  411. static int
  412. tsince(u32 tag)
  413. {
  414. int n;
  415. n = jiffies & 0xffff;
  416. n -= tag & 0xffff;
  417. if (n < 0)
  418. n += 1<<16;
  419. return n;
  420. }
  421. static struct aoeif *
  422. getif(struct aoetgt *t, struct net_device *nd)
  423. {
  424. struct aoeif *p, *e;
  425. p = t->ifs;
  426. e = p + NAOEIFS;
  427. for (; p < e; p++)
  428. if (p->nd == nd)
  429. return p;
  430. return NULL;
  431. }
  432. static void
  433. ejectif(struct aoetgt *t, struct aoeif *ifp)
  434. {
  435. struct aoeif *e;
  436. struct net_device *nd;
  437. ulong n;
  438. nd = ifp->nd;
  439. e = t->ifs + NAOEIFS - 1;
  440. n = (e - ifp) * sizeof *ifp;
  441. memmove(ifp, ifp+1, n);
  442. e->nd = NULL;
  443. dev_put(nd);
  444. }
  445. static int
  446. sthtith(struct aoedev *d)
  447. {
  448. struct frame *f, *nf;
  449. struct list_head *nx, *pos, *head;
  450. struct sk_buff *skb;
  451. struct aoetgt *ht = d->htgt;
  452. int i;
  453. for (i = 0; i < NFACTIVE; i++) {
  454. head = &d->factive[i];
  455. list_for_each_safe(pos, nx, head) {
  456. f = list_entry(pos, struct frame, head);
  457. if (f->t != ht)
  458. continue;
  459. nf = newframe(d);
  460. if (!nf)
  461. return 0;
  462. /* remove frame from active list */
  463. list_del(pos);
  464. /* reassign all pertinent bits to new outbound frame */
  465. skb = nf->skb;
  466. nf->skb = f->skb;
  467. nf->buf = f->buf;
  468. nf->bcnt = f->bcnt;
  469. nf->lba = f->lba;
  470. nf->bv = f->bv;
  471. nf->bv_off = f->bv_off;
  472. nf->waited = 0;
  473. f->skb = skb;
  474. aoe_freetframe(f);
  475. ht->nout--;
  476. nf->t->nout++;
  477. resend(d, nf);
  478. }
  479. }
  480. /* We've cleaned up the outstanding so take away his
  481. * interfaces so he won't be used. We should remove him from
  482. * the target array here, but cleaning up a target is
  483. * involved. PUNT!
  484. */
  485. memset(ht->ifs, 0, sizeof ht->ifs);
  486. d->htgt = NULL;
  487. return 1;
  488. }
  489. static inline unsigned char
  490. ata_scnt(unsigned char *packet) {
  491. struct aoe_hdr *h;
  492. struct aoe_atahdr *ah;
  493. h = (struct aoe_hdr *) packet;
  494. ah = (struct aoe_atahdr *) (h+1);
  495. return ah->scnt;
  496. }
  497. static void
  498. rexmit_timer(ulong vp)
  499. {
  500. struct aoedev *d;
  501. struct aoetgt *t, **tt, **te;
  502. struct aoeif *ifp;
  503. struct frame *f;
  504. struct list_head *head, *pos, *nx;
  505. LIST_HEAD(flist);
  506. register long timeout;
  507. ulong flags, n;
  508. int i;
  509. d = (struct aoedev *) vp;
  510. /* timeout is always ~150% of the moving average */
  511. timeout = d->rttavg;
  512. timeout += timeout >> 1;
  513. spin_lock_irqsave(&d->lock, flags);
  514. if (d->flags & DEVFL_TKILL) {
  515. spin_unlock_irqrestore(&d->lock, flags);
  516. return;
  517. }
  518. /* collect all frames to rexmit into flist */
  519. for (i = 0; i < NFACTIVE; i++) {
  520. head = &d->factive[i];
  521. list_for_each_safe(pos, nx, head) {
  522. f = list_entry(pos, struct frame, head);
  523. if (tsince(f->tag) < timeout)
  524. break; /* end of expired frames */
  525. /* move to flist for later processing */
  526. list_move_tail(pos, &flist);
  527. }
  528. }
  529. /* window check */
  530. tt = d->targets;
  531. te = tt + d->ntargets;
  532. for (; tt < te && (t = *tt); tt++) {
  533. if (t->nout == t->maxout
  534. && t->maxout < t->nframes
  535. && (jiffies - t->lastwadj)/HZ > 10) {
  536. t->maxout++;
  537. t->lastwadj = jiffies;
  538. }
  539. }
  540. if (!list_empty(&flist)) { /* retransmissions necessary */
  541. n = d->rttavg <<= 1;
  542. if (n > MAXTIMER)
  543. d->rttavg = MAXTIMER;
  544. }
  545. /* process expired frames */
  546. while (!list_empty(&flist)) {
  547. pos = flist.next;
  548. f = list_entry(pos, struct frame, head);
  549. n = f->waited += timeout;
  550. n /= HZ;
  551. if (n > aoe_deadsecs) {
  552. /* Waited too long. Device failure.
  553. * Hang all frames on first hash bucket for downdev
  554. * to clean up.
  555. */
  556. list_splice(&flist, &d->factive[0]);
  557. aoedev_downdev(d);
  558. break;
  559. }
  560. list_del(pos);
  561. t = f->t;
  562. if (n > aoe_deadsecs/2)
  563. d->htgt = t; /* see if another target can help */
  564. if (t->nout == t->maxout) {
  565. if (t->maxout > 1)
  566. t->maxout--;
  567. t->lastwadj = jiffies;
  568. }
  569. ifp = getif(t, f->skb->dev);
  570. if (ifp && ++ifp->lost > (t->nframes << 1)
  571. && (ifp != t->ifs || t->ifs[1].nd)) {
  572. ejectif(t, ifp);
  573. ifp = NULL;
  574. }
  575. resend(d, f);
  576. }
  577. if ((d->flags & DEVFL_KICKME || d->htgt) && d->blkq) {
  578. d->flags &= ~DEVFL_KICKME;
  579. d->blkq->request_fn(d->blkq);
  580. }
  581. d->timer.expires = jiffies + TIMERTICK;
  582. add_timer(&d->timer);
  583. spin_unlock_irqrestore(&d->lock, flags);
  584. }
  585. static unsigned long
  586. rqbiocnt(struct request *r)
  587. {
  588. struct bio *bio;
  589. unsigned long n = 0;
  590. __rq_for_each_bio(bio, r)
  591. n++;
  592. return n;
  593. }
  594. /* This can be removed if we are certain that no users of the block
  595. * layer will ever use zero-count pages in bios. Otherwise we have to
  596. * protect against the put_page sometimes done by the network layer.
  597. *
  598. * See http://oss.sgi.com/archives/xfs/2007-01/msg00594.html for
  599. * discussion.
  600. *
  601. * We cannot use get_page in the workaround, because it insists on a
  602. * positive page count as a precondition. So we use _count directly.
  603. */
  604. static void
  605. bio_pageinc(struct bio *bio)
  606. {
  607. struct bio_vec *bv;
  608. struct page *page;
  609. int i;
  610. bio_for_each_segment(bv, bio, i) {
  611. page = bv->bv_page;
  612. /* Non-zero page count for non-head members of
  613. * compound pages is no longer allowed by the kernel,
  614. * but this has never been seen here.
  615. */
  616. if (unlikely(PageCompound(page)))
  617. if (compound_trans_head(page) != page) {
  618. pr_crit("page tail used for block I/O\n");
  619. BUG();
  620. }
  621. atomic_inc(&page->_count);
  622. }
  623. }
  624. static void
  625. bio_pagedec(struct bio *bio)
  626. {
  627. struct bio_vec *bv;
  628. int i;
  629. bio_for_each_segment(bv, bio, i)
  630. atomic_dec(&bv->bv_page->_count);
  631. }
  632. static void
  633. bufinit(struct buf *buf, struct request *rq, struct bio *bio)
  634. {
  635. struct bio_vec *bv;
  636. memset(buf, 0, sizeof(*buf));
  637. buf->rq = rq;
  638. buf->bio = bio;
  639. buf->resid = bio->bi_size;
  640. buf->sector = bio->bi_sector;
  641. bio_pageinc(bio);
  642. buf->bv = bv = &bio->bi_io_vec[bio->bi_idx];
  643. buf->bv_resid = bv->bv_len;
  644. WARN_ON(buf->bv_resid == 0);
  645. }
  646. static struct buf *
  647. nextbuf(struct aoedev *d)
  648. {
  649. struct request *rq;
  650. struct request_queue *q;
  651. struct buf *buf;
  652. struct bio *bio;
  653. q = d->blkq;
  654. if (q == NULL)
  655. return NULL; /* initializing */
  656. if (d->ip.buf)
  657. return d->ip.buf;
  658. rq = d->ip.rq;
  659. if (rq == NULL) {
  660. rq = blk_peek_request(q);
  661. if (rq == NULL)
  662. return NULL;
  663. blk_start_request(rq);
  664. d->ip.rq = rq;
  665. d->ip.nxbio = rq->bio;
  666. rq->special = (void *) rqbiocnt(rq);
  667. }
  668. buf = mempool_alloc(d->bufpool, GFP_ATOMIC);
  669. if (buf == NULL) {
  670. pr_err("aoe: nextbuf: unable to mempool_alloc!\n");
  671. return NULL;
  672. }
  673. bio = d->ip.nxbio;
  674. bufinit(buf, rq, bio);
  675. bio = bio->bi_next;
  676. d->ip.nxbio = bio;
  677. if (bio == NULL)
  678. d->ip.rq = NULL;
  679. return d->ip.buf = buf;
  680. }
  681. /* enters with d->lock held */
  682. void
  683. aoecmd_work(struct aoedev *d)
  684. {
  685. if (d->htgt && !sthtith(d))
  686. return;
  687. while (aoecmd_ata_rw(d))
  688. ;
  689. }
  690. /* this function performs work that has been deferred until sleeping is OK
  691. */
  692. void
  693. aoecmd_sleepwork(struct work_struct *work)
  694. {
  695. struct aoedev *d = container_of(work, struct aoedev, work);
  696. struct block_device *bd;
  697. u64 ssize;
  698. if (d->flags & DEVFL_GDALLOC)
  699. aoeblk_gdalloc(d);
  700. if (d->flags & DEVFL_NEWSIZE) {
  701. ssize = get_capacity(d->gd);
  702. bd = bdget_disk(d->gd, 0);
  703. if (bd) {
  704. mutex_lock(&bd->bd_inode->i_mutex);
  705. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  706. mutex_unlock(&bd->bd_inode->i_mutex);
  707. bdput(bd);
  708. }
  709. spin_lock_irq(&d->lock);
  710. d->flags |= DEVFL_UP;
  711. d->flags &= ~DEVFL_NEWSIZE;
  712. spin_unlock_irq(&d->lock);
  713. }
  714. }
  715. static void
  716. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  717. {
  718. u64 ssize;
  719. u16 n;
  720. /* word 83: command set supported */
  721. n = get_unaligned_le16(&id[83 << 1]);
  722. /* word 86: command set/feature enabled */
  723. n |= get_unaligned_le16(&id[86 << 1]);
  724. if (n & (1<<10)) { /* bit 10: LBA 48 */
  725. d->flags |= DEVFL_EXT;
  726. /* word 100: number lba48 sectors */
  727. ssize = get_unaligned_le64(&id[100 << 1]);
  728. /* set as in ide-disk.c:init_idedisk_capacity */
  729. d->geo.cylinders = ssize;
  730. d->geo.cylinders /= (255 * 63);
  731. d->geo.heads = 255;
  732. d->geo.sectors = 63;
  733. } else {
  734. d->flags &= ~DEVFL_EXT;
  735. /* number lba28 sectors */
  736. ssize = get_unaligned_le32(&id[60 << 1]);
  737. /* NOTE: obsolete in ATA 6 */
  738. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  739. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  740. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  741. }
  742. if (d->ssize != ssize)
  743. printk(KERN_INFO
  744. "aoe: %pm e%ld.%d v%04x has %llu sectors\n",
  745. t->addr,
  746. d->aoemajor, d->aoeminor,
  747. d->fw_ver, (long long)ssize);
  748. d->ssize = ssize;
  749. d->geo.start = 0;
  750. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  751. return;
  752. if (d->gd != NULL) {
  753. set_capacity(d->gd, ssize);
  754. d->flags |= DEVFL_NEWSIZE;
  755. } else
  756. d->flags |= DEVFL_GDALLOC;
  757. schedule_work(&d->work);
  758. }
  759. static void
  760. calc_rttavg(struct aoedev *d, int rtt)
  761. {
  762. register long n;
  763. n = rtt;
  764. if (n < 0) {
  765. n = -rtt;
  766. if (n < MINTIMER)
  767. n = MINTIMER;
  768. else if (n > MAXTIMER)
  769. n = MAXTIMER;
  770. d->mintimer += (n - d->mintimer) >> 1;
  771. } else if (n < d->mintimer)
  772. n = d->mintimer;
  773. else if (n > MAXTIMER)
  774. n = MAXTIMER;
  775. /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
  776. n -= d->rttavg;
  777. d->rttavg += n >> 2;
  778. }
  779. static struct aoetgt *
  780. gettgt(struct aoedev *d, char *addr)
  781. {
  782. struct aoetgt **t, **e;
  783. t = d->targets;
  784. e = t + NTARGETS;
  785. for (; t < e && *t; t++)
  786. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  787. return *t;
  788. return NULL;
  789. }
  790. static void
  791. bvcpy(struct bio_vec *bv, ulong off, struct sk_buff *skb, long cnt)
  792. {
  793. ulong fcnt;
  794. char *p;
  795. int soff = 0;
  796. loop:
  797. fcnt = bv->bv_len - (off - bv->bv_offset);
  798. if (fcnt > cnt)
  799. fcnt = cnt;
  800. p = page_address(bv->bv_page) + off;
  801. skb_copy_bits(skb, soff, p, fcnt);
  802. soff += fcnt;
  803. cnt -= fcnt;
  804. if (cnt <= 0)
  805. return;
  806. bv++;
  807. off = bv->bv_offset;
  808. goto loop;
  809. }
  810. void
  811. aoe_end_request(struct aoedev *d, struct request *rq, int fastfail)
  812. {
  813. struct bio *bio;
  814. int bok;
  815. struct request_queue *q;
  816. q = d->blkq;
  817. if (rq == d->ip.rq)
  818. d->ip.rq = NULL;
  819. do {
  820. bio = rq->bio;
  821. bok = !fastfail && test_bit(BIO_UPTODATE, &bio->bi_flags);
  822. } while (__blk_end_request(rq, bok ? 0 : -EIO, bio->bi_size));
  823. /* cf. http://lkml.org/lkml/2006/10/31/28 */
  824. if (!fastfail)
  825. q->request_fn(q);
  826. }
  827. static void
  828. aoe_end_buf(struct aoedev *d, struct buf *buf)
  829. {
  830. struct request *rq;
  831. unsigned long n;
  832. if (buf == d->ip.buf)
  833. d->ip.buf = NULL;
  834. rq = buf->rq;
  835. bio_pagedec(buf->bio);
  836. mempool_free(buf, d->bufpool);
  837. n = (unsigned long) rq->special;
  838. rq->special = (void *) --n;
  839. if (n == 0)
  840. aoe_end_request(d, rq, 0);
  841. }
  842. static void
  843. ktiocomplete(struct frame *f)
  844. {
  845. struct aoe_hdr *hin, *hout;
  846. struct aoe_atahdr *ahin, *ahout;
  847. struct buf *buf;
  848. struct sk_buff *skb;
  849. struct aoetgt *t;
  850. struct aoeif *ifp;
  851. struct aoedev *d;
  852. long n;
  853. if (f == NULL)
  854. return;
  855. t = f->t;
  856. d = t->d;
  857. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  858. ahout = (struct aoe_atahdr *) (hout+1);
  859. buf = f->buf;
  860. skb = f->r_skb;
  861. if (skb == NULL)
  862. goto noskb; /* just fail the buf. */
  863. hin = (struct aoe_hdr *) skb->data;
  864. skb_pull(skb, sizeof(*hin));
  865. ahin = (struct aoe_atahdr *) skb->data;
  866. skb_pull(skb, sizeof(*ahin));
  867. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  868. pr_err("aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  869. ahout->cmdstat, ahin->cmdstat,
  870. d->aoemajor, d->aoeminor);
  871. noskb: if (buf)
  872. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  873. goto badrsp;
  874. }
  875. n = ahout->scnt << 9;
  876. switch (ahout->cmdstat) {
  877. case ATA_CMD_PIO_READ:
  878. case ATA_CMD_PIO_READ_EXT:
  879. if (skb->len < n) {
  880. pr_err("aoe: runt data size in read. skb->len=%d need=%ld\n",
  881. skb->len, n);
  882. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  883. break;
  884. }
  885. bvcpy(f->bv, f->bv_off, skb, n);
  886. case ATA_CMD_PIO_WRITE:
  887. case ATA_CMD_PIO_WRITE_EXT:
  888. spin_lock_irq(&d->lock);
  889. ifp = getif(t, skb->dev);
  890. if (ifp)
  891. ifp->lost = 0;
  892. if (d->htgt == t) /* I'll help myself, thank you. */
  893. d->htgt = NULL;
  894. spin_unlock_irq(&d->lock);
  895. break;
  896. case ATA_CMD_ID_ATA:
  897. if (skb->len < 512) {
  898. pr_info("aoe: runt data size in ataid. skb->len=%d\n",
  899. skb->len);
  900. break;
  901. }
  902. if (skb_linearize(skb))
  903. break;
  904. spin_lock_irq(&d->lock);
  905. ataid_complete(d, t, skb->data);
  906. spin_unlock_irq(&d->lock);
  907. break;
  908. default:
  909. pr_info("aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  910. ahout->cmdstat,
  911. be16_to_cpu(get_unaligned(&hin->major)),
  912. hin->minor);
  913. }
  914. badrsp:
  915. spin_lock_irq(&d->lock);
  916. aoe_freetframe(f);
  917. if (buf && --buf->nframesout == 0 && buf->resid == 0)
  918. aoe_end_buf(d, buf);
  919. aoecmd_work(d);
  920. spin_unlock_irq(&d->lock);
  921. aoedev_put(d);
  922. dev_kfree_skb(skb);
  923. }
  924. /* Enters with iocq.lock held.
  925. * Returns true iff responses needing processing remain.
  926. */
  927. static int
  928. ktio(void)
  929. {
  930. struct frame *f;
  931. struct list_head *pos;
  932. int i;
  933. for (i = 0; ; ++i) {
  934. if (i == MAXIOC)
  935. return 1;
  936. if (list_empty(&iocq.head))
  937. return 0;
  938. pos = iocq.head.next;
  939. list_del(pos);
  940. spin_unlock_irq(&iocq.lock);
  941. f = list_entry(pos, struct frame, head);
  942. ktiocomplete(f);
  943. spin_lock_irq(&iocq.lock);
  944. }
  945. }
  946. static int
  947. kthread(void *vp)
  948. {
  949. struct ktstate *k;
  950. DECLARE_WAITQUEUE(wait, current);
  951. int more;
  952. k = vp;
  953. current->flags |= PF_NOFREEZE;
  954. set_user_nice(current, -10);
  955. complete(&k->rendez); /* tell spawner we're running */
  956. do {
  957. spin_lock_irq(k->lock);
  958. more = k->fn();
  959. if (!more) {
  960. add_wait_queue(k->waitq, &wait);
  961. __set_current_state(TASK_INTERRUPTIBLE);
  962. }
  963. spin_unlock_irq(k->lock);
  964. if (!more) {
  965. schedule();
  966. remove_wait_queue(k->waitq, &wait);
  967. } else
  968. cond_resched();
  969. } while (!kthread_should_stop());
  970. complete(&k->rendez); /* tell spawner we're stopping */
  971. return 0;
  972. }
  973. void
  974. aoe_ktstop(struct ktstate *k)
  975. {
  976. kthread_stop(k->task);
  977. wait_for_completion(&k->rendez);
  978. }
  979. int
  980. aoe_ktstart(struct ktstate *k)
  981. {
  982. struct task_struct *task;
  983. init_completion(&k->rendez);
  984. task = kthread_run(kthread, k, k->name);
  985. if (task == NULL || IS_ERR(task))
  986. return -ENOMEM;
  987. k->task = task;
  988. wait_for_completion(&k->rendez); /* allow kthread to start */
  989. init_completion(&k->rendez); /* for waiting for exit later */
  990. return 0;
  991. }
  992. /* pass it off to kthreads for processing */
  993. static void
  994. ktcomplete(struct frame *f, struct sk_buff *skb)
  995. {
  996. ulong flags;
  997. f->r_skb = skb;
  998. spin_lock_irqsave(&iocq.lock, flags);
  999. list_add_tail(&f->head, &iocq.head);
  1000. spin_unlock_irqrestore(&iocq.lock, flags);
  1001. wake_up(&ktiowq);
  1002. }
  1003. struct sk_buff *
  1004. aoecmd_ata_rsp(struct sk_buff *skb)
  1005. {
  1006. struct aoedev *d;
  1007. struct aoe_hdr *h;
  1008. struct frame *f;
  1009. struct aoetgt *t;
  1010. u32 n;
  1011. ulong flags;
  1012. char ebuf[128];
  1013. u16 aoemajor;
  1014. h = (struct aoe_hdr *) skb->data;
  1015. aoemajor = be16_to_cpu(get_unaligned(&h->major));
  1016. d = aoedev_by_aoeaddr(aoemajor, h->minor);
  1017. if (d == NULL) {
  1018. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  1019. "for unknown device %d.%d\n",
  1020. aoemajor, h->minor);
  1021. aoechr_error(ebuf);
  1022. return skb;
  1023. }
  1024. spin_lock_irqsave(&d->lock, flags);
  1025. n = be32_to_cpu(get_unaligned(&h->tag));
  1026. f = getframe(d, n);
  1027. if (f == NULL) {
  1028. calc_rttavg(d, -tsince(n));
  1029. spin_unlock_irqrestore(&d->lock, flags);
  1030. aoedev_put(d);
  1031. snprintf(ebuf, sizeof ebuf,
  1032. "%15s e%d.%d tag=%08x@%08lx\n",
  1033. "unexpected rsp",
  1034. get_unaligned_be16(&h->major),
  1035. h->minor,
  1036. get_unaligned_be32(&h->tag),
  1037. jiffies);
  1038. aoechr_error(ebuf);
  1039. return skb;
  1040. }
  1041. t = f->t;
  1042. calc_rttavg(d, tsince(f->tag));
  1043. t->nout--;
  1044. aoecmd_work(d);
  1045. spin_unlock_irqrestore(&d->lock, flags);
  1046. ktcomplete(f, skb);
  1047. /*
  1048. * Note here that we do not perform an aoedev_put, as we are
  1049. * leaving this reference for the ktio to release.
  1050. */
  1051. return NULL;
  1052. }
  1053. void
  1054. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  1055. {
  1056. struct sk_buff_head queue;
  1057. __skb_queue_head_init(&queue);
  1058. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  1059. aoenet_xmit(&queue);
  1060. }
  1061. struct sk_buff *
  1062. aoecmd_ata_id(struct aoedev *d)
  1063. {
  1064. struct aoe_hdr *h;
  1065. struct aoe_atahdr *ah;
  1066. struct frame *f;
  1067. struct sk_buff *skb;
  1068. struct aoetgt *t;
  1069. f = newframe(d);
  1070. if (f == NULL)
  1071. return NULL;
  1072. t = *d->tgt;
  1073. /* initialize the headers & frame */
  1074. skb = f->skb;
  1075. h = (struct aoe_hdr *) skb_mac_header(skb);
  1076. ah = (struct aoe_atahdr *) (h+1);
  1077. skb_put(skb, sizeof *h + sizeof *ah);
  1078. memset(h, 0, skb->len);
  1079. f->tag = aoehdr_atainit(d, t, h);
  1080. fhash(f);
  1081. t->nout++;
  1082. f->waited = 0;
  1083. /* set up ata header */
  1084. ah->scnt = 1;
  1085. ah->cmdstat = ATA_CMD_ID_ATA;
  1086. ah->lba3 = 0xa0;
  1087. skb->dev = t->ifp->nd;
  1088. d->rttavg = MAXTIMER;
  1089. d->timer.function = rexmit_timer;
  1090. return skb_clone(skb, GFP_ATOMIC);
  1091. }
  1092. static struct aoetgt *
  1093. addtgt(struct aoedev *d, char *addr, ulong nframes)
  1094. {
  1095. struct aoetgt *t, **tt, **te;
  1096. tt = d->targets;
  1097. te = tt + NTARGETS;
  1098. for (; tt < te && *tt; tt++)
  1099. ;
  1100. if (tt == te) {
  1101. printk(KERN_INFO
  1102. "aoe: device addtgt failure; too many targets\n");
  1103. return NULL;
  1104. }
  1105. t = kzalloc(sizeof(*t), GFP_ATOMIC);
  1106. if (!t) {
  1107. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  1108. return NULL;
  1109. }
  1110. d->ntargets++;
  1111. t->nframes = nframes;
  1112. t->d = d;
  1113. memcpy(t->addr, addr, sizeof t->addr);
  1114. t->ifp = t->ifs;
  1115. t->maxout = t->nframes;
  1116. INIT_LIST_HEAD(&t->ffree);
  1117. return *tt = t;
  1118. }
  1119. static void
  1120. setdbcnt(struct aoedev *d)
  1121. {
  1122. struct aoetgt **t, **e;
  1123. int bcnt = 0;
  1124. t = d->targets;
  1125. e = t + NTARGETS;
  1126. for (; t < e && *t; t++)
  1127. if (bcnt == 0 || bcnt > (*t)->minbcnt)
  1128. bcnt = (*t)->minbcnt;
  1129. if (bcnt != d->maxbcnt) {
  1130. d->maxbcnt = bcnt;
  1131. pr_info("aoe: e%ld.%d: setting %d byte data frames\n",
  1132. d->aoemajor, d->aoeminor, bcnt);
  1133. }
  1134. }
  1135. static void
  1136. setifbcnt(struct aoetgt *t, struct net_device *nd, int bcnt)
  1137. {
  1138. struct aoedev *d;
  1139. struct aoeif *p, *e;
  1140. int minbcnt;
  1141. d = t->d;
  1142. minbcnt = bcnt;
  1143. p = t->ifs;
  1144. e = p + NAOEIFS;
  1145. for (; p < e; p++) {
  1146. if (p->nd == NULL)
  1147. break; /* end of the valid interfaces */
  1148. if (p->nd == nd) {
  1149. p->bcnt = bcnt; /* we're updating */
  1150. nd = NULL;
  1151. } else if (minbcnt > p->bcnt)
  1152. minbcnt = p->bcnt; /* find the min interface */
  1153. }
  1154. if (nd) {
  1155. if (p == e) {
  1156. pr_err("aoe: device setifbcnt failure; too many interfaces.\n");
  1157. return;
  1158. }
  1159. dev_hold(nd);
  1160. p->nd = nd;
  1161. p->bcnt = bcnt;
  1162. }
  1163. t->minbcnt = minbcnt;
  1164. setdbcnt(d);
  1165. }
  1166. void
  1167. aoecmd_cfg_rsp(struct sk_buff *skb)
  1168. {
  1169. struct aoedev *d;
  1170. struct aoe_hdr *h;
  1171. struct aoe_cfghdr *ch;
  1172. struct aoetgt *t;
  1173. ulong flags, sysminor, aoemajor;
  1174. struct sk_buff *sl;
  1175. struct sk_buff_head queue;
  1176. u16 n;
  1177. sl = NULL;
  1178. h = (struct aoe_hdr *) skb_mac_header(skb);
  1179. ch = (struct aoe_cfghdr *) (h+1);
  1180. /*
  1181. * Enough people have their dip switches set backwards to
  1182. * warrant a loud message for this special case.
  1183. */
  1184. aoemajor = get_unaligned_be16(&h->major);
  1185. if (aoemajor == 0xfff) {
  1186. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  1187. "Check shelf dip switches.\n");
  1188. return;
  1189. }
  1190. if (h->minor >= NPERSHELF) {
  1191. pr_err("aoe: e%ld.%d %s, %d\n",
  1192. aoemajor, h->minor,
  1193. "slot number larger than the maximum",
  1194. NPERSHELF-1);
  1195. return;
  1196. }
  1197. sysminor = SYSMINOR(aoemajor, h->minor);
  1198. if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
  1199. printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
  1200. aoemajor, (int) h->minor);
  1201. return;
  1202. }
  1203. n = be16_to_cpu(ch->bufcnt);
  1204. if (n > aoe_maxout) /* keep it reasonable */
  1205. n = aoe_maxout;
  1206. d = aoedev_by_sysminor_m(sysminor);
  1207. if (d == NULL) {
  1208. printk(KERN_INFO "aoe: device sysminor_m failure\n");
  1209. return;
  1210. }
  1211. spin_lock_irqsave(&d->lock, flags);
  1212. t = gettgt(d, h->src);
  1213. if (!t) {
  1214. t = addtgt(d, h->src, n);
  1215. if (!t)
  1216. goto bail;
  1217. }
  1218. n = skb->dev->mtu;
  1219. n -= sizeof(struct aoe_hdr) + sizeof(struct aoe_atahdr);
  1220. n /= 512;
  1221. if (n > ch->scnt)
  1222. n = ch->scnt;
  1223. n = n ? n * 512 : DEFAULTBCNT;
  1224. setifbcnt(t, skb->dev, n);
  1225. /* don't change users' perspective */
  1226. if (d->nopen == 0) {
  1227. d->fw_ver = be16_to_cpu(ch->fwver);
  1228. sl = aoecmd_ata_id(d);
  1229. }
  1230. bail:
  1231. spin_unlock_irqrestore(&d->lock, flags);
  1232. aoedev_put(d);
  1233. if (sl) {
  1234. __skb_queue_head_init(&queue);
  1235. __skb_queue_tail(&queue, sl);
  1236. aoenet_xmit(&queue);
  1237. }
  1238. }
  1239. void
  1240. aoecmd_cleanslate(struct aoedev *d)
  1241. {
  1242. struct aoetgt **t, **te;
  1243. d->mintimer = MINTIMER;
  1244. d->maxbcnt = 0;
  1245. t = d->targets;
  1246. te = t + NTARGETS;
  1247. for (; t < te && *t; t++)
  1248. (*t)->maxout = (*t)->nframes;
  1249. }
  1250. void
  1251. aoe_failbuf(struct aoedev *d, struct buf *buf)
  1252. {
  1253. if (buf == NULL)
  1254. return;
  1255. buf->resid = 0;
  1256. clear_bit(BIO_UPTODATE, &buf->bio->bi_flags);
  1257. if (buf->nframesout == 0)
  1258. aoe_end_buf(d, buf);
  1259. }
  1260. void
  1261. aoe_flush_iocq(void)
  1262. {
  1263. struct frame *f;
  1264. struct aoedev *d;
  1265. LIST_HEAD(flist);
  1266. struct list_head *pos;
  1267. struct sk_buff *skb;
  1268. ulong flags;
  1269. spin_lock_irqsave(&iocq.lock, flags);
  1270. list_splice_init(&iocq.head, &flist);
  1271. spin_unlock_irqrestore(&iocq.lock, flags);
  1272. while (!list_empty(&flist)) {
  1273. pos = flist.next;
  1274. list_del(pos);
  1275. f = list_entry(pos, struct frame, head);
  1276. d = f->t->d;
  1277. skb = f->r_skb;
  1278. spin_lock_irqsave(&d->lock, flags);
  1279. if (f->buf) {
  1280. f->buf->nframesout--;
  1281. aoe_failbuf(d, f->buf);
  1282. }
  1283. aoe_freetframe(f);
  1284. spin_unlock_irqrestore(&d->lock, flags);
  1285. dev_kfree_skb(skb);
  1286. aoedev_put(d);
  1287. }
  1288. }
  1289. int __init
  1290. aoecmd_init(void)
  1291. {
  1292. INIT_LIST_HEAD(&iocq.head);
  1293. spin_lock_init(&iocq.lock);
  1294. init_waitqueue_head(&ktiowq);
  1295. kts.name = "aoe_ktio";
  1296. kts.fn = ktio;
  1297. kts.waitq = &ktiowq;
  1298. kts.lock = &iocq.lock;
  1299. return aoe_ktstart(&kts);
  1300. }
  1301. void
  1302. aoecmd_exit(void)
  1303. {
  1304. aoe_ktstop(&kts);
  1305. aoe_flush_iocq();
  1306. }