aoecmd.c 35 KB

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