scsi_transport_spi.c 41 KB

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