aoecmd.c 35 KB

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