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