aoecmd.c 23 KB

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