libiscsi.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <asm/unaligned.h>
  31. #include <net/tcp.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/iscsi_proto.h>
  39. #include <scsi/scsi_transport.h>
  40. #include <scsi/scsi_transport_iscsi.h>
  41. #include <scsi/libiscsi.h>
  42. static int iscsi_dbg_lib_conn;
  43. module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
  44. S_IRUGO | S_IWUSR);
  45. MODULE_PARM_DESC(debug_libiscsi_conn,
  46. "Turn on debugging for connections in libiscsi module. "
  47. "Set to 1 to turn on, and zero to turn off. Default is off.");
  48. static int iscsi_dbg_lib_session;
  49. module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
  50. S_IRUGO | S_IWUSR);
  51. MODULE_PARM_DESC(debug_libiscsi_session,
  52. "Turn on debugging for sessions in libiscsi module. "
  53. "Set to 1 to turn on, and zero to turn off. Default is off.");
  54. static int iscsi_dbg_lib_eh;
  55. module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
  56. S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(debug_libiscsi_eh,
  58. "Turn on debugging for error handling in libiscsi module. "
  59. "Set to 1 to turn on, and zero to turn off. Default is off.");
  60. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  61. do { \
  62. if (iscsi_dbg_lib_conn) \
  63. iscsi_conn_printk(KERN_INFO, _conn, \
  64. "%s " dbg_fmt, \
  65. __func__, ##arg); \
  66. } while (0);
  67. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  68. do { \
  69. if (iscsi_dbg_lib_session) \
  70. iscsi_session_printk(KERN_INFO, _session, \
  71. "%s " dbg_fmt, \
  72. __func__, ##arg); \
  73. } while (0);
  74. #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
  75. do { \
  76. if (iscsi_dbg_lib_eh) \
  77. iscsi_session_printk(KERN_INFO, _session, \
  78. "%s " dbg_fmt, \
  79. __func__, ##arg); \
  80. } while (0);
  81. inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
  82. {
  83. struct Scsi_Host *shost = conn->session->host;
  84. struct iscsi_host *ihost = shost_priv(shost);
  85. if (ihost->workq)
  86. queue_work(ihost->workq, &conn->xmitwork);
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
  89. static void __iscsi_update_cmdsn(struct iscsi_session *session,
  90. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  91. {
  92. /*
  93. * standard specifies this check for when to update expected and
  94. * max sequence numbers
  95. */
  96. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  97. return;
  98. if (exp_cmdsn != session->exp_cmdsn &&
  99. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  100. session->exp_cmdsn = exp_cmdsn;
  101. if (max_cmdsn != session->max_cmdsn &&
  102. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
  103. session->max_cmdsn = max_cmdsn;
  104. }
  105. void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  106. {
  107. __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
  108. be32_to_cpu(hdr->max_cmdsn));
  109. }
  110. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  111. /**
  112. * iscsi_prep_data_out_pdu - initialize Data-Out
  113. * @task: scsi command task
  114. * @r2t: R2T info
  115. * @hdr: iscsi data in pdu
  116. *
  117. * Notes:
  118. * Initialize Data-Out within this R2T sequence and finds
  119. * proper data_offset within this SCSI command.
  120. *
  121. * This function is called with connection lock taken.
  122. **/
  123. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  124. struct iscsi_data *hdr)
  125. {
  126. struct iscsi_conn *conn = task->conn;
  127. unsigned int left = r2t->data_length - r2t->sent;
  128. task->hdr_len = sizeof(struct iscsi_data);
  129. memset(hdr, 0, sizeof(struct iscsi_data));
  130. hdr->ttt = r2t->ttt;
  131. hdr->datasn = cpu_to_be32(r2t->datasn);
  132. r2t->datasn++;
  133. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  134. hdr->lun = task->lun;
  135. hdr->itt = task->hdr_itt;
  136. hdr->exp_statsn = r2t->exp_statsn;
  137. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  138. if (left > conn->max_xmit_dlength) {
  139. hton24(hdr->dlength, conn->max_xmit_dlength);
  140. r2t->data_count = conn->max_xmit_dlength;
  141. hdr->flags = 0;
  142. } else {
  143. hton24(hdr->dlength, left);
  144. r2t->data_count = left;
  145. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  146. }
  147. conn->dataout_pdus_cnt++;
  148. }
  149. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  150. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  151. {
  152. unsigned exp_len = task->hdr_len + len;
  153. if (exp_len > task->hdr_max) {
  154. WARN_ON(1);
  155. return -EINVAL;
  156. }
  157. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  158. task->hdr_len = exp_len;
  159. return 0;
  160. }
  161. /*
  162. * make an extended cdb AHS
  163. */
  164. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  165. {
  166. struct scsi_cmnd *cmd = task->sc;
  167. unsigned rlen, pad_len;
  168. unsigned short ahslength;
  169. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  170. int rc;
  171. ecdb_ahdr = iscsi_next_hdr(task);
  172. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  173. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  174. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  175. pad_len = iscsi_padding(rlen);
  176. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  177. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  178. if (rc)
  179. return rc;
  180. if (pad_len)
  181. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  182. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  183. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  184. ecdb_ahdr->reserved = 0;
  185. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  186. ISCSI_DBG_SESSION(task->conn->session,
  187. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  188. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  189. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  190. task->hdr_len);
  191. return 0;
  192. }
  193. static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
  194. {
  195. struct scsi_cmnd *sc = task->sc;
  196. struct iscsi_rlength_ahdr *rlen_ahdr;
  197. int rc;
  198. rlen_ahdr = iscsi_next_hdr(task);
  199. rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
  200. if (rc)
  201. return rc;
  202. rlen_ahdr->ahslength =
  203. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  204. sizeof(rlen_ahdr->reserved));
  205. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  206. rlen_ahdr->reserved = 0;
  207. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  208. ISCSI_DBG_SESSION(task->conn->session,
  209. "bidi-in rlen_ahdr->read_length(%d) "
  210. "rlen_ahdr->ahslength(%d)\n",
  211. be32_to_cpu(rlen_ahdr->read_length),
  212. be16_to_cpu(rlen_ahdr->ahslength));
  213. return 0;
  214. }
  215. /**
  216. * iscsi_check_tmf_restrictions - check if a task is affected by TMF
  217. * @task: iscsi task
  218. * @opcode: opcode to check for
  219. *
  220. * During TMF a task has to be checked if it's affected.
  221. * All unrelated I/O can be passed through, but I/O to the
  222. * affected LUN should be restricted.
  223. * If 'fast_abort' is set we won't be sending any I/O to the
  224. * affected LUN.
  225. * Otherwise the target is waiting for all TTTs to be completed,
  226. * so we have to send all outstanding Data-Out PDUs to the target.
  227. */
  228. static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
  229. {
  230. struct iscsi_conn *conn = task->conn;
  231. struct iscsi_tm *tmf = &conn->tmhdr;
  232. u64 hdr_lun;
  233. if (conn->tmf_state == TMF_INITIAL)
  234. return 0;
  235. if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
  236. return 0;
  237. switch (ISCSI_TM_FUNC_VALUE(tmf)) {
  238. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  239. /*
  240. * Allow PDUs for unrelated LUNs
  241. */
  242. hdr_lun = scsilun_to_int(&tmf->lun);
  243. if (hdr_lun != task->sc->device->lun)
  244. return 0;
  245. /* fall through */
  246. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  247. /*
  248. * Fail all SCSI cmd PDUs
  249. */
  250. if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
  251. iscsi_conn_printk(KERN_INFO, conn,
  252. "task [op %x/%x itt "
  253. "0x%x/0x%x] "
  254. "rejected.\n",
  255. task->hdr->opcode, opcode,
  256. task->itt, task->hdr_itt);
  257. return -EACCES;
  258. }
  259. /*
  260. * And also all data-out PDUs in response to R2T
  261. * if fast_abort is set.
  262. */
  263. if (conn->session->fast_abort) {
  264. iscsi_conn_printk(KERN_INFO, conn,
  265. "task [op %x/%x itt "
  266. "0x%x/0x%x] fast abort.\n",
  267. task->hdr->opcode, opcode,
  268. task->itt, task->hdr_itt);
  269. return -EACCES;
  270. }
  271. break;
  272. case ISCSI_TM_FUNC_ABORT_TASK:
  273. /*
  274. * the caller has already checked if the task
  275. * they want to abort was in the pending queue so if
  276. * we are here the cmd pdu has gone out already, and
  277. * we will only hit this for data-outs
  278. */
  279. if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
  280. task->hdr_itt == tmf->rtt) {
  281. ISCSI_DBG_SESSION(conn->session,
  282. "Preventing task %x/%x from sending "
  283. "data-out due to abort task in "
  284. "progress\n", task->itt,
  285. task->hdr_itt);
  286. return -EACCES;
  287. }
  288. break;
  289. }
  290. return 0;
  291. }
  292. /**
  293. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  294. * @task: iscsi task
  295. *
  296. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  297. * fields like dlength or final based on how much data it sends
  298. */
  299. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  300. {
  301. struct iscsi_conn *conn = task->conn;
  302. struct iscsi_session *session = conn->session;
  303. struct scsi_cmnd *sc = task->sc;
  304. struct iscsi_scsi_req *hdr;
  305. unsigned hdrlength, cmd_len, transfer_length;
  306. itt_t itt;
  307. int rc;
  308. rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
  309. if (rc)
  310. return rc;
  311. if (conn->session->tt->alloc_pdu) {
  312. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  313. if (rc)
  314. return rc;
  315. }
  316. hdr = (struct iscsi_scsi_req *)task->hdr;
  317. itt = hdr->itt;
  318. memset(hdr, 0, sizeof(*hdr));
  319. if (session->tt->parse_pdu_itt)
  320. hdr->itt = task->hdr_itt = itt;
  321. else
  322. hdr->itt = task->hdr_itt = build_itt(task->itt,
  323. task->conn->session->age);
  324. task->hdr_len = 0;
  325. rc = iscsi_add_hdr(task, sizeof(*hdr));
  326. if (rc)
  327. return rc;
  328. hdr->opcode = ISCSI_OP_SCSI_CMD;
  329. hdr->flags = ISCSI_ATTR_SIMPLE;
  330. int_to_scsilun(sc->device->lun, &hdr->lun);
  331. task->lun = hdr->lun;
  332. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  333. cmd_len = sc->cmd_len;
  334. if (cmd_len < ISCSI_CDB_SIZE)
  335. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  336. else if (cmd_len > ISCSI_CDB_SIZE) {
  337. rc = iscsi_prep_ecdb_ahs(task);
  338. if (rc)
  339. return rc;
  340. cmd_len = ISCSI_CDB_SIZE;
  341. }
  342. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  343. task->imm_count = 0;
  344. if (scsi_bidi_cmnd(sc)) {
  345. hdr->flags |= ISCSI_FLAG_CMD_READ;
  346. rc = iscsi_prep_bidi_ahs(task);
  347. if (rc)
  348. return rc;
  349. }
  350. if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
  351. task->protected = true;
  352. transfer_length = scsi_transfer_length(sc);
  353. hdr->data_length = cpu_to_be32(transfer_length);
  354. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  355. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  356. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  357. /*
  358. * Write counters:
  359. *
  360. * imm_count bytes to be sent right after
  361. * SCSI PDU Header
  362. *
  363. * unsol_count bytes(as Data-Out) to be sent
  364. * without R2T ack right after
  365. * immediate data
  366. *
  367. * r2t data_length bytes to be sent via R2T ack's
  368. *
  369. * pad_count bytes to be sent as zero-padding
  370. */
  371. memset(r2t, 0, sizeof(*r2t));
  372. if (session->imm_data_en) {
  373. if (transfer_length >= session->first_burst)
  374. task->imm_count = min(session->first_burst,
  375. conn->max_xmit_dlength);
  376. else
  377. task->imm_count = min(transfer_length,
  378. conn->max_xmit_dlength);
  379. hton24(hdr->dlength, task->imm_count);
  380. } else
  381. zero_data(hdr->dlength);
  382. if (!session->initial_r2t_en) {
  383. r2t->data_length = min(session->first_burst,
  384. transfer_length) -
  385. task->imm_count;
  386. r2t->data_offset = task->imm_count;
  387. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  388. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  389. }
  390. if (!task->unsol_r2t.data_length)
  391. /* No unsolicit Data-Out's */
  392. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  393. } else {
  394. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  395. zero_data(hdr->dlength);
  396. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  397. hdr->flags |= ISCSI_FLAG_CMD_READ;
  398. }
  399. /* calculate size of additional header segments (AHSs) */
  400. hdrlength = task->hdr_len - sizeof(*hdr);
  401. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  402. hdrlength /= ISCSI_PAD_LEN;
  403. WARN_ON(hdrlength >= 256);
  404. hdr->hlength = hdrlength & 0xFF;
  405. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  406. if (session->tt->init_task && session->tt->init_task(task))
  407. return -EIO;
  408. task->state = ISCSI_TASK_RUNNING;
  409. session->cmdsn++;
  410. conn->scsicmd_pdus_cnt++;
  411. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  412. "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
  413. scsi_bidi_cmnd(sc) ? "bidirectional" :
  414. sc->sc_data_direction == DMA_TO_DEVICE ?
  415. "write" : "read", conn->id, sc, sc->cmnd[0],
  416. task->itt, transfer_length,
  417. scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  418. session->cmdsn,
  419. session->max_cmdsn - session->exp_cmdsn + 1);
  420. return 0;
  421. }
  422. /**
  423. * iscsi_free_task - free a task
  424. * @task: iscsi cmd task
  425. *
  426. * Must be called with session back_lock.
  427. * This function returns the scsi command to scsi-ml or cleans
  428. * up mgmt tasks then returns the task to the pool.
  429. */
  430. static void iscsi_free_task(struct iscsi_task *task)
  431. {
  432. struct iscsi_conn *conn = task->conn;
  433. struct iscsi_session *session = conn->session;
  434. struct scsi_cmnd *sc = task->sc;
  435. int oldstate = task->state;
  436. ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
  437. task->itt, task->state, task->sc);
  438. session->tt->cleanup_task(task);
  439. task->state = ISCSI_TASK_FREE;
  440. task->sc = NULL;
  441. /*
  442. * login task is preallocated so do not free
  443. */
  444. if (conn->login_task == task)
  445. return;
  446. kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
  447. if (sc) {
  448. /* SCSI eh reuses commands to verify us */
  449. sc->SCp.ptr = NULL;
  450. /*
  451. * queue command may call this to free the task, so
  452. * it will decide how to return sc to scsi-ml.
  453. */
  454. if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
  455. sc->scsi_done(sc);
  456. }
  457. }
  458. void __iscsi_get_task(struct iscsi_task *task)
  459. {
  460. atomic_inc(&task->refcount);
  461. }
  462. EXPORT_SYMBOL_GPL(__iscsi_get_task);
  463. void __iscsi_put_task(struct iscsi_task *task)
  464. {
  465. if (atomic_dec_and_test(&task->refcount))
  466. iscsi_free_task(task);
  467. }
  468. EXPORT_SYMBOL_GPL(__iscsi_put_task);
  469. void iscsi_put_task(struct iscsi_task *task)
  470. {
  471. struct iscsi_session *session = task->conn->session;
  472. /* regular RX path uses back_lock */
  473. spin_lock_bh(&session->back_lock);
  474. __iscsi_put_task(task);
  475. spin_unlock_bh(&session->back_lock);
  476. }
  477. EXPORT_SYMBOL_GPL(iscsi_put_task);
  478. /**
  479. * iscsi_complete_task - finish a task
  480. * @task: iscsi cmd task
  481. * @state: state to complete task with
  482. *
  483. * Must be called with session back_lock.
  484. */
  485. static void iscsi_complete_task(struct iscsi_task *task, int state)
  486. {
  487. struct iscsi_conn *conn = task->conn;
  488. ISCSI_DBG_SESSION(conn->session,
  489. "complete task itt 0x%x state %d sc %p\n",
  490. task->itt, task->state, task->sc);
  491. if (task->state == ISCSI_TASK_COMPLETED ||
  492. task->state == ISCSI_TASK_ABRT_TMF ||
  493. task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
  494. task->state == ISCSI_TASK_REQUEUE_SCSIQ)
  495. return;
  496. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  497. task->state = state;
  498. if (!list_empty(&task->running))
  499. list_del_init(&task->running);
  500. if (conn->task == task)
  501. conn->task = NULL;
  502. if (conn->ping_task == task)
  503. conn->ping_task = NULL;
  504. /* release get from queueing */
  505. __iscsi_put_task(task);
  506. }
  507. /**
  508. * iscsi_complete_scsi_task - finish scsi task normally
  509. * @task: iscsi task for scsi cmd
  510. * @exp_cmdsn: expected cmd sn in cpu format
  511. * @max_cmdsn: max cmd sn in cpu format
  512. *
  513. * This is used when drivers do not need or cannot perform
  514. * lower level pdu processing.
  515. *
  516. * Called with session back_lock
  517. */
  518. void iscsi_complete_scsi_task(struct iscsi_task *task,
  519. uint32_t exp_cmdsn, uint32_t max_cmdsn)
  520. {
  521. struct iscsi_conn *conn = task->conn;
  522. ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
  523. conn->last_recv = jiffies;
  524. __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
  525. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  526. }
  527. EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
  528. /*
  529. * session back_lock must be held and if not called for a task that is
  530. * still pending or from the xmit thread, then xmit thread must
  531. * be suspended.
  532. */
  533. static void fail_scsi_task(struct iscsi_task *task, int err)
  534. {
  535. struct iscsi_conn *conn = task->conn;
  536. struct scsi_cmnd *sc;
  537. int state;
  538. /*
  539. * if a command completes and we get a successful tmf response
  540. * we will hit this because the scsi eh abort code does not take
  541. * a ref to the task.
  542. */
  543. sc = task->sc;
  544. if (!sc)
  545. return;
  546. if (task->state == ISCSI_TASK_PENDING) {
  547. /*
  548. * cmd never made it to the xmit thread, so we should not count
  549. * the cmd in the sequencing
  550. */
  551. conn->session->queued_cmdsn--;
  552. /* it was never sent so just complete like normal */
  553. state = ISCSI_TASK_COMPLETED;
  554. } else if (err == DID_TRANSPORT_DISRUPTED)
  555. state = ISCSI_TASK_ABRT_SESS_RECOV;
  556. else
  557. state = ISCSI_TASK_ABRT_TMF;
  558. sc->result = err << 16;
  559. if (!scsi_bidi_cmnd(sc))
  560. scsi_set_resid(sc, scsi_bufflen(sc));
  561. else {
  562. scsi_out(sc)->resid = scsi_out(sc)->length;
  563. scsi_in(sc)->resid = scsi_in(sc)->length;
  564. }
  565. /* regular RX path uses back_lock */
  566. spin_lock_bh(&conn->session->back_lock);
  567. iscsi_complete_task(task, state);
  568. spin_unlock_bh(&conn->session->back_lock);
  569. }
  570. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  571. struct iscsi_task *task)
  572. {
  573. struct iscsi_session *session = conn->session;
  574. struct iscsi_hdr *hdr = task->hdr;
  575. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  576. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  577. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  578. return -ENOTCONN;
  579. if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
  580. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  581. /*
  582. * pre-format CmdSN for outgoing PDU.
  583. */
  584. nop->cmdsn = cpu_to_be32(session->cmdsn);
  585. if (hdr->itt != RESERVED_ITT) {
  586. /*
  587. * TODO: We always use immediate for normal session pdus.
  588. * If we start to send tmfs or nops as non-immediate then
  589. * we should start checking the cmdsn numbers for mgmt tasks.
  590. *
  591. * During discovery sessions iscsid sends TEXT as non immediate,
  592. * but we always only send one PDU at a time.
  593. */
  594. if (conn->c_stage == ISCSI_CONN_STARTED &&
  595. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  596. session->queued_cmdsn++;
  597. session->cmdsn++;
  598. }
  599. }
  600. if (session->tt->init_task && session->tt->init_task(task))
  601. return -EIO;
  602. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  603. session->state = ISCSI_STATE_LOGGING_OUT;
  604. task->state = ISCSI_TASK_RUNNING;
  605. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  606. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  607. hdr->itt, task->data_count);
  608. return 0;
  609. }
  610. static struct iscsi_task *
  611. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  612. char *data, uint32_t data_size)
  613. {
  614. struct iscsi_session *session = conn->session;
  615. struct iscsi_host *ihost = shost_priv(session->host);
  616. uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  617. struct iscsi_task *task;
  618. itt_t itt;
  619. if (session->state == ISCSI_STATE_TERMINATE)
  620. return NULL;
  621. if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
  622. /*
  623. * Login and Text are sent serially, in
  624. * request-followed-by-response sequence.
  625. * Same task can be used. Same ITT must be used.
  626. * Note that login_task is preallocated at conn_create().
  627. */
  628. if (conn->login_task->state != ISCSI_TASK_FREE) {
  629. iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
  630. "progress. Cannot start new task.\n");
  631. return NULL;
  632. }
  633. if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
  634. iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
  635. return NULL;
  636. }
  637. task = conn->login_task;
  638. } else {
  639. if (session->state != ISCSI_STATE_LOGGED_IN)
  640. return NULL;
  641. if (data_size != 0) {
  642. iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
  643. return NULL;
  644. }
  645. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  646. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  647. if (!kfifo_out(&session->cmdpool.queue,
  648. (void*)&task, sizeof(void*)))
  649. return NULL;
  650. }
  651. /*
  652. * released in complete pdu for task we expect a response for, and
  653. * released by the lld when it has transmitted the task for
  654. * pdus we do not expect a response for.
  655. */
  656. atomic_set(&task->refcount, 1);
  657. task->conn = conn;
  658. task->sc = NULL;
  659. INIT_LIST_HEAD(&task->running);
  660. task->state = ISCSI_TASK_PENDING;
  661. if (data_size) {
  662. memcpy(task->data, data, data_size);
  663. task->data_count = data_size;
  664. } else
  665. task->data_count = 0;
  666. if (conn->session->tt->alloc_pdu) {
  667. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  668. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  669. "pdu for mgmt task.\n");
  670. goto free_task;
  671. }
  672. }
  673. itt = task->hdr->itt;
  674. task->hdr_len = sizeof(struct iscsi_hdr);
  675. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  676. if (hdr->itt != RESERVED_ITT) {
  677. if (session->tt->parse_pdu_itt)
  678. task->hdr->itt = itt;
  679. else
  680. task->hdr->itt = build_itt(task->itt,
  681. task->conn->session->age);
  682. }
  683. if (!ihost->workq) {
  684. if (iscsi_prep_mgmt_task(conn, task))
  685. goto free_task;
  686. if (session->tt->xmit_task(task))
  687. goto free_task;
  688. } else {
  689. list_add_tail(&task->running, &conn->mgmtqueue);
  690. iscsi_conn_queue_work(conn);
  691. }
  692. return task;
  693. free_task:
  694. /* regular RX path uses back_lock */
  695. spin_lock_bh(&session->back_lock);
  696. __iscsi_put_task(task);
  697. spin_unlock_bh(&session->back_lock);
  698. return NULL;
  699. }
  700. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  701. char *data, uint32_t data_size)
  702. {
  703. struct iscsi_conn *conn = cls_conn->dd_data;
  704. struct iscsi_session *session = conn->session;
  705. int err = 0;
  706. spin_lock_bh(&session->frwd_lock);
  707. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  708. err = -EPERM;
  709. spin_unlock_bh(&session->frwd_lock);
  710. return err;
  711. }
  712. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  713. /**
  714. * iscsi_cmd_rsp - SCSI Command Response processing
  715. * @conn: iscsi connection
  716. * @hdr: iscsi header
  717. * @task: scsi command task
  718. * @data: cmd data buffer
  719. * @datalen: len of buffer
  720. *
  721. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  722. * then completes the command and task.
  723. **/
  724. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  725. struct iscsi_task *task, char *data,
  726. int datalen)
  727. {
  728. struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
  729. struct iscsi_session *session = conn->session;
  730. struct scsi_cmnd *sc = task->sc;
  731. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  732. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  733. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  734. if (task->protected) {
  735. sector_t sector;
  736. u8 ascq;
  737. /**
  738. * Transports that didn't implement check_protection
  739. * callback but still published T10-PI support to scsi-mid
  740. * deserve this BUG_ON.
  741. **/
  742. BUG_ON(!session->tt->check_protection);
  743. ascq = session->tt->check_protection(task, &sector);
  744. if (ascq) {
  745. sc->result = DRIVER_SENSE << 24 |
  746. SAM_STAT_CHECK_CONDITION;
  747. scsi_build_sense_buffer(1, sc->sense_buffer,
  748. ILLEGAL_REQUEST, 0x10, ascq);
  749. sc->sense_buffer[7] = 0xc; /* Additional sense length */
  750. sc->sense_buffer[8] = 0; /* Information desc type */
  751. sc->sense_buffer[9] = 0xa; /* Additional desc length */
  752. sc->sense_buffer[10] = 0x80; /* Validity bit */
  753. put_unaligned_be64(sector, &sc->sense_buffer[12]);
  754. goto out;
  755. }
  756. }
  757. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  758. sc->result = DID_ERROR << 16;
  759. goto out;
  760. }
  761. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  762. uint16_t senselen;
  763. if (datalen < 2) {
  764. invalid_datalen:
  765. iscsi_conn_printk(KERN_ERR, conn,
  766. "Got CHECK_CONDITION but invalid data "
  767. "buffer size of %d\n", datalen);
  768. sc->result = DID_BAD_TARGET << 16;
  769. goto out;
  770. }
  771. senselen = get_unaligned_be16(data);
  772. if (datalen < senselen)
  773. goto invalid_datalen;
  774. memcpy(sc->sense_buffer, data + 2,
  775. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  776. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  777. min_t(uint16_t, senselen,
  778. SCSI_SENSE_BUFFERSIZE));
  779. }
  780. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  781. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  782. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  783. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  784. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  785. res_count <= scsi_in(sc)->length))
  786. scsi_in(sc)->resid = res_count;
  787. else
  788. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  789. }
  790. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  791. ISCSI_FLAG_CMD_OVERFLOW)) {
  792. int res_count = be32_to_cpu(rhdr->residual_count);
  793. if (res_count > 0 &&
  794. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  795. res_count <= scsi_bufflen(sc)))
  796. /* write side for bidi or uni-io set_resid */
  797. scsi_set_resid(sc, res_count);
  798. else
  799. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  800. }
  801. out:
  802. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  803. sc, sc->result, task->itt);
  804. conn->scsirsp_pdus_cnt++;
  805. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  806. }
  807. /**
  808. * iscsi_data_in_rsp - SCSI Data-In Response processing
  809. * @conn: iscsi connection
  810. * @hdr: iscsi pdu
  811. * @task: scsi command task
  812. **/
  813. static void
  814. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  815. struct iscsi_task *task)
  816. {
  817. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  818. struct scsi_cmnd *sc = task->sc;
  819. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  820. return;
  821. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  822. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  823. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  824. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  825. ISCSI_FLAG_DATA_OVERFLOW)) {
  826. int res_count = be32_to_cpu(rhdr->residual_count);
  827. if (res_count > 0 &&
  828. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  829. res_count <= scsi_in(sc)->length))
  830. scsi_in(sc)->resid = res_count;
  831. else
  832. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  833. }
  834. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  835. "[sc %p res %d itt 0x%x]\n",
  836. sc, sc->result, task->itt);
  837. conn->scsirsp_pdus_cnt++;
  838. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  839. }
  840. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  841. {
  842. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  843. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  844. conn->tmfrsp_pdus_cnt++;
  845. if (conn->tmf_state != TMF_QUEUED)
  846. return;
  847. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  848. conn->tmf_state = TMF_SUCCESS;
  849. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  850. conn->tmf_state = TMF_NOT_FOUND;
  851. else
  852. conn->tmf_state = TMF_FAILED;
  853. wake_up(&conn->ehwait);
  854. }
  855. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  856. {
  857. struct iscsi_nopout hdr;
  858. struct iscsi_task *task;
  859. if (!rhdr && conn->ping_task)
  860. return;
  861. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  862. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  863. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  864. if (rhdr) {
  865. hdr.lun = rhdr->lun;
  866. hdr.ttt = rhdr->ttt;
  867. hdr.itt = RESERVED_ITT;
  868. } else
  869. hdr.ttt = RESERVED_ITT;
  870. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  871. if (!task)
  872. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  873. else if (!rhdr) {
  874. /* only track our nops */
  875. conn->ping_task = task;
  876. conn->last_ping = jiffies;
  877. }
  878. }
  879. static int iscsi_nop_out_rsp(struct iscsi_task *task,
  880. struct iscsi_nopin *nop, char *data, int datalen)
  881. {
  882. struct iscsi_conn *conn = task->conn;
  883. int rc = 0;
  884. if (conn->ping_task != task) {
  885. /*
  886. * If this is not in response to one of our
  887. * nops then it must be from userspace.
  888. */
  889. if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
  890. data, datalen))
  891. rc = ISCSI_ERR_CONN_FAILED;
  892. } else
  893. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  894. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  895. return rc;
  896. }
  897. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  898. char *data, int datalen)
  899. {
  900. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  901. struct iscsi_hdr rejected_pdu;
  902. int opcode, rc = 0;
  903. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  904. if (ntoh24(reject->dlength) > datalen ||
  905. ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
  906. iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
  907. "pdu. Invalid data length (pdu dlength "
  908. "%u, datalen %d\n", ntoh24(reject->dlength),
  909. datalen);
  910. return ISCSI_ERR_PROTO;
  911. }
  912. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  913. opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
  914. switch (reject->reason) {
  915. case ISCSI_REASON_DATA_DIGEST_ERROR:
  916. iscsi_conn_printk(KERN_ERR, conn,
  917. "pdu (op 0x%x itt 0x%x) rejected "
  918. "due to DataDigest error.\n",
  919. opcode, rejected_pdu.itt);
  920. break;
  921. case ISCSI_REASON_IMM_CMD_REJECT:
  922. iscsi_conn_printk(KERN_ERR, conn,
  923. "pdu (op 0x%x itt 0x%x) rejected. Too many "
  924. "immediate commands.\n",
  925. opcode, rejected_pdu.itt);
  926. /*
  927. * We only send one TMF at a time so if the target could not
  928. * handle it, then it should get fixed (RFC mandates that
  929. * a target can handle one immediate TMF per conn).
  930. *
  931. * For nops-outs, we could have sent more than one if
  932. * the target is sending us lots of nop-ins
  933. */
  934. if (opcode != ISCSI_OP_NOOP_OUT)
  935. return 0;
  936. if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  937. /*
  938. * nop-out in response to target's nop-out rejected.
  939. * Just resend.
  940. */
  941. /* In RX path we are under back lock */
  942. spin_unlock(&conn->session->back_lock);
  943. spin_lock(&conn->session->frwd_lock);
  944. iscsi_send_nopout(conn,
  945. (struct iscsi_nopin*)&rejected_pdu);
  946. spin_unlock(&conn->session->frwd_lock);
  947. spin_lock(&conn->session->back_lock);
  948. } else {
  949. struct iscsi_task *task;
  950. /*
  951. * Our nop as ping got dropped. We know the target
  952. * and transport are ok so just clean up
  953. */
  954. task = iscsi_itt_to_task(conn, rejected_pdu.itt);
  955. if (!task) {
  956. iscsi_conn_printk(KERN_ERR, conn,
  957. "Invalid pdu reject. Could "
  958. "not lookup rejected task.\n");
  959. rc = ISCSI_ERR_BAD_ITT;
  960. } else
  961. rc = iscsi_nop_out_rsp(task,
  962. (struct iscsi_nopin*)&rejected_pdu,
  963. NULL, 0);
  964. }
  965. break;
  966. default:
  967. iscsi_conn_printk(KERN_ERR, conn,
  968. "pdu (op 0x%x itt 0x%x) rejected. Reason "
  969. "code 0x%x\n", rejected_pdu.opcode,
  970. rejected_pdu.itt, reject->reason);
  971. break;
  972. }
  973. return rc;
  974. }
  975. /**
  976. * iscsi_itt_to_task - look up task by itt
  977. * @conn: iscsi connection
  978. * @itt: itt
  979. *
  980. * This should be used for mgmt tasks like login and nops, or if
  981. * the LDD's itt space does not include the session age.
  982. *
  983. * The session back_lock must be held.
  984. */
  985. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  986. {
  987. struct iscsi_session *session = conn->session;
  988. int i;
  989. if (itt == RESERVED_ITT)
  990. return NULL;
  991. if (session->tt->parse_pdu_itt)
  992. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  993. else
  994. i = get_itt(itt);
  995. if (i >= session->cmds_max)
  996. return NULL;
  997. return session->cmds[i];
  998. }
  999. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  1000. /**
  1001. * __iscsi_complete_pdu - complete pdu
  1002. * @conn: iscsi conn
  1003. * @hdr: iscsi header
  1004. * @data: data buffer
  1005. * @datalen: len of data buffer
  1006. *
  1007. * Completes pdu processing by freeing any resources allocated at
  1008. * queuecommand or send generic. session back_lock must be held and verify
  1009. * itt must have been called.
  1010. */
  1011. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1012. char *data, int datalen)
  1013. {
  1014. struct iscsi_session *session = conn->session;
  1015. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  1016. struct iscsi_task *task;
  1017. uint32_t itt;
  1018. conn->last_recv = jiffies;
  1019. rc = iscsi_verify_itt(conn, hdr->itt);
  1020. if (rc)
  1021. return rc;
  1022. if (hdr->itt != RESERVED_ITT)
  1023. itt = get_itt(hdr->itt);
  1024. else
  1025. itt = ~0U;
  1026. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  1027. opcode, conn->id, itt, datalen);
  1028. if (itt == ~0U) {
  1029. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1030. switch(opcode) {
  1031. case ISCSI_OP_NOOP_IN:
  1032. if (datalen) {
  1033. rc = ISCSI_ERR_PROTO;
  1034. break;
  1035. }
  1036. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  1037. break;
  1038. /* In RX path we are under back lock */
  1039. spin_unlock(&session->back_lock);
  1040. spin_lock(&session->frwd_lock);
  1041. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  1042. spin_unlock(&session->frwd_lock);
  1043. spin_lock(&session->back_lock);
  1044. break;
  1045. case ISCSI_OP_REJECT:
  1046. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  1047. break;
  1048. case ISCSI_OP_ASYNC_EVENT:
  1049. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1050. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1051. rc = ISCSI_ERR_CONN_FAILED;
  1052. break;
  1053. default:
  1054. rc = ISCSI_ERR_BAD_OPCODE;
  1055. break;
  1056. }
  1057. goto out;
  1058. }
  1059. switch(opcode) {
  1060. case ISCSI_OP_SCSI_CMD_RSP:
  1061. case ISCSI_OP_SCSI_DATA_IN:
  1062. task = iscsi_itt_to_ctask(conn, hdr->itt);
  1063. if (!task)
  1064. return ISCSI_ERR_BAD_ITT;
  1065. task->last_xfer = jiffies;
  1066. break;
  1067. case ISCSI_OP_R2T:
  1068. /*
  1069. * LLD handles R2Ts if they need to.
  1070. */
  1071. return 0;
  1072. case ISCSI_OP_LOGOUT_RSP:
  1073. case ISCSI_OP_LOGIN_RSP:
  1074. case ISCSI_OP_TEXT_RSP:
  1075. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1076. case ISCSI_OP_NOOP_IN:
  1077. task = iscsi_itt_to_task(conn, hdr->itt);
  1078. if (!task)
  1079. return ISCSI_ERR_BAD_ITT;
  1080. break;
  1081. default:
  1082. return ISCSI_ERR_BAD_OPCODE;
  1083. }
  1084. switch(opcode) {
  1085. case ISCSI_OP_SCSI_CMD_RSP:
  1086. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  1087. break;
  1088. case ISCSI_OP_SCSI_DATA_IN:
  1089. iscsi_data_in_rsp(conn, hdr, task);
  1090. break;
  1091. case ISCSI_OP_LOGOUT_RSP:
  1092. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1093. if (datalen) {
  1094. rc = ISCSI_ERR_PROTO;
  1095. break;
  1096. }
  1097. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1098. goto recv_pdu;
  1099. case ISCSI_OP_LOGIN_RSP:
  1100. case ISCSI_OP_TEXT_RSP:
  1101. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1102. /*
  1103. * login related PDU's exp_statsn is handled in
  1104. * userspace
  1105. */
  1106. goto recv_pdu;
  1107. case ISCSI_OP_SCSI_TMFUNC_RSP:
  1108. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1109. if (datalen) {
  1110. rc = ISCSI_ERR_PROTO;
  1111. break;
  1112. }
  1113. iscsi_tmf_rsp(conn, hdr);
  1114. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1115. break;
  1116. case ISCSI_OP_NOOP_IN:
  1117. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  1118. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  1119. rc = ISCSI_ERR_PROTO;
  1120. break;
  1121. }
  1122. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  1123. rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
  1124. data, datalen);
  1125. break;
  1126. default:
  1127. rc = ISCSI_ERR_BAD_OPCODE;
  1128. break;
  1129. }
  1130. out:
  1131. return rc;
  1132. recv_pdu:
  1133. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  1134. rc = ISCSI_ERR_CONN_FAILED;
  1135. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1136. return rc;
  1137. }
  1138. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  1139. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  1140. char *data, int datalen)
  1141. {
  1142. int rc;
  1143. spin_lock(&conn->session->back_lock);
  1144. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  1145. spin_unlock(&conn->session->back_lock);
  1146. return rc;
  1147. }
  1148. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  1149. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  1150. {
  1151. struct iscsi_session *session = conn->session;
  1152. int age = 0, i = 0;
  1153. if (itt == RESERVED_ITT)
  1154. return 0;
  1155. if (session->tt->parse_pdu_itt)
  1156. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  1157. else {
  1158. i = get_itt(itt);
  1159. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  1160. }
  1161. if (age != session->age) {
  1162. iscsi_conn_printk(KERN_ERR, conn,
  1163. "received itt %x expected session age (%x)\n",
  1164. (__force u32)itt, session->age);
  1165. return ISCSI_ERR_BAD_ITT;
  1166. }
  1167. if (i >= session->cmds_max) {
  1168. iscsi_conn_printk(KERN_ERR, conn,
  1169. "received invalid itt index %u (max cmds "
  1170. "%u.\n", i, session->cmds_max);
  1171. return ISCSI_ERR_BAD_ITT;
  1172. }
  1173. return 0;
  1174. }
  1175. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  1176. /**
  1177. * iscsi_itt_to_ctask - look up ctask by itt
  1178. * @conn: iscsi connection
  1179. * @itt: itt
  1180. *
  1181. * This should be used for cmd tasks.
  1182. *
  1183. * The session back_lock must be held.
  1184. */
  1185. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  1186. {
  1187. struct iscsi_task *task;
  1188. if (iscsi_verify_itt(conn, itt))
  1189. return NULL;
  1190. task = iscsi_itt_to_task(conn, itt);
  1191. if (!task || !task->sc)
  1192. return NULL;
  1193. if (task->sc->SCp.phase != conn->session->age) {
  1194. iscsi_session_printk(KERN_ERR, conn->session,
  1195. "task's session age %d, expected %d\n",
  1196. task->sc->SCp.phase, conn->session->age);
  1197. return NULL;
  1198. }
  1199. return task;
  1200. }
  1201. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  1202. void iscsi_session_failure(struct iscsi_session *session,
  1203. enum iscsi_err err)
  1204. {
  1205. struct iscsi_conn *conn;
  1206. struct device *dev;
  1207. spin_lock_bh(&session->frwd_lock);
  1208. conn = session->leadconn;
  1209. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  1210. spin_unlock_bh(&session->frwd_lock);
  1211. return;
  1212. }
  1213. dev = get_device(&conn->cls_conn->dev);
  1214. spin_unlock_bh(&session->frwd_lock);
  1215. if (!dev)
  1216. return;
  1217. /*
  1218. * if the host is being removed bypass the connection
  1219. * recovery initialization because we are going to kill
  1220. * the session.
  1221. */
  1222. if (err == ISCSI_ERR_INVALID_HOST)
  1223. iscsi_conn_error_event(conn->cls_conn, err);
  1224. else
  1225. iscsi_conn_failure(conn, err);
  1226. put_device(dev);
  1227. }
  1228. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  1229. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  1230. {
  1231. struct iscsi_session *session = conn->session;
  1232. spin_lock_bh(&session->frwd_lock);
  1233. if (session->state == ISCSI_STATE_FAILED) {
  1234. spin_unlock_bh(&session->frwd_lock);
  1235. return;
  1236. }
  1237. if (conn->stop_stage == 0)
  1238. session->state = ISCSI_STATE_FAILED;
  1239. spin_unlock_bh(&session->frwd_lock);
  1240. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1241. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1242. iscsi_conn_error_event(conn->cls_conn, err);
  1243. }
  1244. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1245. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1246. {
  1247. struct iscsi_session *session = conn->session;
  1248. /*
  1249. * Check for iSCSI window and take care of CmdSN wrap-around
  1250. */
  1251. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1252. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1253. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1254. session->exp_cmdsn, session->max_cmdsn,
  1255. session->cmdsn, session->queued_cmdsn);
  1256. return -ENOSPC;
  1257. }
  1258. return 0;
  1259. }
  1260. static int iscsi_xmit_task(struct iscsi_conn *conn)
  1261. {
  1262. struct iscsi_task *task = conn->task;
  1263. int rc;
  1264. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
  1265. return -ENODATA;
  1266. __iscsi_get_task(task);
  1267. spin_unlock_bh(&conn->session->frwd_lock);
  1268. rc = conn->session->tt->xmit_task(task);
  1269. spin_lock_bh(&conn->session->frwd_lock);
  1270. if (!rc) {
  1271. /* done with this task */
  1272. task->last_xfer = jiffies;
  1273. conn->task = NULL;
  1274. }
  1275. /* regular RX path uses back_lock */
  1276. spin_lock(&conn->session->back_lock);
  1277. __iscsi_put_task(task);
  1278. spin_unlock(&conn->session->back_lock);
  1279. return rc;
  1280. }
  1281. /**
  1282. * iscsi_requeue_task - requeue task to run from session workqueue
  1283. * @task: task to requeue
  1284. *
  1285. * LLDs that need to run a task from the session workqueue should call
  1286. * this. The session frwd_lock must be held. This should only be called
  1287. * by software drivers.
  1288. */
  1289. void iscsi_requeue_task(struct iscsi_task *task)
  1290. {
  1291. struct iscsi_conn *conn = task->conn;
  1292. /*
  1293. * this may be on the requeue list already if the xmit_task callout
  1294. * is handling the r2ts while we are adding new ones
  1295. */
  1296. if (list_empty(&task->running))
  1297. list_add_tail(&task->running, &conn->requeue);
  1298. iscsi_conn_queue_work(conn);
  1299. }
  1300. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1301. /**
  1302. * iscsi_data_xmit - xmit any command into the scheduled connection
  1303. * @conn: iscsi connection
  1304. *
  1305. * Notes:
  1306. * The function can return -EAGAIN in which case the caller must
  1307. * re-schedule it again later or recover. '0' return code means
  1308. * successful xmit.
  1309. **/
  1310. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1311. {
  1312. struct iscsi_task *task;
  1313. int rc = 0;
  1314. spin_lock_bh(&conn->session->frwd_lock);
  1315. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1316. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1317. spin_unlock_bh(&conn->session->frwd_lock);
  1318. return -ENODATA;
  1319. }
  1320. if (conn->task) {
  1321. rc = iscsi_xmit_task(conn);
  1322. if (rc)
  1323. goto done;
  1324. }
  1325. /*
  1326. * process mgmt pdus like nops before commands since we should
  1327. * only have one nop-out as a ping from us and targets should not
  1328. * overflow us with nop-ins
  1329. */
  1330. check_mgmt:
  1331. while (!list_empty(&conn->mgmtqueue)) {
  1332. conn->task = list_entry(conn->mgmtqueue.next,
  1333. struct iscsi_task, running);
  1334. list_del_init(&conn->task->running);
  1335. if (iscsi_prep_mgmt_task(conn, conn->task)) {
  1336. /* regular RX path uses back_lock */
  1337. spin_lock_bh(&conn->session->back_lock);
  1338. __iscsi_put_task(conn->task);
  1339. spin_unlock_bh(&conn->session->back_lock);
  1340. conn->task = NULL;
  1341. continue;
  1342. }
  1343. rc = iscsi_xmit_task(conn);
  1344. if (rc)
  1345. goto done;
  1346. }
  1347. /* process pending command queue */
  1348. while (!list_empty(&conn->cmdqueue)) {
  1349. conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
  1350. running);
  1351. list_del_init(&conn->task->running);
  1352. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1353. fail_scsi_task(conn->task, DID_IMM_RETRY);
  1354. continue;
  1355. }
  1356. rc = iscsi_prep_scsi_cmd_pdu(conn->task);
  1357. if (rc) {
  1358. if (rc == -ENOMEM || rc == -EACCES) {
  1359. list_add_tail(&conn->task->running,
  1360. &conn->cmdqueue);
  1361. conn->task = NULL;
  1362. goto done;
  1363. } else
  1364. fail_scsi_task(conn->task, DID_ABORT);
  1365. continue;
  1366. }
  1367. rc = iscsi_xmit_task(conn);
  1368. if (rc)
  1369. goto done;
  1370. /*
  1371. * we could continuously get new task requests so
  1372. * we need to check the mgmt queue for nops that need to
  1373. * be sent to aviod starvation
  1374. */
  1375. if (!list_empty(&conn->mgmtqueue))
  1376. goto check_mgmt;
  1377. }
  1378. while (!list_empty(&conn->requeue)) {
  1379. /*
  1380. * we always do fastlogout - conn stop code will clean up.
  1381. */
  1382. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1383. break;
  1384. task = list_entry(conn->requeue.next, struct iscsi_task,
  1385. running);
  1386. if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
  1387. break;
  1388. conn->task = task;
  1389. list_del_init(&conn->task->running);
  1390. conn->task->state = ISCSI_TASK_RUNNING;
  1391. rc = iscsi_xmit_task(conn);
  1392. if (rc)
  1393. goto done;
  1394. if (!list_empty(&conn->mgmtqueue))
  1395. goto check_mgmt;
  1396. }
  1397. spin_unlock_bh(&conn->session->frwd_lock);
  1398. return -ENODATA;
  1399. done:
  1400. spin_unlock_bh(&conn->session->frwd_lock);
  1401. return rc;
  1402. }
  1403. static void iscsi_xmitworker(struct work_struct *work)
  1404. {
  1405. struct iscsi_conn *conn =
  1406. container_of(work, struct iscsi_conn, xmitwork);
  1407. int rc;
  1408. /*
  1409. * serialize Xmit worker on a per-connection basis.
  1410. */
  1411. do {
  1412. rc = iscsi_data_xmit(conn);
  1413. } while (rc >= 0 || rc == -EAGAIN);
  1414. }
  1415. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1416. struct scsi_cmnd *sc)
  1417. {
  1418. struct iscsi_task *task;
  1419. if (!kfifo_out(&conn->session->cmdpool.queue,
  1420. (void *) &task, sizeof(void *)))
  1421. return NULL;
  1422. sc->SCp.phase = conn->session->age;
  1423. sc->SCp.ptr = (char *) task;
  1424. atomic_set(&task->refcount, 1);
  1425. task->state = ISCSI_TASK_PENDING;
  1426. task->conn = conn;
  1427. task->sc = sc;
  1428. task->have_checked_conn = false;
  1429. task->last_timeout = jiffies;
  1430. task->last_xfer = jiffies;
  1431. task->protected = false;
  1432. INIT_LIST_HEAD(&task->running);
  1433. return task;
  1434. }
  1435. enum {
  1436. FAILURE_BAD_HOST = 1,
  1437. FAILURE_SESSION_FAILED,
  1438. FAILURE_SESSION_FREED,
  1439. FAILURE_WINDOW_CLOSED,
  1440. FAILURE_OOM,
  1441. FAILURE_SESSION_TERMINATE,
  1442. FAILURE_SESSION_IN_RECOVERY,
  1443. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1444. FAILURE_SESSION_LOGGING_OUT,
  1445. FAILURE_SESSION_NOT_READY,
  1446. };
  1447. int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
  1448. {
  1449. struct iscsi_cls_session *cls_session;
  1450. struct iscsi_host *ihost;
  1451. int reason = 0;
  1452. struct iscsi_session *session;
  1453. struct iscsi_conn *conn;
  1454. struct iscsi_task *task = NULL;
  1455. sc->result = 0;
  1456. sc->SCp.ptr = NULL;
  1457. ihost = shost_priv(host);
  1458. cls_session = starget_to_session(scsi_target(sc->device));
  1459. session = cls_session->dd_data;
  1460. spin_lock_bh(&session->frwd_lock);
  1461. reason = iscsi_session_chkready(cls_session);
  1462. if (reason) {
  1463. sc->result = reason;
  1464. goto fault;
  1465. }
  1466. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1467. /*
  1468. * to handle the race between when we set the recovery state
  1469. * and block the session we requeue here (commands could
  1470. * be entering our queuecommand while a block is starting
  1471. * up because the block code is not locked)
  1472. */
  1473. switch (session->state) {
  1474. case ISCSI_STATE_FAILED:
  1475. case ISCSI_STATE_IN_RECOVERY:
  1476. reason = FAILURE_SESSION_IN_RECOVERY;
  1477. sc->result = DID_IMM_RETRY << 16;
  1478. break;
  1479. case ISCSI_STATE_LOGGING_OUT:
  1480. reason = FAILURE_SESSION_LOGGING_OUT;
  1481. sc->result = DID_IMM_RETRY << 16;
  1482. break;
  1483. case ISCSI_STATE_RECOVERY_FAILED:
  1484. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1485. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1486. break;
  1487. case ISCSI_STATE_TERMINATE:
  1488. reason = FAILURE_SESSION_TERMINATE;
  1489. sc->result = DID_NO_CONNECT << 16;
  1490. break;
  1491. default:
  1492. reason = FAILURE_SESSION_FREED;
  1493. sc->result = DID_NO_CONNECT << 16;
  1494. }
  1495. goto fault;
  1496. }
  1497. conn = session->leadconn;
  1498. if (!conn) {
  1499. reason = FAILURE_SESSION_FREED;
  1500. sc->result = DID_NO_CONNECT << 16;
  1501. goto fault;
  1502. }
  1503. if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
  1504. reason = FAILURE_SESSION_IN_RECOVERY;
  1505. sc->result = DID_REQUEUE;
  1506. goto fault;
  1507. }
  1508. if (iscsi_check_cmdsn_window_closed(conn)) {
  1509. reason = FAILURE_WINDOW_CLOSED;
  1510. goto reject;
  1511. }
  1512. task = iscsi_alloc_task(conn, sc);
  1513. if (!task) {
  1514. reason = FAILURE_OOM;
  1515. goto reject;
  1516. }
  1517. if (!ihost->workq) {
  1518. reason = iscsi_prep_scsi_cmd_pdu(task);
  1519. if (reason) {
  1520. if (reason == -ENOMEM || reason == -EACCES) {
  1521. reason = FAILURE_OOM;
  1522. goto prepd_reject;
  1523. } else {
  1524. sc->result = DID_ABORT << 16;
  1525. goto prepd_fault;
  1526. }
  1527. }
  1528. if (session->tt->xmit_task(task)) {
  1529. session->cmdsn--;
  1530. reason = FAILURE_SESSION_NOT_READY;
  1531. goto prepd_reject;
  1532. }
  1533. } else {
  1534. list_add_tail(&task->running, &conn->cmdqueue);
  1535. iscsi_conn_queue_work(conn);
  1536. }
  1537. session->queued_cmdsn++;
  1538. spin_unlock_bh(&session->frwd_lock);
  1539. return 0;
  1540. prepd_reject:
  1541. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1542. reject:
  1543. spin_unlock_bh(&session->frwd_lock);
  1544. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1545. sc->cmnd[0], reason);
  1546. return SCSI_MLQUEUE_TARGET_BUSY;
  1547. prepd_fault:
  1548. iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
  1549. fault:
  1550. spin_unlock_bh(&session->frwd_lock);
  1551. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1552. sc->cmnd[0], reason);
  1553. if (!scsi_bidi_cmnd(sc))
  1554. scsi_set_resid(sc, scsi_bufflen(sc));
  1555. else {
  1556. scsi_out(sc)->resid = scsi_out(sc)->length;
  1557. scsi_in(sc)->resid = scsi_in(sc)->length;
  1558. }
  1559. sc->scsi_done(sc);
  1560. return 0;
  1561. }
  1562. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1563. int iscsi_target_alloc(struct scsi_target *starget)
  1564. {
  1565. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1566. struct iscsi_session *session = cls_session->dd_data;
  1567. starget->can_queue = session->scsi_cmds_max;
  1568. return 0;
  1569. }
  1570. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1571. static void iscsi_tmf_timedout(unsigned long data)
  1572. {
  1573. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1574. struct iscsi_session *session = conn->session;
  1575. spin_lock(&session->frwd_lock);
  1576. if (conn->tmf_state == TMF_QUEUED) {
  1577. conn->tmf_state = TMF_TIMEDOUT;
  1578. ISCSI_DBG_EH(session, "tmf timedout\n");
  1579. /* unblock eh_abort() */
  1580. wake_up(&conn->ehwait);
  1581. }
  1582. spin_unlock(&session->frwd_lock);
  1583. }
  1584. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1585. struct iscsi_tm *hdr, int age,
  1586. int timeout)
  1587. {
  1588. struct iscsi_session *session = conn->session;
  1589. struct iscsi_task *task;
  1590. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1591. NULL, 0);
  1592. if (!task) {
  1593. spin_unlock_bh(&session->frwd_lock);
  1594. iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
  1595. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1596. spin_lock_bh(&session->frwd_lock);
  1597. return -EPERM;
  1598. }
  1599. conn->tmfcmd_pdus_cnt++;
  1600. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1601. conn->tmf_timer.function = iscsi_tmf_timedout;
  1602. conn->tmf_timer.data = (unsigned long)conn;
  1603. add_timer(&conn->tmf_timer);
  1604. ISCSI_DBG_EH(session, "tmf set timeout\n");
  1605. spin_unlock_bh(&session->frwd_lock);
  1606. mutex_unlock(&session->eh_mutex);
  1607. /*
  1608. * block eh thread until:
  1609. *
  1610. * 1) tmf response
  1611. * 2) tmf timeout
  1612. * 3) session is terminated or restarted or userspace has
  1613. * given up on recovery
  1614. */
  1615. wait_event_interruptible(conn->ehwait, age != session->age ||
  1616. session->state != ISCSI_STATE_LOGGED_IN ||
  1617. conn->tmf_state != TMF_QUEUED);
  1618. if (signal_pending(current))
  1619. flush_signals(current);
  1620. del_timer_sync(&conn->tmf_timer);
  1621. mutex_lock(&session->eh_mutex);
  1622. spin_lock_bh(&session->frwd_lock);
  1623. /* if the session drops it will clean up the task */
  1624. if (age != session->age ||
  1625. session->state != ISCSI_STATE_LOGGED_IN)
  1626. return -ENOTCONN;
  1627. return 0;
  1628. }
  1629. /*
  1630. * Fail commands. session lock held and recv side suspended and xmit
  1631. * thread flushed
  1632. */
  1633. static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
  1634. {
  1635. struct iscsi_task *task;
  1636. int i;
  1637. for (i = 0; i < conn->session->cmds_max; i++) {
  1638. task = conn->session->cmds[i];
  1639. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1640. continue;
  1641. if (lun != -1 && lun != task->sc->device->lun)
  1642. continue;
  1643. ISCSI_DBG_SESSION(conn->session,
  1644. "failing sc %p itt 0x%x state %d\n",
  1645. task->sc, task->itt, task->state);
  1646. fail_scsi_task(task, error);
  1647. }
  1648. }
  1649. /**
  1650. * iscsi_suspend_queue - suspend iscsi_queuecommand
  1651. * @conn: iscsi conn to stop queueing IO on
  1652. *
  1653. * This grabs the session frwd_lock to make sure no one is in
  1654. * xmit_task/queuecommand, and then sets suspend to prevent
  1655. * new commands from being queued. This only needs to be called
  1656. * by offload drivers that need to sync a path like ep disconnect
  1657. * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
  1658. * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
  1659. */
  1660. void iscsi_suspend_queue(struct iscsi_conn *conn)
  1661. {
  1662. spin_lock_bh(&conn->session->frwd_lock);
  1663. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1664. spin_unlock_bh(&conn->session->frwd_lock);
  1665. }
  1666. EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
  1667. /**
  1668. * iscsi_suspend_tx - suspend iscsi_data_xmit
  1669. * @conn: iscsi conn tp stop processing IO on.
  1670. *
  1671. * This function sets the suspend bit to prevent iscsi_data_xmit
  1672. * from sending new IO, and if work is queued on the xmit thread
  1673. * it will wait for it to be completed.
  1674. */
  1675. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1676. {
  1677. struct Scsi_Host *shost = conn->session->host;
  1678. struct iscsi_host *ihost = shost_priv(shost);
  1679. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1680. if (ihost->workq)
  1681. flush_workqueue(ihost->workq);
  1682. }
  1683. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1684. static void iscsi_start_tx(struct iscsi_conn *conn)
  1685. {
  1686. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1687. iscsi_conn_queue_work(conn);
  1688. }
  1689. /*
  1690. * We want to make sure a ping is in flight. It has timed out.
  1691. * And we are not busy processing a pdu that is making
  1692. * progress but got started before the ping and is taking a while
  1693. * to complete so the ping is just stuck behind it in a queue.
  1694. */
  1695. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1696. {
  1697. if (conn->ping_task &&
  1698. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1699. (conn->ping_timeout * HZ), jiffies))
  1700. return 1;
  1701. else
  1702. return 0;
  1703. }
  1704. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
  1705. {
  1706. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1707. struct iscsi_task *task = NULL, *running_task;
  1708. struct iscsi_cls_session *cls_session;
  1709. struct iscsi_session *session;
  1710. struct iscsi_conn *conn;
  1711. int i;
  1712. cls_session = starget_to_session(scsi_target(sc->device));
  1713. session = cls_session->dd_data;
  1714. ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
  1715. spin_lock(&session->frwd_lock);
  1716. task = (struct iscsi_task *)sc->SCp.ptr;
  1717. if (!task) {
  1718. /*
  1719. * Raced with completion. Blk layer has taken ownership
  1720. * so let timeout code complete it now.
  1721. */
  1722. rc = BLK_EH_HANDLED;
  1723. goto done;
  1724. }
  1725. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1726. /*
  1727. * We are probably in the middle of iscsi recovery so let
  1728. * that complete and handle the error.
  1729. */
  1730. rc = BLK_EH_RESET_TIMER;
  1731. goto done;
  1732. }
  1733. conn = session->leadconn;
  1734. if (!conn) {
  1735. /* In the middle of shuting down */
  1736. rc = BLK_EH_RESET_TIMER;
  1737. goto done;
  1738. }
  1739. /*
  1740. * If we have sent (at least queued to the network layer) a pdu or
  1741. * recvd one for the task since the last timeout ask for
  1742. * more time. If on the next timeout we have not made progress
  1743. * we can check if it is the task or connection when we send the
  1744. * nop as a ping.
  1745. */
  1746. if (time_after(task->last_xfer, task->last_timeout)) {
  1747. ISCSI_DBG_EH(session, "Command making progress. Asking "
  1748. "scsi-ml for more time to complete. "
  1749. "Last data xfer at %lu. Last timeout was at "
  1750. "%lu\n.", task->last_xfer, task->last_timeout);
  1751. task->have_checked_conn = false;
  1752. rc = BLK_EH_RESET_TIMER;
  1753. goto done;
  1754. }
  1755. if (!conn->recv_timeout && !conn->ping_timeout)
  1756. goto done;
  1757. /*
  1758. * if the ping timedout then we are in the middle of cleaning up
  1759. * and can let the iscsi eh handle it
  1760. */
  1761. if (iscsi_has_ping_timed_out(conn)) {
  1762. rc = BLK_EH_RESET_TIMER;
  1763. goto done;
  1764. }
  1765. for (i = 0; i < conn->session->cmds_max; i++) {
  1766. running_task = conn->session->cmds[i];
  1767. if (!running_task->sc || running_task == task ||
  1768. running_task->state != ISCSI_TASK_RUNNING)
  1769. continue;
  1770. /*
  1771. * Only check if cmds started before this one have made
  1772. * progress, or this could never fail
  1773. */
  1774. if (time_after(running_task->sc->jiffies_at_alloc,
  1775. task->sc->jiffies_at_alloc))
  1776. continue;
  1777. if (time_after(running_task->last_xfer, task->last_timeout)) {
  1778. /*
  1779. * This task has not made progress, but a task
  1780. * started before us has transferred data since
  1781. * we started/last-checked. We could be queueing
  1782. * too many tasks or the LU is bad.
  1783. *
  1784. * If the device is bad the cmds ahead of us on
  1785. * other devs will complete, and this loop will
  1786. * eventually fail starting the scsi eh.
  1787. */
  1788. ISCSI_DBG_EH(session, "Command has not made progress "
  1789. "but commands ahead of it have. "
  1790. "Asking scsi-ml for more time to "
  1791. "complete. Our last xfer vs running task "
  1792. "last xfer %lu/%lu. Last check %lu.\n",
  1793. task->last_xfer, running_task->last_xfer,
  1794. task->last_timeout);
  1795. rc = BLK_EH_RESET_TIMER;
  1796. goto done;
  1797. }
  1798. }
  1799. /* Assumes nop timeout is shorter than scsi cmd timeout */
  1800. if (task->have_checked_conn)
  1801. goto done;
  1802. /*
  1803. * Checking the transport already or nop from a cmd timeout still
  1804. * running
  1805. */
  1806. if (conn->ping_task) {
  1807. task->have_checked_conn = true;
  1808. rc = BLK_EH_RESET_TIMER;
  1809. goto done;
  1810. }
  1811. /* Make sure there is a transport check done */
  1812. iscsi_send_nopout(conn, NULL);
  1813. task->have_checked_conn = true;
  1814. rc = BLK_EH_RESET_TIMER;
  1815. done:
  1816. if (task)
  1817. task->last_timeout = jiffies;
  1818. spin_unlock(&session->frwd_lock);
  1819. ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1820. "timer reset" : "nh");
  1821. return rc;
  1822. }
  1823. static void iscsi_check_transport_timeouts(unsigned long data)
  1824. {
  1825. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1826. struct iscsi_session *session = conn->session;
  1827. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1828. spin_lock(&session->frwd_lock);
  1829. if (session->state != ISCSI_STATE_LOGGED_IN)
  1830. goto done;
  1831. recv_timeout = conn->recv_timeout;
  1832. if (!recv_timeout)
  1833. goto done;
  1834. recv_timeout *= HZ;
  1835. last_recv = conn->last_recv;
  1836. if (iscsi_has_ping_timed_out(conn)) {
  1837. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1838. "expired, recv timeout %d, last rx %lu, "
  1839. "last ping %lu, now %lu\n",
  1840. conn->ping_timeout, conn->recv_timeout,
  1841. last_recv, conn->last_ping, jiffies);
  1842. spin_unlock(&session->frwd_lock);
  1843. iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
  1844. return;
  1845. }
  1846. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1847. /* send a ping to try to provoke some traffic */
  1848. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1849. iscsi_send_nopout(conn, NULL);
  1850. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1851. } else
  1852. next_timeout = last_recv + recv_timeout;
  1853. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1854. mod_timer(&conn->transport_timer, next_timeout);
  1855. done:
  1856. spin_unlock(&session->frwd_lock);
  1857. }
  1858. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1859. struct iscsi_tm *hdr)
  1860. {
  1861. memset(hdr, 0, sizeof(*hdr));
  1862. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1863. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1864. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1865. hdr->lun = task->lun;
  1866. hdr->rtt = task->hdr_itt;
  1867. hdr->refcmdsn = task->cmdsn;
  1868. }
  1869. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1870. {
  1871. struct iscsi_cls_session *cls_session;
  1872. struct iscsi_session *session;
  1873. struct iscsi_conn *conn;
  1874. struct iscsi_task *task;
  1875. struct iscsi_tm *hdr;
  1876. int rc, age;
  1877. cls_session = starget_to_session(scsi_target(sc->device));
  1878. session = cls_session->dd_data;
  1879. ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
  1880. mutex_lock(&session->eh_mutex);
  1881. spin_lock_bh(&session->frwd_lock);
  1882. /*
  1883. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1884. * got the command.
  1885. */
  1886. if (!sc->SCp.ptr) {
  1887. ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
  1888. "it completed.\n");
  1889. spin_unlock_bh(&session->frwd_lock);
  1890. mutex_unlock(&session->eh_mutex);
  1891. return SUCCESS;
  1892. }
  1893. /*
  1894. * If we are not logged in or we have started a new session
  1895. * then let the host reset code handle this
  1896. */
  1897. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1898. sc->SCp.phase != session->age) {
  1899. spin_unlock_bh(&session->frwd_lock);
  1900. mutex_unlock(&session->eh_mutex);
  1901. ISCSI_DBG_EH(session, "failing abort due to dropped "
  1902. "session.\n");
  1903. return FAILED;
  1904. }
  1905. conn = session->leadconn;
  1906. conn->eh_abort_cnt++;
  1907. age = session->age;
  1908. task = (struct iscsi_task *)sc->SCp.ptr;
  1909. ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
  1910. sc, task->itt);
  1911. /* task completed before time out */
  1912. if (!task->sc) {
  1913. ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
  1914. goto success;
  1915. }
  1916. if (task->state == ISCSI_TASK_PENDING) {
  1917. fail_scsi_task(task, DID_ABORT);
  1918. goto success;
  1919. }
  1920. /* only have one tmf outstanding at a time */
  1921. if (conn->tmf_state != TMF_INITIAL)
  1922. goto failed;
  1923. conn->tmf_state = TMF_QUEUED;
  1924. hdr = &conn->tmhdr;
  1925. iscsi_prep_abort_task_pdu(task, hdr);
  1926. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1927. rc = FAILED;
  1928. goto failed;
  1929. }
  1930. switch (conn->tmf_state) {
  1931. case TMF_SUCCESS:
  1932. spin_unlock_bh(&session->frwd_lock);
  1933. /*
  1934. * stop tx side incase the target had sent a abort rsp but
  1935. * the initiator was still writing out data.
  1936. */
  1937. iscsi_suspend_tx(conn);
  1938. /*
  1939. * we do not stop the recv side because targets have been
  1940. * good and have never sent us a successful tmf response
  1941. * then sent more data for the cmd.
  1942. */
  1943. spin_lock_bh(&session->frwd_lock);
  1944. fail_scsi_task(task, DID_ABORT);
  1945. conn->tmf_state = TMF_INITIAL;
  1946. memset(hdr, 0, sizeof(*hdr));
  1947. spin_unlock_bh(&session->frwd_lock);
  1948. iscsi_start_tx(conn);
  1949. goto success_unlocked;
  1950. case TMF_TIMEDOUT:
  1951. spin_unlock_bh(&session->frwd_lock);
  1952. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  1953. goto failed_unlocked;
  1954. case TMF_NOT_FOUND:
  1955. if (!sc->SCp.ptr) {
  1956. conn->tmf_state = TMF_INITIAL;
  1957. memset(hdr, 0, sizeof(*hdr));
  1958. /* task completed before tmf abort response */
  1959. ISCSI_DBG_EH(session, "sc completed while abort in "
  1960. "progress\n");
  1961. goto success;
  1962. }
  1963. /* fall through */
  1964. default:
  1965. conn->tmf_state = TMF_INITIAL;
  1966. goto failed;
  1967. }
  1968. success:
  1969. spin_unlock_bh(&session->frwd_lock);
  1970. success_unlocked:
  1971. ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
  1972. sc, task->itt);
  1973. mutex_unlock(&session->eh_mutex);
  1974. return SUCCESS;
  1975. failed:
  1976. spin_unlock_bh(&session->frwd_lock);
  1977. failed_unlocked:
  1978. ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
  1979. task ? task->itt : 0);
  1980. mutex_unlock(&session->eh_mutex);
  1981. return FAILED;
  1982. }
  1983. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1984. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1985. {
  1986. memset(hdr, 0, sizeof(*hdr));
  1987. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1988. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1989. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1990. int_to_scsilun(sc->device->lun, &hdr->lun);
  1991. hdr->rtt = RESERVED_ITT;
  1992. }
  1993. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1994. {
  1995. struct iscsi_cls_session *cls_session;
  1996. struct iscsi_session *session;
  1997. struct iscsi_conn *conn;
  1998. struct iscsi_tm *hdr;
  1999. int rc = FAILED;
  2000. cls_session = starget_to_session(scsi_target(sc->device));
  2001. session = cls_session->dd_data;
  2002. ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
  2003. sc->device->lun);
  2004. mutex_lock(&session->eh_mutex);
  2005. spin_lock_bh(&session->frwd_lock);
  2006. /*
  2007. * Just check if we are not logged in. We cannot check for
  2008. * the phase because the reset could come from a ioctl.
  2009. */
  2010. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2011. goto unlock;
  2012. conn = session->leadconn;
  2013. /* only have one tmf outstanding at a time */
  2014. if (conn->tmf_state != TMF_INITIAL)
  2015. goto unlock;
  2016. conn->tmf_state = TMF_QUEUED;
  2017. hdr = &conn->tmhdr;
  2018. iscsi_prep_lun_reset_pdu(sc, hdr);
  2019. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2020. session->lu_reset_timeout)) {
  2021. rc = FAILED;
  2022. goto unlock;
  2023. }
  2024. switch (conn->tmf_state) {
  2025. case TMF_SUCCESS:
  2026. break;
  2027. case TMF_TIMEDOUT:
  2028. spin_unlock_bh(&session->frwd_lock);
  2029. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2030. goto done;
  2031. default:
  2032. conn->tmf_state = TMF_INITIAL;
  2033. goto unlock;
  2034. }
  2035. rc = SUCCESS;
  2036. spin_unlock_bh(&session->frwd_lock);
  2037. iscsi_suspend_tx(conn);
  2038. spin_lock_bh(&session->frwd_lock);
  2039. memset(hdr, 0, sizeof(*hdr));
  2040. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  2041. conn->tmf_state = TMF_INITIAL;
  2042. spin_unlock_bh(&session->frwd_lock);
  2043. iscsi_start_tx(conn);
  2044. goto done;
  2045. unlock:
  2046. spin_unlock_bh(&session->frwd_lock);
  2047. done:
  2048. ISCSI_DBG_EH(session, "dev reset result = %s\n",
  2049. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2050. mutex_unlock(&session->eh_mutex);
  2051. return rc;
  2052. }
  2053. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  2054. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  2055. {
  2056. struct iscsi_session *session = cls_session->dd_data;
  2057. spin_lock_bh(&session->frwd_lock);
  2058. if (session->state != ISCSI_STATE_LOGGED_IN) {
  2059. session->state = ISCSI_STATE_RECOVERY_FAILED;
  2060. if (session->leadconn)
  2061. wake_up(&session->leadconn->ehwait);
  2062. }
  2063. spin_unlock_bh(&session->frwd_lock);
  2064. }
  2065. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  2066. /**
  2067. * iscsi_eh_session_reset - drop session and attempt relogin
  2068. * @sc: scsi command
  2069. *
  2070. * This function will wait for a relogin, session termination from
  2071. * userspace, or a recovery/replacement timeout.
  2072. */
  2073. int iscsi_eh_session_reset(struct scsi_cmnd *sc)
  2074. {
  2075. struct iscsi_cls_session *cls_session;
  2076. struct iscsi_session *session;
  2077. struct iscsi_conn *conn;
  2078. cls_session = starget_to_session(scsi_target(sc->device));
  2079. session = cls_session->dd_data;
  2080. conn = session->leadconn;
  2081. mutex_lock(&session->eh_mutex);
  2082. spin_lock_bh(&session->frwd_lock);
  2083. if (session->state == ISCSI_STATE_TERMINATE) {
  2084. failed:
  2085. ISCSI_DBG_EH(session,
  2086. "failing session reset: Could not log back into "
  2087. "%s, %s [age %d]\n", session->targetname,
  2088. conn->persistent_address, session->age);
  2089. spin_unlock_bh(&session->frwd_lock);
  2090. mutex_unlock(&session->eh_mutex);
  2091. return FAILED;
  2092. }
  2093. spin_unlock_bh(&session->frwd_lock);
  2094. mutex_unlock(&session->eh_mutex);
  2095. /*
  2096. * we drop the lock here but the leadconn cannot be destoyed while
  2097. * we are in the scsi eh
  2098. */
  2099. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2100. ISCSI_DBG_EH(session, "wait for relogin\n");
  2101. wait_event_interruptible(conn->ehwait,
  2102. session->state == ISCSI_STATE_TERMINATE ||
  2103. session->state == ISCSI_STATE_LOGGED_IN ||
  2104. session->state == ISCSI_STATE_RECOVERY_FAILED);
  2105. if (signal_pending(current))
  2106. flush_signals(current);
  2107. mutex_lock(&session->eh_mutex);
  2108. spin_lock_bh(&session->frwd_lock);
  2109. if (session->state == ISCSI_STATE_LOGGED_IN) {
  2110. ISCSI_DBG_EH(session,
  2111. "session reset succeeded for %s,%s\n",
  2112. session->targetname, conn->persistent_address);
  2113. } else
  2114. goto failed;
  2115. spin_unlock_bh(&session->frwd_lock);
  2116. mutex_unlock(&session->eh_mutex);
  2117. return SUCCESS;
  2118. }
  2119. EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
  2120. static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  2121. {
  2122. memset(hdr, 0, sizeof(*hdr));
  2123. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  2124. hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  2125. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  2126. hdr->rtt = RESERVED_ITT;
  2127. }
  2128. /**
  2129. * iscsi_eh_target_reset - reset target
  2130. * @sc: scsi command
  2131. *
  2132. * This will attempt to send a warm target reset.
  2133. */
  2134. int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  2135. {
  2136. struct iscsi_cls_session *cls_session;
  2137. struct iscsi_session *session;
  2138. struct iscsi_conn *conn;
  2139. struct iscsi_tm *hdr;
  2140. int rc = FAILED;
  2141. cls_session = starget_to_session(scsi_target(sc->device));
  2142. session = cls_session->dd_data;
  2143. ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
  2144. session->targetname);
  2145. mutex_lock(&session->eh_mutex);
  2146. spin_lock_bh(&session->frwd_lock);
  2147. /*
  2148. * Just check if we are not logged in. We cannot check for
  2149. * the phase because the reset could come from a ioctl.
  2150. */
  2151. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  2152. goto unlock;
  2153. conn = session->leadconn;
  2154. /* only have one tmf outstanding at a time */
  2155. if (conn->tmf_state != TMF_INITIAL)
  2156. goto unlock;
  2157. conn->tmf_state = TMF_QUEUED;
  2158. hdr = &conn->tmhdr;
  2159. iscsi_prep_tgt_reset_pdu(sc, hdr);
  2160. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  2161. session->tgt_reset_timeout)) {
  2162. rc = FAILED;
  2163. goto unlock;
  2164. }
  2165. switch (conn->tmf_state) {
  2166. case TMF_SUCCESS:
  2167. break;
  2168. case TMF_TIMEDOUT:
  2169. spin_unlock_bh(&session->frwd_lock);
  2170. iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
  2171. goto done;
  2172. default:
  2173. conn->tmf_state = TMF_INITIAL;
  2174. goto unlock;
  2175. }
  2176. rc = SUCCESS;
  2177. spin_unlock_bh(&session->frwd_lock);
  2178. iscsi_suspend_tx(conn);
  2179. spin_lock_bh(&session->frwd_lock);
  2180. memset(hdr, 0, sizeof(*hdr));
  2181. fail_scsi_tasks(conn, -1, DID_ERROR);
  2182. conn->tmf_state = TMF_INITIAL;
  2183. spin_unlock_bh(&session->frwd_lock);
  2184. iscsi_start_tx(conn);
  2185. goto done;
  2186. unlock:
  2187. spin_unlock_bh(&session->frwd_lock);
  2188. done:
  2189. ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
  2190. rc == SUCCESS ? "SUCCESS" : "FAILED");
  2191. mutex_unlock(&session->eh_mutex);
  2192. return rc;
  2193. }
  2194. EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
  2195. /**
  2196. * iscsi_eh_recover_target - reset target and possibly the session
  2197. * @sc: scsi command
  2198. *
  2199. * This will attempt to send a warm target reset. If that fails,
  2200. * we will escalate to ERL0 session recovery.
  2201. */
  2202. int iscsi_eh_recover_target(struct scsi_cmnd *sc)
  2203. {
  2204. int rc;
  2205. rc = iscsi_eh_target_reset(sc);
  2206. if (rc == FAILED)
  2207. rc = iscsi_eh_session_reset(sc);
  2208. return rc;
  2209. }
  2210. EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
  2211. /*
  2212. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  2213. * should be accessed via kfifo_{get,put} on q->queue.
  2214. * Optionally, the caller can obtain the array of object pointers
  2215. * by passing in a non-NULL @items pointer
  2216. */
  2217. int
  2218. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  2219. {
  2220. int i, num_arrays = 1;
  2221. memset(q, 0, sizeof(*q));
  2222. q->max = max;
  2223. /* If the user passed an items pointer, he wants a copy of
  2224. * the array. */
  2225. if (items)
  2226. num_arrays++;
  2227. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  2228. if (q->pool == NULL)
  2229. return -ENOMEM;
  2230. kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
  2231. for (i = 0; i < max; i++) {
  2232. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  2233. if (q->pool[i] == NULL) {
  2234. q->max = i;
  2235. goto enomem;
  2236. }
  2237. kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
  2238. }
  2239. if (items) {
  2240. *items = q->pool + max;
  2241. memcpy(*items, q->pool, max * sizeof(void *));
  2242. }
  2243. return 0;
  2244. enomem:
  2245. iscsi_pool_free(q);
  2246. return -ENOMEM;
  2247. }
  2248. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  2249. void iscsi_pool_free(struct iscsi_pool *q)
  2250. {
  2251. int i;
  2252. for (i = 0; i < q->max; i++)
  2253. kfree(q->pool[i]);
  2254. kfree(q->pool);
  2255. }
  2256. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  2257. /**
  2258. * iscsi_host_add - add host to system
  2259. * @shost: scsi host
  2260. * @pdev: parent device
  2261. *
  2262. * This should be called by partial offload and software iscsi drivers
  2263. * to add a host to the system.
  2264. */
  2265. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  2266. {
  2267. if (!shost->can_queue)
  2268. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  2269. if (!shost->cmd_per_lun)
  2270. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  2271. if (!shost->transportt->eh_timed_out)
  2272. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  2273. return scsi_add_host(shost, pdev);
  2274. }
  2275. EXPORT_SYMBOL_GPL(iscsi_host_add);
  2276. /**
  2277. * iscsi_host_alloc - allocate a host and driver data
  2278. * @sht: scsi host template
  2279. * @dd_data_size: driver host data size
  2280. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  2281. *
  2282. * This should be called by partial offload and software iscsi drivers.
  2283. * To access the driver specific memory use the iscsi_host_priv() macro.
  2284. */
  2285. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  2286. int dd_data_size, bool xmit_can_sleep)
  2287. {
  2288. struct Scsi_Host *shost;
  2289. struct iscsi_host *ihost;
  2290. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  2291. if (!shost)
  2292. return NULL;
  2293. ihost = shost_priv(shost);
  2294. if (xmit_can_sleep) {
  2295. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  2296. "iscsi_q_%d", shost->host_no);
  2297. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  2298. if (!ihost->workq)
  2299. goto free_host;
  2300. }
  2301. spin_lock_init(&ihost->lock);
  2302. ihost->state = ISCSI_HOST_SETUP;
  2303. ihost->num_sessions = 0;
  2304. init_waitqueue_head(&ihost->session_removal_wq);
  2305. return shost;
  2306. free_host:
  2307. scsi_host_put(shost);
  2308. return NULL;
  2309. }
  2310. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  2311. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  2312. {
  2313. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  2314. }
  2315. /**
  2316. * iscsi_host_remove - remove host and sessions
  2317. * @shost: scsi host
  2318. *
  2319. * If there are any sessions left, this will initiate the removal and wait
  2320. * for the completion.
  2321. */
  2322. void iscsi_host_remove(struct Scsi_Host *shost)
  2323. {
  2324. struct iscsi_host *ihost = shost_priv(shost);
  2325. unsigned long flags;
  2326. spin_lock_irqsave(&ihost->lock, flags);
  2327. ihost->state = ISCSI_HOST_REMOVED;
  2328. spin_unlock_irqrestore(&ihost->lock, flags);
  2329. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  2330. wait_event_interruptible(ihost->session_removal_wq,
  2331. ihost->num_sessions == 0);
  2332. if (signal_pending(current))
  2333. flush_signals(current);
  2334. scsi_remove_host(shost);
  2335. if (ihost->workq)
  2336. destroy_workqueue(ihost->workq);
  2337. }
  2338. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  2339. void iscsi_host_free(struct Scsi_Host *shost)
  2340. {
  2341. struct iscsi_host *ihost = shost_priv(shost);
  2342. kfree(ihost->netdev);
  2343. kfree(ihost->hwaddress);
  2344. kfree(ihost->initiatorname);
  2345. scsi_host_put(shost);
  2346. }
  2347. EXPORT_SYMBOL_GPL(iscsi_host_free);
  2348. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  2349. {
  2350. struct iscsi_host *ihost = shost_priv(shost);
  2351. unsigned long flags;
  2352. shost = scsi_host_get(shost);
  2353. if (!shost) {
  2354. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  2355. "of session teardown event because host already "
  2356. "removed.\n");
  2357. return;
  2358. }
  2359. spin_lock_irqsave(&ihost->lock, flags);
  2360. ihost->num_sessions--;
  2361. if (ihost->num_sessions == 0)
  2362. wake_up(&ihost->session_removal_wq);
  2363. spin_unlock_irqrestore(&ihost->lock, flags);
  2364. scsi_host_put(shost);
  2365. }
  2366. /**
  2367. * iscsi_session_setup - create iscsi cls session and host and session
  2368. * @iscsit: iscsi transport template
  2369. * @shost: scsi host
  2370. * @cmds_max: session can queue
  2371. * @cmd_task_size: LLD task private data size
  2372. * @initial_cmdsn: initial CmdSN
  2373. *
  2374. * This can be used by software iscsi_transports that allocate
  2375. * a session per scsi host.
  2376. *
  2377. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  2378. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  2379. * for nop handling and login/logout requests.
  2380. */
  2381. struct iscsi_cls_session *
  2382. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  2383. uint16_t cmds_max, int dd_size, int cmd_task_size,
  2384. uint32_t initial_cmdsn, unsigned int id)
  2385. {
  2386. struct iscsi_host *ihost = shost_priv(shost);
  2387. struct iscsi_session *session;
  2388. struct iscsi_cls_session *cls_session;
  2389. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  2390. unsigned long flags;
  2391. spin_lock_irqsave(&ihost->lock, flags);
  2392. if (ihost->state == ISCSI_HOST_REMOVED) {
  2393. spin_unlock_irqrestore(&ihost->lock, flags);
  2394. return NULL;
  2395. }
  2396. ihost->num_sessions++;
  2397. spin_unlock_irqrestore(&ihost->lock, flags);
  2398. if (!total_cmds)
  2399. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  2400. /*
  2401. * The iscsi layer needs some tasks for nop handling and tmfs,
  2402. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  2403. * + 1 command for scsi IO.
  2404. */
  2405. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  2406. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2407. "must be a power of two that is at least %d.\n",
  2408. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  2409. goto dec_session_count;
  2410. }
  2411. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  2412. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2413. "must be a power of 2 less than or equal to %d.\n",
  2414. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  2415. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  2416. }
  2417. if (!is_power_of_2(total_cmds)) {
  2418. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  2419. "must be a power of 2.\n", total_cmds);
  2420. total_cmds = rounddown_pow_of_two(total_cmds);
  2421. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  2422. return NULL;
  2423. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  2424. total_cmds);
  2425. }
  2426. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  2427. cls_session = iscsi_alloc_session(shost, iscsit,
  2428. sizeof(struct iscsi_session) +
  2429. dd_size);
  2430. if (!cls_session)
  2431. goto dec_session_count;
  2432. session = cls_session->dd_data;
  2433. session->cls_session = cls_session;
  2434. session->host = shost;
  2435. session->state = ISCSI_STATE_FREE;
  2436. session->fast_abort = 1;
  2437. session->tgt_reset_timeout = 30;
  2438. session->lu_reset_timeout = 15;
  2439. session->abort_timeout = 10;
  2440. session->scsi_cmds_max = scsi_cmds;
  2441. session->cmds_max = total_cmds;
  2442. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2443. session->exp_cmdsn = initial_cmdsn + 1;
  2444. session->max_cmdsn = initial_cmdsn + 1;
  2445. session->max_r2t = 1;
  2446. session->tt = iscsit;
  2447. session->dd_data = cls_session->dd_data + sizeof(*session);
  2448. mutex_init(&session->eh_mutex);
  2449. spin_lock_init(&session->frwd_lock);
  2450. spin_lock_init(&session->back_lock);
  2451. /* initialize SCSI PDU commands pool */
  2452. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2453. (void***)&session->cmds,
  2454. cmd_task_size + sizeof(struct iscsi_task)))
  2455. goto cmdpool_alloc_fail;
  2456. /* pre-format cmds pool with ITT */
  2457. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2458. struct iscsi_task *task = session->cmds[cmd_i];
  2459. if (cmd_task_size)
  2460. task->dd_data = &task[1];
  2461. task->itt = cmd_i;
  2462. task->state = ISCSI_TASK_FREE;
  2463. INIT_LIST_HEAD(&task->running);
  2464. }
  2465. if (!try_module_get(iscsit->owner))
  2466. goto module_get_fail;
  2467. if (iscsi_add_session(cls_session, id))
  2468. goto cls_session_fail;
  2469. return cls_session;
  2470. cls_session_fail:
  2471. module_put(iscsit->owner);
  2472. module_get_fail:
  2473. iscsi_pool_free(&session->cmdpool);
  2474. cmdpool_alloc_fail:
  2475. iscsi_free_session(cls_session);
  2476. dec_session_count:
  2477. iscsi_host_dec_session_cnt(shost);
  2478. return NULL;
  2479. }
  2480. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2481. /**
  2482. * iscsi_session_teardown - destroy session, host, and cls_session
  2483. * @cls_session: iscsi session
  2484. *
  2485. * The driver must have called iscsi_remove_session before
  2486. * calling this.
  2487. */
  2488. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2489. {
  2490. struct iscsi_session *session = cls_session->dd_data;
  2491. struct module *owner = cls_session->transport->owner;
  2492. struct Scsi_Host *shost = session->host;
  2493. iscsi_pool_free(&session->cmdpool);
  2494. kfree(session->password);
  2495. kfree(session->password_in);
  2496. kfree(session->username);
  2497. kfree(session->username_in);
  2498. kfree(session->targetname);
  2499. kfree(session->targetalias);
  2500. kfree(session->initiatorname);
  2501. kfree(session->boot_root);
  2502. kfree(session->boot_nic);
  2503. kfree(session->boot_target);
  2504. kfree(session->ifacename);
  2505. kfree(session->portal_type);
  2506. kfree(session->discovery_parent_type);
  2507. iscsi_destroy_session(cls_session);
  2508. iscsi_host_dec_session_cnt(shost);
  2509. module_put(owner);
  2510. }
  2511. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2512. /**
  2513. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2514. * @cls_session: iscsi_cls_session
  2515. * @dd_size: private driver data size
  2516. * @conn_idx: cid
  2517. */
  2518. struct iscsi_cls_conn *
  2519. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2520. uint32_t conn_idx)
  2521. {
  2522. struct iscsi_session *session = cls_session->dd_data;
  2523. struct iscsi_conn *conn;
  2524. struct iscsi_cls_conn *cls_conn;
  2525. char *data;
  2526. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2527. conn_idx);
  2528. if (!cls_conn)
  2529. return NULL;
  2530. conn = cls_conn->dd_data;
  2531. memset(conn, 0, sizeof(*conn) + dd_size);
  2532. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2533. conn->session = session;
  2534. conn->cls_conn = cls_conn;
  2535. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2536. conn->id = conn_idx;
  2537. conn->exp_statsn = 0;
  2538. conn->tmf_state = TMF_INITIAL;
  2539. init_timer(&conn->transport_timer);
  2540. conn->transport_timer.data = (unsigned long)conn;
  2541. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2542. INIT_LIST_HEAD(&conn->mgmtqueue);
  2543. INIT_LIST_HEAD(&conn->cmdqueue);
  2544. INIT_LIST_HEAD(&conn->requeue);
  2545. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2546. /* allocate login_task used for the login/text sequences */
  2547. spin_lock_bh(&session->frwd_lock);
  2548. if (!kfifo_out(&session->cmdpool.queue,
  2549. (void*)&conn->login_task,
  2550. sizeof(void*))) {
  2551. spin_unlock_bh(&session->frwd_lock);
  2552. goto login_task_alloc_fail;
  2553. }
  2554. spin_unlock_bh(&session->frwd_lock);
  2555. data = (char *) __get_free_pages(GFP_KERNEL,
  2556. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2557. if (!data)
  2558. goto login_task_data_alloc_fail;
  2559. conn->login_task->data = conn->data = data;
  2560. init_timer(&conn->tmf_timer);
  2561. init_waitqueue_head(&conn->ehwait);
  2562. return cls_conn;
  2563. login_task_data_alloc_fail:
  2564. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2565. sizeof(void*));
  2566. login_task_alloc_fail:
  2567. iscsi_destroy_conn(cls_conn);
  2568. return NULL;
  2569. }
  2570. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2571. /**
  2572. * iscsi_conn_teardown - teardown iscsi connection
  2573. * cls_conn: iscsi class connection
  2574. *
  2575. * TODO: we may need to make this into a two step process
  2576. * like scsi-mls remove + put host
  2577. */
  2578. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2579. {
  2580. struct iscsi_conn *conn = cls_conn->dd_data;
  2581. struct iscsi_session *session = conn->session;
  2582. unsigned long flags;
  2583. del_timer_sync(&conn->transport_timer);
  2584. spin_lock_bh(&session->frwd_lock);
  2585. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2586. if (session->leadconn == conn) {
  2587. /*
  2588. * leading connection? then give up on recovery.
  2589. */
  2590. session->state = ISCSI_STATE_TERMINATE;
  2591. wake_up(&conn->ehwait);
  2592. }
  2593. spin_unlock_bh(&session->frwd_lock);
  2594. /*
  2595. * Block until all in-progress commands for this connection
  2596. * time out or fail.
  2597. */
  2598. for (;;) {
  2599. spin_lock_irqsave(session->host->host_lock, flags);
  2600. if (!atomic_read(&session->host->host_busy)) { /* OK for ERL == 0 */
  2601. spin_unlock_irqrestore(session->host->host_lock, flags);
  2602. break;
  2603. }
  2604. spin_unlock_irqrestore(session->host->host_lock, flags);
  2605. msleep_interruptible(500);
  2606. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  2607. "host_busy %d host_failed %d\n",
  2608. atomic_read(&session->host->host_busy),
  2609. session->host->host_failed);
  2610. /*
  2611. * force eh_abort() to unblock
  2612. */
  2613. wake_up(&conn->ehwait);
  2614. }
  2615. /* flush queued up work because we free the connection below */
  2616. iscsi_suspend_tx(conn);
  2617. spin_lock_bh(&session->frwd_lock);
  2618. free_pages((unsigned long) conn->data,
  2619. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2620. kfree(conn->persistent_address);
  2621. kfree(conn->local_ipaddr);
  2622. /* regular RX path uses back_lock */
  2623. spin_lock_bh(&session->back_lock);
  2624. kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
  2625. sizeof(void*));
  2626. spin_unlock_bh(&session->back_lock);
  2627. if (session->leadconn == conn)
  2628. session->leadconn = NULL;
  2629. spin_unlock_bh(&session->frwd_lock);
  2630. iscsi_destroy_conn(cls_conn);
  2631. }
  2632. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2633. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2634. {
  2635. struct iscsi_conn *conn = cls_conn->dd_data;
  2636. struct iscsi_session *session = conn->session;
  2637. if (!session) {
  2638. iscsi_conn_printk(KERN_ERR, conn,
  2639. "can't start unbound connection\n");
  2640. return -EPERM;
  2641. }
  2642. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2643. session->first_burst > session->max_burst) {
  2644. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2645. "first_burst %d max_burst %d\n",
  2646. session->first_burst, session->max_burst);
  2647. return -EINVAL;
  2648. }
  2649. if (conn->ping_timeout && !conn->recv_timeout) {
  2650. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2651. "zero. Using 5 seconds\n.");
  2652. conn->recv_timeout = 5;
  2653. }
  2654. if (conn->recv_timeout && !conn->ping_timeout) {
  2655. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2656. "zero. Using 5 seconds.\n");
  2657. conn->ping_timeout = 5;
  2658. }
  2659. spin_lock_bh(&session->frwd_lock);
  2660. conn->c_stage = ISCSI_CONN_STARTED;
  2661. session->state = ISCSI_STATE_LOGGED_IN;
  2662. session->queued_cmdsn = session->cmdsn;
  2663. conn->last_recv = jiffies;
  2664. conn->last_ping = jiffies;
  2665. if (conn->recv_timeout && conn->ping_timeout)
  2666. mod_timer(&conn->transport_timer,
  2667. jiffies + (conn->recv_timeout * HZ));
  2668. switch(conn->stop_stage) {
  2669. case STOP_CONN_RECOVER:
  2670. /*
  2671. * unblock eh_abort() if it is blocked. re-try all
  2672. * commands after successful recovery
  2673. */
  2674. conn->stop_stage = 0;
  2675. conn->tmf_state = TMF_INITIAL;
  2676. session->age++;
  2677. if (session->age == 16)
  2678. session->age = 0;
  2679. break;
  2680. case STOP_CONN_TERM:
  2681. conn->stop_stage = 0;
  2682. break;
  2683. default:
  2684. break;
  2685. }
  2686. spin_unlock_bh(&session->frwd_lock);
  2687. iscsi_unblock_session(session->cls_session);
  2688. wake_up(&conn->ehwait);
  2689. return 0;
  2690. }
  2691. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2692. static void
  2693. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2694. {
  2695. struct iscsi_task *task;
  2696. int i, state;
  2697. for (i = 0; i < conn->session->cmds_max; i++) {
  2698. task = conn->session->cmds[i];
  2699. if (task->sc)
  2700. continue;
  2701. if (task->state == ISCSI_TASK_FREE)
  2702. continue;
  2703. ISCSI_DBG_SESSION(conn->session,
  2704. "failing mgmt itt 0x%x state %d\n",
  2705. task->itt, task->state);
  2706. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2707. if (task->state == ISCSI_TASK_PENDING)
  2708. state = ISCSI_TASK_COMPLETED;
  2709. iscsi_complete_task(task, state);
  2710. }
  2711. }
  2712. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2713. struct iscsi_conn *conn, int flag)
  2714. {
  2715. int old_stop_stage;
  2716. mutex_lock(&session->eh_mutex);
  2717. spin_lock_bh(&session->frwd_lock);
  2718. if (conn->stop_stage == STOP_CONN_TERM) {
  2719. spin_unlock_bh(&session->frwd_lock);
  2720. mutex_unlock(&session->eh_mutex);
  2721. return;
  2722. }
  2723. /*
  2724. * When this is called for the in_login state, we only want to clean
  2725. * up the login task and connection. We do not need to block and set
  2726. * the recovery state again
  2727. */
  2728. if (flag == STOP_CONN_TERM)
  2729. session->state = ISCSI_STATE_TERMINATE;
  2730. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2731. session->state = ISCSI_STATE_IN_RECOVERY;
  2732. old_stop_stage = conn->stop_stage;
  2733. conn->stop_stage = flag;
  2734. spin_unlock_bh(&session->frwd_lock);
  2735. del_timer_sync(&conn->transport_timer);
  2736. iscsi_suspend_tx(conn);
  2737. spin_lock_bh(&session->frwd_lock);
  2738. conn->c_stage = ISCSI_CONN_STOPPED;
  2739. spin_unlock_bh(&session->frwd_lock);
  2740. /*
  2741. * for connection level recovery we should not calculate
  2742. * header digest. conn->hdr_size used for optimization
  2743. * in hdr_extract() and will be re-negotiated at
  2744. * set_param() time.
  2745. */
  2746. if (flag == STOP_CONN_RECOVER) {
  2747. conn->hdrdgst_en = 0;
  2748. conn->datadgst_en = 0;
  2749. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2750. old_stop_stage != STOP_CONN_RECOVER) {
  2751. ISCSI_DBG_SESSION(session, "blocking session\n");
  2752. iscsi_block_session(session->cls_session);
  2753. }
  2754. }
  2755. /*
  2756. * flush queues.
  2757. */
  2758. spin_lock_bh(&session->frwd_lock);
  2759. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2760. fail_mgmt_tasks(session, conn);
  2761. memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
  2762. spin_unlock_bh(&session->frwd_lock);
  2763. mutex_unlock(&session->eh_mutex);
  2764. }
  2765. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2766. {
  2767. struct iscsi_conn *conn = cls_conn->dd_data;
  2768. struct iscsi_session *session = conn->session;
  2769. switch (flag) {
  2770. case STOP_CONN_RECOVER:
  2771. case STOP_CONN_TERM:
  2772. iscsi_start_session_recovery(session, conn, flag);
  2773. break;
  2774. default:
  2775. iscsi_conn_printk(KERN_ERR, conn,
  2776. "invalid stop flag %d\n", flag);
  2777. }
  2778. }
  2779. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2780. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2781. struct iscsi_cls_conn *cls_conn, int is_leading)
  2782. {
  2783. struct iscsi_session *session = cls_session->dd_data;
  2784. struct iscsi_conn *conn = cls_conn->dd_data;
  2785. spin_lock_bh(&session->frwd_lock);
  2786. if (is_leading)
  2787. session->leadconn = conn;
  2788. spin_unlock_bh(&session->frwd_lock);
  2789. /*
  2790. * Unblock xmitworker(), Login Phase will pass through.
  2791. */
  2792. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2793. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2794. return 0;
  2795. }
  2796. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2797. int iscsi_switch_str_param(char **param, char *new_val_buf)
  2798. {
  2799. char *new_val;
  2800. if (*param) {
  2801. if (!strcmp(*param, new_val_buf))
  2802. return 0;
  2803. }
  2804. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2805. if (!new_val)
  2806. return -ENOMEM;
  2807. kfree(*param);
  2808. *param = new_val;
  2809. return 0;
  2810. }
  2811. EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
  2812. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2813. enum iscsi_param param, char *buf, int buflen)
  2814. {
  2815. struct iscsi_conn *conn = cls_conn->dd_data;
  2816. struct iscsi_session *session = conn->session;
  2817. int val;
  2818. switch(param) {
  2819. case ISCSI_PARAM_FAST_ABORT:
  2820. sscanf(buf, "%d", &session->fast_abort);
  2821. break;
  2822. case ISCSI_PARAM_ABORT_TMO:
  2823. sscanf(buf, "%d", &session->abort_timeout);
  2824. break;
  2825. case ISCSI_PARAM_LU_RESET_TMO:
  2826. sscanf(buf, "%d", &session->lu_reset_timeout);
  2827. break;
  2828. case ISCSI_PARAM_TGT_RESET_TMO:
  2829. sscanf(buf, "%d", &session->tgt_reset_timeout);
  2830. break;
  2831. case ISCSI_PARAM_PING_TMO:
  2832. sscanf(buf, "%d", &conn->ping_timeout);
  2833. break;
  2834. case ISCSI_PARAM_RECV_TMO:
  2835. sscanf(buf, "%d", &conn->recv_timeout);
  2836. break;
  2837. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2838. sscanf(buf, "%d", &conn->max_recv_dlength);
  2839. break;
  2840. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2841. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2842. break;
  2843. case ISCSI_PARAM_HDRDGST_EN:
  2844. sscanf(buf, "%d", &conn->hdrdgst_en);
  2845. break;
  2846. case ISCSI_PARAM_DATADGST_EN:
  2847. sscanf(buf, "%d", &conn->datadgst_en);
  2848. break;
  2849. case ISCSI_PARAM_INITIAL_R2T_EN:
  2850. sscanf(buf, "%d", &session->initial_r2t_en);
  2851. break;
  2852. case ISCSI_PARAM_MAX_R2T:
  2853. sscanf(buf, "%hu", &session->max_r2t);
  2854. break;
  2855. case ISCSI_PARAM_IMM_DATA_EN:
  2856. sscanf(buf, "%d", &session->imm_data_en);
  2857. break;
  2858. case ISCSI_PARAM_FIRST_BURST:
  2859. sscanf(buf, "%d", &session->first_burst);
  2860. break;
  2861. case ISCSI_PARAM_MAX_BURST:
  2862. sscanf(buf, "%d", &session->max_burst);
  2863. break;
  2864. case ISCSI_PARAM_PDU_INORDER_EN:
  2865. sscanf(buf, "%d", &session->pdu_inorder_en);
  2866. break;
  2867. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2868. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2869. break;
  2870. case ISCSI_PARAM_ERL:
  2871. sscanf(buf, "%d", &session->erl);
  2872. break;
  2873. case ISCSI_PARAM_EXP_STATSN:
  2874. sscanf(buf, "%u", &conn->exp_statsn);
  2875. break;
  2876. case ISCSI_PARAM_USERNAME:
  2877. return iscsi_switch_str_param(&session->username, buf);
  2878. case ISCSI_PARAM_USERNAME_IN:
  2879. return iscsi_switch_str_param(&session->username_in, buf);
  2880. case ISCSI_PARAM_PASSWORD:
  2881. return iscsi_switch_str_param(&session->password, buf);
  2882. case ISCSI_PARAM_PASSWORD_IN:
  2883. return iscsi_switch_str_param(&session->password_in, buf);
  2884. case ISCSI_PARAM_TARGET_NAME:
  2885. return iscsi_switch_str_param(&session->targetname, buf);
  2886. case ISCSI_PARAM_TARGET_ALIAS:
  2887. return iscsi_switch_str_param(&session->targetalias, buf);
  2888. case ISCSI_PARAM_TPGT:
  2889. sscanf(buf, "%d", &session->tpgt);
  2890. break;
  2891. case ISCSI_PARAM_PERSISTENT_PORT:
  2892. sscanf(buf, "%d", &conn->persistent_port);
  2893. break;
  2894. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2895. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2896. case ISCSI_PARAM_IFACE_NAME:
  2897. return iscsi_switch_str_param(&session->ifacename, buf);
  2898. case ISCSI_PARAM_INITIATOR_NAME:
  2899. return iscsi_switch_str_param(&session->initiatorname, buf);
  2900. case ISCSI_PARAM_BOOT_ROOT:
  2901. return iscsi_switch_str_param(&session->boot_root, buf);
  2902. case ISCSI_PARAM_BOOT_NIC:
  2903. return iscsi_switch_str_param(&session->boot_nic, buf);
  2904. case ISCSI_PARAM_BOOT_TARGET:
  2905. return iscsi_switch_str_param(&session->boot_target, buf);
  2906. case ISCSI_PARAM_PORTAL_TYPE:
  2907. return iscsi_switch_str_param(&session->portal_type, buf);
  2908. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  2909. return iscsi_switch_str_param(&session->discovery_parent_type,
  2910. buf);
  2911. case ISCSI_PARAM_DISCOVERY_SESS:
  2912. sscanf(buf, "%d", &val);
  2913. session->discovery_sess = !!val;
  2914. break;
  2915. case ISCSI_PARAM_LOCAL_IPADDR:
  2916. return iscsi_switch_str_param(&conn->local_ipaddr, buf);
  2917. default:
  2918. return -ENOSYS;
  2919. }
  2920. return 0;
  2921. }
  2922. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2923. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2924. enum iscsi_param param, char *buf)
  2925. {
  2926. struct iscsi_session *session = cls_session->dd_data;
  2927. int len;
  2928. switch(param) {
  2929. case ISCSI_PARAM_FAST_ABORT:
  2930. len = sprintf(buf, "%d\n", session->fast_abort);
  2931. break;
  2932. case ISCSI_PARAM_ABORT_TMO:
  2933. len = sprintf(buf, "%d\n", session->abort_timeout);
  2934. break;
  2935. case ISCSI_PARAM_LU_RESET_TMO:
  2936. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2937. break;
  2938. case ISCSI_PARAM_TGT_RESET_TMO:
  2939. len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
  2940. break;
  2941. case ISCSI_PARAM_INITIAL_R2T_EN:
  2942. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2943. break;
  2944. case ISCSI_PARAM_MAX_R2T:
  2945. len = sprintf(buf, "%hu\n", session->max_r2t);
  2946. break;
  2947. case ISCSI_PARAM_IMM_DATA_EN:
  2948. len = sprintf(buf, "%d\n", session->imm_data_en);
  2949. break;
  2950. case ISCSI_PARAM_FIRST_BURST:
  2951. len = sprintf(buf, "%u\n", session->first_burst);
  2952. break;
  2953. case ISCSI_PARAM_MAX_BURST:
  2954. len = sprintf(buf, "%u\n", session->max_burst);
  2955. break;
  2956. case ISCSI_PARAM_PDU_INORDER_EN:
  2957. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2958. break;
  2959. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2960. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2961. break;
  2962. case ISCSI_PARAM_DEF_TASKMGMT_TMO:
  2963. len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
  2964. break;
  2965. case ISCSI_PARAM_ERL:
  2966. len = sprintf(buf, "%d\n", session->erl);
  2967. break;
  2968. case ISCSI_PARAM_TARGET_NAME:
  2969. len = sprintf(buf, "%s\n", session->targetname);
  2970. break;
  2971. case ISCSI_PARAM_TARGET_ALIAS:
  2972. len = sprintf(buf, "%s\n", session->targetalias);
  2973. break;
  2974. case ISCSI_PARAM_TPGT:
  2975. len = sprintf(buf, "%d\n", session->tpgt);
  2976. break;
  2977. case ISCSI_PARAM_USERNAME:
  2978. len = sprintf(buf, "%s\n", session->username);
  2979. break;
  2980. case ISCSI_PARAM_USERNAME_IN:
  2981. len = sprintf(buf, "%s\n", session->username_in);
  2982. break;
  2983. case ISCSI_PARAM_PASSWORD:
  2984. len = sprintf(buf, "%s\n", session->password);
  2985. break;
  2986. case ISCSI_PARAM_PASSWORD_IN:
  2987. len = sprintf(buf, "%s\n", session->password_in);
  2988. break;
  2989. case ISCSI_PARAM_IFACE_NAME:
  2990. len = sprintf(buf, "%s\n", session->ifacename);
  2991. break;
  2992. case ISCSI_PARAM_INITIATOR_NAME:
  2993. len = sprintf(buf, "%s\n", session->initiatorname);
  2994. break;
  2995. case ISCSI_PARAM_BOOT_ROOT:
  2996. len = sprintf(buf, "%s\n", session->boot_root);
  2997. break;
  2998. case ISCSI_PARAM_BOOT_NIC:
  2999. len = sprintf(buf, "%s\n", session->boot_nic);
  3000. break;
  3001. case ISCSI_PARAM_BOOT_TARGET:
  3002. len = sprintf(buf, "%s\n", session->boot_target);
  3003. break;
  3004. case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
  3005. len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
  3006. break;
  3007. case ISCSI_PARAM_DISCOVERY_SESS:
  3008. len = sprintf(buf, "%u\n", session->discovery_sess);
  3009. break;
  3010. case ISCSI_PARAM_PORTAL_TYPE:
  3011. len = sprintf(buf, "%s\n", session->portal_type);
  3012. break;
  3013. case ISCSI_PARAM_CHAP_AUTH_EN:
  3014. len = sprintf(buf, "%u\n", session->chap_auth_en);
  3015. break;
  3016. case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
  3017. len = sprintf(buf, "%u\n", session->discovery_logout_en);
  3018. break;
  3019. case ISCSI_PARAM_BIDI_CHAP_EN:
  3020. len = sprintf(buf, "%u\n", session->bidi_chap_en);
  3021. break;
  3022. case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
  3023. len = sprintf(buf, "%u\n", session->discovery_auth_optional);
  3024. break;
  3025. case ISCSI_PARAM_DEF_TIME2WAIT:
  3026. len = sprintf(buf, "%d\n", session->time2wait);
  3027. break;
  3028. case ISCSI_PARAM_DEF_TIME2RETAIN:
  3029. len = sprintf(buf, "%d\n", session->time2retain);
  3030. break;
  3031. case ISCSI_PARAM_TSID:
  3032. len = sprintf(buf, "%u\n", session->tsid);
  3033. break;
  3034. case ISCSI_PARAM_ISID:
  3035. len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
  3036. session->isid[0], session->isid[1],
  3037. session->isid[2], session->isid[3],
  3038. session->isid[4], session->isid[5]);
  3039. break;
  3040. case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
  3041. len = sprintf(buf, "%u\n", session->discovery_parent_idx);
  3042. break;
  3043. case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
  3044. if (session->discovery_parent_type)
  3045. len = sprintf(buf, "%s\n",
  3046. session->discovery_parent_type);
  3047. else
  3048. len = sprintf(buf, "\n");
  3049. break;
  3050. default:
  3051. return -ENOSYS;
  3052. }
  3053. return len;
  3054. }
  3055. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  3056. int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
  3057. enum iscsi_param param, char *buf)
  3058. {
  3059. struct sockaddr_in6 *sin6 = NULL;
  3060. struct sockaddr_in *sin = NULL;
  3061. int len;
  3062. switch (addr->ss_family) {
  3063. case AF_INET:
  3064. sin = (struct sockaddr_in *)addr;
  3065. break;
  3066. case AF_INET6:
  3067. sin6 = (struct sockaddr_in6 *)addr;
  3068. break;
  3069. default:
  3070. return -EINVAL;
  3071. }
  3072. switch (param) {
  3073. case ISCSI_PARAM_CONN_ADDRESS:
  3074. case ISCSI_HOST_PARAM_IPADDRESS:
  3075. if (sin)
  3076. len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
  3077. else
  3078. len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
  3079. break;
  3080. case ISCSI_PARAM_CONN_PORT:
  3081. case ISCSI_PARAM_LOCAL_PORT:
  3082. if (sin)
  3083. len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
  3084. else
  3085. len = sprintf(buf, "%hu\n",
  3086. be16_to_cpu(sin6->sin6_port));
  3087. break;
  3088. default:
  3089. return -EINVAL;
  3090. }
  3091. return len;
  3092. }
  3093. EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
  3094. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  3095. enum iscsi_param param, char *buf)
  3096. {
  3097. struct iscsi_conn *conn = cls_conn->dd_data;
  3098. int len;
  3099. switch(param) {
  3100. case ISCSI_PARAM_PING_TMO:
  3101. len = sprintf(buf, "%u\n", conn->ping_timeout);
  3102. break;
  3103. case ISCSI_PARAM_RECV_TMO:
  3104. len = sprintf(buf, "%u\n", conn->recv_timeout);
  3105. break;
  3106. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  3107. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  3108. break;
  3109. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  3110. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  3111. break;
  3112. case ISCSI_PARAM_HDRDGST_EN:
  3113. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  3114. break;
  3115. case ISCSI_PARAM_DATADGST_EN:
  3116. len = sprintf(buf, "%d\n", conn->datadgst_en);
  3117. break;
  3118. case ISCSI_PARAM_IFMARKER_EN:
  3119. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  3120. break;
  3121. case ISCSI_PARAM_OFMARKER_EN:
  3122. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  3123. break;
  3124. case ISCSI_PARAM_EXP_STATSN:
  3125. len = sprintf(buf, "%u\n", conn->exp_statsn);
  3126. break;
  3127. case ISCSI_PARAM_PERSISTENT_PORT:
  3128. len = sprintf(buf, "%d\n", conn->persistent_port);
  3129. break;
  3130. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  3131. len = sprintf(buf, "%s\n", conn->persistent_address);
  3132. break;
  3133. case ISCSI_PARAM_STATSN:
  3134. len = sprintf(buf, "%u\n", conn->statsn);
  3135. break;
  3136. case ISCSI_PARAM_MAX_SEGMENT_SIZE:
  3137. len = sprintf(buf, "%u\n", conn->max_segment_size);
  3138. break;
  3139. case ISCSI_PARAM_KEEPALIVE_TMO:
  3140. len = sprintf(buf, "%u\n", conn->keepalive_tmo);
  3141. break;
  3142. case ISCSI_PARAM_LOCAL_PORT:
  3143. len = sprintf(buf, "%u\n", conn->local_port);
  3144. break;
  3145. case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
  3146. len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
  3147. break;
  3148. case ISCSI_PARAM_TCP_NAGLE_DISABLE:
  3149. len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
  3150. break;
  3151. case ISCSI_PARAM_TCP_WSF_DISABLE:
  3152. len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
  3153. break;
  3154. case ISCSI_PARAM_TCP_TIMER_SCALE:
  3155. len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
  3156. break;
  3157. case ISCSI_PARAM_TCP_TIMESTAMP_EN:
  3158. len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
  3159. break;
  3160. case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
  3161. len = sprintf(buf, "%u\n", conn->fragment_disable);
  3162. break;
  3163. case ISCSI_PARAM_IPV4_TOS:
  3164. len = sprintf(buf, "%u\n", conn->ipv4_tos);
  3165. break;
  3166. case ISCSI_PARAM_IPV6_TC:
  3167. len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
  3168. break;
  3169. case ISCSI_PARAM_IPV6_FLOW_LABEL:
  3170. len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
  3171. break;
  3172. case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
  3173. len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
  3174. break;
  3175. case ISCSI_PARAM_TCP_XMIT_WSF:
  3176. len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
  3177. break;
  3178. case ISCSI_PARAM_TCP_RECV_WSF:
  3179. len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
  3180. break;
  3181. case ISCSI_PARAM_LOCAL_IPADDR:
  3182. len = sprintf(buf, "%s\n", conn->local_ipaddr);
  3183. break;
  3184. default:
  3185. return -ENOSYS;
  3186. }
  3187. return len;
  3188. }
  3189. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  3190. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3191. char *buf)
  3192. {
  3193. struct iscsi_host *ihost = shost_priv(shost);
  3194. int len;
  3195. switch (param) {
  3196. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3197. len = sprintf(buf, "%s\n", ihost->netdev);
  3198. break;
  3199. case ISCSI_HOST_PARAM_HWADDRESS:
  3200. len = sprintf(buf, "%s\n", ihost->hwaddress);
  3201. break;
  3202. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3203. len = sprintf(buf, "%s\n", ihost->initiatorname);
  3204. break;
  3205. default:
  3206. return -ENOSYS;
  3207. }
  3208. return len;
  3209. }
  3210. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  3211. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  3212. char *buf, int buflen)
  3213. {
  3214. struct iscsi_host *ihost = shost_priv(shost);
  3215. switch (param) {
  3216. case ISCSI_HOST_PARAM_NETDEV_NAME:
  3217. return iscsi_switch_str_param(&ihost->netdev, buf);
  3218. case ISCSI_HOST_PARAM_HWADDRESS:
  3219. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  3220. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  3221. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  3222. default:
  3223. return -ENOSYS;
  3224. }
  3225. return 0;
  3226. }
  3227. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  3228. MODULE_AUTHOR("Mike Christie");
  3229. MODULE_DESCRIPTION("iSCSI library functions");
  3230. MODULE_LICENSE("GPL");