aoecmd.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoecmd.c
  4. * Filesystem request handling methods
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/genhd.h>
  11. #include <linux/moduleparam.h>
  12. #include <net/net_namespace.h>
  13. #include <asm/unaligned.h>
  14. #include "aoe.h"
  15. static int aoe_deadsecs = 60 * 3;
  16. module_param(aoe_deadsecs, int, 0644);
  17. MODULE_PARM_DESC(aoe_deadsecs, "After aoe_deadsecs seconds, give up and fail dev.");
  18. static int aoe_maxout = 16;
  19. module_param(aoe_maxout, int, 0644);
  20. MODULE_PARM_DESC(aoe_maxout,
  21. "Only aoe_maxout outstanding packets for every MAC on eX.Y.");
  22. static struct sk_buff *
  23. new_skb(ulong len)
  24. {
  25. struct sk_buff *skb;
  26. skb = alloc_skb(len, GFP_ATOMIC);
  27. if (skb) {
  28. skb_reset_mac_header(skb);
  29. skb_reset_network_header(skb);
  30. skb->protocol = __constant_htons(ETH_P_AOE);
  31. skb->priority = 0;
  32. skb->next = skb->prev = NULL;
  33. /* tell the network layer not to perform IP checksums
  34. * or to get the NIC to do it
  35. */
  36. skb->ip_summed = CHECKSUM_NONE;
  37. }
  38. return skb;
  39. }
  40. static struct frame *
  41. getframe(struct aoetgt *t, int tag)
  42. {
  43. struct frame *f, *e;
  44. f = t->frames;
  45. e = f + t->nframes;
  46. for (; f<e; f++)
  47. if (f->tag == tag)
  48. return f;
  49. return NULL;
  50. }
  51. /*
  52. * Leave the top bit clear so we have tagspace for userland.
  53. * The bottom 16 bits are the xmit tick for rexmit/rttavg processing.
  54. * This driver reserves tag -1 to mean "unused frame."
  55. */
  56. static int
  57. newtag(struct aoetgt *t)
  58. {
  59. register ulong n;
  60. n = jiffies & 0xffff;
  61. return n |= (++t->lasttag & 0x7fff) << 16;
  62. }
  63. static int
  64. aoehdr_atainit(struct aoedev *d, struct aoetgt *t, struct aoe_hdr *h)
  65. {
  66. u32 host_tag = newtag(t);
  67. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  68. memcpy(h->dst, t->addr, sizeof h->dst);
  69. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  70. h->verfl = AOE_HVER;
  71. h->major = cpu_to_be16(d->aoemajor);
  72. h->minor = d->aoeminor;
  73. h->cmd = AOECMD_ATA;
  74. h->tag = cpu_to_be32(host_tag);
  75. return host_tag;
  76. }
  77. static inline void
  78. put_lba(struct aoe_atahdr *ah, sector_t lba)
  79. {
  80. ah->lba0 = lba;
  81. ah->lba1 = lba >>= 8;
  82. ah->lba2 = lba >>= 8;
  83. ah->lba3 = lba >>= 8;
  84. ah->lba4 = lba >>= 8;
  85. ah->lba5 = lba >>= 8;
  86. }
  87. static void
  88. ifrotate(struct aoetgt *t)
  89. {
  90. t->ifp++;
  91. if (t->ifp >= &t->ifs[NAOEIFS] || t->ifp->nd == NULL)
  92. t->ifp = t->ifs;
  93. if (t->ifp->nd == NULL) {
  94. printk(KERN_INFO "aoe: no interface to rotate to\n");
  95. BUG();
  96. }
  97. }
  98. static void
  99. skb_pool_put(struct aoedev *d, struct sk_buff *skb)
  100. {
  101. __skb_queue_tail(&d->skbpool, skb);
  102. }
  103. static struct sk_buff *
  104. skb_pool_get(struct aoedev *d)
  105. {
  106. struct sk_buff *skb = skb_peek(&d->skbpool);
  107. if (skb && atomic_read(&skb_shinfo(skb)->dataref) == 1) {
  108. __skb_unlink(skb, &d->skbpool);
  109. return skb;
  110. }
  111. if (skb_queue_len(&d->skbpool) < NSKBPOOLMAX &&
  112. (skb = new_skb(ETH_ZLEN)))
  113. return skb;
  114. return NULL;
  115. }
  116. /* freeframe is where we do our load balancing so it's a little hairy. */
  117. static struct frame *
  118. freeframe(struct aoedev *d)
  119. {
  120. struct frame *f, *e, *rf;
  121. struct aoetgt **t;
  122. struct sk_buff *skb;
  123. if (d->targets[0] == NULL) { /* shouldn't happen, but I'm paranoid */
  124. printk(KERN_ERR "aoe: NULL TARGETS!\n");
  125. return NULL;
  126. }
  127. t = d->tgt;
  128. t++;
  129. if (t >= &d->targets[NTARGETS] || !*t)
  130. t = d->targets;
  131. for (;;) {
  132. if ((*t)->nout < (*t)->maxout
  133. && t != d->htgt
  134. && (*t)->ifp->nd) {
  135. rf = NULL;
  136. f = (*t)->frames;
  137. e = f + (*t)->nframes;
  138. for (; f < e; f++) {
  139. if (f->tag != FREETAG)
  140. continue;
  141. skb = f->skb;
  142. if (!skb
  143. && !(f->skb = skb = new_skb(ETH_ZLEN)))
  144. continue;
  145. if (atomic_read(&skb_shinfo(skb)->dataref)
  146. != 1) {
  147. if (!rf)
  148. rf = f;
  149. continue;
  150. }
  151. gotone: skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  152. skb_trim(skb, 0);
  153. d->tgt = t;
  154. ifrotate(*t);
  155. return f;
  156. }
  157. /* Work can be done, but the network layer is
  158. holding our precious packets. Try to grab
  159. one from the pool. */
  160. f = rf;
  161. if (f == NULL) { /* more paranoia */
  162. printk(KERN_ERR
  163. "aoe: freeframe: %s.\n",
  164. "unexpected null rf");
  165. d->flags |= DEVFL_KICKME;
  166. return NULL;
  167. }
  168. skb = skb_pool_get(d);
  169. if (skb) {
  170. skb_pool_put(d, f->skb);
  171. f->skb = skb;
  172. goto gotone;
  173. }
  174. (*t)->dataref++;
  175. if ((*t)->nout == 0)
  176. d->flags |= DEVFL_KICKME;
  177. }
  178. if (t == d->tgt) /* we've looped and found nada */
  179. break;
  180. t++;
  181. if (t >= &d->targets[NTARGETS] || !*t)
  182. t = d->targets;
  183. }
  184. return NULL;
  185. }
  186. static int
  187. aoecmd_ata_rw(struct aoedev *d)
  188. {
  189. struct frame *f;
  190. struct aoe_hdr *h;
  191. struct aoe_atahdr *ah;
  192. struct buf *buf;
  193. struct bio_vec *bv;
  194. struct aoetgt *t;
  195. struct sk_buff *skb;
  196. ulong bcnt;
  197. char writebit, extbit;
  198. writebit = 0x10;
  199. extbit = 0x4;
  200. f = freeframe(d);
  201. if (f == NULL)
  202. return 0;
  203. t = *d->tgt;
  204. buf = d->inprocess;
  205. bv = buf->bv;
  206. bcnt = t->ifp->maxbcnt;
  207. if (bcnt == 0)
  208. bcnt = DEFAULTBCNT;
  209. if (bcnt > buf->bv_resid)
  210. bcnt = buf->bv_resid;
  211. /* initialize the headers & frame */
  212. skb = f->skb;
  213. h = (struct aoe_hdr *) skb_mac_header(skb);
  214. ah = (struct aoe_atahdr *) (h+1);
  215. skb_put(skb, sizeof *h + sizeof *ah);
  216. memset(h, 0, skb->len);
  217. f->tag = aoehdr_atainit(d, t, h);
  218. t->nout++;
  219. f->waited = 0;
  220. f->buf = buf;
  221. f->bufaddr = page_address(bv->bv_page) + buf->bv_off;
  222. f->bcnt = bcnt;
  223. f->lba = buf->sector;
  224. /* set up ata header */
  225. ah->scnt = bcnt >> 9;
  226. put_lba(ah, buf->sector);
  227. if (d->flags & DEVFL_EXT) {
  228. ah->aflags |= AOEAFL_EXT;
  229. } else {
  230. extbit = 0;
  231. ah->lba3 &= 0x0f;
  232. ah->lba3 |= 0xe0; /* LBA bit + obsolete 0xa0 */
  233. }
  234. if (bio_data_dir(buf->bio) == WRITE) {
  235. skb_fill_page_desc(skb, 0, bv->bv_page, buf->bv_off, bcnt);
  236. ah->aflags |= AOEAFL_WRITE;
  237. skb->len += bcnt;
  238. skb->data_len = bcnt;
  239. t->wpkts++;
  240. } else {
  241. t->rpkts++;
  242. writebit = 0;
  243. }
  244. ah->cmdstat = WIN_READ | writebit | extbit;
  245. /* mark all tracking fields and load out */
  246. buf->nframesout += 1;
  247. buf->bv_off += bcnt;
  248. buf->bv_resid -= bcnt;
  249. buf->resid -= bcnt;
  250. buf->sector += bcnt >> 9;
  251. if (buf->resid == 0) {
  252. d->inprocess = NULL;
  253. } else if (buf->bv_resid == 0) {
  254. buf->bv = ++bv;
  255. buf->bv_resid = bv->bv_len;
  256. WARN_ON(buf->bv_resid == 0);
  257. buf->bv_off = bv->bv_offset;
  258. }
  259. skb->dev = t->ifp->nd;
  260. skb = skb_clone(skb, GFP_ATOMIC);
  261. if (skb)
  262. __skb_queue_tail(&d->sendq, skb);
  263. return 1;
  264. }
  265. /* some callers cannot sleep, and they can call this function,
  266. * transmitting the packets later, when interrupts are on
  267. */
  268. static void
  269. aoecmd_cfg_pkts(ushort aoemajor, unsigned char aoeminor, struct sk_buff_head *queue)
  270. {
  271. struct aoe_hdr *h;
  272. struct aoe_cfghdr *ch;
  273. struct sk_buff *skb;
  274. struct net_device *ifp;
  275. read_lock(&dev_base_lock);
  276. for_each_netdev(&init_net, ifp) {
  277. dev_hold(ifp);
  278. if (!is_aoe_netif(ifp))
  279. goto cont;
  280. skb = new_skb(sizeof *h + sizeof *ch);
  281. if (skb == NULL) {
  282. printk(KERN_INFO "aoe: skb alloc failure\n");
  283. goto cont;
  284. }
  285. skb_put(skb, sizeof *h + sizeof *ch);
  286. skb->dev = ifp;
  287. __skb_queue_tail(queue, skb);
  288. h = (struct aoe_hdr *) skb_mac_header(skb);
  289. memset(h, 0, sizeof *h + sizeof *ch);
  290. memset(h->dst, 0xff, sizeof h->dst);
  291. memcpy(h->src, ifp->dev_addr, sizeof h->src);
  292. h->type = __constant_cpu_to_be16(ETH_P_AOE);
  293. h->verfl = AOE_HVER;
  294. h->major = cpu_to_be16(aoemajor);
  295. h->minor = aoeminor;
  296. h->cmd = AOECMD_CFG;
  297. cont:
  298. dev_put(ifp);
  299. }
  300. read_unlock(&dev_base_lock);
  301. }
  302. static void
  303. resend(struct aoedev *d, struct aoetgt *t, struct frame *f)
  304. {
  305. struct sk_buff *skb;
  306. struct aoe_hdr *h;
  307. struct aoe_atahdr *ah;
  308. char buf[128];
  309. u32 n;
  310. ifrotate(t);
  311. n = newtag(t);
  312. skb = f->skb;
  313. h = (struct aoe_hdr *) skb_mac_header(skb);
  314. ah = (struct aoe_atahdr *) (h+1);
  315. snprintf(buf, sizeof buf,
  316. "%15s e%ld.%d oldtag=%08x@%08lx newtag=%08x "
  317. "s=%012llx d=%012llx nout=%d\n",
  318. "retransmit", d->aoemajor, d->aoeminor, f->tag, jiffies, n,
  319. mac_addr(h->src),
  320. mac_addr(h->dst), t->nout);
  321. aoechr_error(buf);
  322. f->tag = n;
  323. h->tag = cpu_to_be32(n);
  324. memcpy(h->dst, t->addr, sizeof h->dst);
  325. memcpy(h->src, t->ifp->nd->dev_addr, sizeof h->src);
  326. switch (ah->cmdstat) {
  327. default:
  328. break;
  329. case WIN_READ:
  330. case WIN_READ_EXT:
  331. case WIN_WRITE:
  332. case WIN_WRITE_EXT:
  333. put_lba(ah, f->lba);
  334. n = f->bcnt;
  335. if (n > DEFAULTBCNT)
  336. n = DEFAULTBCNT;
  337. ah->scnt = n >> 9;
  338. if (ah->aflags & AOEAFL_WRITE) {
  339. skb_fill_page_desc(skb, 0, virt_to_page(f->bufaddr),
  340. offset_in_page(f->bufaddr), n);
  341. skb->len = sizeof *h + sizeof *ah + n;
  342. skb->data_len = n;
  343. }
  344. }
  345. skb->dev = t->ifp->nd;
  346. skb = skb_clone(skb, GFP_ATOMIC);
  347. if (skb == NULL)
  348. return;
  349. __skb_queue_tail(&d->sendq, skb);
  350. }
  351. static int
  352. tsince(int tag)
  353. {
  354. int n;
  355. n = jiffies & 0xffff;
  356. n -= tag & 0xffff;
  357. if (n < 0)
  358. n += 1<<16;
  359. return n;
  360. }
  361. static struct aoeif *
  362. getif(struct aoetgt *t, struct net_device *nd)
  363. {
  364. struct aoeif *p, *e;
  365. p = t->ifs;
  366. e = p + NAOEIFS;
  367. for (; p < e; p++)
  368. if (p->nd == nd)
  369. return p;
  370. return NULL;
  371. }
  372. static struct aoeif *
  373. addif(struct aoetgt *t, struct net_device *nd)
  374. {
  375. struct aoeif *p;
  376. p = getif(t, NULL);
  377. if (!p)
  378. return NULL;
  379. p->nd = nd;
  380. p->maxbcnt = DEFAULTBCNT;
  381. p->lost = 0;
  382. p->lostjumbo = 0;
  383. return p;
  384. }
  385. static void
  386. ejectif(struct aoetgt *t, struct aoeif *ifp)
  387. {
  388. struct aoeif *e;
  389. ulong n;
  390. e = t->ifs + NAOEIFS - 1;
  391. n = (e - ifp) * sizeof *ifp;
  392. memmove(ifp, ifp+1, n);
  393. e->nd = NULL;
  394. }
  395. static int
  396. sthtith(struct aoedev *d)
  397. {
  398. struct frame *f, *e, *nf;
  399. struct sk_buff *skb;
  400. struct aoetgt *ht = *d->htgt;
  401. f = ht->frames;
  402. e = f + ht->nframes;
  403. for (; f < e; f++) {
  404. if (f->tag == FREETAG)
  405. continue;
  406. nf = freeframe(d);
  407. if (!nf)
  408. return 0;
  409. skb = nf->skb;
  410. *nf = *f;
  411. f->skb = skb;
  412. f->tag = FREETAG;
  413. nf->waited = 0;
  414. ht->nout--;
  415. (*d->tgt)->nout++;
  416. resend(d, *d->tgt, nf);
  417. }
  418. /* he's clean, he's useless. take away his interfaces */
  419. memset(ht->ifs, 0, sizeof ht->ifs);
  420. d->htgt = NULL;
  421. return 1;
  422. }
  423. static inline unsigned char
  424. ata_scnt(unsigned char *packet) {
  425. struct aoe_hdr *h;
  426. struct aoe_atahdr *ah;
  427. h = (struct aoe_hdr *) packet;
  428. ah = (struct aoe_atahdr *) (h+1);
  429. return ah->scnt;
  430. }
  431. static void
  432. rexmit_timer(ulong vp)
  433. {
  434. struct sk_buff_head queue;
  435. struct aoedev *d;
  436. struct aoetgt *t, **tt, **te;
  437. struct aoeif *ifp;
  438. struct frame *f, *e;
  439. register long timeout;
  440. ulong flags, n;
  441. d = (struct aoedev *) vp;
  442. /* timeout is always ~150% of the moving average */
  443. timeout = d->rttavg;
  444. timeout += timeout >> 1;
  445. spin_lock_irqsave(&d->lock, flags);
  446. if (d->flags & DEVFL_TKILL) {
  447. spin_unlock_irqrestore(&d->lock, flags);
  448. return;
  449. }
  450. tt = d->targets;
  451. te = tt + NTARGETS;
  452. for (; tt < te && *tt; tt++) {
  453. t = *tt;
  454. f = t->frames;
  455. e = f + t->nframes;
  456. for (; f < e; f++) {
  457. if (f->tag == FREETAG
  458. || tsince(f->tag) < timeout)
  459. continue;
  460. n = f->waited += timeout;
  461. n /= HZ;
  462. if (n > aoe_deadsecs) {
  463. /* waited too long. device failure. */
  464. aoedev_downdev(d);
  465. break;
  466. }
  467. if (n > HELPWAIT /* see if another target can help */
  468. && (tt != d->targets || d->targets[1]))
  469. d->htgt = tt;
  470. if (t->nout == t->maxout) {
  471. if (t->maxout > 1)
  472. t->maxout--;
  473. t->lastwadj = jiffies;
  474. }
  475. ifp = getif(t, f->skb->dev);
  476. if (ifp && ++ifp->lost > (t->nframes << 1)
  477. && (ifp != t->ifs || t->ifs[1].nd)) {
  478. ejectif(t, ifp);
  479. ifp = NULL;
  480. }
  481. if (ata_scnt(skb_mac_header(f->skb)) > DEFAULTBCNT / 512
  482. && ifp && ++ifp->lostjumbo > (t->nframes << 1)
  483. && ifp->maxbcnt != DEFAULTBCNT) {
  484. printk(KERN_INFO
  485. "aoe: e%ld.%d: "
  486. "too many lost jumbo on "
  487. "%s:%012llx - "
  488. "falling back to %d frames.\n",
  489. d->aoemajor, d->aoeminor,
  490. ifp->nd->name, mac_addr(t->addr),
  491. DEFAULTBCNT);
  492. ifp->maxbcnt = 0;
  493. }
  494. resend(d, t, f);
  495. }
  496. /* window check */
  497. if (t->nout == t->maxout
  498. && t->maxout < t->nframes
  499. && (jiffies - t->lastwadj)/HZ > 10) {
  500. t->maxout++;
  501. t->lastwadj = jiffies;
  502. }
  503. }
  504. if (!skb_queue_empty(&d->sendq)) {
  505. n = d->rttavg <<= 1;
  506. if (n > MAXTIMER)
  507. d->rttavg = MAXTIMER;
  508. }
  509. if (d->flags & DEVFL_KICKME || d->htgt) {
  510. d->flags &= ~DEVFL_KICKME;
  511. aoecmd_work(d);
  512. }
  513. __skb_queue_head_init(&queue);
  514. skb_queue_splice_init(&d->sendq, &queue);
  515. d->timer.expires = jiffies + TIMERTICK;
  516. add_timer(&d->timer);
  517. spin_unlock_irqrestore(&d->lock, flags);
  518. aoenet_xmit(&queue);
  519. }
  520. /* enters with d->lock held */
  521. void
  522. aoecmd_work(struct aoedev *d)
  523. {
  524. struct buf *buf;
  525. loop:
  526. if (d->htgt && !sthtith(d))
  527. return;
  528. if (d->inprocess == NULL) {
  529. if (list_empty(&d->bufq))
  530. return;
  531. buf = container_of(d->bufq.next, struct buf, bufs);
  532. list_del(d->bufq.next);
  533. d->inprocess = buf;
  534. }
  535. if (aoecmd_ata_rw(d))
  536. goto loop;
  537. }
  538. /* this function performs work that has been deferred until sleeping is OK
  539. */
  540. void
  541. aoecmd_sleepwork(struct work_struct *work)
  542. {
  543. struct aoedev *d = container_of(work, struct aoedev, work);
  544. if (d->flags & DEVFL_GDALLOC)
  545. aoeblk_gdalloc(d);
  546. if (d->flags & DEVFL_NEWSIZE) {
  547. struct block_device *bd;
  548. unsigned long flags;
  549. u64 ssize;
  550. ssize = d->gd->capacity;
  551. bd = bdget_disk(d->gd, 0);
  552. if (bd) {
  553. mutex_lock(&bd->bd_inode->i_mutex);
  554. i_size_write(bd->bd_inode, (loff_t)ssize<<9);
  555. mutex_unlock(&bd->bd_inode->i_mutex);
  556. bdput(bd);
  557. }
  558. spin_lock_irqsave(&d->lock, flags);
  559. d->flags |= DEVFL_UP;
  560. d->flags &= ~DEVFL_NEWSIZE;
  561. spin_unlock_irqrestore(&d->lock, flags);
  562. }
  563. }
  564. static void
  565. ataid_complete(struct aoedev *d, struct aoetgt *t, unsigned char *id)
  566. {
  567. u64 ssize;
  568. u16 n;
  569. /* word 83: command set supported */
  570. n = get_unaligned_le16(&id[83 << 1]);
  571. /* word 86: command set/feature enabled */
  572. n |= get_unaligned_le16(&id[86 << 1]);
  573. if (n & (1<<10)) { /* bit 10: LBA 48 */
  574. d->flags |= DEVFL_EXT;
  575. /* word 100: number lba48 sectors */
  576. ssize = get_unaligned_le64(&id[100 << 1]);
  577. /* set as in ide-disk.c:init_idedisk_capacity */
  578. d->geo.cylinders = ssize;
  579. d->geo.cylinders /= (255 * 63);
  580. d->geo.heads = 255;
  581. d->geo.sectors = 63;
  582. } else {
  583. d->flags &= ~DEVFL_EXT;
  584. /* number lba28 sectors */
  585. ssize = get_unaligned_le32(&id[60 << 1]);
  586. /* NOTE: obsolete in ATA 6 */
  587. d->geo.cylinders = get_unaligned_le16(&id[54 << 1]);
  588. d->geo.heads = get_unaligned_le16(&id[55 << 1]);
  589. d->geo.sectors = get_unaligned_le16(&id[56 << 1]);
  590. }
  591. if (d->ssize != ssize)
  592. printk(KERN_INFO
  593. "aoe: %012llx e%ld.%d v%04x has %llu sectors\n",
  594. mac_addr(t->addr),
  595. d->aoemajor, d->aoeminor,
  596. d->fw_ver, (long long)ssize);
  597. d->ssize = ssize;
  598. d->geo.start = 0;
  599. if (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  600. return;
  601. if (d->gd != NULL) {
  602. d->gd->capacity = ssize;
  603. d->flags |= DEVFL_NEWSIZE;
  604. } else
  605. d->flags |= DEVFL_GDALLOC;
  606. schedule_work(&d->work);
  607. }
  608. static void
  609. calc_rttavg(struct aoedev *d, int rtt)
  610. {
  611. register long n;
  612. n = rtt;
  613. if (n < 0) {
  614. n = -rtt;
  615. if (n < MINTIMER)
  616. n = MINTIMER;
  617. else if (n > MAXTIMER)
  618. n = MAXTIMER;
  619. d->mintimer += (n - d->mintimer) >> 1;
  620. } else if (n < d->mintimer)
  621. n = d->mintimer;
  622. else if (n > MAXTIMER)
  623. n = MAXTIMER;
  624. /* g == .25; cf. Congestion Avoidance and Control, Jacobson & Karels; 1988 */
  625. n -= d->rttavg;
  626. d->rttavg += n >> 2;
  627. }
  628. static struct aoetgt *
  629. gettgt(struct aoedev *d, char *addr)
  630. {
  631. struct aoetgt **t, **e;
  632. t = d->targets;
  633. e = t + NTARGETS;
  634. for (; t < e && *t; t++)
  635. if (memcmp((*t)->addr, addr, sizeof((*t)->addr)) == 0)
  636. return *t;
  637. return NULL;
  638. }
  639. static inline void
  640. diskstats(struct gendisk *disk, struct bio *bio, ulong duration, sector_t sector)
  641. {
  642. unsigned long n_sect = bio->bi_size >> 9;
  643. const int rw = bio_data_dir(bio);
  644. struct hd_struct *part;
  645. part = get_part(disk, sector);
  646. all_stat_inc(disk, part, ios[rw], sector);
  647. all_stat_add(disk, part, ticks[rw], duration, sector);
  648. all_stat_add(disk, part, sectors[rw], n_sect, sector);
  649. all_stat_add(disk, part, io_ticks, duration, sector);
  650. }
  651. void
  652. aoecmd_ata_rsp(struct sk_buff *skb)
  653. {
  654. struct sk_buff_head queue;
  655. struct aoedev *d;
  656. struct aoe_hdr *hin, *hout;
  657. struct aoe_atahdr *ahin, *ahout;
  658. struct frame *f;
  659. struct buf *buf;
  660. struct aoetgt *t;
  661. struct aoeif *ifp;
  662. register long n;
  663. ulong flags;
  664. char ebuf[128];
  665. u16 aoemajor;
  666. hin = (struct aoe_hdr *) skb_mac_header(skb);
  667. aoemajor = get_unaligned_be16(&hin->major);
  668. d = aoedev_by_aoeaddr(aoemajor, hin->minor);
  669. if (d == NULL) {
  670. snprintf(ebuf, sizeof ebuf, "aoecmd_ata_rsp: ata response "
  671. "for unknown device %d.%d\n",
  672. aoemajor, hin->minor);
  673. aoechr_error(ebuf);
  674. return;
  675. }
  676. spin_lock_irqsave(&d->lock, flags);
  677. n = get_unaligned_be32(&hin->tag);
  678. t = gettgt(d, hin->src);
  679. if (t == NULL) {
  680. printk(KERN_INFO "aoe: can't find target e%ld.%d:%012llx\n",
  681. d->aoemajor, d->aoeminor, mac_addr(hin->src));
  682. spin_unlock_irqrestore(&d->lock, flags);
  683. return;
  684. }
  685. f = getframe(t, n);
  686. if (f == NULL) {
  687. calc_rttavg(d, -tsince(n));
  688. spin_unlock_irqrestore(&d->lock, flags);
  689. snprintf(ebuf, sizeof ebuf,
  690. "%15s e%d.%d tag=%08x@%08lx\n",
  691. "unexpected rsp",
  692. get_unaligned_be16(&hin->major),
  693. hin->minor,
  694. get_unaligned_be32(&hin->tag),
  695. jiffies);
  696. aoechr_error(ebuf);
  697. return;
  698. }
  699. calc_rttavg(d, tsince(f->tag));
  700. ahin = (struct aoe_atahdr *) (hin+1);
  701. hout = (struct aoe_hdr *) skb_mac_header(f->skb);
  702. ahout = (struct aoe_atahdr *) (hout+1);
  703. buf = f->buf;
  704. if (ahin->cmdstat & 0xa9) { /* these bits cleared on success */
  705. printk(KERN_ERR
  706. "aoe: ata error cmd=%2.2Xh stat=%2.2Xh from e%ld.%d\n",
  707. ahout->cmdstat, ahin->cmdstat,
  708. d->aoemajor, d->aoeminor);
  709. if (buf)
  710. buf->flags |= BUFFL_FAIL;
  711. } else {
  712. if (d->htgt && t == *d->htgt) /* I'll help myself, thank you. */
  713. d->htgt = NULL;
  714. n = ahout->scnt << 9;
  715. switch (ahout->cmdstat) {
  716. case WIN_READ:
  717. case WIN_READ_EXT:
  718. if (skb->len - sizeof *hin - sizeof *ahin < n) {
  719. printk(KERN_ERR
  720. "aoe: %s. skb->len=%d need=%ld\n",
  721. "runt data size in read", skb->len, n);
  722. /* fail frame f? just returning will rexmit. */
  723. spin_unlock_irqrestore(&d->lock, flags);
  724. return;
  725. }
  726. memcpy(f->bufaddr, ahin+1, n);
  727. case WIN_WRITE:
  728. case WIN_WRITE_EXT:
  729. ifp = getif(t, skb->dev);
  730. if (ifp) {
  731. ifp->lost = 0;
  732. if (n > DEFAULTBCNT)
  733. ifp->lostjumbo = 0;
  734. }
  735. if (f->bcnt -= n) {
  736. f->lba += n >> 9;
  737. f->bufaddr += n;
  738. resend(d, t, f);
  739. goto xmit;
  740. }
  741. break;
  742. case WIN_IDENTIFY:
  743. if (skb->len - sizeof *hin - sizeof *ahin < 512) {
  744. printk(KERN_INFO
  745. "aoe: runt data size in ataid. skb->len=%d\n",
  746. skb->len);
  747. spin_unlock_irqrestore(&d->lock, flags);
  748. return;
  749. }
  750. ataid_complete(d, t, (char *) (ahin+1));
  751. break;
  752. default:
  753. printk(KERN_INFO
  754. "aoe: unrecognized ata command %2.2Xh for %d.%d\n",
  755. ahout->cmdstat,
  756. get_unaligned_be16(&hin->major),
  757. hin->minor);
  758. }
  759. }
  760. if (buf && --buf->nframesout == 0 && buf->resid == 0) {
  761. diskstats(d->gd, buf->bio, jiffies - buf->stime, buf->sector);
  762. n = (buf->flags & BUFFL_FAIL) ? -EIO : 0;
  763. bio_endio(buf->bio, n);
  764. mempool_free(buf, d->bufpool);
  765. }
  766. f->buf = NULL;
  767. f->tag = FREETAG;
  768. t->nout--;
  769. aoecmd_work(d);
  770. xmit:
  771. __skb_queue_head_init(&queue);
  772. skb_queue_splice_init(&d->sendq, &queue);
  773. spin_unlock_irqrestore(&d->lock, flags);
  774. aoenet_xmit(&queue);
  775. }
  776. void
  777. aoecmd_cfg(ushort aoemajor, unsigned char aoeminor)
  778. {
  779. struct sk_buff_head queue;
  780. __skb_queue_head_init(&queue);
  781. aoecmd_cfg_pkts(aoemajor, aoeminor, &queue);
  782. aoenet_xmit(&queue);
  783. }
  784. struct sk_buff *
  785. aoecmd_ata_id(struct aoedev *d)
  786. {
  787. struct aoe_hdr *h;
  788. struct aoe_atahdr *ah;
  789. struct frame *f;
  790. struct sk_buff *skb;
  791. struct aoetgt *t;
  792. f = freeframe(d);
  793. if (f == NULL)
  794. return NULL;
  795. t = *d->tgt;
  796. /* initialize the headers & frame */
  797. skb = f->skb;
  798. h = (struct aoe_hdr *) skb_mac_header(skb);
  799. ah = (struct aoe_atahdr *) (h+1);
  800. skb_put(skb, sizeof *h + sizeof *ah);
  801. memset(h, 0, skb->len);
  802. f->tag = aoehdr_atainit(d, t, h);
  803. t->nout++;
  804. f->waited = 0;
  805. /* set up ata header */
  806. ah->scnt = 1;
  807. ah->cmdstat = WIN_IDENTIFY;
  808. ah->lba3 = 0xa0;
  809. skb->dev = t->ifp->nd;
  810. d->rttavg = MAXTIMER;
  811. d->timer.function = rexmit_timer;
  812. return skb_clone(skb, GFP_ATOMIC);
  813. }
  814. static struct aoetgt *
  815. addtgt(struct aoedev *d, char *addr, ulong nframes)
  816. {
  817. struct aoetgt *t, **tt, **te;
  818. struct frame *f, *e;
  819. tt = d->targets;
  820. te = tt + NTARGETS;
  821. for (; tt < te && *tt; tt++)
  822. ;
  823. if (tt == te) {
  824. printk(KERN_INFO
  825. "aoe: device addtgt failure; too many targets\n");
  826. return NULL;
  827. }
  828. t = kcalloc(1, sizeof *t, GFP_ATOMIC);
  829. f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
  830. if (!t || !f) {
  831. kfree(f);
  832. kfree(t);
  833. printk(KERN_INFO "aoe: cannot allocate memory to add target\n");
  834. return NULL;
  835. }
  836. t->nframes = nframes;
  837. t->frames = f;
  838. e = f + nframes;
  839. for (; f < e; f++)
  840. f->tag = FREETAG;
  841. memcpy(t->addr, addr, sizeof t->addr);
  842. t->ifp = t->ifs;
  843. t->maxout = t->nframes;
  844. return *tt = t;
  845. }
  846. void
  847. aoecmd_cfg_rsp(struct sk_buff *skb)
  848. {
  849. struct aoedev *d;
  850. struct aoe_hdr *h;
  851. struct aoe_cfghdr *ch;
  852. struct aoetgt *t;
  853. struct aoeif *ifp;
  854. ulong flags, sysminor, aoemajor;
  855. struct sk_buff *sl;
  856. u16 n;
  857. h = (struct aoe_hdr *) skb_mac_header(skb);
  858. ch = (struct aoe_cfghdr *) (h+1);
  859. /*
  860. * Enough people have their dip switches set backwards to
  861. * warrant a loud message for this special case.
  862. */
  863. aoemajor = get_unaligned_be16(&h->major);
  864. if (aoemajor == 0xfff) {
  865. printk(KERN_ERR "aoe: Warning: shelf address is all ones. "
  866. "Check shelf dip switches.\n");
  867. return;
  868. }
  869. sysminor = SYSMINOR(aoemajor, h->minor);
  870. if (sysminor * AOE_PARTITIONS + AOE_PARTITIONS > MINORMASK) {
  871. printk(KERN_INFO "aoe: e%ld.%d: minor number too large\n",
  872. aoemajor, (int) h->minor);
  873. return;
  874. }
  875. n = be16_to_cpu(ch->bufcnt);
  876. if (n > aoe_maxout) /* keep it reasonable */
  877. n = aoe_maxout;
  878. d = aoedev_by_sysminor_m(sysminor);
  879. if (d == NULL) {
  880. printk(KERN_INFO "aoe: device sysminor_m failure\n");
  881. return;
  882. }
  883. spin_lock_irqsave(&d->lock, flags);
  884. t = gettgt(d, h->src);
  885. if (!t) {
  886. t = addtgt(d, h->src, n);
  887. if (!t) {
  888. spin_unlock_irqrestore(&d->lock, flags);
  889. return;
  890. }
  891. }
  892. ifp = getif(t, skb->dev);
  893. if (!ifp) {
  894. ifp = addif(t, skb->dev);
  895. if (!ifp) {
  896. printk(KERN_INFO
  897. "aoe: device addif failure; "
  898. "too many interfaces?\n");
  899. spin_unlock_irqrestore(&d->lock, flags);
  900. return;
  901. }
  902. }
  903. if (ifp->maxbcnt) {
  904. n = ifp->nd->mtu;
  905. n -= sizeof (struct aoe_hdr) + sizeof (struct aoe_atahdr);
  906. n /= 512;
  907. if (n > ch->scnt)
  908. n = ch->scnt;
  909. n = n ? n * 512 : DEFAULTBCNT;
  910. if (n != ifp->maxbcnt) {
  911. printk(KERN_INFO
  912. "aoe: e%ld.%d: setting %d%s%s:%012llx\n",
  913. d->aoemajor, d->aoeminor, n,
  914. " byte data frames on ", ifp->nd->name,
  915. mac_addr(t->addr));
  916. ifp->maxbcnt = n;
  917. }
  918. }
  919. /* don't change users' perspective */
  920. if (d->nopen) {
  921. spin_unlock_irqrestore(&d->lock, flags);
  922. return;
  923. }
  924. d->fw_ver = be16_to_cpu(ch->fwver);
  925. sl = aoecmd_ata_id(d);
  926. spin_unlock_irqrestore(&d->lock, flags);
  927. if (sl) {
  928. struct sk_buff_head queue;
  929. __skb_queue_head_init(&queue);
  930. __skb_queue_tail(&queue, sl);
  931. aoenet_xmit(&queue);
  932. }
  933. }
  934. void
  935. aoecmd_cleanslate(struct aoedev *d)
  936. {
  937. struct aoetgt **t, **te;
  938. struct aoeif *p, *e;
  939. d->mintimer = MINTIMER;
  940. t = d->targets;
  941. te = t + NTARGETS;
  942. for (; t < te && *t; t++) {
  943. (*t)->maxout = (*t)->nframes;
  944. p = (*t)->ifs;
  945. e = p + NAOEIFS;
  946. for (; p < e; p++) {
  947. p->lostjumbo = 0;
  948. p->lost = 0;
  949. p->maxbcnt = DEFAULTBCNT;
  950. }
  951. }
  952. }