scsi_transport_spi.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  4. *
  5. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  6. * Copyright (c) 2004, 2005 James Bottomley <[email protected]>
  7. */
  8. #include <linux/ctype.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/mutex.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/slab.h>
  16. #include <linux/suspend.h>
  17. #include <scsi/scsi.h>
  18. #include "scsi_priv.h"
  19. #include <scsi/scsi_device.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_tcq.h>
  24. #include <scsi/scsi_transport.h>
  25. #include <scsi/scsi_transport_spi.h>
  26. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  27. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  28. * on" attributes */
  29. #define SPI_HOST_ATTRS 1
  30. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  31. #define DV_LOOPS 3
  32. #define DV_TIMEOUT (10*HZ)
  33. #define DV_RETRIES 3 /* should only need at most
  34. * two cc/ua clears */
  35. /* Our blacklist flags */
  36. enum {
  37. SPI_BLIST_NOIUS = (__force blist_flags_t)0x1,
  38. };
  39. /* blacklist table, modelled on scsi_devinfo.c */
  40. static struct {
  41. char *vendor;
  42. char *model;
  43. blist_flags_t flags;
  44. } spi_static_device_list[] __initdata = {
  45. {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
  46. {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
  47. {NULL, NULL, 0}
  48. };
  49. /* Private data accessors (keep these out of the header file) */
  50. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  51. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  52. struct spi_internal {
  53. struct scsi_transport_template t;
  54. struct spi_function_template *f;
  55. };
  56. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  57. static const int ppr_to_ps[] = {
  58. /* The PPR values 0-6 are reserved, fill them in when
  59. * the committee defines them */
  60. -1, /* 0x00 */
  61. -1, /* 0x01 */
  62. -1, /* 0x02 */
  63. -1, /* 0x03 */
  64. -1, /* 0x04 */
  65. -1, /* 0x05 */
  66. -1, /* 0x06 */
  67. 3125, /* 0x07 */
  68. 6250, /* 0x08 */
  69. 12500, /* 0x09 */
  70. 25000, /* 0x0a */
  71. 30300, /* 0x0b */
  72. 50000, /* 0x0c */
  73. };
  74. /* The PPR values at which you calculate the period in ns by multiplying
  75. * by 4 */
  76. #define SPI_STATIC_PPR 0x0c
  77. static int sprint_frac(char *dest, int value, int denom)
  78. {
  79. int frac = value % denom;
  80. int result = sprintf(dest, "%d", value / denom);
  81. if (frac == 0)
  82. return result;
  83. dest[result++] = '.';
  84. do {
  85. denom /= 10;
  86. sprintf(dest + result, "%d", frac / denom);
  87. result++;
  88. frac %= denom;
  89. } while (frac);
  90. dest[result++] = '\0';
  91. return result;
  92. }
  93. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  94. enum req_op op, void *buffer, unsigned int bufflen,
  95. struct scsi_sense_hdr *sshdr)
  96. {
  97. int i, result;
  98. struct scsi_sense_hdr sshdr_tmp;
  99. blk_opf_t opf = op | REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
  100. REQ_FAILFAST_DRIVER;
  101. const struct scsi_exec_args exec_args = {
  102. .req_flags = BLK_MQ_REQ_PM,
  103. .sshdr = sshdr ? : &sshdr_tmp,
  104. };
  105. sshdr = exec_args.sshdr;
  106. for(i = 0; i < DV_RETRIES; i++) {
  107. /*
  108. * The purpose of the RQF_PM flag below is to bypass the
  109. * SDEV_QUIESCE state.
  110. */
  111. result = scsi_execute_cmd(sdev, cmd, opf, buffer, bufflen,
  112. DV_TIMEOUT, 1, &exec_args);
  113. if (result < 0 || !scsi_sense_valid(sshdr) ||
  114. sshdr->sense_key != UNIT_ATTENTION)
  115. break;
  116. }
  117. return result;
  118. }
  119. static struct {
  120. enum spi_signal_type value;
  121. char *name;
  122. } signal_types[] = {
  123. { SPI_SIGNAL_UNKNOWN, "unknown" },
  124. { SPI_SIGNAL_SE, "SE" },
  125. { SPI_SIGNAL_LVD, "LVD" },
  126. { SPI_SIGNAL_HVD, "HVD" },
  127. };
  128. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  129. {
  130. int i;
  131. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  132. if (type == signal_types[i].value)
  133. return signal_types[i].name;
  134. }
  135. return NULL;
  136. }
  137. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  138. {
  139. int i, len;
  140. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  141. len = strlen(signal_types[i].name);
  142. if (strncmp(name, signal_types[i].name, len) == 0 &&
  143. (name[len] == '\n' || name[len] == '\0'))
  144. return signal_types[i].value;
  145. }
  146. return SPI_SIGNAL_UNKNOWN;
  147. }
  148. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  149. struct device *cdev)
  150. {
  151. struct Scsi_Host *shost = dev_to_shost(dev);
  152. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  153. return 0;
  154. }
  155. static int spi_host_configure(struct transport_container *tc,
  156. struct device *dev,
  157. struct device *cdev);
  158. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  159. "spi_host",
  160. spi_host_setup,
  161. NULL,
  162. spi_host_configure);
  163. static int spi_host_match(struct attribute_container *cont,
  164. struct device *dev)
  165. {
  166. struct Scsi_Host *shost;
  167. if (!scsi_is_host_device(dev))
  168. return 0;
  169. shost = dev_to_shost(dev);
  170. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  171. != &spi_host_class.class)
  172. return 0;
  173. return &shost->transportt->host_attrs.ac == cont;
  174. }
  175. static int spi_target_configure(struct transport_container *tc,
  176. struct device *dev,
  177. struct device *cdev);
  178. static int spi_device_configure(struct transport_container *tc,
  179. struct device *dev,
  180. struct device *cdev)
  181. {
  182. struct scsi_device *sdev = to_scsi_device(dev);
  183. struct scsi_target *starget = sdev->sdev_target;
  184. blist_flags_t bflags;
  185. bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
  186. &sdev->inquiry[16],
  187. SCSI_DEVINFO_SPI);
  188. /* Populate the target capability fields with the values
  189. * gleaned from the device inquiry */
  190. spi_support_sync(starget) = scsi_device_sync(sdev);
  191. spi_support_wide(starget) = scsi_device_wide(sdev);
  192. spi_support_dt(starget) = scsi_device_dt(sdev);
  193. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  194. spi_support_ius(starget) = scsi_device_ius(sdev);
  195. if (bflags & SPI_BLIST_NOIUS) {
  196. dev_info(dev, "Information Units disabled by blacklist\n");
  197. spi_support_ius(starget) = 0;
  198. }
  199. spi_support_qas(starget) = scsi_device_qas(sdev);
  200. return 0;
  201. }
  202. static int spi_setup_transport_attrs(struct transport_container *tc,
  203. struct device *dev,
  204. struct device *cdev)
  205. {
  206. struct scsi_target *starget = to_scsi_target(dev);
  207. spi_period(starget) = -1; /* illegal value */
  208. spi_min_period(starget) = 0;
  209. spi_offset(starget) = 0; /* async */
  210. spi_max_offset(starget) = 255;
  211. spi_width(starget) = 0; /* narrow */
  212. spi_max_width(starget) = 1;
  213. spi_iu(starget) = 0; /* no IU */
  214. spi_max_iu(starget) = 1;
  215. spi_dt(starget) = 0; /* ST */
  216. spi_qas(starget) = 0;
  217. spi_max_qas(starget) = 1;
  218. spi_wr_flow(starget) = 0;
  219. spi_rd_strm(starget) = 0;
  220. spi_rti(starget) = 0;
  221. spi_pcomp_en(starget) = 0;
  222. spi_hold_mcs(starget) = 0;
  223. spi_dv_pending(starget) = 0;
  224. spi_dv_in_progress(starget) = 0;
  225. spi_initial_dv(starget) = 0;
  226. mutex_init(&spi_dv_mutex(starget));
  227. return 0;
  228. }
  229. #define spi_transport_show_simple(field, format_string) \
  230. \
  231. static ssize_t \
  232. show_spi_transport_##field(struct device *dev, \
  233. struct device_attribute *attr, char *buf) \
  234. { \
  235. struct scsi_target *starget = transport_class_to_starget(dev); \
  236. struct spi_transport_attrs *tp; \
  237. \
  238. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  239. return snprintf(buf, 20, format_string, tp->field); \
  240. }
  241. #define spi_transport_store_simple(field, format_string) \
  242. \
  243. static ssize_t \
  244. store_spi_transport_##field(struct device *dev, \
  245. struct device_attribute *attr, \
  246. const char *buf, size_t count) \
  247. { \
  248. int val; \
  249. struct scsi_target *starget = transport_class_to_starget(dev); \
  250. struct spi_transport_attrs *tp; \
  251. \
  252. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  253. val = simple_strtoul(buf, NULL, 0); \
  254. tp->field = val; \
  255. return count; \
  256. }
  257. #define spi_transport_show_function(field, format_string) \
  258. \
  259. static ssize_t \
  260. show_spi_transport_##field(struct device *dev, \
  261. struct device_attribute *attr, char *buf) \
  262. { \
  263. struct scsi_target *starget = transport_class_to_starget(dev); \
  264. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  265. struct spi_transport_attrs *tp; \
  266. struct spi_internal *i = to_spi_internal(shost->transportt); \
  267. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  268. if (i->f->get_##field) \
  269. i->f->get_##field(starget); \
  270. return snprintf(buf, 20, format_string, tp->field); \
  271. }
  272. #define spi_transport_store_function(field, format_string) \
  273. static ssize_t \
  274. store_spi_transport_##field(struct device *dev, \
  275. struct device_attribute *attr, \
  276. const char *buf, size_t count) \
  277. { \
  278. int val; \
  279. struct scsi_target *starget = transport_class_to_starget(dev); \
  280. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  281. struct spi_internal *i = to_spi_internal(shost->transportt); \
  282. \
  283. if (!i->f->set_##field) \
  284. return -EINVAL; \
  285. val = simple_strtoul(buf, NULL, 0); \
  286. i->f->set_##field(starget, val); \
  287. return count; \
  288. }
  289. #define spi_transport_store_max(field, format_string) \
  290. static ssize_t \
  291. store_spi_transport_##field(struct device *dev, \
  292. struct device_attribute *attr, \
  293. const char *buf, size_t count) \
  294. { \
  295. int val; \
  296. struct scsi_target *starget = transport_class_to_starget(dev); \
  297. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  298. struct spi_internal *i = to_spi_internal(shost->transportt); \
  299. struct spi_transport_attrs *tp \
  300. = (struct spi_transport_attrs *)&starget->starget_data; \
  301. \
  302. if (!i->f->set_##field) \
  303. return -EINVAL; \
  304. val = simple_strtoul(buf, NULL, 0); \
  305. if (val > tp->max_##field) \
  306. val = tp->max_##field; \
  307. i->f->set_##field(starget, val); \
  308. return count; \
  309. }
  310. #define spi_transport_rd_attr(field, format_string) \
  311. spi_transport_show_function(field, format_string) \
  312. spi_transport_store_function(field, format_string) \
  313. static DEVICE_ATTR(field, S_IRUGO, \
  314. show_spi_transport_##field, \
  315. store_spi_transport_##field);
  316. #define spi_transport_simple_attr(field, format_string) \
  317. spi_transport_show_simple(field, format_string) \
  318. spi_transport_store_simple(field, format_string) \
  319. static DEVICE_ATTR(field, S_IRUGO, \
  320. show_spi_transport_##field, \
  321. store_spi_transport_##field);
  322. #define spi_transport_max_attr(field, format_string) \
  323. spi_transport_show_function(field, format_string) \
  324. spi_transport_store_max(field, format_string) \
  325. spi_transport_simple_attr(max_##field, format_string) \
  326. static DEVICE_ATTR(field, S_IRUGO, \
  327. show_spi_transport_##field, \
  328. store_spi_transport_##field);
  329. /* The Parallel SCSI Tranport Attributes: */
  330. spi_transport_max_attr(offset, "%d\n");
  331. spi_transport_max_attr(width, "%d\n");
  332. spi_transport_max_attr(iu, "%d\n");
  333. spi_transport_rd_attr(dt, "%d\n");
  334. spi_transport_max_attr(qas, "%d\n");
  335. spi_transport_rd_attr(wr_flow, "%d\n");
  336. spi_transport_rd_attr(rd_strm, "%d\n");
  337. spi_transport_rd_attr(rti, "%d\n");
  338. spi_transport_rd_attr(pcomp_en, "%d\n");
  339. spi_transport_rd_attr(hold_mcs, "%d\n");
  340. /* we only care about the first child device that's a real SCSI device
  341. * so we return 1 to terminate the iteration when we find it */
  342. static int child_iter(struct device *dev, void *data)
  343. {
  344. if (!scsi_is_sdev_device(dev))
  345. return 0;
  346. spi_dv_device(to_scsi_device(dev));
  347. return 1;
  348. }
  349. static ssize_t
  350. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  351. const char *buf, size_t count)
  352. {
  353. struct scsi_target *starget = transport_class_to_starget(dev);
  354. device_for_each_child(&starget->dev, NULL, child_iter);
  355. return count;
  356. }
  357. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  358. /* Translate the period into ns according to the current spec
  359. * for SDTR/PPR messages */
  360. static int period_to_str(char *buf, int period)
  361. {
  362. int len, picosec;
  363. if (period < 0 || period > 0xff) {
  364. picosec = -1;
  365. } else if (period <= SPI_STATIC_PPR) {
  366. picosec = ppr_to_ps[period];
  367. } else {
  368. picosec = period * 4000;
  369. }
  370. if (picosec == -1) {
  371. len = sprintf(buf, "reserved");
  372. } else {
  373. len = sprint_frac(buf, picosec, 1000);
  374. }
  375. return len;
  376. }
  377. static ssize_t
  378. show_spi_transport_period_helper(char *buf, int period)
  379. {
  380. int len = period_to_str(buf, period);
  381. buf[len++] = '\n';
  382. buf[len] = '\0';
  383. return len;
  384. }
  385. static ssize_t
  386. store_spi_transport_period_helper(struct device *dev, const char *buf,
  387. size_t count, int *periodp)
  388. {
  389. int j, picosec, period = -1;
  390. char *endp;
  391. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  392. if (*endp == '.') {
  393. int mult = 100;
  394. do {
  395. endp++;
  396. if (!isdigit(*endp))
  397. break;
  398. picosec += (*endp - '0') * mult;
  399. mult /= 10;
  400. } while (mult > 0);
  401. }
  402. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  403. if (ppr_to_ps[j] < picosec)
  404. continue;
  405. period = j;
  406. break;
  407. }
  408. if (period == -1)
  409. period = picosec / 4000;
  410. if (period > 0xff)
  411. period = 0xff;
  412. *periodp = period;
  413. return count;
  414. }
  415. static ssize_t
  416. show_spi_transport_period(struct device *dev,
  417. struct device_attribute *attr, char *buf)
  418. {
  419. struct scsi_target *starget = transport_class_to_starget(dev);
  420. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  421. struct spi_internal *i = to_spi_internal(shost->transportt);
  422. struct spi_transport_attrs *tp =
  423. (struct spi_transport_attrs *)&starget->starget_data;
  424. if (i->f->get_period)
  425. i->f->get_period(starget);
  426. return show_spi_transport_period_helper(buf, tp->period);
  427. }
  428. static ssize_t
  429. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  430. const char *buf, size_t count)
  431. {
  432. struct scsi_target *starget = transport_class_to_starget(cdev);
  433. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  434. struct spi_internal *i = to_spi_internal(shost->transportt);
  435. struct spi_transport_attrs *tp =
  436. (struct spi_transport_attrs *)&starget->starget_data;
  437. int period, retval;
  438. if (!i->f->set_period)
  439. return -EINVAL;
  440. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  441. if (period < tp->min_period)
  442. period = tp->min_period;
  443. i->f->set_period(starget, period);
  444. return retval;
  445. }
  446. static DEVICE_ATTR(period, S_IRUGO,
  447. show_spi_transport_period,
  448. store_spi_transport_period);
  449. static ssize_t
  450. show_spi_transport_min_period(struct device *cdev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct scsi_target *starget = transport_class_to_starget(cdev);
  454. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  455. struct spi_internal *i = to_spi_internal(shost->transportt);
  456. struct spi_transport_attrs *tp =
  457. (struct spi_transport_attrs *)&starget->starget_data;
  458. if (!i->f->set_period)
  459. return -EINVAL;
  460. return show_spi_transport_period_helper(buf, tp->min_period);
  461. }
  462. static ssize_t
  463. store_spi_transport_min_period(struct device *cdev,
  464. struct device_attribute *attr,
  465. const char *buf, size_t count)
  466. {
  467. struct scsi_target *starget = transport_class_to_starget(cdev);
  468. struct spi_transport_attrs *tp =
  469. (struct spi_transport_attrs *)&starget->starget_data;
  470. return store_spi_transport_period_helper(cdev, buf, count,
  471. &tp->min_period);
  472. }
  473. static DEVICE_ATTR(min_period, S_IRUGO,
  474. show_spi_transport_min_period,
  475. store_spi_transport_min_period);
  476. static ssize_t show_spi_host_signalling(struct device *cdev,
  477. struct device_attribute *attr,
  478. char *buf)
  479. {
  480. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  481. struct spi_internal *i = to_spi_internal(shost->transportt);
  482. if (i->f->get_signalling)
  483. i->f->get_signalling(shost);
  484. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  485. }
  486. static ssize_t store_spi_host_signalling(struct device *dev,
  487. struct device_attribute *attr,
  488. const char *buf, size_t count)
  489. {
  490. struct Scsi_Host *shost = transport_class_to_shost(dev);
  491. struct spi_internal *i = to_spi_internal(shost->transportt);
  492. enum spi_signal_type type = spi_signal_to_value(buf);
  493. if (!i->f->set_signalling)
  494. return -EINVAL;
  495. if (type != SPI_SIGNAL_UNKNOWN)
  496. i->f->set_signalling(shost, type);
  497. return count;
  498. }
  499. static DEVICE_ATTR(signalling, S_IRUGO,
  500. show_spi_host_signalling,
  501. store_spi_host_signalling);
  502. static ssize_t show_spi_host_width(struct device *cdev,
  503. struct device_attribute *attr,
  504. char *buf)
  505. {
  506. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  507. return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
  508. }
  509. static DEVICE_ATTR(host_width, S_IRUGO,
  510. show_spi_host_width, NULL);
  511. static ssize_t show_spi_host_hba_id(struct device *cdev,
  512. struct device_attribute *attr,
  513. char *buf)
  514. {
  515. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  516. return sprintf(buf, "%d\n", shost->this_id);
  517. }
  518. static DEVICE_ATTR(hba_id, S_IRUGO,
  519. show_spi_host_hba_id, NULL);
  520. #define DV_SET(x, y) \
  521. if(i->f->set_##x) \
  522. i->f->set_##x(sdev->sdev_target, y)
  523. enum spi_compare_returns {
  524. SPI_COMPARE_SUCCESS,
  525. SPI_COMPARE_FAILURE,
  526. SPI_COMPARE_SKIP_TEST,
  527. };
  528. /* This is for read/write Domain Validation: If the device supports
  529. * an echo buffer, we do read/write tests to it */
  530. static enum spi_compare_returns
  531. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  532. u8 *ptr, const int retries)
  533. {
  534. int len = ptr - buffer;
  535. int j, k, r, result;
  536. unsigned int pattern = 0x0000ffff;
  537. struct scsi_sense_hdr sshdr;
  538. const char spi_write_buffer[] = {
  539. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  540. };
  541. const char spi_read_buffer[] = {
  542. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  543. };
  544. /* set up the pattern buffer. Doesn't matter if we spill
  545. * slightly beyond since that's where the read buffer is */
  546. for (j = 0; j < len; ) {
  547. /* fill the buffer with counting (test a) */
  548. for ( ; j < min(len, 32); j++)
  549. buffer[j] = j;
  550. k = j;
  551. /* fill the buffer with alternating words of 0x0 and
  552. * 0xffff (test b) */
  553. for ( ; j < min(len, k + 32); j += 2) {
  554. u16 *word = (u16 *)&buffer[j];
  555. *word = (j & 0x02) ? 0x0000 : 0xffff;
  556. }
  557. k = j;
  558. /* fill with crosstalk (alternating 0x5555 0xaaa)
  559. * (test c) */
  560. for ( ; j < min(len, k + 32); j += 2) {
  561. u16 *word = (u16 *)&buffer[j];
  562. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  563. }
  564. k = j;
  565. /* fill with shifting bits (test d) */
  566. for ( ; j < min(len, k + 32); j += 4) {
  567. u32 *word = (unsigned int *)&buffer[j];
  568. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  569. *word = pattern;
  570. pattern = (pattern << 1) | roll;
  571. }
  572. /* don't bother with random data (test e) */
  573. }
  574. for (r = 0; r < retries; r++) {
  575. result = spi_execute(sdev, spi_write_buffer, REQ_OP_DRV_OUT,
  576. buffer, len, &sshdr);
  577. if(result || !scsi_device_online(sdev)) {
  578. scsi_device_set_state(sdev, SDEV_QUIESCE);
  579. if (scsi_sense_valid(&sshdr)
  580. && sshdr.sense_key == ILLEGAL_REQUEST
  581. /* INVALID FIELD IN CDB */
  582. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  583. /* This would mean that the drive lied
  584. * to us about supporting an echo
  585. * buffer (unfortunately some Western
  586. * Digital drives do precisely this)
  587. */
  588. return SPI_COMPARE_SKIP_TEST;
  589. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  590. return SPI_COMPARE_FAILURE;
  591. }
  592. memset(ptr, 0, len);
  593. spi_execute(sdev, spi_read_buffer, REQ_OP_DRV_IN,
  594. ptr, len, NULL);
  595. scsi_device_set_state(sdev, SDEV_QUIESCE);
  596. if (memcmp(buffer, ptr, len) != 0)
  597. return SPI_COMPARE_FAILURE;
  598. }
  599. return SPI_COMPARE_SUCCESS;
  600. }
  601. /* This is for the simplest form of Domain Validation: a read test
  602. * on the inquiry data from the device */
  603. static enum spi_compare_returns
  604. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  605. u8 *ptr, const int retries)
  606. {
  607. int r, result;
  608. const int len = sdev->inquiry_len;
  609. const char spi_inquiry[] = {
  610. INQUIRY, 0, 0, 0, len, 0
  611. };
  612. for (r = 0; r < retries; r++) {
  613. memset(ptr, 0, len);
  614. result = spi_execute(sdev, spi_inquiry, REQ_OP_DRV_IN,
  615. ptr, len, NULL);
  616. if(result || !scsi_device_online(sdev)) {
  617. scsi_device_set_state(sdev, SDEV_QUIESCE);
  618. return SPI_COMPARE_FAILURE;
  619. }
  620. /* If we don't have the inquiry data already, the
  621. * first read gets it */
  622. if (ptr == buffer) {
  623. ptr += len;
  624. --r;
  625. continue;
  626. }
  627. if (memcmp(buffer, ptr, len) != 0)
  628. /* failure */
  629. return SPI_COMPARE_FAILURE;
  630. }
  631. return SPI_COMPARE_SUCCESS;
  632. }
  633. static enum spi_compare_returns
  634. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  635. enum spi_compare_returns
  636. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  637. {
  638. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  639. struct scsi_target *starget = sdev->sdev_target;
  640. int period = 0, prevperiod = 0;
  641. enum spi_compare_returns retval;
  642. for (;;) {
  643. int newperiod;
  644. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  645. if (retval == SPI_COMPARE_SUCCESS
  646. || retval == SPI_COMPARE_SKIP_TEST)
  647. break;
  648. /* OK, retrain, fallback */
  649. if (i->f->get_iu)
  650. i->f->get_iu(starget);
  651. if (i->f->get_qas)
  652. i->f->get_qas(starget);
  653. if (i->f->get_period)
  654. i->f->get_period(sdev->sdev_target);
  655. /* Here's the fallback sequence; first try turning off
  656. * IU, then QAS (if we can control them), then finally
  657. * fall down the periods */
  658. if (i->f->set_iu && spi_iu(starget)) {
  659. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
  660. DV_SET(iu, 0);
  661. } else if (i->f->set_qas && spi_qas(starget)) {
  662. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
  663. DV_SET(qas, 0);
  664. } else {
  665. newperiod = spi_period(starget);
  666. period = newperiod > period ? newperiod : period;
  667. if (period < 0x0d)
  668. period++;
  669. else
  670. period += period >> 1;
  671. if (unlikely(period > 0xff || period == prevperiod)) {
  672. /* Total failure; set to async and return */
  673. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  674. DV_SET(offset, 0);
  675. return SPI_COMPARE_FAILURE;
  676. }
  677. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  678. DV_SET(period, period);
  679. prevperiod = period;
  680. }
  681. }
  682. return retval;
  683. }
  684. static int
  685. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  686. {
  687. int l, result;
  688. /* first off do a test unit ready. This can error out
  689. * because of reservations or some other reason. If it
  690. * fails, the device won't let us write to the echo buffer
  691. * so just return failure */
  692. static const char spi_test_unit_ready[] = {
  693. TEST_UNIT_READY, 0, 0, 0, 0, 0
  694. };
  695. static const char spi_read_buffer_descriptor[] = {
  696. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  697. };
  698. /* We send a set of three TURs to clear any outstanding
  699. * unit attention conditions if they exist (Otherwise the
  700. * buffer tests won't be happy). If the TUR still fails
  701. * (reservation conflict, device not ready, etc) just
  702. * skip the write tests */
  703. for (l = 0; ; l++) {
  704. result = spi_execute(sdev, spi_test_unit_ready, REQ_OP_DRV_IN,
  705. NULL, 0, NULL);
  706. if(result) {
  707. if(l >= 3)
  708. return 0;
  709. } else {
  710. /* TUR succeeded */
  711. break;
  712. }
  713. }
  714. result = spi_execute(sdev, spi_read_buffer_descriptor,
  715. REQ_OP_DRV_IN, buffer, 4, NULL);
  716. if (result)
  717. /* Device has no echo buffer */
  718. return 0;
  719. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  720. }
  721. static void
  722. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  723. {
  724. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  725. struct scsi_target *starget = sdev->sdev_target;
  726. struct Scsi_Host *shost = sdev->host;
  727. int len = sdev->inquiry_len;
  728. int min_period = spi_min_period(starget);
  729. int max_width = spi_max_width(starget);
  730. /* first set us up for narrow async */
  731. DV_SET(offset, 0);
  732. DV_SET(width, 0);
  733. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  734. != SPI_COMPARE_SUCCESS) {
  735. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  736. /* FIXME: should probably offline the device here? */
  737. return;
  738. }
  739. if (!spi_support_wide(starget)) {
  740. spi_max_width(starget) = 0;
  741. max_width = 0;
  742. }
  743. /* test width */
  744. if (i->f->set_width && max_width) {
  745. i->f->set_width(starget, 1);
  746. if (spi_dv_device_compare_inquiry(sdev, buffer,
  747. buffer + len,
  748. DV_LOOPS)
  749. != SPI_COMPARE_SUCCESS) {
  750. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  751. i->f->set_width(starget, 0);
  752. /* Make sure we don't force wide back on by asking
  753. * for a transfer period that requires it */
  754. max_width = 0;
  755. if (min_period < 10)
  756. min_period = 10;
  757. }
  758. }
  759. if (!i->f->set_period)
  760. return;
  761. /* device can't handle synchronous */
  762. if (!spi_support_sync(starget) && !spi_support_dt(starget))
  763. return;
  764. /* len == -1 is the signal that we need to ascertain the
  765. * presence of an echo buffer before trying to use it. len ==
  766. * 0 means we don't have an echo buffer */
  767. len = -1;
  768. retry:
  769. /* now set up to the maximum */
  770. DV_SET(offset, spi_max_offset(starget));
  771. DV_SET(period, min_period);
  772. /* try QAS requests; this should be harmless to set if the
  773. * target supports it */
  774. if (spi_support_qas(starget) && spi_max_qas(starget)) {
  775. DV_SET(qas, 1);
  776. } else {
  777. DV_SET(qas, 0);
  778. }
  779. if (spi_support_ius(starget) && spi_max_iu(starget) &&
  780. min_period < 9) {
  781. /* This u320 (or u640). Set IU transfers */
  782. DV_SET(iu, 1);
  783. /* Then set the optional parameters */
  784. DV_SET(rd_strm, 1);
  785. DV_SET(wr_flow, 1);
  786. DV_SET(rti, 1);
  787. if (min_period == 8)
  788. DV_SET(pcomp_en, 1);
  789. } else {
  790. DV_SET(iu, 0);
  791. }
  792. /* now that we've done all this, actually check the bus
  793. * signal type (if known). Some devices are stupid on
  794. * a SE bus and still claim they can try LVD only settings */
  795. if (i->f->get_signalling)
  796. i->f->get_signalling(shost);
  797. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  798. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  799. !spi_support_dt(starget)) {
  800. DV_SET(dt, 0);
  801. } else {
  802. DV_SET(dt, 1);
  803. }
  804. /* set width last because it will pull all the other
  805. * parameters down to required values */
  806. DV_SET(width, max_width);
  807. /* Do the read only INQUIRY tests */
  808. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  809. spi_dv_device_compare_inquiry);
  810. /* See if we actually managed to negotiate and sustain DT */
  811. if (i->f->get_dt)
  812. i->f->get_dt(starget);
  813. /* see if the device has an echo buffer. If it does we can do
  814. * the SPI pattern write tests. Because of some broken
  815. * devices, we *only* try this on a device that has actually
  816. * negotiated DT */
  817. if (len == -1 && spi_dt(starget))
  818. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  819. if (len <= 0) {
  820. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  821. return;
  822. }
  823. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  824. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  825. len = SPI_MAX_ECHO_BUFFER_SIZE;
  826. }
  827. if (spi_dv_retrain(sdev, buffer, buffer + len,
  828. spi_dv_device_echo_buffer)
  829. == SPI_COMPARE_SKIP_TEST) {
  830. /* OK, the stupid drive can't do a write echo buffer
  831. * test after all, fall back to the read tests */
  832. len = 0;
  833. goto retry;
  834. }
  835. }
  836. /** spi_dv_device - Do Domain Validation on the device
  837. * @sdev: scsi device to validate
  838. *
  839. * Performs the domain validation on the given device in the
  840. * current execution thread. Since DV operations may sleep,
  841. * the current thread must have user context. Also no SCSI
  842. * related locks that would deadlock I/O issued by the DV may
  843. * be held.
  844. */
  845. void
  846. spi_dv_device(struct scsi_device *sdev)
  847. {
  848. struct scsi_target *starget = sdev->sdev_target;
  849. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  850. unsigned int sleep_flags;
  851. u8 *buffer;
  852. /*
  853. * Because this function and the power management code both call
  854. * scsi_device_quiesce(), it is not safe to perform domain validation
  855. * while suspend or resume is in progress. Hence the
  856. * lock/unlock_system_sleep() calls.
  857. */
  858. sleep_flags = lock_system_sleep();
  859. if (scsi_autopm_get_device(sdev))
  860. goto unlock_system_sleep;
  861. if (unlikely(spi_dv_in_progress(starget)))
  862. goto put_autopm;
  863. if (unlikely(scsi_device_get(sdev)))
  864. goto put_autopm;
  865. spi_dv_in_progress(starget) = 1;
  866. buffer = kzalloc(len, GFP_KERNEL);
  867. if (unlikely(!buffer))
  868. goto put_sdev;
  869. /* We need to verify that the actual device will quiesce; the
  870. * later target quiesce is just a nice to have */
  871. if (unlikely(scsi_device_quiesce(sdev)))
  872. goto free_buffer;
  873. scsi_target_quiesce(starget);
  874. spi_dv_pending(starget) = 1;
  875. mutex_lock(&spi_dv_mutex(starget));
  876. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  877. spi_dv_device_internal(sdev, buffer);
  878. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  879. mutex_unlock(&spi_dv_mutex(starget));
  880. spi_dv_pending(starget) = 0;
  881. scsi_target_resume(starget);
  882. spi_initial_dv(starget) = 1;
  883. free_buffer:
  884. kfree(buffer);
  885. put_sdev:
  886. spi_dv_in_progress(starget) = 0;
  887. scsi_device_put(sdev);
  888. put_autopm:
  889. scsi_autopm_put_device(sdev);
  890. unlock_system_sleep:
  891. unlock_system_sleep(sleep_flags);
  892. }
  893. EXPORT_SYMBOL(spi_dv_device);
  894. struct work_queue_wrapper {
  895. struct work_struct work;
  896. struct scsi_device *sdev;
  897. };
  898. static void
  899. spi_dv_device_work_wrapper(struct work_struct *work)
  900. {
  901. struct work_queue_wrapper *wqw =
  902. container_of(work, struct work_queue_wrapper, work);
  903. struct scsi_device *sdev = wqw->sdev;
  904. kfree(wqw);
  905. spi_dv_device(sdev);
  906. spi_dv_pending(sdev->sdev_target) = 0;
  907. scsi_device_put(sdev);
  908. }
  909. /**
  910. * spi_schedule_dv_device - schedule domain validation to occur on the device
  911. * @sdev: The device to validate
  912. *
  913. * Identical to spi_dv_device() above, except that the DV will be
  914. * scheduled to occur in a workqueue later. All memory allocations
  915. * are atomic, so may be called from any context including those holding
  916. * SCSI locks.
  917. */
  918. void
  919. spi_schedule_dv_device(struct scsi_device *sdev)
  920. {
  921. struct work_queue_wrapper *wqw =
  922. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  923. if (unlikely(!wqw))
  924. return;
  925. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  926. kfree(wqw);
  927. return;
  928. }
  929. /* Set pending early (dv_device doesn't check it, only sets it) */
  930. spi_dv_pending(sdev->sdev_target) = 1;
  931. if (unlikely(scsi_device_get(sdev))) {
  932. kfree(wqw);
  933. spi_dv_pending(sdev->sdev_target) = 0;
  934. return;
  935. }
  936. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  937. wqw->sdev = sdev;
  938. schedule_work(&wqw->work);
  939. }
  940. EXPORT_SYMBOL(spi_schedule_dv_device);
  941. /**
  942. * spi_display_xfer_agreement - Print the current target transfer agreement
  943. * @starget: The target for which to display the agreement
  944. *
  945. * Each SPI port is required to maintain a transfer agreement for each
  946. * other port on the bus. This function prints a one-line summary of
  947. * the current agreement; more detailed information is available in sysfs.
  948. */
  949. void spi_display_xfer_agreement(struct scsi_target *starget)
  950. {
  951. struct spi_transport_attrs *tp;
  952. tp = (struct spi_transport_attrs *)&starget->starget_data;
  953. if (tp->offset > 0 && tp->period > 0) {
  954. unsigned int picosec, kb100;
  955. char *scsi = "FAST-?";
  956. char tmp[8];
  957. if (tp->period <= SPI_STATIC_PPR) {
  958. picosec = ppr_to_ps[tp->period];
  959. switch (tp->period) {
  960. case 7: scsi = "FAST-320"; break;
  961. case 8: scsi = "FAST-160"; break;
  962. case 9: scsi = "FAST-80"; break;
  963. case 10:
  964. case 11: scsi = "FAST-40"; break;
  965. case 12: scsi = "FAST-20"; break;
  966. }
  967. } else {
  968. picosec = tp->period * 4000;
  969. if (tp->period < 25)
  970. scsi = "FAST-20";
  971. else if (tp->period < 50)
  972. scsi = "FAST-10";
  973. else
  974. scsi = "FAST-5";
  975. }
  976. kb100 = (10000000 + picosec / 2) / picosec;
  977. if (tp->width)
  978. kb100 *= 2;
  979. sprint_frac(tmp, picosec, 1000);
  980. dev_info(&starget->dev,
  981. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  982. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  983. tp->dt ? "DT" : "ST",
  984. tp->iu ? " IU" : "",
  985. tp->qas ? " QAS" : "",
  986. tp->rd_strm ? " RDSTRM" : "",
  987. tp->rti ? " RTI" : "",
  988. tp->wr_flow ? " WRFLOW" : "",
  989. tp->pcomp_en ? " PCOMP" : "",
  990. tp->hold_mcs ? " HMCS" : "",
  991. tmp, tp->offset);
  992. } else {
  993. dev_info(&starget->dev, "%sasynchronous\n",
  994. tp->width ? "wide " : "");
  995. }
  996. }
  997. EXPORT_SYMBOL(spi_display_xfer_agreement);
  998. int spi_populate_width_msg(unsigned char *msg, int width)
  999. {
  1000. msg[0] = EXTENDED_MESSAGE;
  1001. msg[1] = 2;
  1002. msg[2] = EXTENDED_WDTR;
  1003. msg[3] = width;
  1004. return 4;
  1005. }
  1006. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  1007. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  1008. {
  1009. msg[0] = EXTENDED_MESSAGE;
  1010. msg[1] = 3;
  1011. msg[2] = EXTENDED_SDTR;
  1012. msg[3] = period;
  1013. msg[4] = offset;
  1014. return 5;
  1015. }
  1016. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  1017. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  1018. int width, int options)
  1019. {
  1020. msg[0] = EXTENDED_MESSAGE;
  1021. msg[1] = 6;
  1022. msg[2] = EXTENDED_PPR;
  1023. msg[3] = period;
  1024. msg[4] = 0;
  1025. msg[5] = offset;
  1026. msg[6] = width;
  1027. msg[7] = options;
  1028. return 8;
  1029. }
  1030. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  1031. /**
  1032. * spi_populate_tag_msg - place a tag message in a buffer
  1033. * @msg: pointer to the area to place the tag
  1034. * @cmd: pointer to the scsi command for the tag
  1035. *
  1036. * Notes:
  1037. * designed to create the correct type of tag message for the
  1038. * particular request. Returns the size of the tag message.
  1039. * May return 0 if TCQ is disabled for this device.
  1040. **/
  1041. int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
  1042. {
  1043. if (cmd->flags & SCMD_TAGGED) {
  1044. *msg++ = SIMPLE_QUEUE_TAG;
  1045. *msg++ = scsi_cmd_to_rq(cmd)->tag;
  1046. return 2;
  1047. }
  1048. return 0;
  1049. }
  1050. EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
  1051. #ifdef CONFIG_SCSI_CONSTANTS
  1052. static const char * const one_byte_msgs[] = {
  1053. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  1054. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  1055. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  1056. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  1057. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  1058. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  1059. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  1060. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  1061. };
  1062. static const char * const two_byte_msgs[] = {
  1063. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  1064. /* 0x23 */ "Ignore Wide Residue", "ACA"
  1065. };
  1066. static const char * const extended_msgs[] = {
  1067. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  1068. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1069. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1070. };
  1071. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1072. {
  1073. if (per) {
  1074. char buf[20];
  1075. period_to_str(buf, msg[per]);
  1076. printk("period = %s ns ", buf);
  1077. }
  1078. if (off)
  1079. printk("offset = %d ", msg[off]);
  1080. if (width)
  1081. printk("width = %d ", 8 << msg[width]);
  1082. }
  1083. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1084. {
  1085. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1086. msg[msb+3];
  1087. printk("%s = %d ", desc, ptr);
  1088. }
  1089. int spi_print_msg(const unsigned char *msg)
  1090. {
  1091. int len = 1, i;
  1092. if (msg[0] == EXTENDED_MESSAGE) {
  1093. len = 2 + msg[1];
  1094. if (len == 2)
  1095. len += 256;
  1096. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1097. printk ("%s ", extended_msgs[msg[2]]);
  1098. else
  1099. printk ("Extended Message, reserved code (0x%02x) ",
  1100. (int) msg[2]);
  1101. switch (msg[2]) {
  1102. case EXTENDED_MODIFY_DATA_POINTER:
  1103. print_ptr(msg, 3, "pointer");
  1104. break;
  1105. case EXTENDED_SDTR:
  1106. print_nego(msg, 3, 4, 0);
  1107. break;
  1108. case EXTENDED_WDTR:
  1109. print_nego(msg, 0, 0, 3);
  1110. break;
  1111. case EXTENDED_PPR:
  1112. print_nego(msg, 3, 5, 6);
  1113. break;
  1114. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1115. print_ptr(msg, 3, "out");
  1116. print_ptr(msg, 7, "in");
  1117. break;
  1118. default:
  1119. for (i = 2; i < len; ++i)
  1120. printk("%02x ", msg[i]);
  1121. }
  1122. /* Identify */
  1123. } else if (msg[0] & 0x80) {
  1124. printk("Identify disconnect %sallowed %s %d ",
  1125. (msg[0] & 0x40) ? "" : "not ",
  1126. (msg[0] & 0x20) ? "target routine" : "lun",
  1127. msg[0] & 0x7);
  1128. /* Normal One byte */
  1129. } else if (msg[0] < 0x1f) {
  1130. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1131. printk("%s ", one_byte_msgs[msg[0]]);
  1132. else
  1133. printk("reserved (%02x) ", msg[0]);
  1134. } else if (msg[0] == 0x55) {
  1135. printk("QAS Request ");
  1136. /* Two byte */
  1137. } else if (msg[0] <= 0x2f) {
  1138. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1139. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1140. msg[1]);
  1141. else
  1142. printk("reserved two byte (%02x %02x) ",
  1143. msg[0], msg[1]);
  1144. len = 2;
  1145. } else
  1146. printk("reserved ");
  1147. return len;
  1148. }
  1149. EXPORT_SYMBOL(spi_print_msg);
  1150. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1151. int spi_print_msg(const unsigned char *msg)
  1152. {
  1153. int len = 1, i;
  1154. if (msg[0] == EXTENDED_MESSAGE) {
  1155. len = 2 + msg[1];
  1156. if (len == 2)
  1157. len += 256;
  1158. for (i = 0; i < len; ++i)
  1159. printk("%02x ", msg[i]);
  1160. /* Identify */
  1161. } else if (msg[0] & 0x80) {
  1162. printk("%02x ", msg[0]);
  1163. /* Normal One byte */
  1164. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1165. printk("%02x ", msg[0]);
  1166. /* Two byte */
  1167. } else if (msg[0] <= 0x2f) {
  1168. printk("%02x %02x", msg[0], msg[1]);
  1169. len = 2;
  1170. } else
  1171. printk("%02x ", msg[0]);
  1172. return len;
  1173. }
  1174. EXPORT_SYMBOL(spi_print_msg);
  1175. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1176. static int spi_device_match(struct attribute_container *cont,
  1177. struct device *dev)
  1178. {
  1179. struct scsi_device *sdev;
  1180. struct Scsi_Host *shost;
  1181. struct spi_internal *i;
  1182. if (!scsi_is_sdev_device(dev))
  1183. return 0;
  1184. sdev = to_scsi_device(dev);
  1185. shost = sdev->host;
  1186. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1187. != &spi_host_class.class)
  1188. return 0;
  1189. /* Note: this class has no device attributes, so it has
  1190. * no per-HBA allocation and thus we don't need to distinguish
  1191. * the attribute containers for the device */
  1192. i = to_spi_internal(shost->transportt);
  1193. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1194. return 0;
  1195. return 1;
  1196. }
  1197. static int spi_target_match(struct attribute_container *cont,
  1198. struct device *dev)
  1199. {
  1200. struct Scsi_Host *shost;
  1201. struct scsi_target *starget;
  1202. struct spi_internal *i;
  1203. if (!scsi_is_target_device(dev))
  1204. return 0;
  1205. shost = dev_to_shost(dev->parent);
  1206. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1207. != &spi_host_class.class)
  1208. return 0;
  1209. i = to_spi_internal(shost->transportt);
  1210. starget = to_scsi_target(dev);
  1211. if (i->f->deny_binding && i->f->deny_binding(starget))
  1212. return 0;
  1213. return &i->t.target_attrs.ac == cont;
  1214. }
  1215. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1216. "spi_transport",
  1217. spi_setup_transport_attrs,
  1218. NULL,
  1219. spi_target_configure);
  1220. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1221. spi_device_match,
  1222. spi_device_configure);
  1223. static struct attribute *host_attributes[] = {
  1224. &dev_attr_signalling.attr,
  1225. &dev_attr_host_width.attr,
  1226. &dev_attr_hba_id.attr,
  1227. NULL
  1228. };
  1229. static struct attribute_group host_attribute_group = {
  1230. .attrs = host_attributes,
  1231. };
  1232. static int spi_host_configure(struct transport_container *tc,
  1233. struct device *dev,
  1234. struct device *cdev)
  1235. {
  1236. struct kobject *kobj = &cdev->kobj;
  1237. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1238. struct spi_internal *si = to_spi_internal(shost->transportt);
  1239. struct attribute *attr = &dev_attr_signalling.attr;
  1240. int rc = 0;
  1241. if (si->f->set_signalling)
  1242. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1243. return rc;
  1244. }
  1245. /* returns true if we should be showing the variable. Also
  1246. * overloads the return by setting 1<<1 if the attribute should
  1247. * be writeable */
  1248. #define TARGET_ATTRIBUTE_HELPER(name) \
  1249. (si->f->show_##name ? S_IRUGO : 0) | \
  1250. (si->f->set_##name ? S_IWUSR : 0)
  1251. static umode_t target_attribute_is_visible(struct kobject *kobj,
  1252. struct attribute *attr, int i)
  1253. {
  1254. struct device *cdev = container_of(kobj, struct device, kobj);
  1255. struct scsi_target *starget = transport_class_to_starget(cdev);
  1256. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1257. struct spi_internal *si = to_spi_internal(shost->transportt);
  1258. if (attr == &dev_attr_period.attr &&
  1259. spi_support_sync(starget))
  1260. return TARGET_ATTRIBUTE_HELPER(period);
  1261. else if (attr == &dev_attr_min_period.attr &&
  1262. spi_support_sync(starget))
  1263. return TARGET_ATTRIBUTE_HELPER(period);
  1264. else if (attr == &dev_attr_offset.attr &&
  1265. spi_support_sync(starget))
  1266. return TARGET_ATTRIBUTE_HELPER(offset);
  1267. else if (attr == &dev_attr_max_offset.attr &&
  1268. spi_support_sync(starget))
  1269. return TARGET_ATTRIBUTE_HELPER(offset);
  1270. else if (attr == &dev_attr_width.attr &&
  1271. spi_support_wide(starget))
  1272. return TARGET_ATTRIBUTE_HELPER(width);
  1273. else if (attr == &dev_attr_max_width.attr &&
  1274. spi_support_wide(starget))
  1275. return TARGET_ATTRIBUTE_HELPER(width);
  1276. else if (attr == &dev_attr_iu.attr &&
  1277. spi_support_ius(starget))
  1278. return TARGET_ATTRIBUTE_HELPER(iu);
  1279. else if (attr == &dev_attr_max_iu.attr &&
  1280. spi_support_ius(starget))
  1281. return TARGET_ATTRIBUTE_HELPER(iu);
  1282. else if (attr == &dev_attr_dt.attr &&
  1283. spi_support_dt(starget))
  1284. return TARGET_ATTRIBUTE_HELPER(dt);
  1285. else if (attr == &dev_attr_qas.attr &&
  1286. spi_support_qas(starget))
  1287. return TARGET_ATTRIBUTE_HELPER(qas);
  1288. else if (attr == &dev_attr_max_qas.attr &&
  1289. spi_support_qas(starget))
  1290. return TARGET_ATTRIBUTE_HELPER(qas);
  1291. else if (attr == &dev_attr_wr_flow.attr &&
  1292. spi_support_ius(starget))
  1293. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1294. else if (attr == &dev_attr_rd_strm.attr &&
  1295. spi_support_ius(starget))
  1296. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1297. else if (attr == &dev_attr_rti.attr &&
  1298. spi_support_ius(starget))
  1299. return TARGET_ATTRIBUTE_HELPER(rti);
  1300. else if (attr == &dev_attr_pcomp_en.attr &&
  1301. spi_support_ius(starget))
  1302. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1303. else if (attr == &dev_attr_hold_mcs.attr &&
  1304. spi_support_ius(starget))
  1305. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1306. else if (attr == &dev_attr_revalidate.attr)
  1307. return S_IWUSR;
  1308. return 0;
  1309. }
  1310. static struct attribute *target_attributes[] = {
  1311. &dev_attr_period.attr,
  1312. &dev_attr_min_period.attr,
  1313. &dev_attr_offset.attr,
  1314. &dev_attr_max_offset.attr,
  1315. &dev_attr_width.attr,
  1316. &dev_attr_max_width.attr,
  1317. &dev_attr_iu.attr,
  1318. &dev_attr_max_iu.attr,
  1319. &dev_attr_dt.attr,
  1320. &dev_attr_qas.attr,
  1321. &dev_attr_max_qas.attr,
  1322. &dev_attr_wr_flow.attr,
  1323. &dev_attr_rd_strm.attr,
  1324. &dev_attr_rti.attr,
  1325. &dev_attr_pcomp_en.attr,
  1326. &dev_attr_hold_mcs.attr,
  1327. &dev_attr_revalidate.attr,
  1328. NULL
  1329. };
  1330. static struct attribute_group target_attribute_group = {
  1331. .attrs = target_attributes,
  1332. .is_visible = target_attribute_is_visible,
  1333. };
  1334. static int spi_target_configure(struct transport_container *tc,
  1335. struct device *dev,
  1336. struct device *cdev)
  1337. {
  1338. struct kobject *kobj = &cdev->kobj;
  1339. /* force an update based on parameters read from the device */
  1340. sysfs_update_group(kobj, &target_attribute_group);
  1341. return 0;
  1342. }
  1343. struct scsi_transport_template *
  1344. spi_attach_transport(struct spi_function_template *ft)
  1345. {
  1346. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1347. GFP_KERNEL);
  1348. if (unlikely(!i))
  1349. return NULL;
  1350. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1351. i->t.target_attrs.ac.grp = &target_attribute_group;
  1352. i->t.target_attrs.ac.match = spi_target_match;
  1353. transport_container_register(&i->t.target_attrs);
  1354. i->t.target_size = sizeof(struct spi_transport_attrs);
  1355. i->t.host_attrs.ac.class = &spi_host_class.class;
  1356. i->t.host_attrs.ac.grp = &host_attribute_group;
  1357. i->t.host_attrs.ac.match = spi_host_match;
  1358. transport_container_register(&i->t.host_attrs);
  1359. i->t.host_size = sizeof(struct spi_host_attrs);
  1360. i->f = ft;
  1361. return &i->t;
  1362. }
  1363. EXPORT_SYMBOL(spi_attach_transport);
  1364. void spi_release_transport(struct scsi_transport_template *t)
  1365. {
  1366. struct spi_internal *i = to_spi_internal(t);
  1367. transport_container_unregister(&i->t.target_attrs);
  1368. transport_container_unregister(&i->t.host_attrs);
  1369. kfree(i);
  1370. }
  1371. EXPORT_SYMBOL(spi_release_transport);
  1372. static __init int spi_transport_init(void)
  1373. {
  1374. int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
  1375. "SCSI Parallel Transport Class");
  1376. if (!error) {
  1377. int i;
  1378. for (i = 0; spi_static_device_list[i].vendor; i++)
  1379. scsi_dev_info_list_add_keyed(1, /* compatible */
  1380. spi_static_device_list[i].vendor,
  1381. spi_static_device_list[i].model,
  1382. NULL,
  1383. spi_static_device_list[i].flags,
  1384. SCSI_DEVINFO_SPI);
  1385. }
  1386. error = transport_class_register(&spi_transport_class);
  1387. if (error)
  1388. return error;
  1389. error = anon_transport_class_register(&spi_device_class);
  1390. return transport_class_register(&spi_host_class);
  1391. }
  1392. static void __exit spi_transport_exit(void)
  1393. {
  1394. transport_class_unregister(&spi_transport_class);
  1395. anon_transport_class_unregister(&spi_device_class);
  1396. transport_class_unregister(&spi_host_class);
  1397. scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
  1398. }
  1399. MODULE_AUTHOR("Martin Hicks");
  1400. MODULE_DESCRIPTION("SPI Transport Attributes");
  1401. MODULE_LICENSE("GPL");
  1402. module_init(spi_transport_init);
  1403. module_exit(spi_transport_exit);