aoecmd.c 23 KB

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